@powerlines/plugin-webpack 0.5.129 → 0.5.131

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 (130) hide show
  1. package/dist/helpers/index.cjs +3 -2
  2. package/dist/helpers/index.d.cts +3 -1
  3. package/dist/helpers/index.d.mts +3 -1
  4. package/dist/helpers/index.mjs +2 -1
  5. package/dist/helpers/unplugin.cjs +2 -11
  6. package/dist/helpers/unplugin.d.cts +2 -6
  7. package/dist/helpers/unplugin.d.mts +2 -6
  8. package/dist/helpers/unplugin.mjs +1 -9
  9. package/dist/helpers-B15z10jN.mjs +1 -0
  10. package/dist/helpers-LF26RHol.cjs +0 -0
  11. package/dist/index-9iG2qHLe.d.mts +1 -0
  12. package/dist/index-D4ELpJXS.d.cts +1 -0
  13. package/dist/index-D6CnpA_r.d.cts +1 -0
  14. package/dist/index-DL0uimUT.d.mts +1 -0
  15. package/dist/index.cjs +50 -8
  16. package/dist/index.d.cts +4 -3
  17. package/dist/index.d.mts +5 -5
  18. package/dist/index.mjs +46 -3
  19. package/dist/plugin-CsQFPhIt.d.cts +1762 -0
  20. package/dist/plugin-ifZVa20V.mjs +1 -0
  21. package/dist/plugin-pBKbb5K9.cjs +0 -0
  22. package/dist/plugin-wiaZGMv0.d.mts +1764 -0
  23. package/dist/types/index.cjs +2 -0
  24. package/dist/types/index.d.cts +2 -1
  25. package/dist/types/index.d.mts +2 -1
  26. package/dist/types/index.mjs +3 -0
  27. package/dist/types/plugin.cjs +1 -0
  28. package/dist/types/plugin.d.cts +1 -12
  29. package/dist/types/plugin.d.mts +1 -12
  30. package/dist/types/plugin.mjs +2 -0
  31. package/dist/types-U3zd8PTP.mjs +1 -0
  32. package/dist/types-o3zWarRp.cjs +0 -0
  33. package/dist/unplugin-BVSyqmCe.d.cts +7 -0
  34. package/dist/unplugin-CVrlqRhX.mjs +4615 -0
  35. package/dist/unplugin-Cemsz8kP.cjs +4659 -0
  36. package/dist/unplugin-vRGq3odL.d.mts +7 -0
  37. package/package.json +5 -5
  38. package/dist/_virtual/rolldown_runtime.cjs +0 -29
  39. package/dist/powerlines/schemas/fs.cjs +0 -226
  40. package/dist/powerlines/schemas/fs.mjs +0 -224
  41. package/dist/powerlines/src/api.cjs +0 -580
  42. package/dist/powerlines/src/api.mjs +0 -578
  43. package/dist/powerlines/src/internal/babel/module-resolver-plugin.cjs +0 -86
  44. package/dist/powerlines/src/internal/babel/module-resolver-plugin.mjs +0 -84
  45. package/dist/powerlines/src/internal/helpers/environment.cjs +0 -52
  46. package/dist/powerlines/src/internal/helpers/environment.mjs +0 -48
  47. package/dist/powerlines/src/internal/helpers/generate-types.cjs +0 -51
  48. package/dist/powerlines/src/internal/helpers/generate-types.mjs +0 -49
  49. package/dist/powerlines/src/internal/helpers/hooks.cjs +0 -63
  50. package/dist/powerlines/src/internal/helpers/hooks.mjs +0 -61
  51. package/dist/powerlines/src/internal/helpers/install-dependencies.cjs +0 -25
  52. package/dist/powerlines/src/internal/helpers/install-dependencies.mjs +0 -24
  53. package/dist/powerlines/src/internal/helpers/install.cjs +0 -37
  54. package/dist/powerlines/src/internal/helpers/install.mjs +0 -36
  55. package/dist/powerlines/src/internal/helpers/resolve-tsconfig.cjs +0 -98
  56. package/dist/powerlines/src/internal/helpers/resolve-tsconfig.mjs +0 -94
  57. package/dist/powerlines/src/internal/helpers/resolver.cjs +0 -43
  58. package/dist/powerlines/src/internal/helpers/resolver.mjs +0 -41
  59. package/dist/powerlines/src/lib/build/webpack.cjs +0 -47
  60. package/dist/powerlines/src/lib/build/webpack.mjs +0 -45
  61. package/dist/powerlines/src/lib/config-file.cjs +0 -79
  62. package/dist/powerlines/src/lib/config-file.mjs +0 -76
  63. package/dist/powerlines/src/lib/contexts/api-context.cjs +0 -184
  64. package/dist/powerlines/src/lib/contexts/api-context.mjs +0 -182
  65. package/dist/powerlines/src/lib/contexts/context.cjs +0 -931
  66. package/dist/powerlines/src/lib/contexts/context.mjs +0 -929
  67. package/dist/powerlines/src/lib/contexts/environment-context.cjs +0 -160
  68. package/dist/powerlines/src/lib/contexts/environment-context.mjs +0 -159
  69. package/dist/powerlines/src/lib/contexts/plugin-context.cjs +0 -75
  70. package/dist/powerlines/src/lib/contexts/plugin-context.mjs +0 -74
  71. package/dist/powerlines/src/lib/entry.cjs +0 -69
  72. package/dist/powerlines/src/lib/entry.mjs +0 -67
  73. package/dist/powerlines/src/lib/fs/helpers.cjs +0 -85
  74. package/dist/powerlines/src/lib/fs/helpers.mjs +0 -81
  75. package/dist/powerlines/src/lib/fs/storage/base.cjs +0 -196
  76. package/dist/powerlines/src/lib/fs/storage/base.mjs +0 -195
  77. package/dist/powerlines/src/lib/fs/storage/file-system.cjs +0 -170
  78. package/dist/powerlines/src/lib/fs/storage/file-system.mjs +0 -169
  79. package/dist/powerlines/src/lib/fs/storage/virtual.cjs +0 -88
  80. package/dist/powerlines/src/lib/fs/storage/virtual.mjs +0 -87
  81. package/dist/powerlines/src/lib/fs/vfs.cjs +0 -830
  82. package/dist/powerlines/src/lib/fs/vfs.mjs +0 -828
  83. package/dist/powerlines/src/lib/logger.cjs +0 -58
  84. package/dist/powerlines/src/lib/logger.mjs +0 -55
  85. package/dist/powerlines/src/lib/typescript/ts-morph.cjs +0 -104
  86. package/dist/powerlines/src/lib/typescript/ts-morph.mjs +0 -102
  87. package/dist/powerlines/src/lib/typescript/tsconfig.cjs +0 -149
  88. package/dist/powerlines/src/lib/typescript/tsconfig.mjs +0 -144
  89. package/dist/powerlines/src/lib/unplugin/factory.cjs +0 -6
  90. package/dist/powerlines/src/lib/unplugin/factory.mjs +0 -8
  91. package/dist/powerlines/src/lib/unplugin/helpers.cjs +0 -17
  92. package/dist/powerlines/src/lib/unplugin/helpers.mjs +0 -16
  93. package/dist/powerlines/src/lib/unplugin/index.cjs +0 -3
  94. package/dist/powerlines/src/lib/unplugin/index.mjs +0 -5
  95. package/dist/powerlines/src/lib/unplugin/plugin.cjs +0 -128
  96. package/dist/powerlines/src/lib/unplugin/plugin.mjs +0 -127
  97. package/dist/powerlines/src/lib/utilities/file-header.cjs +0 -22
  98. package/dist/powerlines/src/lib/utilities/file-header.mjs +0 -21
  99. package/dist/powerlines/src/lib/utilities/meta.cjs +0 -45
  100. package/dist/powerlines/src/lib/utilities/meta.mjs +0 -41
  101. package/dist/powerlines/src/lib/utilities/source-file.cjs +0 -22
  102. package/dist/powerlines/src/lib/utilities/source-file.mjs +0 -21
  103. package/dist/powerlines/src/plugin-utils/helpers.cjs +0 -148
  104. package/dist/powerlines/src/plugin-utils/helpers.mjs +0 -138
  105. package/dist/powerlines/src/plugin-utils/paths.cjs +0 -36
  106. package/dist/powerlines/src/plugin-utils/paths.mjs +0 -35
  107. package/dist/powerlines/src/types/babel.d.mts +0 -4
  108. package/dist/powerlines/src/types/build.cjs +0 -15
  109. package/dist/powerlines/src/types/build.d.cts +0 -149
  110. package/dist/powerlines/src/types/build.d.mts +0 -149
  111. package/dist/powerlines/src/types/build.mjs +0 -14
  112. package/dist/powerlines/src/types/commands.cjs +0 -16
  113. package/dist/powerlines/src/types/commands.d.cts +0 -8
  114. package/dist/powerlines/src/types/commands.d.mts +0 -9
  115. package/dist/powerlines/src/types/commands.mjs +0 -15
  116. package/dist/powerlines/src/types/config.d.cts +0 -370
  117. package/dist/powerlines/src/types/config.d.mts +0 -372
  118. package/dist/powerlines/src/types/context.d.cts +0 -403
  119. package/dist/powerlines/src/types/context.d.mts +0 -405
  120. package/dist/powerlines/src/types/fs.d.cts +0 -486
  121. package/dist/powerlines/src/types/fs.d.mts +0 -486
  122. package/dist/powerlines/src/types/hooks.d.mts +0 -2
  123. package/dist/powerlines/src/types/plugin.cjs +0 -32
  124. package/dist/powerlines/src/types/plugin.d.cts +0 -231
  125. package/dist/powerlines/src/types/plugin.d.mts +0 -231
  126. package/dist/powerlines/src/types/plugin.mjs +0 -31
  127. package/dist/powerlines/src/types/resolved.d.cts +0 -82
  128. package/dist/powerlines/src/types/resolved.d.mts +0 -83
  129. package/dist/powerlines/src/types/tsconfig.d.cts +0 -69
  130. package/dist/powerlines/src/types/tsconfig.d.mts +0 -69
