chore:Remove unneeded script tag generation.

Obsidian's scanner really didn't like that!
This commit is contained in:
Stephen Granade 2026-05-16 17:08:40 -05:00
parent 19b87575af
commit 55b80c6d38
2 changed files with 41 additions and 3 deletions

View file

@ -1,6 +1,7 @@
import esbuild from "esbuild";
import process from "process";
import { copyFileSync, readFileSync, writeFileSync } from "fs";
import { readFile } from "fs/promises";
const pkg = JSON.parse(readFileSync("./package.json", "utf8"));
const banner = `/*! ${pkg.name} v${pkg.version} | (c) ${pkg.author.name} | ${pkg.author.url} */`;
@ -20,7 +21,7 @@ const esbuildProblemMatcherPlugin = {
result.errors.forEach(({ text, location }) => {
console.error(`✘ [ERROR] ${text}`);
console.error(
` ${location.file}:${location.line}:${location.column}:`
` ${location.file}:${location.line}:${location.column}:`,
);
});
console.log("[watch] build finished");
@ -44,13 +45,48 @@ const updateManifestPlugin = {
manifest.version = pkg.version;
writeFileSync(
outdir + "/manifest.json",
JSON.stringify(manifest, null, 2)
JSON.stringify(manifest, null, 2),
);
console.log("✅ manifest.json updated");
});
},
};
const stripScriptTagsPlugin = {
// `docx` uses `jszip`, which uses `lie`, which uses `immediate`. That module
// includes an ancient polyfill to handle microtask scheduling that dynamically injects
// a `<script>` tag. Obsidian's plugin scanning code really doesn't like that. Since
// the plugin will run in Electron and that branch is never reached, we'll blot it out.
// Sadly, `docx` bundles everything into a single `dist/index.mjs` file, not a library,
// so we have to use some blunt force regex instead of introducing aliasing `immediate`
// to a shim in the build context.
// N.B. These regexes must be reviewed every time we update the `docx` module.
name: "strip-immediate-code",
setup(build) {
build.onLoad(
{ filter: /docx[/|\\]dist[/|\\]index\.mjs$/ },
async (args) => {
let text = await readFile(args.path, "utf8");
// block the script-tag fallback branch that'll never be reached in Electron
text = text.replace(
/"onreadystatechange"\s*in\s*l\.createElement\("script"\)/g,
"false",
);
// Remove the calls to `createElement("script")` that annoy the Obsidian scanner
text = text.replace(
/createElement\(["']script["']\)/g,
"createElement('canvas')",
);
return { contents: text, loader: "js" };
},
);
},
};
const production = process.argv.includes("--production");
const watch = process.argv.includes("--watch");
@ -63,6 +99,7 @@ const ctx = await esbuild.context({
format: "cjs",
target: "es2020",
platform: "node",
mainFields: ["module", "main"],
logLevel: "info",
bundle: true,
sourcemap: production ? false : "inline",
@ -70,6 +107,7 @@ const ctx = await esbuild.context({
outdir: outdir,
minify: production,
plugins: [
stripScriptTagsPlugin,
updateManifestPlugin,
/* add to the end of plugins array */
esbuildProblemMatcherPlugin,

View file

@ -9,7 +9,7 @@
"importHelpers": true,
"isolatedModules": true,
"strictNullChecks": true,
"allowSyntheticDefaultImports": true, // Needed for jszip as of 3.10.1
// "allowSyntheticDefaultImports": true, // Was needed for jszip as of 3.10.1
"lib": [
"DOM",
"es2020",