Files
eSIM-Tools/scripts/build-static.js
Abner d37961c614 feat: 添加 JS 转译脚本以支持 Chrome 77 目标
此提交引入了一个新的 JavaScript 转译功能,用于在构建过程中将现代 JavaScript 代码转换为与 Chrome 77 兼容的版本。

- 新增 transpile-dist-js.js 脚本,使用 Babel 将 dist/src 目录下的 JS 文件转译为 Chrome 77 目标
- 在 build-static.js 中集成转译步骤,确保在注入 Sentry 配置前完成代码转换
- 转译过程会自动跳过 minified 文件和 vendor 目录,仅处理需要转换的源文件
- 添加了详细的日志记录,显示处理文件数量和更新文件数量
2026-03-26 21:43:16 +08:00

88 lines
2.4 KiB
JavaScript
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
const BuildLogger = require('./logger.js');
const transpileDistJs = require('./transpile-dist-js.js');
const fs = require('fs');
const path = require('path');
const projectRoot = path.join(__dirname, '..');
const distDir = path.join(projectRoot, 'dist');
const entries = [
'index.html',
'manifest.webmanifest',
'src'
];
async function removeDist() {
await fs.promises.rm(distDir, { recursive: true, force: true });
}
async function copyEntry(entry) {
const from = path.join(projectRoot, entry);
const to = path.join(distDir, entry);
if (!fs.existsSync(from)) {
console.warn(`⚠️ 跳过不存在的入口: ${entry}`);
return;
}
try {
const stats = await fs.promises.stat(from);
if (stats.isDirectory()) {
await copyDirectory(from, to);
} else {
await fs.promises.mkdir(path.dirname(to), { recursive: true });
await fs.promises.copyFile(from, to);
}
} catch (err) {
console.error(`❌ 复制失败 ${entry}:`, err.message);
throw err;
}
}
async function copyDirectory(source, destination) {
try {
await fs.promises.mkdir(destination, { recursive: true });
const entries = await fs.promises.readdir(source, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(source, entry.name);
const destPath = path.join(destination, entry.name);
try {
if (entry.isDirectory()) {
await copyDirectory(srcPath, destPath);
} else if (entry.isFile()) {
await fs.promises.copyFile(srcPath, destPath);
}
} catch (fileErr) {
console.warn(`⚠️ 跳过文件 ${entry.name}:`, fileErr.message);
// 继续处理其他文件
}
}
} catch (err) {
throw new Error(`复制目录失败 ${source}: ${err.message}`);
}
}
(async () => {
BuildLogger.log('🧹 清理 dist 目录...');
await removeDist();
await fs.promises.mkdir(distDir, { recursive: true });
for (const entry of entries) {
BuildLogger.log(`📦 复制 ${entry} -> dist/${entry}`);
await copyEntry(entry);
}
BuildLogger.log('🧩 转译 dist/src 下的 JS目标 Chrome 77...');
await transpileDistJs();
// 注入 Sentry 配置
BuildLogger.log('🔧 注入 Sentry 配置...');
require('./inject-sentry-config.js');
BuildLogger.success(' 静态资源构建完成,输出目录 dist/');
})().catch(err => {
console.error('构建静态资源失败:', err);
process.exitCode = 1;
});