@powerlines/plugin-unimport 0.1.232 → 0.1.233

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.
Files changed (30) hide show
  1. package/dist/index.d.cts +1 -2
  2. package/dist/index.d.mts +1 -1
  3. package/dist/powerlines/src/internal/helpers/hooks.d.cts +47 -0
  4. package/dist/powerlines/src/internal/helpers/hooks.d.mts +49 -0
  5. package/dist/powerlines/src/types/api.d.cts +104 -0
  6. package/dist/powerlines/src/types/api.d.mts +104 -0
  7. package/dist/powerlines/src/types/babel.d.mts +2 -0
  8. package/dist/powerlines/src/types/build.d.cts +185 -0
  9. package/dist/powerlines/src/types/build.d.mts +185 -0
  10. package/dist/powerlines/src/types/commands.d.cts +8 -0
  11. package/dist/powerlines/src/types/commands.d.mts +9 -0
  12. package/dist/powerlines/src/types/config.d.cts +424 -0
  13. package/dist/powerlines/src/types/config.d.mts +424 -0
  14. package/dist/powerlines/src/types/context.d.cts +515 -0
  15. package/dist/powerlines/src/types/context.d.mts +515 -0
  16. package/dist/powerlines/src/types/fs.d.cts +486 -0
  17. package/dist/powerlines/src/types/fs.d.mts +486 -0
  18. package/dist/powerlines/src/types/hooks.d.cts +32 -0
  19. package/dist/powerlines/src/types/hooks.d.mts +32 -0
  20. package/dist/powerlines/src/types/plugin.d.cts +205 -0
  21. package/dist/powerlines/src/types/plugin.d.mts +205 -0
  22. package/dist/powerlines/src/types/resolved.d.cts +93 -0
  23. package/dist/powerlines/src/types/resolved.d.mts +93 -0
  24. package/dist/powerlines/src/types/tsconfig.d.cts +69 -0
  25. package/dist/powerlines/src/types/tsconfig.d.mts +69 -0
  26. package/dist/powerlines/src/types/unplugin.d.cts +22 -0
  27. package/dist/powerlines/src/types/unplugin.d.mts +23 -0
  28. package/dist/types/plugin.d.cts +3 -3
  29. package/dist/types/plugin.d.mts +3 -3
  30. package/package.json +4 -4
