mirror of
https://github.com/quartz-community/reader-mode.git
synced 2026-07-22 02:50:23 +00:00
67 lines
1.9 KiB
TypeScript
67 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,
|
|
tsconfig: "tsconfig.build.json",
|
|
sourcemap: true,
|
|
clean: true,
|
|
treeshake: true,
|
|
target: "es2022",
|
|
splitting: false,
|
|
noExternal: ["@quartz-community/utils"],
|
|
outDir: "dist",
|
|
platform: "node",
|
|
esbuildOptions(options) {
|
|
options.jsx = "automatic";
|
|
options.jsxImportSource = "preact";
|
|
},
|
|
esbuildPlugins: [inlineScriptPlugin],
|
|
});
|