Skip to content

Commit 84cfbfb

Browse files
authored
feat: create a plugin for virtual 'usts:runtime' module (#8)
1 parent 7727cd5 commit 84cfbfb

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

packages/usts/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"./bin/cli": "./dist/bin/cli.mjs",
2323
"./config": "./dist/config.mjs",
2424
"./core": "./dist/core.mjs",
25-
"./package.json": "./package.json"
25+
"./package.json": "./package.json",
26+
"./types": "./virtual.d.ts"
2627
},
2728
"bin": {
2829
"usts": "./dist/bin/cli.mjs"

packages/usts/src/core/build/options.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
11
import * as path from "node:path";
2-
import type { InputOptions, OutputOptions } from "rolldown";
2+
import type { InputOptions, OutputOptions, Plugin } from "rolldown";
33
import type { ResolvedUserscriptConfig } from "~/config/schema";
44
import { serializeMetaHeader } from "./meta-header";
55

6+
function userscriptPlugin(options?: { dev?: boolean }) {
7+
const id = "usts:runtime";
8+
const resolvedId = `\0${id}`;
9+
return {
10+
name: "userscript-plugin",
11+
resolveId(source) {
12+
if (source === id) {
13+
return resolvedId;
14+
}
15+
return null;
16+
},
17+
load(id) {
18+
if (id === resolvedId) {
19+
return `const IS_DEV = ${options?.dev ?? false};
20+
export { IS_DEV };`;
21+
}
22+
return null;
23+
},
24+
} satisfies Plugin;
25+
}
26+
627
interface ResolvedOutputOptions extends OutputOptions {
728
readonly file: string;
829
}
@@ -15,7 +36,7 @@ interface ResolvedOptions extends InputOptions {
1536

1637
export function resolveOptions(
1738
config: ResolvedUserscriptConfig,
18-
options?: { write?: boolean },
39+
options?: { write?: boolean; dev?: boolean },
1940
): ResolvedOptions {
2041
const header = serializeMetaHeader(config.header);
2142

@@ -25,7 +46,7 @@ export function resolveOptions(
2546
return {
2647
input: config.entryPoint,
2748
tsconfig: true,
28-
plugins: [config.plugins],
49+
plugins: [config.plugins, userscriptPlugin({ dev: options?.dev })],
2950
output: {
3051
format: "iife",
3152
sourcemap: false,
@@ -34,6 +55,9 @@ export function resolveOptions(
3455
cleanDir: config.clean,
3556
file: outFile,
3657
},
58+
transform: {
59+
define: { "process.env.NODE_ENV": `"${process.env.NODE_ENV}"` },
60+
},
3761
write: options?.write ?? false,
3862
};
3963
}

packages/usts/src/core/build/watch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ async function watchUserscript(
1010
options?: { port?: number },
1111
): Promise<void> {
1212
const USERSCRIPT_OUTPUT_FILE_NAME = "index.user.js";
13-
const watchOptions = resolveOptions(config);
13+
const watchOptions = resolveOptions(config, { dev: true });
1414
rolldown.watch(watchOptions);
1515
await serve({
1616
port: options?.port ?? 3000,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module "usts:runtime" {
2+
const IS_DEV: boolean;
3+
export { IS_DEV };
4+
}

packages/usts/tsdown.config.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ export default defineConfig({
66
config: "src/config/index.ts",
77
"bin/cli": "src/bin/cli.ts",
88
},
9-
exports: true,
9+
copy: { from: "src/types/*.d.ts", to: "dist" },
10+
exports: {
11+
enabled: true,
12+
customExports(pkg) {
13+
pkg["./types"] = "./virtual.d.ts";
14+
return pkg;
15+
},
16+
},
1017
hash: false,
1118
outputOptions: { minify: "dce-only", minifyInternalExports: false },
1219
dts: true,

0 commit comments

Comments
 (0)