@@ -0,0 +1,515 @@
1
+ import { ResolveOptions, VirtualFile, VirtualFileSystemInterface, WriteOptions } from "./fs.mjs";
2
+ import { ParsedTypeScriptConfig } from "./tsconfig.mjs";
3
+ import { InlineConfig, LogFn, UserConfig, WorkspaceConfig } from "./config.mjs";
4
+ import { HooksList, InferHooksListItem } from "./hooks.mjs";
5
+ import { EnvironmentResolvedConfig, ResolvedConfig, ResolvedEntryTypeDefinition } from "./resolved.mjs";
6
+ import { Plugin } from "./plugin.mjs";
7
+ import { Unimport } from "unimport";
8
+ import { NonUndefined } from "@stryke/types/base";
9
+ import { ExternalIdResult, UnpluginBuildContext, UnpluginContext, UnpluginMessage } from "unplugin";
10
+ import { EnvPaths } from "@stryke/env/get-env-paths";
11
+ import { FetchRequestOptions } from "@stryke/http/fetch";
12
+ import { PackageJson } from "@stryke/types/package-json";
13
+ import { Jiti } from "jiti";
14
+ import { SourceMap } from "magic-string";
15
+ import { ParseResult, ParserOptions } from "oxc-parser";
16
+ import { Range } from "semver";
17
+ import { RequestInfo, Response } from "undici";
18
+
19
+ //#region ../powerlines/src/types/context.d.ts
20
+
21
+ interface MetaInfo {
22
+ /**
23
+ * The checksum generated from the resolved options
24
+ */
25
+ checksum: string;
26
+ /**
27
+ * The build id
28
+ */
29
+ buildId: string;
30
+ /**
31
+ * The release id
32
+ */
33
+ releaseId: string;
34
+ /**
35
+ * The build timestamp
36
+ */
37
+ timestamp: number;
38
+ /**
39
+ * A hash that represents the path to the project root directory
40
+ */
41
+ projectRootHash: string;
42
+ /**
43
+ * A hash that represents the path to the project root directory
44
+ */
45
+ configHash: string;
46
+ }
47
+ interface Resolver extends Jiti {
48
+ plugin: Jiti;
49
+ }
50
+ interface TransformResult$1 {
51
+ code: string;
52
+ map: SourceMap | null;
53
+ }
54
+ interface SelectHooksOptions {
55
+ order?: "pre" | "post" | "normal";
56
+ }
57
+ /**
58
+ * Options for initializing or updating the context with new configuration values
59
+ */
60
+ interface InitContextOptions {
61
+ /**
62
+ * If false, the plugin will be loaded after all other plugins.
63
+ *
64
+ * @defaultValue true
65
+ */
66
+ isHighPriority: boolean;
67
+ }
68
+ /**
69
+ * Options for fetch requests made via the context's {@link Context.fetch} method
70
+ */
71
+ interface FetchOptions extends FetchRequestOptions {
72
+ /**
73
+ * An indicator specifying that the request should bypass any caching
74
+ */
75
+ skipCache?: boolean;
76
+ }
77
+ /**
78
+ * Options for parsing code using [Oxc-Parser](https://github.com/oxc/oxc)
79
+ */
80
+ interface ParseOptions extends ParserOptions {
81
+ /**
82
+ * When true this allows return statements to be outside functions to e.g. support parsing CommonJS code.
83
+ */
84
+ allowReturnOutsideFunction?: boolean;
85
+ }
86
+ interface EmitOptions extends WriteOptions {
87
+ /**
88
+ * The file extension to use when emitting the file
89
+ */
90
+ extension?: string;
91
+ /**
92
+ * If true, will emit the file using {@link UnpluginBuildContext.emitFile | the bundler's emit function}.
93
+ */
94
+ emitWithBundler?: boolean;
95
+ needsCodeReference?: Parameters<UnpluginBuildContext["emitFile"]>[0]["needsCodeReference"];
96
+ originalFileName?: Parameters<UnpluginBuildContext["emitFile"]>[0]["originalFileName"];
97
+ }
98
+ /**
99
+ * Options for emitting entry virtual files
100
+ */
101
+ type EmitEntryOptions = EmitOptions & Omit<ResolvedEntryTypeDefinition, "file">;
102
+ /**
103
+ * The unresolved Powerlines context.
104
+ *
105
+ * @remarks
106
+ * This context is used before the user configuration has been fully resolved after the `config`.
107
+ */
108
+ interface UnresolvedContext<TResolvedConfig extends ResolvedConfig = ResolvedConfig> {
109
+ /**
110
+ * The Storm workspace configuration
111
+ */
112
+ workspaceConfig: WorkspaceConfig;
113
+ /**
114
+ * An object containing the options provided to Powerlines
115
+ */
116
+ config: Omit<TResolvedConfig["userConfig"], "build" | "output"> & Required<Pick<TResolvedConfig["userConfig"], "build" | "output">> & {
117
+ projectRoot: NonUndefined<TResolvedConfig["userConfig"]["root"]>;
118
+ sourceRoot: NonUndefined<TResolvedConfig["userConfig"]["sourceRoot"]>;
119
+ output: TResolvedConfig["output"];
120
+ };
121
+ /**
122
+ * A logging function for the Powerlines engine
123
+ */
124
+ log: LogFn;
125
+ /**
126
+ * A logging function for fatal messages
127
+ */
128
+ fatal: (message: string | UnpluginMessage) => void;
129
+ /**
130
+ * A logging function for error messages
131
+ */
132
+ error: (message: string | UnpluginMessage) => void;
133
+ /**
134
+ * A logging function for warning messages
135
+ */
136
+ warn: (message: string | UnpluginMessage) => void;
137
+ /**
138
+ * A logging function for informational messages
139
+ */
140
+ info: (message: string | UnpluginMessage) => void;
141
+ /**
142
+ * A logging function for debug messages
143
+ */
144
+ debug: (message: string | UnpluginMessage) => void;
145
+ /**
146
+ * A logging function for trace messages
147
+ */
148
+ trace: (message: string | UnpluginMessage) => void;
149
+ /**
150
+ * The metadata information
151
+ */
152
+ meta: MetaInfo;
153
+ /**
154
+ * The metadata information currently written to disk
155
+ */
156
+ persistedMeta?: MetaInfo;
157
+ /**
158
+ * The Powerlines artifacts directory
159
+ */
160
+ artifactsPath: string;
161
+ /**
162
+ * The path to the Powerlines builtin runtime modules directory
163
+ */
164
+ builtinsPath: string;
165
+ /**
166
+ * The path to the Powerlines entry modules directory
167
+ */
168
+ entryPath: string;
169
+ /**
170
+ * The path to the Powerlines TypeScript declaration files directory
171
+ */
172
+ dtsPath: string;
173
+ /**
174
+ * The path to a directory where the reflection data buffers (used by the build processes) are stored
175
+ */
176
+ dataPath: string;
177
+ /**
178
+ * The path to a directory where the project cache (used by the build processes) is stored
179
+ */
180
+ cachePath: string;
181
+ /**
182
+ * The Powerlines environment paths
183
+ */
184
+ envPaths: EnvPaths;
185
+ /**
186
+ * The file system path to the Powerlines package installation
187
+ */
188
+ powerlinesPath: string;
189
+ /**
190
+ * The relative path to the Powerlines workspace root directory
191
+ */
192
+ relativeToWorkspaceRoot: string;
193
+ /**
194
+ * The project's `package.json` file content
195
+ */
196
+ packageJson: PackageJson & Record<string, any>;
197
+ /**
198
+ * The project's `project.json` file content
199
+ */
200
+ projectJson?: Record<string, any>;
201
+ /**
202
+ * The dependency installations required by the project
203
+ */
204
+ dependencies: Record<string, string | Range>;
205
+ /**
206
+ * The development dependency installations required by the project
207
+ */
208
+ devDependencies: Record<string, string | Range>;
209
+ /**
210
+ * The parsed TypeScript configuration from the `tsconfig.json` file
211
+ */
212
+ tsconfig: ParsedTypeScriptConfig;
213
+ /**
214
+ * The entry points of the source code
215
+ */
216
+ entry: ResolvedEntryTypeDefinition[];
217
+ /**
218
+ * The virtual file system manager used during the build process to reference generated runtime files
219
+ */
220
+ fs: VirtualFileSystemInterface;
221
+ /**
222
+ * The Jiti module resolver
223
+ */
224
+ resolver: Resolver;
225
+ /**
226
+ * The builtin module id that exist in the Powerlines virtual file system
227
+ */
228
+ builtins: string[];
229
+ /**
230
+ * The alias mappings for the project used during module resolution
231
+ *
232
+ * @remarks
233
+ * This includes both the built-in module aliases as well as any custom aliases defined in the build configuration.
234
+ */
235
+ alias: Record<string, string>;
236
+ /**
237
+ * A function to perform HTTP fetch requests
238
+ *
239
+ * @remarks
240
+ * This function uses a caching layer to avoid duplicate requests during the Powerlines process.
241
+ *
242
+ * @example
243
+ * ```ts
244
+ * const response = await context.fetch("https://api.example.com/data");
245
+ * const data = await response.json();
246
+ * ```
247
+ *
248
+ * @see https://github.com/nodejs/undici
249
+ *
250
+ * @param input - The URL to fetch.
251
+ * @param options - The fetch request options.
252
+ * @returns A promise that resolves to a response returned by the fetch.
253
+ */
254
+ fetch: (input: RequestInfo, options?: FetchOptions) => Promise<Response>;
255
+ /**
256
+ * Parse code using [Oxc-Parser](https://github.com/oxc/oxc) into an (ESTree-compatible)[https://github.com/estree/estree] AST object.
257
+ *
258
+ * @remarks
259
+ * This function can be used to parse TypeScript code into an AST for further analysis or transformation.
260
+ *
261
+ * @example
262
+ * ```ts
263
+ * const ast = context.parse("const x: number = 42;");
264
+ * ```
265
+ *
266
+ * @see https://rollupjs.org/plugin-development/#this-parse
267
+ * @see https://github.com/oxc/oxc
268
+ *
269
+ * @param code - The source code to parse.
270
+ * @param options - The options to pass to the parser.
271
+ * @returns An (ESTree-compatible)[https://github.com/estree/estree] AST object.
272
+ */
273
+ parse: (code: string, options?: ParseOptions) => Promise<ParseResult>;
274
+ /**
275
+ * A helper function to resolve modules using the Jiti resolver
276
+ *
277
+ * @remarks
278
+ * This function can be used to resolve modules relative to the project root directory.
279
+ *
280
+ * @example
281
+ * ```ts
282
+ * const resolvedPath = await context.resolve("some-module", "/path/to/importer");
283
+ * ```
284
+ *
285
+ * @param id - The module to resolve.
286
+ * @param importer - An optional path to the importer module.
287
+ * @param options - Additional resolution options.
288
+ * @returns A promise that resolves to the resolved module path.
289
+ */
290
+ resolve: (id: string, importer?: string, options?: ResolveOptions) => Promise<ExternalIdResult | undefined>;
291
+ /**
292
+ * A helper function to load modules using the Jiti resolver
293
+ *
294
+ * @remarks
295
+ * This function can be used to load modules relative to the project root directory.
296
+ *
297
+ * @example
298
+ * ```ts
299
+ * const module = await context.load("some-module", "/path/to/importer");
300
+ * ```
301
+ *
302
+ * @param id - The module to load.
303
+ * @returns A promise that resolves to the loaded module.
304
+ */
305
+ load: (id: string) => Promise<TransformResult$1 | undefined>;
306
+ /**
307
+ * The Powerlines builtin virtual files
308
+ */
309
+ getBuiltins: () => Promise<VirtualFile[]>;
310
+ /**
311
+ * Resolves a file and writes it to the VFS if it does not already exist
312
+ *
313
+ * @param code - The source code of the file
314
+ * @param path - The path to write the file to
315
+ * @param options - Additional options for writing the file
316
+ */
317
+ emit: (code: string, path: string, options?: EmitOptions) => Promise<void>;
318
+ /**
319
+ * Synchronously resolves a file and writes it to the VFS if it does not already exist
320
+ *
321
+ * @param code - The source code of the file
322
+ * @param path - The path to write the file to
323
+ * @param options - Additional options for writing the file
324
+ */
325
+ emitSync: (code: string, path: string, options?: EmitOptions) => void;
326
+ /**
327
+ * Resolves a builtin virtual file and writes it to the VFS if it does not already exist
328
+ *
329
+ * @param code - The source code of the builtin file
330
+ * @param id - The unique identifier of the builtin file
331
+ * @param options - Additional options for writing the builtin file
332
+ */
333
+ emitBuiltin: (code: string, id: string, options?: EmitOptions) => Promise<void>;
334
+ /**
335
+ * Synchronously resolves a builtin virtual file and writes it to the VFS if it does not already exist
336
+ *
337
+ * @param code - The source code of the builtin file
338
+ * @param id - The unique identifier of the builtin file
339
+ * @param options - Additional options for writing the builtin file
340
+ */
341
+ emitBuiltinSync: (code: string, id: string, options?: EmitOptions) => void;
342
+ /**
343
+ * Resolves a entry virtual file and writes it to the VFS if it does not already exist
344
+ *
345
+ * @param code - The source code of the entry file
346
+ * @param path - An optional path to write the entry file to
347
+ * @param options - Additional options for writing the entry file
348
+ */
349
+ emitEntry: (code: string, path: string, options?: EmitEntryOptions) => Promise<void>;
350
+ /**
351
+ * Synchronously resolves a entry virtual file and writes it to the VFS if it does not already exist
352
+ *
353
+ * @param code - The source code of the entry file
354
+ * @param path - An optional path to write the entry file to
355
+ * @param options - Additional options for writing the entry file
356
+ */
357
+ emitEntrySync: (code: string, path: string, options?: EmitEntryOptions) => void;
358
+ /**
359
+ * A function to update the context fields using a new user configuration options
360
+ */
361
+ withUserConfig: (userConfig: UserConfig, options?: InitContextOptions) => Promise<void>;
362
+ /**
363
+ * A function to update the context fields using inline configuration options
364
+ */
365
+ withInlineConfig: (inlineConfig: InlineConfig, options?: InitContextOptions) => Promise<void>;
366
+ /**
367
+ * Create a new logger instance
368
+ *
369
+ * @param name - The name to use for the logger instance
370
+ * @returns A logger function
371
+ */
372
+ createLog: (name: string | null) => LogFn;
373
+ /**
374
+ * Extend the current logger instance with a new name
375
+ *
376
+ * @param name - The name to use for the extended logger instance
377
+ * @returns A logger function
378
+ */
379
+ extendLog: (name: string) => LogFn;
380
+ /**
381
+ * Generates a checksum representing the current context state
382
+ *
383
+ * @returns A promise that resolves to a string representing the checksum
384
+ */
385
+ generateChecksum: () => Promise<string>;
386
+ }
387
+ type Context<TResolvedConfig extends ResolvedConfig = ResolvedConfig> = Omit<UnresolvedContext<TResolvedConfig>, "config"> & {
388
+ /**
389
+ * The fully resolved Powerlines configuration
390
+ */
391
+ config: TResolvedConfig;
392
+ };
393
+ interface APIContext<TResolvedConfig extends ResolvedConfig = ResolvedConfig> extends Context<TResolvedConfig> {
394
+ /**
395
+ * The expected plugins options for the Powerlines project.
396
+ *
397
+ * @remarks
398
+ * This is a record of plugin identifiers to their respective options. This field is populated by the Powerlines engine during both plugin initialization and the `init` command.
399
+ */
400
+ plugins: Plugin<PluginContext<TResolvedConfig>>[];
401
+ /**
402
+ * A function to add a plugin to the context and update the configuration options
403
+ */
404
+ addPlugin: (plugin: Plugin<PluginContext<TResolvedConfig>>) => Promise<void>;
405
+ /**
406
+ * A table for storing the current context for each configured environment
407
+ */
408
+ environments: Record<string, EnvironmentContext<TResolvedConfig>>;
409
+ /**
410
+ * Retrieves the context for a specific environment by name
411
+ *
412
+ * @throws Will throw an error if the environment does not exist
413
+ *
414
+ * @param name - The name of the environment to retrieve. If not provided, the default environment is returned.
415
+ * @returns A promise that resolves to the environment context.
416
+ *
417
+ * @example
418
+ * ```ts
419
+ * const devEnv = await apiContext.getEnvironment("development");
420
+ * const defaultEnv = await apiContext.getEnvironment();
421
+ * ```
422
+ */
423
+ getEnvironment: (name?: string) => Promise<EnvironmentContext<TResolvedConfig>>;
424
+ /**
425
+ * Safely retrieves the context for a specific environment by name
426
+ *
427
+ * @param name - The name of the environment to retrieve. If not provided, the default environment is returned.
428
+ * @returns A promise that resolves to the environment context, or undefined if the environment does not exist.
429
+ *
430
+ * @example
431
+ * ```ts
432
+ * const devEnv = await apiContext.getEnvironmentSafe("development");
433
+ * const defaultEnv = await apiContext.getEnvironmentSafe();
434
+ * ```
435
+ *
436
+ * @remarks
437
+ * This method is similar to `getEnvironment`, but it returns `undefined` instead of throwing an error if the specified environment does not exist.
438
+ * This can be useful in scenarios where the existence of an environment is optional or uncertain.
439
+ *
440
+ * ```ts
441
+ * const testEnv = await apiContext.getEnvironmentSafe("test");
442
+ * if (testEnv) {
443
+ * // Environment exists, safe to use it
444
+ * } else {
445
+ * // Environment does not exist, handle accordingly
446
+ * }
447
+ * ```
448
+ *
449
+ * Using this method helps avoid unhandled exceptions in cases where an environment might not be defined.
450
+ */
451
+ getEnvironmentSafe: (name?: string) => Promise<EnvironmentContext<TResolvedConfig> | undefined>;
452
+ /**
453
+ * A function to copy the context and update the fields for a specific environment
454
+ *
455
+ * @param environment - The environment configuration to use.
456
+ * @returns A new context instance with the updated environment.
457
+ */
458
+ in: (environment: EnvironmentResolvedConfig) => Promise<EnvironmentContext<TResolvedConfig>>;
459
+ /**
460
+ * A function to merge all configured environments into a single context
461
+ *
462
+ * @returns A promise that resolves to the merged environment context.
463
+ */
464
+ toEnvironment: () => Promise<EnvironmentContext<TResolvedConfig>>;
465
+ }
466
+ interface EnvironmentContextPlugin<TResolvedConfig extends ResolvedConfig = ResolvedConfig> {
467
+ plugin: Plugin<PluginContext<TResolvedConfig>>;
468
+ context: PluginContext<TResolvedConfig>;
469
+ }
470
+ type SelectHookResultItem<TContext extends PluginContext, TKey extends string> = InferHooksListItem<TContext, TKey> & {
471
+ context: TContext;
472
+ };
473
+ type SelectHookResult<TContext extends PluginContext, TKey extends string> = SelectHookResultItem<TContext, TKey>[];
474
+ interface EnvironmentContext<TResolvedConfig extends ResolvedConfig = ResolvedConfig> extends Context<TResolvedConfig> {
475
+ /**
476
+ * The expected plugins options for the Powerlines project.
477
+ *
478
+ * @remarks
479
+ * This is a record of plugin identifiers to their respective options. This field is populated by the Powerlines engine during both plugin initialization and the `init` command.
480
+ */
481
+ plugins: EnvironmentContextPlugin<TResolvedConfig>[];
482
+ /**
483
+ * A function to add a plugin to the context and update the configuration options
484
+ */
485
+ addPlugin: (plugin: Plugin<PluginContext<TResolvedConfig>>) => Promise<void>;
486
+ /**
487
+ * The environment specific resolved configuration
488
+ */
489
+ environment: EnvironmentResolvedConfig;
490
+ /**
491
+ * A table holding references to hook functions registered by plugins
492
+ */
493
+ hooks: HooksList<PluginContext<TResolvedConfig>>;
494
+ /**
495
+ * Retrieves the hook handlers for a specific hook name
496
+ */
497
+ selectHooks: <TKey extends string>(key: TKey, options?: SelectHooksOptions) => SelectHookResult<PluginContext<TResolvedConfig>, TKey>;
498
+ }
499
+ interface PluginContext<out TResolvedConfig extends ResolvedConfig = ResolvedConfig> extends Context<TResolvedConfig>, UnpluginContext {
500
+ /**
501
+ * The environment specific resolved configuration
502
+ */
503
+ environment: EnvironmentResolvedConfig;
504
+ /**
505
+ * An alternative property name for the {@link log} property
506
+ *
507
+ * @remarks
508
+ * This is provided for compatibility with other logging libraries that expect a `logger` property.
509
+ */
510
+ logger: LogFn;
511
+ }
512
+ type BuildPluginContext<TResolvedConfig extends ResolvedConfig = ResolvedConfig> = UnpluginBuildContext & PluginContext<TResolvedConfig>;
513
+ type WithUnpluginBuildContext<TContext extends PluginContext> = UnpluginBuildContext & TContext;
514
+ //#endregion
515
+ export { APIContext, BuildPluginContext, Context, EnvironmentContext, PluginContext, SelectHooksOptions, UnresolvedContext, WithUnpluginBuildContext };