reader-mode/tsup.config.ts
saberzero1 2250ce03e9
chore: set tsup platform to node
Plugins run server-side during the Quartz build step, not in the
browser. Setting platform: 'node' tells esbuild to treat Node builtins
as available externals and avoids CJS-in-ESM require() failures for
any bundled dependencies that use Node APIs.
2026-03-17 20:41:14 +01:00

65 lines
1.9 KiB
TypeScript

import { defineConfig } from "tsup";
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 sass = await import("sass");
const result = sass.compile(args.path);
return { contents: result.css, 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: {
index: "src/index.ts",
"components/index": "src/components/index.ts",
},
format: ["esm"],
dts: true,
sourcemap: true,
clean: true,
treeshake: true,
target: "es2022",
splitting: false,
outDir: "dist",
platform: "node",
esbuildOptions(options) {
options.jsx = "automatic";
options.jsxImportSource = "preact";
},
esbuildPlugins: [inlineScriptPlugin],
});