egret 加载优化尝试-jsZip 解压js(附测试代码)

egret 相关

原文链接:
http://www.makaidong.com/yihoudangxian/3052_9032985.html

扩展链接:
使用jszip进行解压
jszip的下载地址:http://stuk.github.io/jszip/
jszip的基础教程:https://blog.csdn.net/sujun10/article/details/76038886

为了防止原文被删 做一下记录

放了方便测试 用的mac自带压缩工具
压缩的比例还是很不错的

字符串变script脚本的方法

 eval() 函数
可计算某个字符串,并执行其中的的 JavaScript 代码。 eval("console.l
og(1000)");

//运行环境指定window这个顶级对象 eval.call(window,text);
var script = document.createElement('script');
 script.setAttribute('type', 'text/javascript'); 
 script.text = text; 
 document.body.appendChild(script);

egret 修改需要修改的内容

  1. 打包 egret web 工程
  2. 压缩js 文件里的内容 为allJs.zip
  3. 从git下载jsZip 拷贝jszip.min.js 到js文件夹
  4. 修改index.html

附index 代码(只是用于测试 项目需要 请自行优化)

<!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>
</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-multi-fingered="1"
         data-content-width="1562"
         data-content-height="1348"
         data-show-fps="false" data-show-log="false"
         data-show-fps-style="x:0,y:0,size:20,textColor:0xffffff,bgAlpha:0.9">
    </div>
<script>

    var timestamp = new Date().getTime()
    
    var loadScript = function (list, callback) {
        var loaded = 0;
        var loadNext = function () {
            loadSingleScript(list[loaded], function () {
                loaded++;
                if (loaded >= list.length) {
                    callback();
                }
                else {
                    loadNext();
                }
            })
        };
        loadNext();
    };

    var loadSingleScript = function (src, callback) {
        var s = document.createElement('script');
        s.async = false;
        s.src = src;
        s.addEventListener('load', function () {
            s.parentNode.removeChild(s);
            s.removeEventListener('load', arguments.callee, false);
            callback();
        }, false);
        document.body.appendChild(s);
    };

    
    var startFunc = function(){
        var xhr = new XMLHttpRequest();
        xhr.open('GET', './manifest.json?v=' + Math.random(), true);
        xhr.addEventListener("load", function () {
            var manifest = JSON.parse(xhr.response);
            // var list = manifest.initial.concat(manifest.game);
            list = manifest.game
            loadScript(list, function () {
                /**
                * {
                * "renderMode":, //Engine rendering mode, "canvas" or "webgl"
                * "audioType": 0 //Use the audio type, 0: default, 2: web audio, 3: audio
                * "antialias": //Whether the anti-aliasing is enabled in WebGL mode, true: on, false: off, defaults to false
                * "calculateCanvasScaleFactor": //a function return canvas scale factor
                * }
                **/

                console.log("总耗时:", timestamp - new Date().getTime())

                egret.runEgret({ renderMode: "canvas", audioType: 0, calculateCanvasScaleFactor:function(context) {
                    var backingStore = context.backingStorePixelRatio ||
                        context.webkitBackingStorePixelRatio ||
                        context.mozBackingStorePixelRatio ||
                        context.msBackingStorePixelRatio ||
                        context.oBackingStorePixelRatio ||
                        context.backingStorePixelRatio || 1;
                    return (window.devicePixelRatio || 1) / backingStore;
                }});
            });
        });
        xhr.send(null);
    }

    var jszipPath = "js/jszip.min.js"
    loadSingleScript(jszipPath, function () {
        var zipPath = "js/allJs.zip"

        var xhr = new XMLHttpRequest();
        xhr.responseType = "arraybuffer"
        xhr.open('GET', zipPath + '?v=' + Math.random(), true);
        xhr.addEventListener("load", function () {
            var content = xhr.response
            
            var new_zip = new JSZip();
            // more files !
            new_zip.loadAsync(content)
            .then(function(zip) {
                // 你现在已经有加载的zip中包含的每个文件
                // new_zip.file("hello.txt").async("string"); // a promise of "Hello World\n"

                console.log("下载zip:", timestamp - new Date().getTime())
                var list = [
                    "egret.min.js",
                    "egret.web.min.js",
                    "eui.min.js",
                    "assetsmanager.min.js",
                    "tween.min.js",
                    "game.min.js",
                    "promise.min.js",
                    "dragonBones.min.js",
                    "socket.min.js",
                    "htsw_framework.min.js",
                    "CoursewareModule.min.js"
                ]
                // let i = 0
                // let num = list.length
                // for(let name of list){
                //     console.log("start", name)
                //     zip.file(name).async("string").then(function(test) {
                //         console.log("end", name)
                //         eval(test);
                //         i++

                //         if(i >= num){
                //             startFunc()
                //         }
                //     });
                // }

                
                let curI = 0
                let num = list.length
                let upFunc = function(){
                    let name = list[curI]
                    console.log("start", curI, name)
                    if(name){
                        zip.file(name).async("string").then(function(text) {
                            console.log("下载:", name,  timestamp - new Date().getTime())
                            // eval(test);
                            //运行环境指定window这个顶级对象 eval.call(window,text);
                            var script = document.createElement('script'); 
                            script.setAttribute('type', 'text/javascript'); 
                            script.text = text; 
                            document.body.appendChild(script);

                            curI++
                            console.log("end", curI)
                            upFunc()
                        });
                    } else {
                        startFunc()
                    }
                    
                }
                upFunc()
                
                
            });
        });
        xhr.send(null);

        
    })
</script>
</body>

</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值