egret 白鹭的编译太慢了 自己写了一个

用的swc
他会检测git的改变

const simpleGit = require('simple-git');
const fs = require('fs');
const path = require('path');
// 初始化 simple-git
const swc = require('@swc/core');
const baseDir = 'D:\\project';
const gameDir = 'game\\module\\abcdefg';
const gitDir = 'D:\\project';
const git = simpleGit(gitDir);
const crypto = require('crypto');

async function buildChangedTsFiles() {
    try {
        // 获取工作目录和暂存区的更改文件
        const statusSummary = await git.status();
        // 合并所有更改的文件名(已修改、已新增、已删除等)
        const changedFiles = [
            ...statusSummary.modified,
            ...statusSummary.created,
            ...statusSummary.not_added
        ];
        const changedTsFiles = changedFiles.filter(file => file.endsWith('.ts'));
        if (changedTsFiles.length > 0) {
            console.log('更改的 TypeScript 文件:');
            changedTsFiles.forEach(file => {
                buildOne(path.join(gitDir, file));
            });
        }
    } catch (error) {
        console.error(`Error: ${error.message}`);
    }
}

function buildPath() {
    const inputDir = baseDir + "src\\" + gameDir;
    const outputDir = baseDir + "bin-debug\\" + gameDir;
    fs.mkdirSync(outputDir, { recursive: true });
    const files = fs.readdirSync(inputDir).filter(file => file.endsWith('.ts'));
    files.forEach(file => {
        buildOne(path.join(inputDir, file));
    });
}

var builded = {};

function buildOne(filePath) {
    if (builded[filePath] != null) {
        return;
    }
    let fileName = path.basename(filePath);
    const code = fs.readFileSync(filePath, 'utf-8');
    const hash = crypto.createHash('md5');
    hash.update(fileName);
    hash.update(code);
    let hashValue = hash.digest("hex");
    // console.log("检测改变:" + filePath);
    if (hashObj[filePath] == hashValue) {
        // console.log("没改变");
        return;
    }
    console.log(filePath, hashValue);
    hashObj[filePath] = hashValue;
    builded[filePath] = 1;
    console.log("编译:" + filePath);

    let out = filePath.replace(/\.ts$/, '.js').replace("src", "bin-debug");
    let output = swc.transformSync(code, {
        filename: fileName
    });
    fs.writeFileSync(out, output.code, 'utf-8');
}

const hashFilePath = path.join(__dirname, 'hashes.json');
var hashObj;

function readHash() {
    if (fs.existsSync(hashFilePath)) {
        hashObj = JSON.parse(fs.readFileSync(hashFilePath, 'utf-8'));
    } else {
        hashObj = {};
    }
}

function saveHashes() {
    // console.log("save success");
    // console.log(hashObj);
    fs.writeFileSync(hashFilePath, JSON.stringify(hashObj, null, 2), 'utf-8');
}

function buildHash() {
    for (const key in hashObj) {
        buildOne(key);
    }
}

async function run() {
    readHash();
    buildHash();
    // buildPath();
    await buildChangedTsFiles();
    saveHashes();
    console.log("编译完成");
}

run();

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 04:48:08       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 04:48:08       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 04:48:08       45 阅读
  4. Python语言-面向对象

    2024-07-20 04:48:08       55 阅读

热门阅读

  1. git泄露

    2024-07-20 04:48:08       18 阅读
  2. 交叉编译aarch64的Qt5.12.2,附带Mysql插件编译

    2024-07-20 04:48:08       17 阅读
  3. 3507软件IIC,IO输入输出函数

    2024-07-20 04:48:08       15 阅读
  4. Elasticsearch扩容与缩容集群

    2024-07-20 04:48:08       17 阅读
  5. 三、模型转换和压缩扩写

    2024-07-20 04:48:08       15 阅读
  6. python实现动态规划算法

    2024-07-20 04:48:08       19 阅读
  7. 分布式锁的最佳实践之Redisson

    2024-07-20 04:48:08       18 阅读
  8. [沫忘录]Golang基础类型与语法

    2024-07-20 04:48:08       18 阅读
  9. python需要掌握那些语法

    2024-07-20 04:48:08       17 阅读
  10. 关于shell的变量替换

    2024-07-20 04:48:08       18 阅读
  11. JVM--内存分配与回收策略

    2024-07-20 04:48:08       16 阅读
  12. autosar mcal I2C

    2024-07-20 04:48:08       15 阅读