@@ -1,929 +0,0 @@
1
- import { createProgram } from "../typescript/ts-morph.mjs";
2
- import { getTsconfigFilePath } from "../typescript/tsconfig.mjs";
3
- import { loadUserConfigFile, loadWorkspaceConfig } from "../config-file.mjs";
4
- import { createResolver } from "../../internal/helpers/resolver.mjs";
5
- import { checkDedupe, isPlugin } from "../../plugin-utils/helpers.mjs";
6
- import { replacePathTokens } from "../../plugin-utils/paths.mjs";
7
- import { getUniqueEntries, resolveEntriesSync } from "../entry.mjs";
8
- import { createLog, extendLog } from "../logger.mjs";
9
- import { VirtualFileSystem } from "../fs/vfs.mjs";
10
- import { CACHE_HASH_LENGTH, PROJECT_ROOT_HASH_LENGTH, getPrefixedProjectRootHash } from "../utilities/meta.mjs";
11
- import { LogLevelLabel } from "@storm-software/config-tools/types";
12
- import defu$1 from "defu";
13
- import { omit } from "@stryke/helpers/omit";
14
- import { relativeToWorkspaceRoot } from "@stryke/fs/get-workspace-root";
15
- import { toArray } from "@stryke/convert/to-array";
16
- import { resolvePackage } from "@stryke/fs/resolve";
17
- import { appendPath } from "@stryke/path/append";
18
- import { replacePath } from "@stryke/path/replace";
19
- import { isFunction } from "@stryke/type-checks/is-function";
20
- import { isSetObject } from "@stryke/type-checks/is-set-object";
21
- import { isSetString } from "@stryke/type-checks/is-set-string";
22
- import { isString } from "@stryke/type-checks/is-string";
23
- import { readJsonFile } from "@stryke/fs/json";
24
- import { StormJSON } from "@stryke/json/storm-json";
25
- import { titleCase } from "@stryke/string-format/title-case";
26
- import { existsSync } from "@stryke/fs/exists";
27
- import { getEnvPaths } from "@stryke/env/get-env-paths";
28
- import { hashDirectory } from "@stryke/hash/hash-files";
29
- import { murmurhash } from "@stryke/hash/murmurhash";
30
- import { getUnique, getUniqueBy } from "@stryke/helpers/get-unique";
31
- import { fetchRequest } from "@stryke/http/fetch";
32
- import { isParentPath } from "@stryke/path/is-parent-path";
33
- import { isAbsolute } from "@stryke/path/is-type";
34
- import { joinPaths } from "@stryke/path/join";
35
- import { isNull } from "@stryke/type-checks/is-null";
36
- import { uuid } from "@stryke/unique-id/uuid";
37
- import { match, tsconfigPathsToRegExp } from "bundle-require";
38
- import { create } from "flat-cache";
39
- import { parse } from "oxc-parser";
40
- import { Agent, Response, interceptors, setGlobalDispatcher } from "undici";
41
-
42
- //#region ../powerlines/src/lib/contexts/context.ts
43
- const configCache = /* @__PURE__ */ new WeakMap();
44
- const envPathCache = /* @__PURE__ */ new WeakMap();
45
- setGlobalDispatcher(new Agent({ keepAliveTimeout: 1e4 }).compose(interceptors.retry({
46
- maxRetries: 3,
47
- minTimeout: 1e3,
48
- maxTimeout: 1e4,
49
- timeoutFactor: 2,
50
- retryAfter: true
51
- })));
52
- var PowerlinesContext = class PowerlinesContext {
53
- /**
54
- * Internal references storage
55
- *
56
- * @danger
57
- * This field is for internal use only and should not be accessed or modified directly. It is unstable and can be changed at anytime.
58
- *
59
- * @internal
60
- */
61
- #internal = {};
62
- #workspaceConfig;
63
- #checksum = null;
64
- #buildId = uuid();
65
- #releaseId = uuid();
66
- #timestamp = Date.now();
67
- #entry = null;
68
- #fs;
69
- #tsconfig;
70
- #program;
71
- #parserCache;
72
- #requestCache;
73
- #getConfigProps(config = {}) {
74
- return defu$1({
75
- variant: config.build?.variant,
76
- projectType: config.type,
77
- projectRoot: config.root,
78
- name: config.name,
79
- title: config.title,
80
- compatibilityDate: config.compatibilityDate,
81
- description: config.description,
82
- sourceRoot: config.sourceRoot,
83
- configFile: config.configFile,
84
- customLogger: config.customLogger,
85
- logLevel: config.logLevel,
86
- tsconfig: config.tsconfig,
87
- tsconfigRaw: config.tsconfigRaw,
88
- skipCache: config.skipCache,
89
- skipInstalls: config.skipInstalls,
90
- entry: config.entry,
91
- output: config.output,
92
- plugins: config.plugins,
93
- mode: config.mode,
94
- lint: config.lint,
95
- transform: config.transform,
96
- build: config.build,
97
- framework: config.framework,
98
- ...config
99
- }, { output: config.framework ? {
100
- artifactsPath: joinPaths(config.root ?? this.config.projectRoot, `.${config.framework ?? "powerlines"}`),
101
- dts: joinPaths(config.root ?? this.config.projectRoot, `${config.framework ?? "powerlines"}.d.ts`),
102
- builtinPrefix: config.framework ?? "powerlines"
103
- } : {} });
104
- }
105
- /**
106
- * Create a new Storm context from the workspace root and user config.
107
- *
108
- * @param workspaceRoot - The root directory of the workspace.
109
- * @param config - The user configuration options.
110
- * @returns A promise that resolves to the new context.
111
- */
112
- static async from(workspaceRoot, config) {
113
- const context = new PowerlinesContext(await loadWorkspaceConfig(workspaceRoot, config.root));
114
- await context.withUserConfig(config);
115
- const powerlinesPath = await resolvePackage("powerlines");
116
- if (!powerlinesPath) throw new Error("Could not resolve `powerlines` package location.");
117
- context.powerlinesPath = powerlinesPath;
118
- return context;
119
- }
120
- /**
121
- * An object containing the dependencies that should be installed for the project
122
- */
123
- dependencies = {};
124
- /**
125
- * An object containing the development dependencies that should be installed for the project
126
- */
127
- devDependencies = {};
128
- /**
129
- * The persisted meta information about the current build
130
- */
131
- persistedMeta = void 0;
132
- /**
133
- * The path to the Powerlines package
134
- */
135
- powerlinesPath;
136
- /**
137
- * The parsed `package.json` file for the project
138
- */
139
- packageJson;
140
- /**
141
- * The parsed `project.json` file for the project
142
- */
143
- projectJson = void 0;
144
- /**
145
- * The module resolver for the project
146
- */
147
- resolver;
148
- /**
149
- * The resolved configuration options
150
- */
151
- resolvePatterns = [];
152
- /**
153
- * Internal context fields and methods
154
- *
155
- * @danger
156
- * This field is for internal use only and should not be accessed or modified directly. It is unstable and can be changed at anytime.
157
- *
158
- * @internal
159
- */
160
- get $$internal() {
161
- return this.#internal;
162
- }
163
- /**
164
- * Internal context fields and methods
165
- *
166
- * @danger
167
- * This field is for internal use only and should not be accessed or modified directly. It is unstable and can be changed at anytime.
168
- *
169
- * @internal
170
- */
171
- set $$internal(value) {
172
- this.#internal = value;
173
- }
174
- /**
175
- * The resolved entry type definitions for the project
176
- */
177
- get entry() {
178
- return resolveEntriesSync(this, !this.#entry ? toArray(this.config.entry) : this.#entry);
179
- }
180
- /**
181
- * Sets the resolved entry type definitions for the project
182
- */
183
- set entry(value) {
184
- this.#entry = value;
185
- }
186
- /**
187
- * The TypeScript configuration parsed from the tsconfig file
188
- */
189
- get tsconfig() {
190
- if (!this.#tsconfig) this.tsconfig = { tsconfigFilePath: this.config.tsconfig };
191
- return this.#tsconfig;
192
- }
193
- /**
194
- * Sets the TypeScript configuration parsed from the tsconfig file
195
- */
196
- set tsconfig(value) {
197
- this.#tsconfig = value;
198
- this.resolvePatterns = tsconfigPathsToRegExp(value?.options?.paths ?? {});
199
- }
200
- /**
201
- * The virtual file system interface for the project
202
- */
203
- get fs() {
204
- if (!this.#fs) this.#fs = VirtualFileSystem.createSync(this);
205
- return this.#fs;
206
- }
207
- /**
208
- * Get the checksum of the project's current state
209
- */
210
- get checksum() {
211
- return this.#checksum;
212
- }
213
- /**
214
- * The meta information about the current build
215
- */
216
- get meta() {
217
- return {
218
- buildId: this.#buildId,
219
- releaseId: this.#releaseId,
220
- checksum: this.#checksum,
221
- timestamp: this.#timestamp,
222
- projectRootHash: murmurhash({
223
- workspaceRoot: this.workspaceConfig?.workspaceRoot,
224
- projectRoot: this.config?.projectRoot
225
- }, { maxLength: PROJECT_ROOT_HASH_LENGTH }),
226
- configHash: murmurhash(this.config, { maxLength: CACHE_HASH_LENGTH })
227
- };
228
- }
229
- /**
230
- * The resolved configuration options
231
- */
232
- get config() {
233
- return this.resolvedConfig ?? {};
234
- }
235
- /**
236
- * The logger function
237
- */
238
- get log() {
239
- if (!this.logFn) this.logFn = this.createLog();
240
- return this.logFn;
241
- }
242
- /**
243
- * The workspace configuration
244
- */
245
- get workspaceConfig() {
246
- return this.#workspaceConfig;
247
- }
248
- /**
249
- * The environment paths for the project
250
- */
251
- get envPaths() {
252
- if (envPathCache.has({
253
- workspaceRoot: this.workspaceConfig.workspaceRoot,
254
- framework: this.config?.framework || "powerlines"
255
- })) return envPathCache.get({
256
- workspaceRoot: this.workspaceConfig.workspaceRoot,
257
- framework: this.config?.framework || "powerlines"
258
- });
259
- const envPaths = getEnvPaths({
260
- orgId: "storm-software",
261
- appId: this.config?.framework || "powerlines",
262
- workspaceRoot: this.workspaceConfig.workspaceRoot
263
- });
264
- envPathCache.set({
265
- workspaceRoot: this.workspaceConfig.workspaceRoot,
266
- framework: this.config?.framework || "powerlines"
267
- }, envPaths);
268
- return envPaths;
269
- }
270
- /**
271
- * Get the path to the artifacts directory for the project
272
- */
273
- get artifactsPath() {
274
- return joinPaths(this.workspaceConfig.workspaceRoot, this.config.projectRoot, this.config.output.artifactsPath);
275
- }
276
- /**
277
- * Get the path to the builtin modules used by the project
278
- */
279
- get builtinsPath() {
280
- return joinPaths(this.artifactsPath, "builtins");
281
- }
282
- /**
283
- * Get the path to the entry directory for the project
284
- */
285
- get entryPath() {
286
- return joinPaths(this.artifactsPath, "entry");
287
- }
288
- /**
289
- * Get the path to the data directory for the project
290
- */
291
- get dataPath() {
292
- return joinPaths(this.envPaths.data, "projects", getPrefixedProjectRootHash(this.config.name, this.meta.projectRootHash));
293
- }
294
- /**
295
- * Get the path to the cache directory for the project
296
- */
297
- get cachePath() {
298
- return joinPaths(this.envPaths.cache, "projects", murmurhash({
299
- checksum: this.#checksum,
300
- config: this.meta.configHash
301
- }, { maxLength: CACHE_HASH_LENGTH }));
302
- }
303
- /**
304
- * Get the path to the generated declaration file for the project
305
- */
306
- get dtsPath() {
307
- return this.config.output.dts ? appendPath(this.config.output.dts, this.workspaceConfig.workspaceRoot) : joinPaths(this.workspaceConfig.workspaceRoot, this.config.projectRoot, "powerlines.d.ts");
308
- }
309
- /**
310
- * Get the project root relative to the workspace root
311
- */
312
- get relativeToWorkspaceRoot() {
313
- return relativeToWorkspaceRoot(this.config.projectRoot);
314
- }
315
- /**
316
- * The builtin module id that exist in the Powerlines virtual file system
317
- */
318
- get builtins() {
319
- return Object.values(this.fs.metadata).filter((meta) => meta && meta.type === "builtin").map((meta) => meta?.id).filter(Boolean);
320
- }
321
- /**
322
- * The {@link Project} instance used for type reflection and module manipulation
323
- *
324
- * @see https://ts-morph.com/
325
- *
326
- * @remarks
327
- * This instance is created lazily on first access.
328
- */
329
- get program() {
330
- if (!this.#program) this.#program = createProgram(this, { skipAddingFilesFromTsConfig: true });
331
- return this.#program;
332
- }
333
- /**
334
- * Gets the parser cache.
335
- */
336
- get parserCache() {
337
- if (!this.#parserCache) this.#parserCache = create({
338
- cacheId: "parser",
339
- cacheDir: this.cachePath,
340
- ttl: 7200 * 1e3,
341
- lruSize: 5e3,
342
- persistInterval: 250
343
- });
344
- return this.#parserCache;
345
- }
346
- /**
347
- * Gets the request cache.
348
- */
349
- get requestCache() {
350
- if (!this.#requestCache) this.#requestCache = create({
351
- cacheId: "http",
352
- cacheDir: this.cachePath,
353
- ttl: 360 * 60 * 1e3,
354
- lruSize: 5e3,
355
- persistInterval: 250
356
- });
357
- return this.#requestCache;
358
- }
359
- /**
360
- * A function to perform HTTP fetch requests
361
- *
362
- * @remarks
363
- * This function uses a caching layer to avoid duplicate requests during the Powerlines process.
364
- *
365
- * @example
366
- * ```ts
367
- * const response = await context.fetch("https://api.example.com/data");
368
- * const data = await response.json();
369
- * ```
370
- *
371
- * @see https://github.com/nodejs/undici
372
- *
373
- * @param input - The URL to fetch.
374
- * @param options - The fetch request options.
375
- * @returns A promise that resolves to a response returned by the fetch.
376
- */
377
- async fetch(input, options = {}) {
378
- const cacheKey = murmurhash({
379
- input: input.toString(),
380
- options: JSON.stringify(options)
381
- });
382
- if (!this.config.skipCache && !options.skipCache) {
383
- const cached = this.requestCache.get(cacheKey);
384
- if (cached) return new Response(cached.body, {
385
- status: cached.status,
386
- statusText: cached.statusText,
387
- headers: cached.headers
388
- });
389
- }
390
- const response = await fetchRequest(input, {
391
- timeout: 12e3,
392
- ...options
393
- });
394
- const result = {
395
- body: await response.text(),
396
- status: response.status,
397
- statusText: response.statusText,
398
- headers: Object.fromEntries(response.headers.entries())
399
- };
400
- if (!this.config.skipCache && !options.skipCache) try {
401
- this.requestCache.set(cacheKey, result);
402
- } catch {}
403
- return new Response(result.body, {
404
- status: result.status,
405
- statusText: result.statusText,
406
- headers: result.headers
407
- });
408
- }
409
- /**
410
- * Parse code using [Oxc-Parser](https://github.com/oxc/oxc) into an (ESTree-compatible)[https://github.com/estree/estree] AST object.
411
- *
412
- * @remarks
413
- * This function can be used to parse TypeScript code into an AST for further analysis or transformation.
414
- *
415
- * @example
416
- * ```ts
417
- * const ast = context.parse("const x: number = 42;");
418
- * ```
419
- *
420
- * @see https://rollupjs.org/plugin-development/#this-parse
421
- * @see https://github.com/oxc/oxc
422
- *
423
- * @param code - The source code to parse.
424
- * @param options - The options to pass to the parser.
425
- * @returns An (ESTree-compatible)[https://github.com/estree/estree] AST object.
426
- */
427
- async parse(code, options = {}) {
428
- const cacheKey = murmurhash({
429
- code,
430
- options
431
- });
432
- let result;
433
- if (!this.config.skipCache) {
434
- result = this.parserCache.get(cacheKey);
435
- if (result) return result;
436
- }
437
- result = await parse(`source.${options.lang || "ts"}`, code, {
438
- ...options,
439
- sourceType: "module",
440
- showSemanticErrors: this.config.mode === "development"
441
- });
442
- if (!this.config.skipCache) this.parserCache.set(cacheKey, result);
443
- return result;
444
- }
445
- /**
446
- * A helper function to resolve modules in the Virtual File System
447
- *
448
- * @remarks
449
- * This function can be used to resolve modules relative to the project root directory.
450
- *
451
- * @example
452
- * ```ts
453
- * const resolved = await context.resolve("some-module", "/path/to/importer");
454
- * ```
455
- *
456
- * @param id - The module to resolve.
457
- * @param importer - An optional path to the importer module.
458
- * @param options - Additional resolution options.
459
- * @returns A promise that resolves to the resolved module path.
460
- */
461
- async resolve(id, importer, options = {}) {
462
- let moduleId = id;
463
- if (this.config.build.alias) {
464
- if (Array.isArray(this.config.build.alias)) {
465
- const alias = this.config.build.alias.find((a) => match(moduleId, [a.find]));
466
- if (alias) moduleId = alias.replacement;
467
- } else if (isSetObject(this.config.build.alias) && this.config.build.alias[id]) moduleId = this.config.build.alias[id];
468
- }
469
- if (this.fs.isVirtual(moduleId)) {
470
- const result = await this.fs.resolve(moduleId, importer, {
471
- conditions: this.config.build.conditions,
472
- extensions: this.config.build.extensions,
473
- ...options
474
- });
475
- if (!result) return;
476
- return {
477
- id: `\0${result}`,
478
- external: this.config.projectType !== "application"
479
- };
480
- }
481
- if (this.config.build.skipNodeModulesBundle) {
482
- if (match(moduleId, this.resolvePatterns) || match(moduleId, this.config.build.noExternal)) return;
483
- if (match(moduleId, this.config.build.external) || moduleId.startsWith("node:")) return {
484
- id: moduleId,
485
- external: true
486
- };
487
- if (!/^[A-Z]:[/\\]|^\.{0,2}\/|^\.{1,2}$/.test(moduleId)) return {
488
- id: moduleId,
489
- external: true
490
- };
491
- } else {
492
- if (match(moduleId, this.config.build.noExternal)) return;
493
- if (match(moduleId, this.config.build.external) || moduleId.startsWith("node:")) return {
494
- id: moduleId,
495
- external: true
496
- };
497
- }
498
- }
499
- /**
500
- * A helper function to load modules from the Virtual File System
501
- *
502
- * @remarks
503
- * This function can be used to load modules relative to the project root directory.
504
- *
505
- * @example
506
- * ```ts
507
- * const module = await context.load("some-module", "/path/to/importer");
508
- * ```
509
- *
510
- * @param id - The module to load.
511
- * @returns A promise that resolves to the loaded module.
512
- */
513
- async load(id) {
514
- const resolvedId = await this.fs.resolve(id);
515
- if (!resolvedId) return;
516
- const code = await this.fs.read(resolvedId);
517
- if (!code) return;
518
- return {
519
- code,
520
- map: null
521
- };
522
- }
523
- /**
524
- * Get the builtin virtual files that exist in the Powerlines virtual file system
525
- */
526
- async getBuiltins() {
527
- return Promise.all(Object.entries(this.fs.metadata).filter(([, meta]) => meta && meta.type === "builtin").map(async ([path, meta]) => {
528
- const code = await this.fs.read(path);
529
- return {
530
- ...meta,
531
- path,
532
- code
533
- };
534
- }));
535
- }
536
- /**
537
- * Resolves a file and writes it to the VFS if it does not already exist
538
- *
539
- * @param code - The source code of the file
540
- * @param path - The path to write the file to
541
- * @param options - Additional options for writing the file
542
- */
543
- async emit(code, path, options = {}) {
544
- if (isFunction(this.emitFile) && options.emitWithBundler) return this.emitFile({
545
- needsCodeReference: options.needsCodeReference,
546
- originalFileName: options.originalFileName,
547
- fileName: path,
548
- source: code,
549
- type: "asset"
550
- });
551
- return this.fs.write(path, code, options);
552
- }
553
- /**
554
- * Synchronously resolves a file and writes it to the VFS if it does not already exist
555
- *
556
- * @param code - The source code of the file
557
- * @param path - The path to write the file to
558
- * @param options - Additional options for writing the file
559
- */
560
- emitSync(code, path, options = {}) {
561
- if (isFunction(this.emitFile) && options.emitWithBundler) return this.emitFile({
562
- needsCodeReference: options.needsCodeReference,
563
- originalFileName: options.originalFileName,
564
- fileName: path,
565
- source: code,
566
- type: "asset"
567
- });
568
- return this.fs.writeSync(path, code, options);
569
- }
570
- /**
571
- * Resolves a entry virtual file and writes it to the VFS if it does not already exist
572
- *
573
- * @param code - The source code of the entry file
574
- * @param path - A path to write the entry file to
575
- * @param options - Optional write file options
576
- */
577
- async emitEntry(code, path, options = {}) {
578
- const entryPath = isAbsolute(path) ? path : appendPath(path, this.entryPath);
579
- this.entry ??= [];
580
- this.entry.push({
581
- name: options.name,
582
- file: entryPath,
583
- input: options.input,
584
- output: options.output
585
- });
586
- return this.emit(code, entryPath, defu$1(omit(options, ["name"]), { meta: {
587
- type: "entry",
588
- properties: {
589
- name: options.name,
590
- output: options.output,
591
- "input.file": options.input?.file,
592
- "input.name": options.input?.name
593
- }
594
- } }));
595
- }
596
- /**
597
- * Synchronously resolves a entry virtual file and writes it to the VFS if it does not already exist
598
- *
599
- * @param code - The source code of the entry file
600
- * @param path - A path to write the entry file to
601
- * @param options - Optional write file options
602
- */
603
- emitEntrySync(code, path, options = {}) {
604
- const entryPath = isAbsolute(path) ? path : appendPath(path, this.entryPath);
605
- this.entry ??= [];
606
- this.entry.push({
607
- name: options?.name,
608
- file: entryPath,
609
- input: options?.input,
610
- output: options?.output
611
- });
612
- return this.emitSync(code, entryPath, defu$1(omit(options, ["name"]), { meta: {
613
- type: "entry",
614
- properties: {
615
- name: options?.name,
616
- output: options?.output,
617
- "input.file": options?.input?.file,
618
- "input.name": options?.input?.name
619
- }
620
- } }));
621
- }
622
- /**
623
- * Resolves a builtin virtual file and writes it to the VFS if it does not already exist
624
- *
625
- * @param code - The source code of the builtin file
626
- * @param id - The unique identifier of the builtin file
627
- * @param path - An optional path to write the builtin file to
628
- * @param options - Optional write file options
629
- */
630
- async emitBuiltin(code, id, path, options = {}) {
631
- return this.emit(code, path ? isAbsolute(path) ? path : joinPaths(this.builtinsPath, path) : appendPath(id, this.builtinsPath), defu$1(options, { meta: { type: "builtin" } }));
632
- }
633
- /**
634
- * Synchronously resolves a builtin virtual file and writes it to the VFS if it does not already exist
635
- *
636
- * @param code - The source code of the builtin file
637
- * @param id - The unique identifier of the builtin file
638
- * @param path - An optional path to write the builtin file to
639
- * @param options - Optional write file options
640
- */
641
- emitBuiltinSync(code, id, path, options = {}) {
642
- return this.emitSync(code, path ? isAbsolute(path) ? path : joinPaths(this.builtinsPath, path) : appendPath(id, this.builtinsPath), defu$1(options, { meta: { type: "builtin" } }));
643
- }
644
- /**
645
- * Update the context using a new user configuration options
646
- *
647
- * @param userConfig - The new user configuration options.
648
- */
649
- async withUserConfig(userConfig, options = { isHighPriority: true }) {
650
- this.mergeUserConfig(userConfig);
651
- await this.init(this.config.userConfig, options);
652
- }
653
- /**
654
- * Update the context using a new inline configuration options
655
- *
656
- * @param inlineConfig - The new inline configuration options.
657
- */
658
- async withInlineConfig(inlineConfig, options = { isHighPriority: true }) {
659
- this.config.inlineConfig = inlineConfig;
660
- if (inlineConfig.command === "new") {
661
- const workspacePackageJsonPath = joinPaths(this.workspaceConfig.workspaceRoot, "package.json");
662
- if (!existsSync(workspacePackageJsonPath)) throw new Error(`The workspace package.json file could not be found at ${workspacePackageJsonPath}`);
663
- this.packageJson = await readJsonFile(workspacePackageJsonPath);
664
- this.workspaceConfig.repository ??= isSetString(this.packageJson?.repository) ? this.packageJson.repository : this.packageJson?.repository?.url;
665
- }
666
- await this.init(this.config.inlineConfig, options);
667
- }
668
- /**
669
- * A logging function for fatal messages
670
- *
671
- * @param message - The message to log.
672
- */
673
- fatal(message) {
674
- this.log(LogLevelLabel.FATAL, isString(message) ? message : StormJSON.stringify(message));
675
- }
676
- /**
677
- * A logging function for error messages
678
- *
679
- * @param message - The message to log.
680
- */
681
- error(message) {
682
- this.log(LogLevelLabel.ERROR, isString(message) ? message : StormJSON.stringify(message));
683
- }
684
- /**
685
- * A logging function for warning messages
686
- *
687
- * @param message - The message to log.
688
- */
689
- warn(message) {
690
- this.log(LogLevelLabel.WARN, isString(message) ? message : StormJSON.stringify(message));
691
- }
692
- /**
693
- * A logging function for informational messages
694
- *
695
- * @param message - The message to log.
696
- */
697
- info(message) {
698
- this.log(LogLevelLabel.INFO, isString(message) ? message : StormJSON.stringify(message));
699
- }
700
- /**
701
- * A logging function for debug messages
702
- *
703
- * @param message - The message to log.
704
- */
705
- debug(message) {
706
- this.log(LogLevelLabel.DEBUG, isString(message) ? message : StormJSON.stringify(message));
707
- }
708
- /**
709
- * A logging function for trace messages
710
- *
711
- * @param message - The message to log.
712
- */
713
- trace(message) {
714
- this.log(LogLevelLabel.TRACE, isString(message) ? message : StormJSON.stringify(message));
715
- }
716
- /**
717
- * Create a new logger instance
718
- *
719
- * @param name - The name to use for the logger instance
720
- * @returns A logger function
721
- */
722
- createLog(name = null) {
723
- return createLog(name, {
724
- ...this.config,
725
- logLevel: isNull(this.config.logLevel) ? "silent" : this.config.logLevel
726
- });
727
- }
728
- /**
729
- * Extend the current logger instance with a new name
730
- *
731
- * @param name - The name to use for the extended logger instance
732
- * @returns A logger function
733
- */
734
- extendLog(name) {
735
- return extendLog(this.log, name);
736
- }
737
- /**
738
- * Generates a checksum representing the current context state
739
- *
740
- * @param root - The root directory of the project to generate the checksum for
741
- * @returns A promise that resolves to a string representing the checksum
742
- */
743
- async generateChecksum(root = this.config.projectRoot) {
744
- this.#checksum = await hashDirectory(root, { ignore: [
745
- "node_modules",
746
- ".git",
747
- ".nx",
748
- ".cache",
749
- ".storm",
750
- "tmp",
751
- "dist"
752
- ] });
753
- return this.#checksum;
754
- }
755
- /**
756
- * Creates a new StormContext instance.
757
- *
758
- * @param workspaceConfig - The workspace configuration.
759
- */
760
- constructor(workspaceConfig) {
761
- this.#workspaceConfig = workspaceConfig;
762
- envPathCache.set({
763
- workspaceRoot: workspaceConfig.workspaceRoot,
764
- framework: "powerlines"
765
- }, getEnvPaths({
766
- orgId: (isSetObject(workspaceConfig.organization) ? workspaceConfig.organization.name : workspaceConfig.organization) || "storm-software",
767
- appId: "powerlines",
768
- workspaceRoot: workspaceConfig.workspaceRoot
769
- }));
770
- }
771
- /**
772
- * The resolved configuration for this context
773
- */
774
- resolvedConfig = {};
775
- /**
776
- * A logger function specific to this context
777
- */
778
- logFn;
779
- /**
780
- * Initialize the context with the provided configuration options
781
- *
782
- * @param config - The partial user configuration to use for initialization.
783
- */
784
- async init(config = {}, options = { isHighPriority: true }) {
785
- const cacheKey = {
786
- projectRoot: config.root ?? this.config.projectRoot ?? this.config.userConfig?.root ?? this.config.inlineConfig?.root,
787
- mode: (config.mode ?? this.config.mode) || this.workspaceConfig.mode,
788
- skipCache: config.skipCache ?? this.config.skipCache ?? false,
789
- configFile: config.configFile ?? this.config.configFile,
790
- framework: config.framework ?? this.config.framework ?? "powerlines",
791
- command: this.config.inlineConfig?.command
792
- };
793
- if (configCache.has(cacheKey)) {
794
- const result = configCache.get(cacheKey);
795
- this.projectJson = result.projectJson;
796
- this.packageJson = result.packageJson;
797
- this.#checksum = result.checksum;
798
- this.resolver = result.resolver;
799
- this.mergeUserConfig(result.userConfig.config, this.config.userConfig);
800
- } else {
801
- const projectJsonPath = joinPaths(cacheKey.projectRoot, "project.json");
802
- if (existsSync(projectJsonPath)) this.projectJson = await readJsonFile(projectJsonPath);
803
- const packageJsonPath = joinPaths(cacheKey.projectRoot, "package.json");
804
- if (existsSync(packageJsonPath)) this.packageJson = await readJsonFile(packageJsonPath);
805
- this.#checksum = await this.generateChecksum(cacheKey.projectRoot);
806
- this.resolver = createResolver({
807
- workspaceRoot: this.workspaceConfig.workspaceRoot,
808
- projectRoot: cacheKey.projectRoot,
809
- cacheDir: this.cachePath,
810
- mode: cacheKey.mode,
811
- logLevel: config.logLevel || this.config?.logLevel || this.workspaceConfig.logLevel || "info",
812
- skipCache: cacheKey.skipCache
813
- });
814
- const userConfig = await loadUserConfigFile(cacheKey.projectRoot, this.workspaceConfig.workspaceRoot, this.resolver, cacheKey.command, cacheKey.mode, cacheKey.configFile, cacheKey.framework);
815
- this.mergeUserConfig(userConfig.config);
816
- configCache.set(cacheKey, {
817
- projectJson: this.projectJson,
818
- packageJson: this.packageJson,
819
- checksum: this.#checksum,
820
- resolver: this.resolver,
821
- userConfig
822
- });
823
- }
824
- config.tsconfig ??= getTsconfigFilePath(this.workspaceConfig.workspaceRoot, cacheKey.projectRoot, config.tsconfig);
825
- if (isSetObject(config)) this.resolvedConfig = defu$1({
826
- inlineConfig: this.config.inlineConfig,
827
- userConfig: this.config.userConfig
828
- }, options.isHighPriority ? this.#getConfigProps(config) : {}, {
829
- command: this.config.inlineConfig?.command,
830
- ...this.#getConfigProps(this.config.inlineConfig)
831
- }, this.#getConfigProps(this.config.userConfig), {
832
- mode: this.workspaceConfig?.mode,
833
- logLevel: this.workspaceConfig?.logLevel,
834
- skipCache: this.workspaceConfig?.skipCache
835
- }, {
836
- name: this.projectJson?.name || this.packageJson?.name,
837
- version: this.packageJson?.version,
838
- description: this.packageJson?.description,
839
- sourceRoot: this.projectJson?.sourceRoot || appendPath("src", cacheKey.projectRoot),
840
- output: defu$1(config.output ?? {}, {
841
- outputPath: cacheKey.projectRoot ? joinPaths(this.workspaceConfig?.directories?.build || "dist", cacheKey.projectRoot) : this.workspaceConfig?.directories?.build || "dist",
842
- artifactsPath: joinPaths(cacheKey.projectRoot, `.${config.framework ?? "powerlines"}`),
843
- dts: joinPaths(cacheKey.projectRoot, `${config.framework ?? "powerlines"}.d.ts`),
844
- builtinPrefix: config.framework ?? "powerlines",
845
- assets: [
846
- { glob: "LICENSE" },
847
- {
848
- input: cacheKey.projectRoot,
849
- glob: "*.md"
850
- },
851
- {
852
- input: cacheKey.projectRoot,
853
- glob: "package.json"
854
- }
855
- ]
856
- })
857
- }, options.isHighPriority ? {} : this.#getConfigProps(config), {
858
- inlineConfig: {},
859
- userConfig: {},
860
- framework: "powerlines",
861
- platform: "neutral",
862
- mode: "production",
863
- projectType: "application",
864
- logLevel: "info",
865
- preview: false,
866
- environments: {},
867
- transform: { babel: {
868
- plugins: [],
869
- presets: []
870
- } },
871
- lint: { eslint: {} },
872
- build: {
873
- target: "esnext",
874
- override: {}
875
- }
876
- });
877
- this.config.entry = getUniqueEntries(this.config.entry);
878
- if (this.config.name?.startsWith("@") && this.config.name.split("/").filter(Boolean).length > 1) this.config.name = this.config.name.split("/").filter(Boolean)[1];
879
- this.config.title ??= titleCase(this.config.name);
880
- if (this.config.userConfig.build?.external) this.config.userConfig.build.external = getUnique(this.config.userConfig.build.external);
881
- if (this.config.userConfig.build?.noExternal) this.config.userConfig.build.noExternal = getUnique(this.config.userConfig.build.noExternal);
882
- if (this.config.build.external) this.config.build.external = getUnique(this.config.build.external);
883
- if (this.config.build.noExternal) this.config.build.noExternal = getUnique(this.config.build.noExternal);
884
- this.config.output.format = getUnique(toArray(this.config.output?.format ?? (this.config.projectType === "library" ? ["cjs", "esm"] : ["esm"])));
885
- if (this.config.projectRoot && this.config.projectRoot !== "." && this.config.projectRoot !== "./" && this.config.projectRoot !== this.workspaceConfig.workspaceRoot) {
886
- this.config.output.outputPath ??= joinPaths("dist", this.config.projectRoot);
887
- this.config.output.buildPath ??= joinPaths(this.config.projectRoot, "dist");
888
- } else {
889
- this.config.output.outputPath ??= "dist";
890
- this.config.output.buildPath ??= "dist";
891
- }
892
- this.config.output.assets = getUniqueBy(this.config.output.assets.map((asset) => {
893
- return {
894
- glob: isSetObject(asset) ? asset.glob : asset,
895
- input: isString(asset) || !asset.input || asset.input === "." || asset.input === "/" || asset.input === "./" ? this.workspaceConfig.workspaceRoot : isParentPath(asset.input, this.workspaceConfig.workspaceRoot) || asset.input === this.workspaceConfig.workspaceRoot ? asset.input : appendPath(asset.input, this.workspaceConfig.workspaceRoot),
896
- output: isSetObject(asset) && asset.output ? isParentPath(asset.output, this.workspaceConfig.workspaceRoot) ? asset.output : appendPath(joinPaths(this.config.output.outputPath, replacePath(replacePath(asset.output, replacePath(this.config.output.outputPath, this.workspaceConfig.workspaceRoot)), this.config.output.outputPath)), this.workspaceConfig.workspaceRoot) : appendPath(this.config.output.outputPath, this.workspaceConfig.workspaceRoot),
897
- ignore: isSetObject(asset) && asset.ignore ? toArray(asset.ignore) : void 0
898
- };
899
- }), (a) => `${a.input}-${a.glob}-${a.output}`);
900
- this.config.plugins = (this.config.plugins ?? []).filter(Boolean).reduce((ret, plugin) => {
901
- if (isPlugin(plugin) && checkDedupe(plugin, ret.filter((p) => isPlugin(p)))) return ret;
902
- ret.push(plugin);
903
- return ret;
904
- }, []);
905
- if (this.config.tsconfig) this.config.tsconfig = replacePathTokens(this, this.config.tsconfig);
906
- if (this.config.output.dts) this.config.output.dts = replacePathTokens(this, this.config.output.dts);
907
- if (this.config.build.polyfill) this.config.build.polyfill = this.config.build.polyfill.map((polyfill) => replacePathTokens(this, polyfill));
908
- if (this.config.output.assets) this.config.output.assets = this.config.output.assets.map((asset) => ({
909
- ...asset,
910
- glob: replacePathTokens(this, asset.glob),
911
- ignore: asset.ignore ? asset.ignore.map((ignore) => replacePathTokens(this, ignore)) : void 0,
912
- input: replacePathTokens(this, asset.input),
913
- output: replacePathTokens(this, asset.output)
914
- }));
915
- this.#fs ??= await VirtualFileSystem.create(this);
916
- }
917
- mergeUserConfig(from = {}, into = this.config.userConfig ?? {}) {
918
- this.config.userConfig = defu$1({ entry: Array.isArray(from.entry) && from.entry.length > 0 ? from.entry : Array.isArray(into?.entry) && into.entry.length > 0 ? into.entry : [] }, omit(from ?? {}, ["entry"]), omit(into ?? {}, ["entry"]));
919
- if (this.config.userConfig.output?.format) this.config.userConfig.output.format = getUnique(toArray(this.config.userConfig.output?.format));
920
- this.config.userConfig.plugins = (this.config.userConfig.plugins ?? []).filter(Boolean).reduce((ret, plugin) => {
921
- if (isPlugin(plugin) && checkDedupe(plugin, ret.filter((p) => isPlugin(p)))) return ret;
922
- ret.push(plugin);
923
- return ret;
924
- }, []);
925
- }
926
- };
927
-
928
- //#endregion
929
- export { PowerlinesContext };