mirror of
https://github.com/quartz-community/darkmode.git
synced 2026-07-22 02:50:22 +00:00
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:
parent
b5d772075e
commit
a3d8cb1a95
1 changed files with 44 additions and 33 deletions
|
|
@ -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],
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue