自学内容网 自学内容网

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();

原文地址:https://blog.csdn.net/qq_38913715/article/details/140555142

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!