fix: standardize inline script transpilation in tsup config

Use stdin-based esbuild.build with proper TS transpilation, minification,
and ESM output format matching v5's proven inline-script-loader pattern.
This commit is contained in:
saberzero1 2026-02-14 01:11:31 +01:00
parent b7d0660d2f
commit 6b10d168ff
No known key found for this signature in database

View file

@ -1,5 +1,47 @@
import { defineConfig } from "tsup";
import * as esbuild from "esbuild";
import type { Plugin } from "esbuild";
import path from "path";
const inlineScriptPlugin: Plugin = {
name: "inline-script-loader",
setup(parentBuild) {
const absWorkingDir = parentBuild.initialOptions.absWorkingDir ?? process.cwd();
parentBuild.onLoad({ filter: /\.scss$/ }, async (args) => {
const fs = await import("fs");
const text = await fs.promises.readFile(args.path, "utf8");
return { contents: text, loader: "text" };
});
parentBuild.onLoad({ filter: /\.inline\.ts$/ }, async (args) => {
const esbuild = await import("esbuild");
const fs = await import("fs");
let text = await fs.promises.readFile(args.path, "utf8");
text = text.replace(/^export default /gm, "");
text = text.replace(/^export /gm, "");
const resolveDir = path.dirname(args.path);
const sourcefile = path.relative(absWorkingDir, args.path);
const result = await esbuild.build({
stdin: { contents: text, loader: "ts", resolveDir, sourcefile },
write: false,
bundle: true,
minify: true,
platform: "browser",
format: "esm",
target: "es2020",
sourcemap: false,
external: ["http://*", "https://*"],
});
const js = result.outputFiles?.[0]?.text;
if (!js) throw new Error(`inline-script-loader: no JS output for ${args.path}`);
return { contents: js, loader: "text" };
});
},
};
export default defineConfig({
entry: {
@ -19,36 +61,5 @@ export default defineConfig({
options.jsx = "automatic";
options.jsxImportSource = "preact";
},
esbuildPlugins: [
{
name: "text-loader",
setup(build) {
build.onLoad({ filter: /\.scss$/ }, async (args) => {
const fs = await import("fs");
const text = await fs.promises.readFile(args.path, "utf8");
return {
contents: text,
loader: "text",
};
});
build.onLoad({ filter: /\.inline\.ts$/ }, async (args) => {
const result = await esbuild.build({
entryPoints: [args.path],
bundle: true,
write: false,
format: "iife",
target: "es2022",
minify: false,
platform: "browser",
});
const code = result.outputFiles?.[0]?.text ?? "";
return {
contents: `export default ${JSON.stringify(code)};`,
loader: "ts",
};
});
},
},
],
esbuildPlugins: [inlineScriptPlugin],
});