且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Egret之JSZip高级应用:压缩JS

更新时间:2021-10-18 09:50:55

本片讲解Egret使用JSZip解析加压的js代码,然后将其还原成可执行的js代码。

一 , 先将egret库打包 :

①:在网站根目录建一个egret文件夹,在其中放入类库

Egret之JSZip高级应用:压缩JS

②:将egret文件夹打包成egret.zip


二 , 将main.min.js打包成main.min.js.zip


三 , index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>Egret</title>
    <meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="full-screen" content="true" />
    <meta name="screen-orientation" content="portrait" />
    <meta name="x5-fullscreen" content="true" />
    <meta name="360-fullscreen" content="true" />
    <style>
        html, body {
            -ms-touch-action: none;
            background: #888888;
            padding: 0;
            border: 0;
            margin: 0;
            height: 100%;
        }
    </style>
 <script egret="libs" src="libs/modules/jszip/jszip.min.js"></script>
</head>
<body>
    <div style="margin: auto;width: 100%;height: 100%;" class="egret-player"
         data-entry-class="Main"
         data-orientation="auto"
         data-scale-mode="showAll"
         data-frame-rate="30"
         data-content-width="640"
         data-content-height="1136"
         data-show-paint-rect="false"
         data-multi-fingered="2"
         data-show-fps="false" data-show-log="false"
         data-show-fps-style="x:0,y:0,size:12,textColor:0xffffff,bgAlpha:0.9">
    </div>
<script>
       //加载egret的引擎库
    try
    {
        loadZip("egret.zip",function(zip)
        {
            //压缩进的egret引擎的各个代码文件
            var files = ["egret.min.js", "egret.web.min.js", "res.min.js", "tween.min.js", 
   "dragonBones.min.js","eui.min.js","game.min.js","jszip.min.js","particle.min.js"];
            for (var i = 0; i < files.length; i++)
            {
                createScript(zip,"egret/"+files[i]);
            }
            //加载游戏代码
            loadZip("main.min.js.zip" + "?v=" + Math.random(),function(zip)
            {
                createScript(zip,"main.min.js");
                //全部加载完成,启动egret游戏
                egret.runEgret({ renderMode: "webgl", audioType: 0,retina:true});
            });
        });
    }
    catch (e)
    {
        //压缩失败,实际项目这里需要改为加载没压缩的js文件。
        console.error("jszip解压文件报错,进行普通文件加载");
    }
    //加载单个zip文件,成功后进行回调
    function loadZip(url,callBack)
    {
        var xhrZip = new XMLHttpRequest();
        xhrZip.open("GET", url, true);
        xhrZip.responseType = "arraybuffer";
        xhrZip.addEventListener("load", function (oEvent)
        {
            var arrayBuffer = xhrZip.response; // 注意:不是oReq.responseText
            if (!arrayBuffer)
            {
                console.log(url + "解析异常:" + xhrZip);
                throw new Error("zip异常");
            }
            callBack(new JSZip(arrayBuffer));
        });
        xhrZip.send(null);
    }
    function createScript(zip,file)
    {
        //解压出来变成script脚本
        var text = zip.file(file).asText();
        var script = document.createElement("script");
        script.setAttribute("type", "text/javascript");
        script.text = text;
        document.body.appendChild(script);
        document.body.removeChild(script);
    }
</script>
</body>
</html>

网站结构:

Egret之JSZip高级应用:压缩JS


使用浏览器查看加载结果:

Egret之JSZip高级应用:压缩JS


如果不使用压缩 , 则结果是:

Egret之JSZip高级应用:压缩JS

可以看出加载的代价比不压缩要高很多。尤其是当项目很大时。这就是压缩js的意义。。。。。















本文转自Aonaufly51CTO博客,原文链接:http://blog.51cto.com/aonaufly/1970528 ,如需转载请自行联系原作者