Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/usts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"./bin/cli": "./dist/bin/cli.mjs",
"./config": "./dist/config.mjs",
"./core": "./dist/core.mjs",
"./package.json": "./package.json"
"./package.json": "./package.json",
"./types": "./virtual.d.ts"
},
"bin": {
"usts": "./dist/bin/cli.mjs"
Expand Down
30 changes: 27 additions & 3 deletions packages/usts/src/core/build/options.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
import * as path from "node:path";
import type { InputOptions, OutputOptions } from "rolldown";
import type { InputOptions, OutputOptions, Plugin } from "rolldown";
import type { ResolvedUserscriptConfig } from "~/config/schema";
import { serializeMetaHeader } from "./meta-header";

function userscriptPlugin(options?: { dev?: boolean }) {
const id = "usts:runtime";
const resolvedId = `\0${id}`;
return {
name: "userscript-plugin",
resolveId(source) {
if (source === id) {
return resolvedId;
}
return null;
},
load(id) {
if (id === resolvedId) {
return `const IS_DEV = ${options?.dev ?? false};
export { IS_DEV };`;
}
return null;
},
} satisfies Plugin;
}

interface ResolvedOutputOptions extends OutputOptions {
readonly file: string;
}
Expand All @@ -15,7 +36,7 @@ interface ResolvedOptions extends InputOptions {

export function resolveOptions(
config: ResolvedUserscriptConfig,
options?: { write?: boolean },
options?: { write?: boolean; dev?: boolean },
): ResolvedOptions {
const header = serializeMetaHeader(config.header);

Expand All @@ -25,7 +46,7 @@ export function resolveOptions(
return {
input: config.entryPoint,
tsconfig: true,
plugins: [config.plugins],
plugins: [config.plugins, userscriptPlugin({ dev: options?.dev })],
output: {
format: "iife",
sourcemap: false,
Expand All @@ -34,6 +55,9 @@ export function resolveOptions(
cleanDir: config.clean,
file: outFile,
},
transform: {
define: { "process.env.NODE_ENV": `"${process.env.NODE_ENV}"` },
},
write: options?.write ?? false,
};
}
2 changes: 1 addition & 1 deletion packages/usts/src/core/build/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async function watchUserscript(
options?: { port?: number },
): Promise<void> {
const USERSCRIPT_OUTPUT_FILE_NAME = "index.user.js";
const watchOptions = resolveOptions(config);
const watchOptions = resolveOptions(config, { dev: true });
rolldown.watch(watchOptions);
await serve({
port: options?.port ?? 3000,
Expand Down
4 changes: 4 additions & 0 deletions packages/usts/src/types/virtual.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "usts:runtime" {
const IS_DEV: boolean;
export { IS_DEV };
}
9 changes: 8 additions & 1 deletion packages/usts/tsdown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ export default defineConfig({
config: "src/config/index.ts",
"bin/cli": "src/bin/cli.ts",
},
exports: true,
copy: { from: "src/types/*.d.ts", to: "dist" },
exports: {
enabled: true,
customExports(pkg) {
pkg["./types"] = "./virtual.d.ts";
return pkg;
},
},
hash: false,
outputOptions: { minify: "dce-only", minifyInternalExports: false },
dts: true,
Expand Down