@powerlines/plugin-unenv 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1427 @@
1
+ import * as unenv from 'unenv';
2
+ import { CreateEnvOptions } from 'unenv';
3
+ import { Format } from '@storm-software/build-tools/types';
4
+ import { LogLevelLabel } from '@storm-software/config-tools/types';
5
+ import { StormWorkspaceConfig } from '@storm-software/config/types';
6
+ import { MaybePromise, NonUndefined, FunctionLike } from '@stryke/types/base';
7
+ import { TypeDefinitionParameter, TypeDefinition } from '@stryke/types/configuration';
8
+ import { AssetGlob } from '@stryke/types/file';
9
+ import { PreviewOptions, ResolvedPreviewOptions } from 'vite';
10
+ import { EnvPaths } from '@stryke/env/get-env-paths';
11
+ import { PackageJson } from '@stryke/types/package-json';
12
+ import { Jiti } from 'jiti';
13
+ import { ParserOptions, ParseResult } from 'oxc-parser';
14
+ import { Range } from 'semver';
15
+ import { UnpluginMessage, UnpluginContext, UnpluginBuildContext, TransformResult, ExternalIdResult, HookFilter, UnpluginOptions } from 'unplugin';
16
+ import { TsConfigJson, CompilerOptions } from '@stryke/types/tsconfig';
17
+ import ts from 'typescript';
18
+ import { PrimitiveJsonValue } from '@stryke/json/types';
19
+ import { PathLike, StatSyncOptions, Stats, RmDirOptions, RmOptions, Mode, MakeDirectoryOptions as MakeDirectoryOptions$1, PathOrFileDescriptor, WriteFileOptions as WriteFileOptions$1 } from 'node:fs';
20
+ import { ArrayValues } from '@stryke/types/array';
21
+
22
+ type UnpluginBuildVariant = "rollup" | "webpack" | "rspack" | "vite" | "esbuild" | "farm" | "unloader" | "rolldown";
23
+ interface BuildConfig {
24
+ /**
25
+ * The platform to build the project for
26
+ *
27
+ * @defaultValue "neutral"
28
+ */
29
+ platform?: "node" | "browser" | "neutral";
30
+ /**
31
+ * Array of strings indicating the polyfills to include for the build.
32
+ *
33
+ * @remarks
34
+ * This option allows you to specify which polyfills should be included in the build process to ensure compatibility with the target environment. The paths for the polyfills can use placeholder tokens (the `replacePathTokens` helper function will be used to resolve the actual values).
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * {
39
+ * polyfill: ['{projectRoot}/custom-polyfill.ts']
40
+ * }
41
+ * ```
42
+ */
43
+ polyfill?: string[];
44
+ /**
45
+ * Array of strings indicating the order in which fields in a package.json file should be resolved to determine the entry point for a module.
46
+ *
47
+ * @defaultValue `['browser', 'module', 'jsnext:main', 'jsnext']`
48
+ */
49
+ mainFields?: string[];
50
+ /**
51
+ * Array of strings indicating what conditions should be used for module resolution.
52
+ */
53
+ conditions?: string[];
54
+ /**
55
+ * Array of strings indicating what file extensions should be used for module resolution.
56
+ *
57
+ * @defaultValue `['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json']`
58
+ */
59
+ extensions?: string[];
60
+ /**
61
+ * Array of strings indicating what modules should be deduplicated to a single version in the build.
62
+ *
63
+ * @remarks
64
+ * This option is useful for ensuring that only one version of a module is included in the bundle, which can help reduce bundle size and avoid conflicts.
65
+ */
66
+ dedupe?: string[];
67
+ /**
68
+ * Array of strings or regular expressions that indicate what modules are builtin for the environment.
69
+ */
70
+ builtins?: (string | RegExp)[];
71
+ /**
72
+ * Define global variable replacements.
73
+ *
74
+ * @remarks
75
+ * This option allows you to specify global constants that will be replaced in the code during the build process. It is similar to the `define` option in esbuild and Vite, enabling you to replace specific identifiers with constant expressions at build time.
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * {
80
+ * define: {
81
+ * __VERSION__: '"1.0.0"',
82
+ * __DEV__: 'process.env.NODE_ENV !== "production"'
83
+ * }
84
+ * }
85
+ * ```
86
+ *
87
+ * @see https://esbuild.github.io/api/#define
88
+ * @see https://vitejs.dev/config/build-options.html#define
89
+ * @see https://github.com/rollup/plugins/tree/master/packages/replace
90
+ */
91
+ define?: Record<string, any>;
92
+ /**
93
+ * Global variables that will have import statements injected where necessary
94
+ *
95
+ * @remarks
96
+ * This option allows you to specify global variables that should be automatically imported from specified modules whenever they are used in the code. This is particularly useful for polyfilling Node.js globals in a browser environment.
97
+ *
98
+ * @example
99
+ * ```ts
100
+ * {
101
+ * inject: {
102
+ * process: 'process/browser',
103
+ * Buffer: ['buffer', 'Buffer'],
104
+ * }
105
+ * }
106
+ * ```
107
+ *
108
+ * @see https://github.com/rollup/plugins/tree/master/packages/inject
109
+ */
110
+ inject?: Record<string, string | string[]>;
111
+ /**
112
+ * The alias mappings to use for module resolution during the build process.
113
+ *
114
+ * @remarks
115
+ * This option allows you to define custom path aliases for modules, which can be useful for simplifying imports and managing dependencies.
116
+ *
117
+ * @example
118
+ * ```ts
119
+ * {
120
+ * alias: {
121
+ * "@utils": "./src/utils",
122
+ * "@components": "./src/components"
123
+ * }
124
+ * }
125
+ * ```
126
+ *
127
+ * @see https://github.com/rollup/plugins/tree/master/packages/alias
128
+ */
129
+ alias?: Record<string, string> | Array<{
130
+ find: string | RegExp;
131
+ replacement: string;
132
+ }>;
133
+ /**
134
+ * A list of modules that should not be bundled, even if they are external dependencies.
135
+ *
136
+ * @remarks
137
+ * This option is useful for excluding specific modules from the bundle, such as Node.js built-in modules or other libraries that should not be bundled.
138
+ */
139
+ external?: (string | RegExp)[];
140
+ /**
141
+ * A list of modules that should always be bundled, even if they are external dependencies.
142
+ */
143
+ noExternal?: (string | RegExp)[];
144
+ /**
145
+ * Should the Powerlines CLI processes skip bundling the `node_modules` directory?
146
+ */
147
+ skipNodeModulesBundle?: boolean;
148
+ /**
149
+ * An optional set of override options to apply to the selected build variant.
150
+ *
151
+ * @remarks
152
+ * This option allows you to provide configuration options with the guarantee that they will **not** be overridden and will take precedence over other build configurations.
153
+ */
154
+ override?: Record<string, any>;
155
+ }
156
+ type BuildResolvedConfig = Omit<BuildConfig, "override">;
157
+
158
+ type ReflectionMode = "default" | "explicit" | "never";
159
+ type RawReflectionMode = ReflectionMode | "" | boolean | string | string[] | undefined;
160
+ /**
161
+ * Defines the level of reflection to be used during the transpilation process.
162
+ *
163
+ * @remarks
164
+ * The level determines how much extra data is captured in the byte code for each type. This can be one of the following values:
165
+ * - `minimal` - Only the essential type information is captured.
166
+ * - `normal` - Additional type information is captured, including some contextual data.
167
+ * - `verbose` - All available type information is captured, including detailed contextual data.
168
+ */
169
+ type ReflectionLevel = "minimal" | "normal" | "verbose";
170
+ interface DeepkitOptions {
171
+ /**
172
+ * Either true to activate reflection for all files compiled using this tsconfig,
173
+ * or a list of globs/file paths relative to this tsconfig.json.
174
+ * Globs/file paths can be prefixed with a ! to exclude them.
175
+ */
176
+ reflection?: RawReflectionMode;
177
+ /**
178
+ * Defines the level of reflection to be used during the transpilation process.
179
+ *
180
+ * @remarks
181
+ * The level determines how much extra data is captured in the byte code for each type. This can be one of the following values:
182
+ * - `minimal` - Only the essential type information is captured.
183
+ * - `normal` - Additional type information is captured, including some contextual data.
184
+ * - `verbose` - All available type information is captured, including detailed contextual data.
185
+ */
186
+ reflectionLevel?: ReflectionLevel;
187
+ }
188
+ type TSCompilerOptions = CompilerOptions & DeepkitOptions;
189
+ /**
190
+ * The TypeScript compiler configuration.
191
+ *
192
+ * @see https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
193
+ */
194
+ interface TSConfig extends Omit<TsConfigJson, "reflection"> {
195
+ /**
196
+ * Either true to activate reflection for all files compiled using this tsconfig,
197
+ * or a list of globs/file paths relative to this tsconfig.json.
198
+ * Globs/file paths can be prefixed with a ! to exclude them.
199
+ */
200
+ reflection?: RawReflectionMode;
201
+ /**
202
+ * Defines the level of reflection to be used during the transpilation process.
203
+ *
204
+ * @remarks
205
+ * The level determines how much extra data is captured in the byte code for each type. This can be one of the following values:
206
+ * - `minimal` - Only the essential type information is captured.
207
+ * - `normal` - Additional type information is captured, including some contextual data.
208
+ * - `verbose` - All available type information is captured, including detailed contextual data.
209
+ */
210
+ reflectionLevel?: ReflectionLevel;
211
+ /**
212
+ * Instructs the TypeScript compiler how to compile `.ts` files.
213
+ */
214
+ compilerOptions?: TSCompilerOptions;
215
+ }
216
+ type ParsedTypeScriptConfig = ts.ParsedCommandLine & {
217
+ originalTsconfigJson: TsConfigJson;
218
+ tsconfigJson: TSConfig;
219
+ tsconfigFilePath: string;
220
+ };
221
+
222
+ declare const __VFS_PATCH__: unique symbol;
223
+ declare const __VFS_REVERT__: unique symbol;
224
+ type OutputModeType = "fs" | "virtual";
225
+ interface VirtualFile {
226
+ /**
227
+ * The unique identifier for the virtual file.
228
+ *
229
+ * @remarks
230
+ * If no specific id is provided, it defaults to the file's {@link path}.
231
+ */
232
+ id: string;
233
+ /**
234
+ * Additional metadata associated with the virtual file.
235
+ */
236
+ details: Record<string, PrimitiveJsonValue>;
237
+ /**
238
+ * The variant of the file.
239
+ *
240
+ * @remarks
241
+ * This string represents the purpose/function of the file in the virtual file system. A potential list of variants includes:
242
+ * - `builtin`: Indicates that the file is a built-in module provided by the system.
243
+ * - `entry`: Indicates that the file is an entry point for execution.
244
+ * - `normal`: Indicates that the file is a standard file without any special role.
245
+ */
246
+ variant: string;
247
+ /**
248
+ * The output mode of the file.
249
+ *
250
+ * @remarks
251
+ * This indicates whether the file is intended to be written to the actual file system (`fs`) or kept in the virtual file system (`virtual`).
252
+ */
253
+ mode: OutputModeType;
254
+ /**
255
+ * A virtual (or actual) path to the file in the file system.
256
+ */
257
+ path: string;
258
+ /**
259
+ * The contents of the file.
260
+ */
261
+ code: string | NodeJS.ArrayBufferView;
262
+ }
263
+ interface VirtualFileMetadata {
264
+ /**
265
+ * The identifier for the file data.
266
+ */
267
+ id: string;
268
+ /**
269
+ * The variant of the file.
270
+ */
271
+ variant: string;
272
+ /**
273
+ * The output mode of the file.
274
+ */
275
+ mode: string;
276
+ /**
277
+ * Additional metadata associated with the file.
278
+ */
279
+ properties: Record<string, string>;
280
+ }
281
+ interface ResolveFSOptions {
282
+ mode?: OutputModeType;
283
+ }
284
+ type MakeDirectoryOptions = (Mode | MakeDirectoryOptions$1) & ResolveFSOptions;
285
+ interface PowerlinesWriteFileOptions extends ResolveFSOptions {
286
+ skipFormat?: boolean;
287
+ }
288
+ type NodeWriteFileOptions = WriteFileOptions$1;
289
+ type WriteFileOptions = NodeWriteFileOptions | PowerlinesWriteFileOptions;
290
+ type PowerLinesWriteFileData = Partial<Omit<VirtualFile, "path" | "mode" | "code">> & Pick<VirtualFile, "code">;
291
+ type WriteFileData = string | NodeJS.ArrayBufferView | PowerLinesWriteFileData;
292
+ interface ResolvePathOptions extends ResolveFSOptions {
293
+ /**
294
+ * Should the resolved path include the file extension?
295
+ *
296
+ * @defaultValue true
297
+ */
298
+ withExtension?: boolean;
299
+ /**
300
+ * The paths to search for the file.
301
+ */
302
+ paths?: string[];
303
+ /**
304
+ * The type of the path to resolve.
305
+ */
306
+ type?: "file" | "directory";
307
+ }
308
+ interface VirtualFileSystemInterface {
309
+ /**
310
+ * Patches the File System to include the virtual file system (VFS) contents.
311
+ */
312
+ [__VFS_PATCH__]: () => void;
313
+ /**
314
+ * Reverts the virtual file system (VFS) to its previous state.
315
+ */
316
+ [__VFS_REVERT__]: () => void;
317
+ /**
318
+ * The underlying file metadata.
319
+ */
320
+ metadata: Record<string, VirtualFileMetadata | undefined>;
321
+ /**
322
+ * A map of module ids to their file paths.
323
+ */
324
+ ids: Record<string, string>;
325
+ /**
326
+ * Check if a path or id corresponds to a virtual file **(does not actually exists on disk)**.
327
+ *
328
+ * @param pathOrId - The path or id to check.
329
+ * @param options - Optional parameters for resolving the path.
330
+ * @returns Whether the path or id corresponds to a virtual file **(does not actually exists on disk)**.
331
+ */
332
+ isVirtual: (pathOrId: string, options?: ResolvePathOptions) => boolean;
333
+ /**
334
+ * Check if a path or id corresponds to a file written to the file system **(actually exists on disk)**.
335
+ *
336
+ * @param pathOrId - The path or id to check.
337
+ * @param options - Optional parameters for resolving the path.
338
+ * @returns Whether the path or id corresponds to a file written to the file system **(actually exists on disk)**.
339
+ */
340
+ isPhysical: (pathOrId: string, options?: ResolvePathOptions) => boolean;
341
+ /**
342
+ * Checks if a file exists in the virtual file system (VFS).
343
+ *
344
+ * @param path - The path of the file to check.
345
+ * @returns `true` if the file exists, otherwise `false`.
346
+ */
347
+ isFile: (path: string) => boolean;
348
+ /**
349
+ * Checks if a directory exists in the virtual file system (VFS).
350
+ *
351
+ * @param path - The path of the directory to check.
352
+ * @returns `true` if the directory exists, otherwise `false`.
353
+ */
354
+ isDirectory: (path: string) => boolean;
355
+ /**
356
+ * Checks if a file exists in the virtual file system (VFS).
357
+ *
358
+ * @param pathOrId - The path or id of the file.
359
+ * @returns `true` if the file exists, otherwise `false`.
360
+ */
361
+ existsSync: (pathOrId: string) => boolean;
362
+ /**
363
+ * Gets the metadata of a file in the virtual file system (VFS).
364
+ *
365
+ * @param pathOrId - The path or id of the file.
366
+ * @returns The metadata of the file if it exists, otherwise undefined.
367
+ */
368
+ getMetadata: (pathOrId: PathLike) => VirtualFileMetadata | undefined;
369
+ /**
370
+ * Gets the stats of a file in the virtual file system (VFS).
371
+ *
372
+ * @param pathOrId - The path or id of the file.
373
+ * @param options - Optional parameters for getting the stats.
374
+ * @returns The stats of the file if it exists, otherwise undefined.
375
+ */
376
+ lstat: (pathOrId: string, options?: StatSyncOptions & {
377
+ bigint?: false | undefined;
378
+ throwIfNoEntry: false;
379
+ }) => Promise<Stats>;
380
+ /**
381
+ * Gets the stats of a file in the virtual file system (VFS).
382
+ *
383
+ * @param pathOrId - The path or id of the file.
384
+ * @param options - Optional parameters for getting the stats.
385
+ * @returns The stats of the file if it exists, otherwise undefined.
386
+ */
387
+ lstatSync: (pathOrId: string, options?: StatSyncOptions & {
388
+ bigint?: false | undefined;
389
+ throwIfNoEntry: false;
390
+ }) => Stats | undefined;
391
+ /**
392
+ * Gets the stats of a file in the virtual file system (VFS).
393
+ *
394
+ * @param pathOrId - The path or id of the file.
395
+ * @returns The stats of the file if it exists, otherwise false.
396
+ */
397
+ stat: (pathOrId: string, options?: StatSyncOptions & {
398
+ bigint?: false | undefined;
399
+ throwIfNoEntry: false;
400
+ }) => Promise<Stats>;
401
+ /**
402
+ * Gets the stats of a file in the virtual file system (VFS).
403
+ *
404
+ * @param pathOrId - The path or id of the file.
405
+ * @returns The stats of the file if it exists, otherwise false.
406
+ */
407
+ statSync: (pathOrId: string, options?: StatSyncOptions & {
408
+ bigint?: false | undefined;
409
+ throwIfNoEntry: false;
410
+ }) => Stats | undefined;
411
+ /**
412
+ * Lists files in a given path.
413
+ *
414
+ * @param path - The path to list files from.
415
+ * @param options - Options for listing files, such as encoding and recursion.
416
+ * @returns An array of file names in the specified path.
417
+ */
418
+ readdirSync: (path: string, options?: {
419
+ encoding: BufferEncoding | null;
420
+ withFileTypes?: false | undefined;
421
+ recursive?: boolean | undefined;
422
+ } | BufferEncoding) => string[];
423
+ /**
424
+ * Lists files in a given path.
425
+ *
426
+ * @param path - The path to list files from.
427
+ * @param options - Options for listing files, such as encoding and recursion.
428
+ * @returns An array of file names in the specified path.
429
+ */
430
+ readdir: (path: string, options?: {
431
+ encoding: BufferEncoding | null;
432
+ withFileTypes?: false | undefined;
433
+ recursive?: boolean | undefined;
434
+ } | BufferEncoding) => Promise<string[]>;
435
+ /**
436
+ * Removes a file or symbolic link in the virtual file system (VFS).
437
+ *
438
+ * @param path - The path to the file to remove.
439
+ * @returns A promise that resolves when the file is removed.
440
+ */
441
+ unlinkSync: (path: PathLike, options?: ResolveFSOptions) => void;
442
+ /**
443
+ * Asynchronously removes a file or symbolic link in the virtual file system (VFS).
444
+ *
445
+ * @param path - The path to the file to remove.
446
+ * @returns A promise that resolves when the file is removed.
447
+ */
448
+ unlink: (path: string, options?: ResolveFSOptions) => Promise<void>;
449
+ /**
450
+ * Removes a directory in the virtual file system (VFS).
451
+ *
452
+ * @param path - The path to create the directory at.
453
+ * @param options - Options for creating the directory.
454
+ */
455
+ rmdirSync: (path: PathLike, options?: RmDirOptions & ResolveFSOptions) => any;
456
+ /**
457
+ * Removes a directory in the virtual file system (VFS).
458
+ *
459
+ * @param path - The path to create the directory at.
460
+ * @param options - Options for creating the directory.
461
+ * @returns A promise that resolves to the path of the created directory, or undefined if the directory could not be created.
462
+ */
463
+ rmdir: (path: PathLike, options?: RmDirOptions & ResolveFSOptions) => Promise<void>;
464
+ /**
465
+ * Removes a file or directory in the virtual file system (VFS).
466
+ *
467
+ * @param path - The path to the file or directory to remove.
468
+ * @param options - Options for removing the file or directory.
469
+ * @returns A promise that resolves when the file or directory is removed.
470
+ */
471
+ rm: (path: PathLike, options?: RmOptions & ResolveFSOptions) => Promise<void>;
472
+ /**
473
+ * Synchronously removes a file or directory in the virtual file system (VFS).
474
+ *
475
+ * @param path - The path to the file or directory to remove.
476
+ * @param options - Options for removing the file or directory.
477
+ */
478
+ rmSync: (path: PathLike, options?: RmOptions & ResolveFSOptions) => void;
479
+ /**
480
+ * Creates a directory in the virtual file system (VFS).
481
+ *
482
+ * @param path - The path to create the directory at.
483
+ * @param options - Options for creating the directory.
484
+ * @returns A promise that resolves to the path of the created directory, or undefined if the directory could not be created.
485
+ */
486
+ mkdirSync: (path: PathLike, options?: MakeDirectoryOptions) => string | undefined;
487
+ /**
488
+ * Creates a directory in the virtual file system (VFS).
489
+ *
490
+ * @param path - The path to create the directory at.
491
+ * @param options - Options for creating the directory.
492
+ * @returns A promise that resolves to the path of the created directory, or undefined if the directory could not be created.
493
+ */
494
+ mkdir: (path: PathLike, options?: MakeDirectoryOptions) => Promise<string | undefined>;
495
+ /**
496
+ * Reads a file from the virtual file system (VFS).
497
+ *
498
+ * @param pathOrId - The path or id of the file.
499
+ * @returns The contents of the file if it exists, otherwise undefined.
500
+ */
501
+ readFile: (pathOrId: string) => Promise<string | undefined>;
502
+ /**
503
+ * Reads a file from the virtual file system (VFS).
504
+ *
505
+ * @param pathOrId - The path or id of the file.
506
+ */
507
+ readFileSync: (pathOrId: string) => string | undefined;
508
+ /**
509
+ * Writes a file to the virtual file system (VFS).
510
+ *
511
+ * @param path - The path to the file.
512
+ * @param data - The contents of the file.
513
+ * @param options - Optional parameters for writing the file.
514
+ * @returns A promise that resolves when the file is written.
515
+ */
516
+ writeFile: (path: PathOrFileDescriptor, data?: WriteFileData, options?: WriteFileOptions) => Promise<void>;
517
+ /**
518
+ * Writes a file to the virtual file system (VFS).
519
+ *
520
+ * @param path - The path to the file.
521
+ * @param data - The contents of the file.
522
+ * @param options - Optional parameters for writing the file.
523
+ */
524
+ writeFileSync: (path: PathOrFileDescriptor, data?: WriteFileData, options?: WriteFileOptions) => void;
525
+ /**
526
+ * Moves a file from one path to another in the virtual file system (VFS).
527
+ *
528
+ * @param srcPath - The source path to move
529
+ * @param destPath - The destination path to move to
530
+ */
531
+ move: (srcPath: string, destPath: string) => Promise<void>;
532
+ /**
533
+ * Synchronously moves a file from one path to another in the virtual file system (VFS).
534
+ *
535
+ * @param srcPath - The source path to move
536
+ * @param destPath - The destination path to move to
537
+ */
538
+ moveSync: (srcPath: string, destPath: string) => void;
539
+ /**
540
+ * Copies a file from one path to another in the virtual file system (VFS).
541
+ *
542
+ * @param srcPath - The source path to copy
543
+ * @param destPath - The destination path to copy to
544
+ */
545
+ copy: (srcPath: string, destPath: string) => Promise<void>;
546
+ /**
547
+ * Synchronously copies a file from one path to another in the virtual file system (VFS).
548
+ *
549
+ * @param srcPath - The source path to copy
550
+ * @param destPath - The destination path to copy to
551
+ */
552
+ copySync: (srcPath: string, destPath: string) => void;
553
+ /**
554
+ * Glob files in the virtual file system (VFS) based on the provided pattern(s).
555
+ *
556
+ * @param pattern - A pattern (or multiple patterns) to use to determine the file paths to return
557
+ * @returns An array of file paths matching the provided pattern(s)
558
+ */
559
+ glob: (pattern: string | string[]) => Promise<string[]>;
560
+ /**
561
+ * Synchronously glob files in the virtual file system (VFS) based on the provided pattern(s).
562
+ *
563
+ * @param pattern - A pattern (or multiple patterns) to use to determine the file paths to return
564
+ * @returns An array of file paths matching the provided pattern(s)
565
+ */
566
+ globSync: (pattern: string | string[]) => string[];
567
+ /**
568
+ * Resolves a path or id to a file path in the virtual file system.
569
+ *
570
+ * @param pathOrId - The path or id of the file to resolve.
571
+ * @param options - Optional parameters for resolving the path.
572
+ * @returns The resolved path of the file if it exists, otherwise false.
573
+ */
574
+ resolve: (pathOrId: string, options?: ResolvePathOptions) => string | false;
575
+ /**
576
+ * Formats a path to match the virtual file system (VFS) structure.
577
+ *
578
+ * @param path - The path to format.
579
+ * @returns The formatted path.
580
+ */
581
+ formatPath: (path: string) => string;
582
+ /**
583
+ * Resolves a path or id to a file path in the virtual file system.
584
+ *
585
+ * @param pathOrId - The path or id of the file to resolve.
586
+ * @returns The resolved path of the file if it exists, otherwise false.
587
+ */
588
+ realpathSync: (pathOrId: string) => string;
589
+ /**
590
+ * Disposes of the virtual file system (VFS), writes any virtual file changes to disk, and releases any associated resources.
591
+ */
592
+ dispose: () => Promise<void>;
593
+ }
594
+
595
+ type LogFn = (type: LogLevelLabel, ...args: string[]) => void;
596
+ /**
597
+ * The {@link StormWorkspaceConfig | configuration} object for an entire Powerlines workspace
598
+ */
599
+ type WorkspaceConfig = Partial<StormWorkspaceConfig> & Required<Pick<StormWorkspaceConfig, "workspaceRoot">>;
600
+ type PluginFactory<in out TContext extends PluginContext = PluginContext, TOptions = any> = (options: TOptions) => MaybePromise<Plugin<TContext>>;
601
+ /**
602
+ * A configuration tuple for a Powerlines plugin.
603
+ */
604
+ type PluginConfigTuple<TContext extends PluginContext = PluginContext, TOptions = any> = [string | PluginFactory<TContext, TOptions>, TOptions] | [Plugin<TContext>];
605
+ /**
606
+ * A configuration object for a Powerlines plugin.
607
+ */
608
+ type PluginConfigObject<TContext extends PluginContext = PluginContext, TOptions = any> = {
609
+ plugin: string | PluginFactory<TContext, TOptions>;
610
+ options: TOptions;
611
+ } | {
612
+ plugin: Plugin<TContext>;
613
+ options?: never;
614
+ };
615
+ /**
616
+ * A configuration tuple for a Powerlines plugin.
617
+ */
618
+ type PluginConfig<TContext extends PluginContext = PluginContext> = string | PluginFactory<TContext, void> | Plugin<TContext> | Promise<Plugin<TContext>> | PluginConfigTuple<TContext> | PluginConfigObject<TContext>;
619
+ type ProjectType = "application" | "library";
620
+ interface OutputConfig {
621
+ /**
622
+ * The path to output the final compiled files to
623
+ *
624
+ * @remarks
625
+ * If a value is not provided, Powerlines will attempt to:
626
+ * 1. Use the `outDir` value in the `tsconfig.json` file.
627
+ * 2. Use the `dist` directory in the project root directory.
628
+ *
629
+ * @defaultValue "dist/\{projectRoot\}"
630
+ */
631
+ outputPath?: string;
632
+ /**
633
+ * The format of the output files
634
+ *
635
+ * @defaultValue "virtual"
636
+ */
637
+ mode?: OutputModeType;
638
+ /**
639
+ * The path of the generated runtime declaration file relative to the workspace root.
640
+ *
641
+ * @defaultValue "\{projectRoot\}/powerlines.d.ts"
642
+ */
643
+ dts?: string | false;
644
+ /**
645
+ * A prefix to use for identifying builtin modules
646
+ *
647
+ * @remarks
648
+ * This prefix will be used to identify all builtin modules generated during the "prepare" phase. An example builtin ID for a module called `"utils"` would be `"{builtinPrefix}:utils"`.
649
+ *
650
+ * @defaultValue "powerlines"
651
+ */
652
+ builtinPrefix?: string;
653
+ /**
654
+ * The folder where the generated runtime artifacts will be located
655
+ *
656
+ * @remarks
657
+ * This folder will contain all runtime artifacts and builtins generated during the "prepare" phase.
658
+ *
659
+ * @defaultValue "\{projectRoot\}/.powerlines"
660
+ */
661
+ artifactsFolder?: string;
662
+ /**
663
+ * The module format of the output files
664
+ *
665
+ * @remarks
666
+ * This option can be a single format or an array of formats. If an array is provided, multiple builds will be generated for each format.
667
+ *
668
+ * @defaultValue "esm"
669
+ */
670
+ format?: Format | Format[];
671
+ /**
672
+ * A list of assets to copy to the output directory
673
+ *
674
+ * @remarks
675
+ * The assets can be specified as a string (path to the asset) or as an object with a `glob` property (to match multiple files). The paths are relative to the project root directory.
676
+ */
677
+ assets?: Array<string | AssetGlob>;
678
+ }
679
+ interface BaseConfig {
680
+ /**
681
+ * The name of the project
682
+ */
683
+ name?: string;
684
+ /**
685
+ * The project display title
686
+ *
687
+ * @remarks
688
+ * This option is used in documentation generation and other places where a human-readable title is needed.
689
+ */
690
+ title?: string;
691
+ /**
692
+ * A description of the project
693
+ *
694
+ * @remarks
695
+ * If this option is not provided, the build process will try to use the \`description\` value from the `\package.json\` file.
696
+ */
697
+ description?: string;
698
+ /**
699
+ * The log level to use for the Powerlines processes.
700
+ *
701
+ * @defaultValue "info"
702
+ */
703
+ logLevel?: LogLevelLabel | null;
704
+ /**
705
+ * A custom logger function to use for logging messages
706
+ */
707
+ customLogger?: LogFn;
708
+ /**
709
+ * Explicitly set a mode to run in. This mode will be used at various points throughout the Powerlines processes, such as when compiling the source code.
710
+ *
711
+ * @defaultValue "production"
712
+ */
713
+ mode?: "development" | "test" | "production";
714
+ /**
715
+ * The entry point(s) for the application
716
+ */
717
+ entry?: TypeDefinitionParameter | TypeDefinitionParameter[];
718
+ /**
719
+ * Configuration for the output of the build process
720
+ */
721
+ output?: OutputConfig;
722
+ /**
723
+ * Configuration for linting the source code
724
+ *
725
+ * @remarks
726
+ * If set to `false`, linting will be disabled.
727
+ */
728
+ lint?: Record<string, any> | false;
729
+ /**
730
+ * Configuration for testing the source code
731
+ *
732
+ * @remarks
733
+ * If set to `false`, testing will be disabled.
734
+ */
735
+ test?: Record<string, any> | false;
736
+ /**
737
+ * Configuration for the transformation of the source code
738
+ */
739
+ transform?: Record<string, any>;
740
+ /**
741
+ * Configuration provided to build processes
742
+ *
743
+ * @remarks
744
+ * This configuration can be used by plugins during the `build` command. It will generally contain options specific to the selected {@link BuildVariant | build variant}.
745
+ */
746
+ build?: BuildConfig;
747
+ /**
748
+ * Configuration for documentation generation
749
+ *
750
+ * @remarks
751
+ * This configuration will be used by the documentation generation plugins during the `docs` command.
752
+ */
753
+ docs?: Record<string, any>;
754
+ /**
755
+ * Configuration for deploying the source code
756
+ *
757
+ * @remarks
758
+ * If set to `false`, the deployment will be disabled.
759
+ */
760
+ deploy?: Record<string, any> | false;
761
+ /**
762
+ * The path to the tsconfig file to be used by the compiler
763
+ *
764
+ * @remarks
765
+ * If a value is not provided, the plugin will attempt to find the `tsconfig.json` file in the project root directory. The parsed tsconfig compiler options will be merged with the {@link Options.tsconfigRaw} value (if provided).
766
+ *
767
+ * @defaultValue "\{projectRoot\}/tsconfig.json"
768
+ */
769
+ tsconfig?: string;
770
+ /**
771
+ * The raw {@link TSConfig} object to be used by the compiler. This object will be merged with the `tsconfig.json` file.
772
+ *
773
+ * @see https://www.typescriptlang.org/tsconfig
774
+ *
775
+ * @remarks
776
+ * If populated, this option takes higher priority than `tsconfig`
777
+ */
778
+ tsconfigRaw?: TSConfig;
779
+ }
780
+ interface EnvironmentConfig extends BaseConfig {
781
+ /**
782
+ * Configuration options for the preview server
783
+ */
784
+ preview?: PreviewOptions;
785
+ /**
786
+ * A flag indicating whether the build is for a Server-Side Rendering environment.
787
+ */
788
+ ssr?: boolean;
789
+ /**
790
+ * Define if this environment is used for Server-Side Rendering
791
+ *
792
+ * @defaultValue "server" (if it isn't the client environment)
793
+ */
794
+ consumer?: "client" | "server";
795
+ }
796
+ interface CommonUserConfig extends BaseConfig {
797
+ /**
798
+ * The type of project being built
799
+ *
800
+ * @defaultValue "application"
801
+ */
802
+ type?: ProjectType;
803
+ /**
804
+ * The root directory of the project
805
+ */
806
+ root: string;
807
+ /**
808
+ * The root directory of the project's source code
809
+ *
810
+ * @defaultValue "\{root\}/src"
811
+ */
812
+ sourceRoot?: string;
813
+ /**
814
+ * A path to a custom configuration file to be used instead of the default `storm.json`, `powerlines.config.js`, or `powerlines.config.ts` files.
815
+ *
816
+ * @remarks
817
+ * This option is useful for running Powerlines commands with different configuration files, such as in CI/CD environments or when testing different configurations.
818
+ */
819
+ configFile?: string;
820
+ /**
821
+ * Should the Powerlines CLI processes skip installing missing packages?
822
+ *
823
+ * @remarks
824
+ * This option is useful for CI/CD environments where the installation of packages is handled by a different process.
825
+ *
826
+ * @defaultValue false
827
+ */
828
+ skipInstalls?: boolean;
829
+ /**
830
+ * Should the compiler processes skip any improvements that make use of cache?
831
+ *
832
+ * @defaultValue false
833
+ */
834
+ skipCache?: boolean;
835
+ /**
836
+ * A list of resolvable paths to plugins used during the build process
837
+ */
838
+ plugins?: PluginConfig<PluginContext<any>>[];
839
+ /**
840
+ * Environment-specific configurations
841
+ */
842
+ environments?: Record<string, EnvironmentConfig>;
843
+ /**
844
+ * A string identifier that allows a child framework or tool to identify itself when using Powerlines.
845
+ *
846
+ * @remarks
847
+ * If no values are provided for {@link OutputConfig.dts | output.dts}, {@link OutputConfig.builtinPrefix | output.builtinPrefix}, or {@link OutputConfig.artifactsFolder | output.artifactsFolder}, this value will be used as the default.
848
+ *
849
+ * @defaultValue "powerlines"
850
+ */
851
+ framework?: string;
852
+ }
853
+ type UserConfig<TBuildConfig extends BuildConfig = BuildConfig, TBuildResolvedConfig extends BuildResolvedConfig = BuildResolvedConfig, TBuildVariant extends string = any> = Omit<CommonUserConfig, "build"> & {
854
+ /**
855
+ * Configuration provided to build processes
856
+ *
857
+ * @remarks
858
+ * This configuration can be used by plugins during the `build` command. It will generally contain options specific to the selected {@link BuildVariant | build variant}.
859
+ */
860
+ build: Omit<TBuildConfig, "override"> & {
861
+ /**
862
+ * The build variant being used by the Powerlines engine.
863
+ */
864
+ variant?: TBuildVariant;
865
+ /**
866
+ * An optional set of override options to apply to the selected build variant.
867
+ *
868
+ * @remarks
869
+ * This option allows you to provide configuration options with the guarantee that they will **not** be overridden and will take precedence over other build configurations.
870
+ */
871
+ override?: Partial<TBuildResolvedConfig>;
872
+ };
873
+ };
874
+ type PowerlinesCommand = "new" | "prepare" | "build" | "lint" | "test" | "docs" | "deploy" | "clean";
875
+ /**
876
+ * The configuration provided while executing Powerlines commands.
877
+ */
878
+ type InlineConfig<TUserConfig extends UserConfig = UserConfig> = Partial<TUserConfig> & {
879
+ /**
880
+ * A string identifier for the Powerlines command being executed
881
+ */
882
+ command: PowerlinesCommand;
883
+ };
884
+
885
+ interface ResolvedEntryTypeDefinition extends TypeDefinition {
886
+ /**
887
+ * The user provided entry point in the source code
888
+ */
889
+ input: TypeDefinition;
890
+ /**
891
+ * An optional name to use in the package export during the build process
892
+ */
893
+ output?: string;
894
+ }
895
+ type EnvironmentResolvedConfig = Omit<EnvironmentConfig, "consumer" | "mode" | "ssr" | "preview"> & Required<Pick<EnvironmentConfig, "consumer" | "mode" | "ssr">> & {
896
+ /**
897
+ * The name of the environment
898
+ */
899
+ name: string;
900
+ /**
901
+ * Configuration options for the preview server
902
+ */
903
+ preview?: ResolvedPreviewOptions;
904
+ };
905
+ type ResolvedAssetGlob = AssetGlob & Required<Pick<AssetGlob, "input">>;
906
+ type OutputResolvedConfig = Required<Omit<OutputConfig, "assets"> & {
907
+ assets: ResolvedAssetGlob[];
908
+ }>;
909
+ /**
910
+ * The resolved options for the Powerlines project configuration.
911
+ */
912
+ type ResolvedConfig<TUserConfig extends UserConfig = UserConfig> = Omit<TUserConfig, "name" | "title" | "plugins" | "mode" | "environments" | "platform" | "tsconfig" | "lint" | "test" | "build" | "transform" | "deploy" | "variant" | "type" | "output" | "logLevel" | "framework"> & Required<Pick<TUserConfig, "name" | "title" | "plugins" | "mode" | "environments" | "tsconfig" | "lint" | "test" | "build" | "transform" | "deploy" | "framework">> & {
913
+ /**
914
+ * The configuration options that were provided inline to the Powerlines CLI.
915
+ */
916
+ inlineConfig: InlineConfig<TUserConfig>;
917
+ /**
918
+ * The original configuration options that were provided by the user to the Powerlines process.
919
+ */
920
+ userConfig: TUserConfig;
921
+ /**
922
+ * A string identifier for the Powerlines command being executed.
923
+ */
924
+ command: NonUndefined<InlineConfig<TUserConfig>["command"]>;
925
+ /**
926
+ * The root directory of the project's source code
927
+ *
928
+ * @defaultValue "\{projectRoot\}/src"
929
+ */
930
+ sourceRoot: NonUndefined<TUserConfig["sourceRoot"]>;
931
+ /**
932
+ * The root directory of the project.
933
+ */
934
+ projectRoot: NonUndefined<TUserConfig["root"]>;
935
+ /**
936
+ * The type of project being built.
937
+ */
938
+ projectType: NonUndefined<TUserConfig["type"]>;
939
+ /**
940
+ * The output configuration options to use for the build process
941
+ */
942
+ output: OutputResolvedConfig;
943
+ /**
944
+ * Configuration provided to build processes
945
+ *
946
+ * @remarks
947
+ * This configuration can be used by plugins during the `build` command. It will generally contain options specific to the selected {@link BuildVariant | build variant}.
948
+ */
949
+ build: Omit<TUserConfig["build"], "override"> & Required<Pick<Required<TUserConfig["build"]>, "override">>;
950
+ /**
951
+ * The log level to use for the Powerlines processes.
952
+ *
953
+ * @defaultValue "info"
954
+ */
955
+ logLevel: "error" | "warn" | "info" | "debug" | "trace" | null;
956
+ };
957
+
958
+ interface MetaInfo {
959
+ /**
960
+ * The checksum generated from the resolved options
961
+ */
962
+ checksum: string;
963
+ /**
964
+ * The build id
965
+ */
966
+ buildId: string;
967
+ /**
968
+ * The release id
969
+ */
970
+ releaseId: string;
971
+ /**
972
+ * The build timestamp
973
+ */
974
+ timestamp: number;
975
+ /**
976
+ * A hash that represents the path to the project root directory
977
+ */
978
+ projectRootHash: string;
979
+ /**
980
+ * A hash that represents the path to the project root directory
981
+ */
982
+ configHash: string;
983
+ }
984
+ interface Resolver extends Jiti {
985
+ plugin: Jiti;
986
+ }
987
+ interface InitContextOptions {
988
+ /**
989
+ * If false, the plugin will be loaded after all other plugins.
990
+ *
991
+ * @defaultValue true
992
+ */
993
+ isHighPriority: boolean;
994
+ }
995
+ /**
996
+ * The unresolved Powerlines context.
997
+ *
998
+ * @remarks
999
+ * This context is used before the user configuration has been fully resolved after the `config`.
1000
+ */
1001
+ interface UnresolvedContext<TResolvedConfig extends ResolvedConfig = ResolvedConfig> {
1002
+ /**
1003
+ * The Storm workspace configuration
1004
+ */
1005
+ workspaceConfig: WorkspaceConfig;
1006
+ /**
1007
+ * An object containing the options provided to Powerlines
1008
+ */
1009
+ config: Omit<TResolvedConfig["userConfig"], "build" | "output"> & Required<Pick<TResolvedConfig["userConfig"], "build" | "output">> & {
1010
+ projectRoot: NonUndefined<TResolvedConfig["userConfig"]["root"]>;
1011
+ output: TResolvedConfig["output"];
1012
+ };
1013
+ /**
1014
+ * A logging function for the Powerlines engine
1015
+ */
1016
+ log: LogFn;
1017
+ /**
1018
+ * A logging function for fatal messages
1019
+ */
1020
+ fatal: (message: string | UnpluginMessage) => void;
1021
+ /**
1022
+ * A logging function for error messages
1023
+ */
1024
+ error: (message: string | UnpluginMessage) => void;
1025
+ /**
1026
+ * A logging function for warning messages
1027
+ */
1028
+ warn: (message: string | UnpluginMessage) => void;
1029
+ /**
1030
+ * A logging function for informational messages
1031
+ */
1032
+ info: (message: string | UnpluginMessage) => void;
1033
+ /**
1034
+ * A logging function for debug messages
1035
+ */
1036
+ debug: (message: string | UnpluginMessage) => void;
1037
+ /**
1038
+ * A logging function for trace messages
1039
+ */
1040
+ trace: (message: string | UnpluginMessage) => void;
1041
+ /**
1042
+ * The metadata information
1043
+ */
1044
+ meta: MetaInfo;
1045
+ /**
1046
+ * The metadata information currently written to disk
1047
+ */
1048
+ persistedMeta?: MetaInfo;
1049
+ /**
1050
+ * The Powerlines artifacts directory
1051
+ */
1052
+ artifactsPath: string;
1053
+ /**
1054
+ * The path to the Powerlines builtin runtime modules directory
1055
+ */
1056
+ builtinsPath: string;
1057
+ /**
1058
+ * The path to the Powerlines entry modules directory
1059
+ */
1060
+ entryPath: string;
1061
+ /**
1062
+ * The path to the Powerlines TypeScript declaration files directory
1063
+ */
1064
+ dtsPath: string;
1065
+ /**
1066
+ * The path to a directory where the reflection data buffers (used by the build processes) are stored
1067
+ */
1068
+ dataPath: string;
1069
+ /**
1070
+ * The path to a directory where the project cache (used by the build processes) is stored
1071
+ */
1072
+ cachePath: string;
1073
+ /**
1074
+ * The Powerlines environment paths
1075
+ */
1076
+ envPaths: EnvPaths;
1077
+ /**
1078
+ * The file system path to the Powerlines package installation
1079
+ */
1080
+ powerlinesPath: string;
1081
+ /**
1082
+ * The relative path to the Powerlines workspace root directory
1083
+ */
1084
+ relativeToWorkspaceRoot: string;
1085
+ /**
1086
+ * The project's `package.json` file content
1087
+ */
1088
+ packageJson: PackageJson & Record<string, any>;
1089
+ /**
1090
+ * The project's `project.json` file content
1091
+ */
1092
+ projectJson?: Record<string, any>;
1093
+ /**
1094
+ * The dependency installations required by the project
1095
+ */
1096
+ dependencies: Record<string, string | Range>;
1097
+ /**
1098
+ * The development dependency installations required by the project
1099
+ */
1100
+ devDependencies: Record<string, string | Range>;
1101
+ /**
1102
+ * The parsed TypeScript configuration from the `tsconfig.json` file
1103
+ */
1104
+ tsconfig: ParsedTypeScriptConfig;
1105
+ /**
1106
+ * The entry points of the source code
1107
+ */
1108
+ entry: ResolvedEntryTypeDefinition[];
1109
+ /**
1110
+ * The virtual file system manager used during the build process to reference generated runtime files
1111
+ */
1112
+ fs: VirtualFileSystemInterface;
1113
+ /**
1114
+ * The Jiti module resolver
1115
+ */
1116
+ resolver: Resolver;
1117
+ /**
1118
+ * The builtin module id that exist in the Powerlines virtual file system
1119
+ */
1120
+ builtins: string[];
1121
+ /**
1122
+ * The Powerlines builtin virtual files
1123
+ */
1124
+ getBuiltins: () => Promise<VirtualFile[]>;
1125
+ /**
1126
+ * Resolves a builtin virtual file and writes it to the VFS if it does not already exist
1127
+ *
1128
+ * @param code - The source code of the builtin file
1129
+ * @param id - The unique identifier of the builtin file
1130
+ * @param path - An optional path to write the builtin file to
1131
+ * @param options - Options for writing the file
1132
+ */
1133
+ writeBuiltin: (code: string, id: string, path?: string, options?: PowerlinesWriteFileOptions) => Promise<void>;
1134
+ /**
1135
+ * Resolves a entry virtual file and writes it to the VFS if it does not already exist
1136
+ *
1137
+ * @param code - The source code of the entry file
1138
+ * @param path - An optional path to write the entry file to
1139
+ * @param options - Options for writing the file
1140
+ */
1141
+ writeEntry: (code: string, path: string, options?: PowerlinesWriteFileOptions) => Promise<void>;
1142
+ /**
1143
+ * Parses the source code and returns a {@link ParseResult} object.
1144
+ */
1145
+ parse: (code: string, id: string, options?: ParserOptions | null) => Promise<ParseResult>;
1146
+ /**
1147
+ * A function to update the context fields using a new user configuration options
1148
+ */
1149
+ withUserConfig: (userConfig: UserConfig, options?: InitContextOptions) => Promise<void>;
1150
+ /**
1151
+ * A function to update the context fields using inline configuration options
1152
+ */
1153
+ withInlineConfig: (inlineConfig: InlineConfig, options?: InitContextOptions) => Promise<void>;
1154
+ /**
1155
+ * Create a new logger instance
1156
+ *
1157
+ * @param name - The name to use for the logger instance
1158
+ * @returns A logger function
1159
+ */
1160
+ createLog: (name: string | null) => LogFn;
1161
+ /**
1162
+ * Extend the current logger instance with a new name
1163
+ *
1164
+ * @param name - The name to use for the extended logger instance
1165
+ * @returns A logger function
1166
+ */
1167
+ extendLog: (name: string) => LogFn;
1168
+ }
1169
+ type Context<TResolvedConfig extends ResolvedConfig = ResolvedConfig> = Omit<UnresolvedContext<TResolvedConfig>, "config"> & {
1170
+ /**
1171
+ * The fully resolved Powerlines configuration
1172
+ */
1173
+ config: TResolvedConfig;
1174
+ };
1175
+ interface PluginContext<out TResolvedConfig extends ResolvedConfig = ResolvedConfig> extends Context<TResolvedConfig>, UnpluginContext {
1176
+ /**
1177
+ * The environment specific resolved configuration
1178
+ */
1179
+ environment: EnvironmentResolvedConfig;
1180
+ /**
1181
+ * An alternative property name for the {@link log} property
1182
+ *
1183
+ * @remarks
1184
+ * This is provided for compatibility with other logging libraries that expect a `logger` property.
1185
+ */
1186
+ logger: LogFn;
1187
+ }
1188
+ type BuildPluginContext<TResolvedConfig extends ResolvedConfig = ResolvedConfig> = PluginContext<TResolvedConfig> & Omit<UnpluginBuildContext, "parse">;
1189
+
1190
+ declare const SUPPORTED_COMMANDS: readonly ["new", "clean", "prepare", "lint", "test", "build", "docs", "deploy", "finalize"];
1191
+ type CommandType = ArrayValues<typeof SUPPORTED_COMMANDS>;
1192
+
1193
+ interface PluginHookObject<THookFunction extends FunctionLike, TFilter extends keyof HookFilter = never> {
1194
+ /**
1195
+ * The order in which the plugin should be applied.
1196
+ */
1197
+ order?: "pre" | "post" | null | undefined;
1198
+ /**
1199
+ * A filter to determine when the hook should be called.
1200
+ */
1201
+ filter?: Pick<HookFilter, TFilter>;
1202
+ /**
1203
+ * The hook function to be called.
1204
+ */
1205
+ handler: THookFunction;
1206
+ }
1207
+ type PluginHook<THookFunction extends FunctionLike, TFilter extends keyof HookFilter = never> = THookFunction | PluginHookObject<THookFunction, TFilter>;
1208
+ /**
1209
+ * A result returned by the plugin from the `generateTypes` hook that describes the declaration types output file.
1210
+ */
1211
+ interface GenerateTypesResult {
1212
+ directives?: string[];
1213
+ code: string;
1214
+ }
1215
+ type DeepPartial<T> = {
1216
+ [K in keyof T]?: DeepPartial<T[K]>;
1217
+ };
1218
+ type ConfigResult<TContext extends PluginContext = PluginContext> = DeepPartial<TContext["config"]> & Record<string, any>;
1219
+ interface BasePluginHookFunctions<TContext extends PluginContext = PluginContext> extends Record<CommandType, (this: TContext) => MaybePromise<void>> {
1220
+ /**
1221
+ * A function that returns configuration options to be merged with the build context's options.
1222
+ *
1223
+ * @remarks
1224
+ * Modify config before it's resolved. The hook can either mutate {@link Context.config} on the passed-in context directly, or return a partial config object that will be deeply merged into existing config.
1225
+ *
1226
+ * @warning User plugins are resolved before running this hook so injecting other plugins inside the config hook will have no effect. If you want to add plugins, consider doing so in the {@link Plugin.dependsOn} property instead.
1227
+ *
1228
+ * @see https://vitejs.dev/guide/api-plugin#config
1229
+ *
1230
+ * @param this - The build context.
1231
+ * @param config - The partial configuration object to be modified.
1232
+ * @returns A promise that resolves to a partial configuration object.
1233
+ */
1234
+ config: (this: UnresolvedContext<TContext["config"]>) => MaybePromise<ConfigResult<TContext>>;
1235
+ /**
1236
+ * Modify environment configs before it's resolved. The hook can either mutate the passed-in environment config directly, or return a partial config object that will be deeply merged into existing config.
1237
+ *
1238
+ * @remarks
1239
+ * This hook is called for each environment with a partially resolved environment config that already accounts for the default environment config values set at the root level. If plugins need to modify the config of a given environment, they should do it in this hook instead of the config hook. Leaving the config hook only for modifying the root default environment config.
1240
+ *
1241
+ * @see https://vitejs.dev/guide/api-plugin#configenvironment
1242
+ *
1243
+ * @param this - The build context.
1244
+ * @param name - The name of the environment being configured.
1245
+ * @param environment - The Vite-like environment object containing information about the current build environment.
1246
+ * @returns A promise that resolves when the hook is complete.
1247
+ */
1248
+ configEnvironment: (this: TContext, name: string, environment: EnvironmentConfig) => MaybePromise<Partial<EnvironmentResolvedConfig> | undefined | null>;
1249
+ /**
1250
+ * A hook that is called when the plugin is resolved.
1251
+ *
1252
+ * @see https://vitejs.dev/guide/api-plugin#configresolved
1253
+ *
1254
+ * @param this - The build context.
1255
+ * @returns A promise that resolves when the hook is complete.
1256
+ */
1257
+ configResolved: (this: TContext) => MaybePromise<void>;
1258
+ /**
1259
+ * A hook that is called to overwrite the generated declaration types file (.d.ts). The generated type definitions should describe the built-in modules/logic added during the `prepare` task.
1260
+ *
1261
+ * @param this - The build context.
1262
+ * @param code - The source code to generate types for.
1263
+ * @returns A promise that resolves when the hook is complete.
1264
+ */
1265
+ generateTypes: (this: TContext, code: string) => MaybePromise<GenerateTypesResult | string | undefined | null>;
1266
+ /**
1267
+ * A hook that is called at the start of the build process.
1268
+ *
1269
+ * @param this - The build context and unplugin build context.
1270
+ * @returns A promise that resolves when the hook is complete.
1271
+ */
1272
+ buildStart: (this: BuildPluginContext<TContext["config"]> & TContext) => MaybePromise<void>;
1273
+ /**
1274
+ * A hook that is called at the end of the build process.
1275
+ *
1276
+ * @param this - The build context and unplugin build context.
1277
+ * @returns A promise that resolves when the hook is complete.
1278
+ */
1279
+ buildEnd: (this: BuildPluginContext<TContext["config"]> & TContext) => MaybePromise<void>;
1280
+ /**
1281
+ * A hook that is called to transform the source code.
1282
+ *
1283
+ * @param this - The build context, unplugin build context, and unplugin context.
1284
+ * @param code - The source code to transform.
1285
+ * @param id - The identifier of the source code.
1286
+ * @returns A promise that resolves when the hook is complete.
1287
+ */
1288
+ transform: (this: BuildPluginContext<TContext["config"]> & TContext, code: string, id: string) => MaybePromise<TransformResult>;
1289
+ /**
1290
+ * A hook that is called to load the source code.
1291
+ *
1292
+ * @param this - The build context, unplugin build context, and unplugin context.
1293
+ * @param id - The identifier of the source code.
1294
+ * @returns A promise that resolves when the hook is complete.
1295
+ */
1296
+ load: (this: BuildPluginContext<TContext["config"]> & TContext, id: string) => MaybePromise<TransformResult>;
1297
+ /**
1298
+ * A hook that is called to resolve the identifier of the source code.
1299
+ *
1300
+ * @param this - The build context, unplugin build context, and unplugin context.
1301
+ * @param id - The identifier of the source code.
1302
+ * @param importer - The importer of the source code.
1303
+ * @param options - The options for resolving the identifier.
1304
+ * @returns A promise that resolves when the hook is complete.
1305
+ */
1306
+ resolveId: (this: BuildPluginContext<TContext["config"]> & TContext, id: string, importer: string | undefined, options: {
1307
+ isEntry: boolean;
1308
+ }) => MaybePromise<string | ExternalIdResult | null | undefined>;
1309
+ /**
1310
+ * A hook that is called to write the bundle to disk.
1311
+ *
1312
+ * @param this - The build context.
1313
+ * @returns A promise that resolves when the hook is complete.
1314
+ */
1315
+ writeBundle: (this: TContext) => MaybePromise<void>;
1316
+ }
1317
+ type BuildPlugin<TContext extends PluginContext = PluginContext, TBuildVariant extends UnpluginBuildVariant = UnpluginBuildVariant, TOptions extends Required<UnpluginOptions>[TBuildVariant] = Required<UnpluginOptions>[TBuildVariant]> = {
1318
+ [TKey in keyof TOptions]: TOptions[TKey] extends FunctionLike ? (this: ThisParameterType<TOptions[TKey]> & TContext, ...args: Parameters<TOptions[TKey]>) => ReturnType<TOptions[TKey]> | MaybePromise<ReturnType<TOptions[TKey]>> : TOptions[TKey];
1319
+ };
1320
+ type PluginHooks<TContext extends PluginContext = PluginContext> = {
1321
+ [TKey in keyof BasePluginHookFunctions<TContext>]: PluginHook<BasePluginHookFunctions<TContext>[TKey]>;
1322
+ } & {
1323
+ /**
1324
+ * A function that returns configuration options to be merged with the build context's options.
1325
+ *
1326
+ * @remarks
1327
+ * Modify config before it's resolved. The hook can either mutate {@link Context.config} on the passed-in context directly, or return a partial config object that will be deeply merged into existing config.
1328
+ *
1329
+ * @warning User plugins are resolved before running this hook so injecting other plugins inside the config hook will have no effect. If you want to add plugins, consider doing so in the {@link Plugin.dependsOn} property instead.
1330
+ *
1331
+ * @see https://vitejs.dev/guide/api-plugin#config
1332
+ *
1333
+ * @param this - The build context.
1334
+ * @param config - The partial configuration object to be modified.
1335
+ * @returns A promise that resolves to a partial configuration object.
1336
+ */
1337
+ config: PluginHook<(this: UnresolvedContext<TContext["config"]>) => MaybePromise<ConfigResult<TContext>>> | ConfigResult<TContext>;
1338
+ /**
1339
+ * A hook that is called to transform the source code.
1340
+ *
1341
+ * @param this - The build context, unplugin build context, and unplugin context.
1342
+ * @param code - The source code to transform.
1343
+ * @param id - The identifier of the source code.
1344
+ * @returns A promise that resolves when the hook is complete.
1345
+ */
1346
+ transform: PluginHook<(this: BuildPluginContext<TContext["config"]> & TContext, code: string, id: string) => MaybePromise<TransformResult>, "code" | "id">;
1347
+ /**
1348
+ * A hook that is called to load the source code.
1349
+ *
1350
+ * @param this - The build context, unplugin build context, and unplugin context.
1351
+ * @param id - The identifier of the source code.
1352
+ * @returns A promise that resolves when the hook is complete.
1353
+ */
1354
+ load: PluginHook<(this: BuildPluginContext<TContext["config"]> & TContext, id: string) => MaybePromise<TransformResult>, "id">;
1355
+ /**
1356
+ * A hook that is called to resolve the identifier of the source code.
1357
+ *
1358
+ * @param this - The build context, unplugin build context, and unplugin context.
1359
+ * @param id - The identifier of the source code.
1360
+ * @param importer - The importer of the source code.
1361
+ * @param options - The options for resolving the identifier.
1362
+ * @returns A promise that resolves when the hook is complete.
1363
+ */
1364
+ resolveId: PluginHook<(this: BuildPluginContext<TContext["config"]> & TContext, id: string, importer: string | undefined, options: {
1365
+ isEntry: boolean;
1366
+ }) => MaybePromise<string | ExternalIdResult | null | undefined>, "id">;
1367
+ };
1368
+ type PluginBuildPlugins<TContext extends PluginContext = PluginContext> = {
1369
+ [TBuildVariant in UnpluginBuildVariant]?: BuildPlugin<TContext, TBuildVariant>;
1370
+ };
1371
+ interface Plugin<in out TContext extends PluginContext<ResolvedConfig> = PluginContext<ResolvedConfig>> extends Partial<PluginHooks<TContext>>, PluginBuildPlugins<TContext> {
1372
+ /**
1373
+ * The name of the plugin, for use in deduplication, error messages and logs.
1374
+ */
1375
+ name: string;
1376
+ /**
1377
+ * Enforce plugin invocation tier similar to webpack loaders. Hooks ordering is still subject to the `order` property in the hook object.
1378
+ *
1379
+ * @remarks
1380
+ * The Plugin invocation order is as follows:
1381
+ * - `enforce: 'pre'` plugins
1382
+ * - `order: 'pre'` plugin hooks
1383
+ * - any other plugins (normal)
1384
+ * - `order: 'post'` plugin hooks
1385
+ * - `enforce: 'post'` plugins
1386
+ *
1387
+ * @see https://vitejs.dev/guide/api-plugin.html#plugin-ordering
1388
+ * @see https://rollupjs.org/plugin-development/#build-hooks
1389
+ * @see https://webpack.js.org/concepts/loaders/#enforce---pre-and-post
1390
+ * @see https://esbuild.github.io/plugins/#concepts
1391
+ */
1392
+ enforce?: "pre" | "post";
1393
+ /**
1394
+ * A function to determine if two plugins are the same and can be de-duplicated.
1395
+ *
1396
+ * @remarks
1397
+ * If this is not provided, plugins are de-duplicated by comparing their names.
1398
+ *
1399
+ * @param other - The other plugin to compare against.
1400
+ * @returns `true` if the two plugins are the same, `false` otherwise.
1401
+ */
1402
+ dedupe?: false | ((other: Plugin<any>) => boolean);
1403
+ /**
1404
+ * A list of pre-requisite plugins that must be loaded before this plugin can be used.
1405
+ */
1406
+ dependsOn?: PluginConfig<any>[];
1407
+ /**
1408
+ * Define environments where this plugin should be active. By default, the plugin is active in all environments.
1409
+ *
1410
+ * @param environment - The environment to check.
1411
+ * @returns `true` if the plugin should be active in the specified environment, `false` otherwise.
1412
+ */
1413
+ applyToEnvironment?: (environment: EnvironmentResolvedConfig) => MaybePromise<boolean | Plugin<any>>;
1414
+ }
1415
+
1416
+ type UnenvPluginOptions = Partial<CreateEnvOptions>;
1417
+ interface UnenvPluginUserConfig extends UserConfig {
1418
+ unenv: UnenvPluginOptions;
1419
+ }
1420
+ interface UnenvPluginResolvedConfig extends ResolvedConfig {
1421
+ unenv: CreateEnvOptions;
1422
+ }
1423
+ type UnenvPluginContext<TResolvedConfig extends UnenvPluginResolvedConfig = UnenvPluginResolvedConfig> = PluginContext<TResolvedConfig> & {
1424
+ unenv: ReturnType<typeof unenv.defineEnv>;
1425
+ };
1426
+
1427
+ export type { Plugin as P, UnenvPluginContext as U, UnenvPluginOptions as a, UnenvPluginUserConfig as b, UnenvPluginResolvedConfig as c };