2025-02-24 09:35:51 +00:00
|
|
|
import esbuild from "esbuild";
|
|
|
|
|
import process from "process";
|
2026-05-17 03:45:29 +00:00
|
|
|
import { builtinModules as builtins } from "module";
|
2026-06-11 15:31:14 +00:00
|
|
|
import fs from "fs";
|
2025-02-24 09:35:51 +00:00
|
|
|
|
|
|
|
|
const banner =
|
|
|
|
|
`/*
|
|
|
|
|
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
|
|
|
|
|
if you want to view the source, please visit the github repository of this plugin
|
|
|
|
|
*/
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const prod = (process.argv[2] === "production");
|
|
|
|
|
|
2026-06-11 15:31:14 +00:00
|
|
|
const fixDynamicScriptPlugin = {
|
|
|
|
|
name: 'fix-dynamic-script',
|
|
|
|
|
setup(build) {
|
|
|
|
|
build.onEnd(() => {
|
|
|
|
|
try {
|
|
|
|
|
if (fs.existsSync("main.js")) {
|
|
|
|
|
let content = fs.readFileSync("main.js", "utf8");
|
2026-06-11 16:12:48 +00:00
|
|
|
// 1. 替換動態 script 建立
|
|
|
|
|
let replaced = content.replace(/createElement\(\s*['"]script['"]\s*\)/g, 'createElement("div")');
|
|
|
|
|
// 2. 替換 setImmediate polyfill 中的 new Function
|
|
|
|
|
replaced = replaced.replace(/new\s+Function\(\s*(['"])\1\s*\+\s*\w+\)/g, 'function(){throw new Error("Dynamic code execution is disabled")}');
|
2026-06-11 15:31:14 +00:00
|
|
|
fs.writeFileSync("main.js", replaced, "utf8");
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Failed to post-process main.js:", err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-24 09:35:51 +00:00
|
|
|
const context = await esbuild.context({
|
|
|
|
|
banner: {
|
|
|
|
|
js: banner,
|
|
|
|
|
},
|
2025-07-14 16:10:42 +00:00
|
|
|
entryPoints: ["src/main.ts"],
|
2025-02-24 09:35:51 +00:00
|
|
|
bundle: true,
|
|
|
|
|
external: [
|
|
|
|
|
"obsidian",
|
|
|
|
|
"electron",
|
|
|
|
|
"@codemirror/autocomplete",
|
|
|
|
|
"@codemirror/collab",
|
|
|
|
|
"@codemirror/commands",
|
|
|
|
|
"@codemirror/language",
|
|
|
|
|
"@codemirror/lint",
|
|
|
|
|
"@codemirror/search",
|
|
|
|
|
"@codemirror/state",
|
|
|
|
|
"@codemirror/view",
|
|
|
|
|
"@lezer/common",
|
|
|
|
|
"@lezer/highlight",
|
|
|
|
|
"@lezer/lr",
|
|
|
|
|
...builtins],
|
|
|
|
|
format: "cjs",
|
|
|
|
|
target: "es2018",
|
|
|
|
|
logLevel: "info",
|
|
|
|
|
sourcemap: prod ? false : "inline",
|
|
|
|
|
treeShaking: true,
|
|
|
|
|
outfile: "main.js",
|
|
|
|
|
minify: prod,
|
2026-06-11 15:31:14 +00:00
|
|
|
plugins: [fixDynamicScriptPlugin],
|
2025-02-24 09:35:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (prod) {
|
|
|
|
|
await context.rebuild();
|
|
|
|
|
process.exit(0);
|
|
|
|
|
} else {
|
|
|
|
|
await context.watch();
|
|
|
|
|
}
|