From a3d8cb1a95a8d31a2e418dd4c09c16dc09efad79 Mon Sep 17 00:00:00 2001 From: saberzero1 Date: Sat, 14 Feb 2026 01:11:34 +0100 Subject: [PATCH] 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. --- tsup.config.ts | 77 ++++++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/tsup.config.ts b/tsup.config.ts index fbe17e1..6ea7abb 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -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: { @@ -18,36 +60,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], });