egret(旧版本)自动增量编译

2020-09-04  本文已影响0人  PurySun

编译耗时分两部分:ts转换js和分析文件依赖顺序。

详细原理见:
https://mp.weixin.qq.com/s/_uMt3uNSiqffzk0EAd6G9A

官方提升速度的版本是Egret Pro

如果egret版本是旧版本,不支持实际应用ts增量编译。

本文用折中方式应用增量编译,目的是提升编译速度,附带提升调试加载速度。

1.安装版本大于3.4的typescript

npm -g install typescript 

查看版本

tsc -v

2.启动监测模式

注释egret默认的编译方式, 清空commands或者替换为第3步的合并文件插件:

egret.png

一般文件改动只会编译对应文件,删除、新增操作会全量或半全量编译,此时最好清理项目尽量全量编译一次。

项目根目录控制台输入:

tsc -w

启动后默认会全量编译一次。

3.合并编译后文件,提高调试加载速度(可选)

参考插件:

/**
 * desc:合并文件插件
 * auth:pury
 * date:2020-08-25 13:40:02
 */
const fs = require('fs');
const path = require("path")

export class BuildPlugin implements plugins.Command {
    /**
     * 项目绝对路径
     */
    private _projectRoot: string;
    constructor() {
    }

    async onFile(file: plugins.File) {
        return file;
    }

    async onFinish(commandContext: plugins.CommandContext) {
        let fileName = "manifest.json";
        let projectRoot = commandContext.projectRoot;
        let manifest = fs.readFileSync(projectRoot + "/manifest.json",'utf-8');
        manifest = JSON.parse(manifest);
        let content = "";

        //-- 备份依赖文件顺序
        if (manifest.game.length > 1) {
            fs.writeFileSync(projectRoot + "/manifest-debug.json", JSON.stringify(manifest, null, '\t'));
        }
        else {
            try {
                manifest = fs.readFileSync(projectRoot + "/manifest-debug.json",'utf-8');
                manifest = JSON.parse(manifest);
            }
            catch (e) {
                console.log("========================================================");
                console.log("                             ");
                console.log("              【需要先清理一次项目!!!】               ");
                console.log("                             ");
                console.log("========================================================");
                return;
            }
        }
     
        try {
            for (let i in manifest)
            {
                let item = manifest[i];
                //-- 仅合并自定义编写的代码文件
                if (i != "game") continue;

                for (let j in item)
                {
                    let temp = item[j];
                    let tempData = fs.readFileSync(projectRoot + "/" + temp);
                    content += tempData.toString() + '\n';
                }
            }

            let outFile = "bin-debug/Main.merge.js";
            fs.writeFileSync(projectRoot + "/" + outFile, content);
            console.log("[Main.merge.js]已更新");

            manifest.game = [outFile];
            fs.writeFileSync(projectRoot + "/manifest.json", JSON.stringify(manifest, null, '\t'));
            console.log("[manifest.json]已重构");
        }
        catch (e) {
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读