11import * as path from "node:path" ;
2- import type { InputOptions , OutputOptions } from "rolldown" ;
2+ import type { InputOptions , OutputOptions , Plugin } from "rolldown" ;
33import type { ResolvedUserscriptConfig } from "~/config/schema" ;
44import { 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+
627interface ResolvedOutputOptions extends OutputOptions {
728 readonly file : string ;
829}
@@ -15,7 +36,7 @@ interface ResolvedOptions extends InputOptions {
1536
1637export 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}
0 commit comments