refactor: 模块化重构 generator 和 runtime
This commit is contained in:
62
scripts/build-runtime.js
Normal file
62
scripts/build-runtime.js
Normal file
@@ -0,0 +1,62 @@
|
||||
const path = require('node:path');
|
||||
const fs = require('node:fs');
|
||||
|
||||
function ensureDir(dirPath) {
|
||||
if (!fs.existsSync(dirPath)) {
|
||||
fs.mkdirSync(dirPath, { recursive: true });
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
let esbuild;
|
||||
try {
|
||||
esbuild = require('esbuild');
|
||||
} catch (error) {
|
||||
console.error('未找到 esbuild,请先执行 npm install。');
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
const projectRoot = path.resolve(__dirname, '..');
|
||||
const entry = path.join(projectRoot, 'src', 'runtime', 'index.js');
|
||||
const outFile = path.join(projectRoot, 'dist', 'script.js');
|
||||
|
||||
if (!fs.existsSync(entry)) {
|
||||
console.error(`运行时入口不存在:${path.relative(projectRoot, entry)}`);
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
ensureDir(path.dirname(outFile));
|
||||
|
||||
try {
|
||||
const result = await esbuild.build({
|
||||
entryPoints: [entry],
|
||||
outfile: outFile,
|
||||
bundle: true,
|
||||
platform: 'browser',
|
||||
format: 'iife',
|
||||
target: ['es2018'],
|
||||
sourcemap: false,
|
||||
minify: true,
|
||||
legalComments: 'none',
|
||||
metafile: true,
|
||||
logLevel: 'info',
|
||||
});
|
||||
|
||||
const outputs = result && result.metafile && result.metafile.outputs ? result.metafile.outputs : null;
|
||||
const outKey = outputs ? Object.keys(outputs).find((k) => k.endsWith('dist/script.js')) : '';
|
||||
const bytes = outKey && outputs && outputs[outKey] ? outputs[outKey].bytes : 0;
|
||||
if (bytes) {
|
||||
console.log(`✅ runtime bundle 完成:dist/script.js (${bytes} bytes)`);
|
||||
} else {
|
||||
console.log('✅ runtime bundle 完成:dist/script.js');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('❌ runtime bundle 失败(禁止回退旧产物):', error && error.message ? error.message : error);
|
||||
process.exitCode = 1;
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
@@ -149,8 +149,8 @@ function shouldCheckFile(filePath) {
|
||||
|
||||
if (normalized === 'package-lock.json') return false;
|
||||
|
||||
// 这两个文件历史上未统一为 Prettier 风格;避免为了启用检查产生巨量格式化 diff
|
||||
if (normalized === 'src/generator.js' || normalized === 'src/script.js') return false;
|
||||
// 这些文件历史上未统一为 Prettier 风格;避免为了启用检查产生巨量格式化 diff
|
||||
if (normalized === 'src/generator/main.js' || normalized === 'src/runtime/index.js') return false;
|
||||
|
||||
// 与现有 npm scripts 的检查范围对齐:不检查 docs/ 与 templates/
|
||||
const allowedRoots = ['src/', 'scripts/', 'test/', '.github/', 'config/'];
|
||||
|
||||
Reference in New Issue
Block a user