@rslib/core 0.0.0-next-20240927040621

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 (36) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +19 -0
  3. package/bin/rslib.js +14 -0
  4. package/compiled/commander/index.d.ts +971 -0
  5. package/compiled/commander/index.js +3805 -0
  6. package/compiled/commander/license +22 -0
  7. package/compiled/commander/package.json +1 -0
  8. package/compiled/fast-glob/index.d.ts +237 -0
  9. package/compiled/fast-glob/index.js +7309 -0
  10. package/compiled/fast-glob/license +21 -0
  11. package/compiled/fast-glob/package.json +1 -0
  12. package/compiled/picocolors/index.d.ts +56 -0
  13. package/compiled/picocolors/index.js +150 -0
  14. package/compiled/picocolors/license +15 -0
  15. package/compiled/picocolors/package.json +1 -0
  16. package/compiled/rslog/index.d.ts +66 -0
  17. package/compiled/rslog/index.js +334 -0
  18. package/compiled/rslog/license +21 -0
  19. package/compiled/rslog/package.json +1 -0
  20. package/dist/index.js +1894 -0
  21. package/dist-types/build.d.ts +4 -0
  22. package/dist-types/cli/commands.d.ts +14 -0
  23. package/dist-types/cli/prepare.d.ts +1 -0
  24. package/dist-types/config.d.ts +30 -0
  25. package/dist-types/constant.d.ts +8 -0
  26. package/dist-types/index.d.ts +7 -0
  27. package/dist-types/plugins/cjsShim.d.ts +2 -0
  28. package/dist-types/types/config/index.d.ts +45 -0
  29. package/dist-types/types/index.d.ts +2 -0
  30. package/dist-types/types/utils.d.ts +6 -0
  31. package/dist-types/utils/extension.d.ts +10 -0
  32. package/dist-types/utils/helper.d.ts +14 -0
  33. package/dist-types/utils/logger.d.ts +5 -0
  34. package/dist-types/utils/syntax.d.ts +13 -0
  35. package/dist-types/utils/tsconfig.d.ts +3 -0
  36. package/package.json +77 -0
package/dist/index.js ADDED
@@ -0,0 +1,1894 @@
1
+ import * as __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__ from "../compiled/rslog/index.js";
2
+ import * as __WEBPACK_EXTERNAL_MODULE_node_fs__ from "node:fs";
3
+ import * as __WEBPACK_EXTERNAL_MODULE_node_fs_promises__ from "node:fs/promises";
4
+ import * as __WEBPACK_EXTERNAL_MODULE_node_path__ from "node:path";
5
+ import * as __WEBPACK_EXTERNAL_MODULE__compiled_picocolors_index_js__ from "../compiled/picocolors/index.js";
6
+ import * as __WEBPACK_EXTERNAL_MODULE__compiled_commander_index_js__ from "../compiled/commander/index.js";
7
+ import * as __WEBPACK_EXTERNAL_MODULE__rsbuild_core__ from "@rsbuild/core";
8
+ import * as __WEBPACK_EXTERNAL_MODULE__compiled_fast_glob_index_js__ from "../compiled/fast-glob/index.js";
9
+ import * as __WEBPACK_EXTERNAL_MODULE_module__ from "module";
10
+ /**
11
+ * Node.js built-in modules.
12
+ * Copied from https://github.com/webpack/webpack/blob/dd44b206a9c50f4b4cb4d134e1a0bd0387b159a3/lib/node/NodeTargetPlugin.js#L12-L72
13
+ */ const nodeBuiltInModules = [
14
+ 'assert',
15
+ 'assert/strict',
16
+ 'async_hooks',
17
+ 'buffer',
18
+ 'child_process',
19
+ 'cluster',
20
+ 'console',
21
+ 'constants',
22
+ 'crypto',
23
+ 'dgram',
24
+ 'diagnostics_channel',
25
+ 'dns',
26
+ 'dns/promises',
27
+ 'domain',
28
+ 'events',
29
+ 'fs',
30
+ 'fs/promises',
31
+ 'http',
32
+ 'http2',
33
+ 'https',
34
+ 'inspector',
35
+ 'inspector/promises',
36
+ 'module',
37
+ 'net',
38
+ 'os',
39
+ 'path',
40
+ 'path/posix',
41
+ 'path/win32',
42
+ 'perf_hooks',
43
+ 'process',
44
+ 'punycode',
45
+ 'querystring',
46
+ 'readline',
47
+ 'readline/promises',
48
+ 'repl',
49
+ 'stream',
50
+ 'stream/consumers',
51
+ 'stream/promises',
52
+ 'stream/web',
53
+ 'string_decoder',
54
+ 'sys',
55
+ 'timers',
56
+ 'timers/promises',
57
+ 'tls',
58
+ 'trace_events',
59
+ 'tty',
60
+ 'url',
61
+ 'util',
62
+ 'util/types',
63
+ 'v8',
64
+ 'vm',
65
+ 'wasi',
66
+ 'worker_threads',
67
+ 'zlib',
68
+ /^node:/,
69
+ // cspell:word pnpapi
70
+ // Yarn PnP adds pnpapi as "builtin"
71
+ 'pnpapi'
72
+ ];
73
+ async function calcLongestCommonPath(absPaths) {
74
+ if (0 === absPaths.length) return null;
75
+ // we support two cases
76
+ // 1. /packages-a/src/index.ts
77
+ // 2. D:/packages-a/src/index.ts
78
+ const sep = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].posix.sep;
79
+ const splitPaths = absPaths.map((p)=>p.split(sep));
80
+ let lcaFragments = splitPaths[0];
81
+ for(let i = 1; i < splitPaths.length; i++){
82
+ const currentPath = splitPaths[i];
83
+ const minLength = Math.min(lcaFragments.length, currentPath.length);
84
+ let j = 0;
85
+ while(j < minLength && lcaFragments[j] === currentPath[j])j++;
86
+ lcaFragments = lcaFragments.slice(0, j);
87
+ }
88
+ let lca = lcaFragments.length > 0 ? lcaFragments.join(sep) : sep;
89
+ const stats = await __WEBPACK_EXTERNAL_MODULE_node_fs_promises__["default"].stat(lca);
90
+ if (stats?.isFile()) lca = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(lca);
91
+ return lca;
92
+ }
93
+ const readPackageJson = (rootPath)=>{
94
+ const pkgJsonPath = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(rootPath, './package.json');
95
+ if (!__WEBPACK_EXTERNAL_MODULE_node_fs__["default"].existsSync(pkgJsonPath)) {
96
+ __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.warn(`package.json does not exist in the ${rootPath} directory`);
97
+ return;
98
+ }
99
+ try {
100
+ return JSON.parse(__WEBPACK_EXTERNAL_MODULE_node_fs__["default"].readFileSync(pkgJsonPath, 'utf8'));
101
+ } catch (err) {
102
+ __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.warn(`Failed to parse ${pkgJsonPath}, it might not be valid JSON`);
103
+ return;
104
+ }
105
+ };
106
+ const isObject = (obj)=>'[object Object]' === Object.prototype.toString.call(obj);
107
+ const isEmptyObject = (obj)=>0 === Object.keys(obj).length;
108
+ function pick(obj, keys) {
109
+ return keys.reduce((ret, key)=>{
110
+ if (void 0 !== obj[key]) ret[key] = obj[key];
111
+ return ret;
112
+ }, {});
113
+ }
114
+ function omit(obj, keys) {
115
+ return Object.keys(obj).reduce((ret, key)=>{
116
+ if (!keys.includes(key)) ret[key] = obj[key];
117
+ return ret;
118
+ }, {});
119
+ }
120
+ // setup the logger level
121
+ if (process.env.DEBUG) __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.level = 'verbose';
122
+ function initNodeEnv() {
123
+ if (!process.env.NODE_ENV) {
124
+ const command = process.argv[2] ?? '';
125
+ process.env.NODE_ENV = [
126
+ 'build'
127
+ ].includes(command) ? 'production' : 'development';
128
+ }
129
+ }
130
+ function prepareCli() {
131
+ initNodeEnv();
132
+ // Print a blank line to keep the greet log nice.
133
+ // Some package managers automatically output a blank line, some do not.
134
+ const { npm_execpath } = process.env;
135
+ if (!npm_execpath || npm_execpath.includes('npx-cli.js') || npm_execpath.includes('.bun')) console.log();
136
+ __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.greet(` Rslib v0.0.7\n`);
137
+ }
138
+ const DEFAULT_CONFIG_NAME = 'rslib.config';
139
+ const DEFAULT_CONFIG_EXTENSIONS = [
140
+ '.js',
141
+ '.ts',
142
+ '.mjs',
143
+ '.mts',
144
+ '.cjs',
145
+ '.cts'
146
+ ];
147
+ const SWC_HELPERS = '@swc/helpers';
148
+ const JS_EXTENSIONS = [
149
+ 'js',
150
+ 'mjs',
151
+ 'jsx',
152
+ 'ts',
153
+ 'mts',
154
+ 'tsx',
155
+ 'cjs',
156
+ 'cjsx',
157
+ 'mjsx',
158
+ 'mtsx',
159
+ 'cts',
160
+ 'ctsx'
161
+ ];
162
+ const CSS_EXTENSIONS = [
163
+ 'css',
164
+ 'sass',
165
+ 'scss',
166
+ 'less'
167
+ ];
168
+ const ENTRY_EXTENSIONS = [
169
+ ...JS_EXTENSIONS,
170
+ ...CSS_EXTENSIONS
171
+ ];
172
+ const JS_EXTENSIONS_PATTERN = new RegExp(`\\.(${JS_EXTENSIONS.join('|')})$`);
173
+ const ENTRY_EXTENSIONS_PATTERN = new RegExp(`\\.(${ENTRY_EXTENSIONS.join('|')})$`);
174
+ const importMetaUrlShim = `var __rslib_import_meta_url__ = /*#__PURE__*/ (function () {
175
+ return typeof document === 'undefined'
176
+ ? new (require('url'.replace('', '')).URL)('file:' + __filename).href
177
+ : (document.currentScript && document.currentScript.src) ||
178
+ new URL('main.js', document.baseURI).href;
179
+ })();
180
+ `;
181
+ // This Rsbuild plugin will shim `import.meta.url` for CommonJS modules.
182
+ // - Replace `import.meta.url` with `importMetaUrl`.
183
+ // - Inject `importMetaUrl` to the end of the module (can't inject at the beginning because of `"use strict";`).
184
+ // This is a short-term solution, and we hope to provide built-in polyfills like `node.__filename` on Rspack side.
185
+ const pluginCjsShim = ()=>({
186
+ name: 'rsbuild-plugin-cjs-shim',
187
+ setup (api) {
188
+ api.modifyEnvironmentConfig((config)=>{
189
+ config.source.define = {
190
+ ...config.source.define,
191
+ 'import.meta.url': '__rslib_import_meta_url__'
192
+ };
193
+ });
194
+ api.modifyRspackConfig((config)=>{
195
+ config.plugins ??= [];
196
+ config.plugins.push(new __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.rspack.BannerPlugin({
197
+ banner: importMetaUrlShim,
198
+ // Just before minify stage, to perform tree shaking.
199
+ stage: __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE - 1,
200
+ raw: true,
201
+ footer: true,
202
+ include: /\.(js|cjs)$/
203
+ }));
204
+ });
205
+ }
206
+ });
207
+ const getDefaultExtension = (options)=>{
208
+ const { format, pkgJson, autoExtension } = options;
209
+ let jsExtension = '.js';
210
+ let dtsExtension = '.d.ts';
211
+ if (!autoExtension) return {
212
+ jsExtension,
213
+ dtsExtension
214
+ };
215
+ if (!pkgJson) {
216
+ __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.warn('autoExtension configuration will not be applied due to read package.json failed');
217
+ return {
218
+ jsExtension,
219
+ dtsExtension
220
+ };
221
+ }
222
+ const isModule = 'module' === pkgJson.type;
223
+ if (isModule && 'cjs' === format) {
224
+ jsExtension = '.cjs';
225
+ dtsExtension = '.d.cts';
226
+ }
227
+ if (!isModule && 'esm' === format) {
228
+ jsExtension = '.mjs';
229
+ dtsExtension = '.d.mts';
230
+ }
231
+ return {
232
+ jsExtension,
233
+ dtsExtension,
234
+ isModule
235
+ };
236
+ };
237
+ const LATEST_TARGET_VERSIONS = {
238
+ node: [
239
+ 'last 1 node versions'
240
+ ],
241
+ web: [
242
+ 'last 1 Chrome versions',
243
+ 'last 1 Firefox versions',
244
+ 'last 1 Edge versions',
245
+ 'last 1 Safari versions',
246
+ 'last 1 ios_saf versions',
247
+ 'not dead'
248
+ ],
249
+ get 'web-worker' () {
250
+ return LATEST_TARGET_VERSIONS.web;
251
+ }
252
+ };
253
+ const calcEsnextBrowserslistByTarget = (target)=>{
254
+ if (!target) return [
255
+ ...LATEST_TARGET_VERSIONS.node,
256
+ ...LATEST_TARGET_VERSIONS.web
257
+ ];
258
+ if ('node' === target) return LATEST_TARGET_VERSIONS.node;
259
+ return LATEST_TARGET_VERSIONS.web;
260
+ };
261
+ const RSPACK_TARGET_UNLISTED_MODERN_ECMA_VERSIONS = [
262
+ 'es2023',
263
+ 'es2024',
264
+ 'esnext'
265
+ ];
266
+ /**
267
+ * The esX to browserslist mapping is transformed from esbuild:
268
+ * https://github.com/evanw/esbuild/blob/main/internal/compat/js_table.go
269
+ * It does not completely align with the browserslist query of Rsbuild now:
270
+ * https://github.com/rspack-contrib/browserslist-to-es-version
271
+ * TODO: align with Rsbuild, we may should align with SWC
272
+ */ const ESX_TO_BROWSERSLIST = {
273
+ es6: {
274
+ Chrome: '63.0.0',
275
+ Edge: '79.0.0',
276
+ Firefox: '67.0.0',
277
+ iOS: '13.0.0',
278
+ Node: [
279
+ 'node > 12.20.0 and node < 13.0.0',
280
+ 'node > 13.2.0'
281
+ ],
282
+ Opera: '50.0.0',
283
+ Safari: '13.0.0'
284
+ },
285
+ es2015: {
286
+ Chrome: '63.0.0',
287
+ Edge: '79.0.0',
288
+ Firefox: '67.0.0',
289
+ iOS: '13.0.0',
290
+ Node: '10.0.0',
291
+ Opera: '50.0.0',
292
+ Safari: '13.0.0'
293
+ },
294
+ es2016: {
295
+ Chrome: '52.0.0',
296
+ Edge: '14.0.0',
297
+ Firefox: '52.0.0',
298
+ iOS: '10.3.0',
299
+ Node: '7.0.0',
300
+ Opera: '39.0.0',
301
+ Safari: '10.1.0'
302
+ },
303
+ es2017: {
304
+ Chrome: '55.0.0',
305
+ Edge: '15.0.0',
306
+ Firefox: '52.0.0',
307
+ iOS: '11.0.0',
308
+ Node: '7.6.0',
309
+ Opera: '42.0.0',
310
+ Safari: '11.0.0'
311
+ },
312
+ es2018: {
313
+ Chrome: '64.0.0',
314
+ Edge: '79.0.0',
315
+ Firefox: '78.0.0',
316
+ iOS: '16.4.0',
317
+ Node: [
318
+ 'node > 18.20.0 and node < 19.0.0',
319
+ 'node > 20.12.0 and node < 21.0.0',
320
+ 'node > 21.3.0'
321
+ ],
322
+ Opera: '51.0.0',
323
+ Safari: '16.4.0'
324
+ },
325
+ es2019: {
326
+ Chrome: '66.0.0',
327
+ Edge: '79.0.0',
328
+ Firefox: '58.0.0',
329
+ iOS: '11.3.0',
330
+ Node: '10.0.0',
331
+ Opera: '53.0.0',
332
+ Safari: '11.1.0'
333
+ },
334
+ es2020: {
335
+ Chrome: '91.0.0',
336
+ Edge: '91.0.0',
337
+ Firefox: '80.0.0',
338
+ iOS: '14.5.0',
339
+ Node: '16.1.0',
340
+ Opera: '77.0.0',
341
+ Safari: '14.1.0'
342
+ },
343
+ es2021: {
344
+ Chrome: '85.0.0',
345
+ Edge: '85.0.0',
346
+ Firefox: '79.0.0',
347
+ iOS: '14.0.0',
348
+ Node: '15.0.0',
349
+ Opera: '71.0.0',
350
+ Safari: '14.0.0'
351
+ },
352
+ es2022: {
353
+ Chrome: '91.0.0',
354
+ Edge: '94.0.0',
355
+ Firefox: '93.0.0',
356
+ iOS: '16.4.0',
357
+ Node: '16.11.0',
358
+ Opera: '80.0.0',
359
+ Safari: '16.4.0'
360
+ },
361
+ es2023: {
362
+ Chrome: '74.0.0',
363
+ Edge: '79.0.0',
364
+ Firefox: '67.0.0',
365
+ iOS: '13.4.0',
366
+ Node: '12.5.0',
367
+ Opera: '62.0.0',
368
+ Safari: '13.1.0'
369
+ },
370
+ es2024: calcEsnextBrowserslistByTarget,
371
+ esnext: calcEsnextBrowserslistByTarget,
372
+ es5: {
373
+ Chrome: '5.0.0',
374
+ Edge: '12.0.0',
375
+ Firefox: '2.0.0',
376
+ ie: '9.0.0',
377
+ iOS: '6.0.0',
378
+ Node: '0.4.0',
379
+ Opera: '10.10.0',
380
+ Safari: '3.1.0'
381
+ }
382
+ };
383
+ function transformSyntaxToRspackTarget(syntax) {
384
+ const handleSyntaxItem = (syntaxItem)=>{
385
+ const normalizedSyntaxItem = syntaxItem.toLowerCase();
386
+ if (normalizedSyntaxItem.startsWith('es')) {
387
+ if (normalizedSyntaxItem in ESX_TO_BROWSERSLIST) {
388
+ // The latest EcmaScript version supported by Rspack's `target` is es2022.
389
+ // Higher versions are treated as es2022.
390
+ if (RSPACK_TARGET_UNLISTED_MODERN_ECMA_VERSIONS.includes(normalizedSyntaxItem)) return 'es2022';
391
+ return normalizedSyntaxItem;
392
+ }
393
+ throw new Error(`Unsupported ES version: ${syntaxItem}`);
394
+ }
395
+ return `browserslist:${syntaxItem}`;
396
+ };
397
+ if (Array.isArray(syntax)) return syntax.map(handleSyntaxItem);
398
+ return [
399
+ handleSyntaxItem(syntax)
400
+ ];
401
+ }
402
+ function transformSyntaxToBrowserslist(syntax, target) {
403
+ const handleSyntaxItem = (syntaxItem)=>{
404
+ const normalizedSyntaxItem = syntaxItem.toLowerCase();
405
+ if (normalizedSyntaxItem.startsWith('es')) {
406
+ if (normalizedSyntaxItem in ESX_TO_BROWSERSLIST) {
407
+ const browserslistItem = ESX_TO_BROWSERSLIST[normalizedSyntaxItem];
408
+ if ('function' == typeof browserslistItem) return browserslistItem(target);
409
+ return Object.entries(browserslistItem).flatMap(([engine, version])=>{
410
+ if (Array.isArray(version)) return version;
411
+ return `${engine} >= ${version}`;
412
+ });
413
+ }
414
+ throw new Error(`Unsupported ES version: ${syntaxItem}`);
415
+ }
416
+ return [
417
+ syntaxItem
418
+ ];
419
+ };
420
+ if (Array.isArray(syntax)) return syntax.flatMap(handleSyntaxItem);
421
+ return handleSyntaxItem(syntax);
422
+ }
423
+ const POSIX_SEP_RE = new RegExp('\\' + __WEBPACK_EXTERNAL_MODULE_node_path__["default"].posix.sep, 'g');
424
+ const NATIVE_SEP_RE = new RegExp('\\' + __WEBPACK_EXTERNAL_MODULE_node_path__["default"].sep, 'g');
425
+ /** @type {Map<string,RegExp>}*/ const PATTERN_REGEX_CACHE = new Map();
426
+ const GLOB_ALL_PATTERN = "**/*";
427
+ const TS_EXTENSIONS = [
428
+ '.ts',
429
+ '.tsx',
430
+ '.mts',
431
+ '.cts'
432
+ ];
433
+ const util_JS_EXTENSIONS = [
434
+ '.js',
435
+ '.jsx',
436
+ '.mjs',
437
+ '.cjs'
438
+ ];
439
+ const TSJS_EXTENSIONS = TS_EXTENSIONS.concat(util_JS_EXTENSIONS);
440
+ const TS_EXTENSIONS_RE_GROUP = `\\.(?:${TS_EXTENSIONS.map((ext)=>ext.substring(1)).join('|')})`;
441
+ const TSJS_EXTENSIONS_RE_GROUP = `\\.(?:${TSJS_EXTENSIONS.map((ext)=>ext.substring(1)).join('|')})`;
442
+ const IS_POSIX = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].posix.sep === __WEBPACK_EXTERNAL_MODULE_node_path__["default"].sep;
443
+ /**
444
+ * @template T
445
+ * @returns {{resolve:(result:T)=>void, reject:(error:any)=>void, promise: Promise<T>}}
446
+ */ function util_makePromise() {
447
+ let resolve, reject;
448
+ const promise = new Promise((res, rej)=>{
449
+ resolve = res;
450
+ reject = rej;
451
+ });
452
+ return {
453
+ promise,
454
+ resolve,
455
+ reject
456
+ };
457
+ }
458
+ /**
459
+ * @param {string} filename
460
+ * @param {import('./cache.js').TSConfckCache} [cache]
461
+ * @returns {Promise<string|void>}
462
+ */ async function util_resolveTSConfigJson(filename, cache) {
463
+ if ('.json' !== __WEBPACK_EXTERNAL_MODULE_node_path__["default"].extname(filename)) return; // ignore files that are not json
464
+ const tsconfig = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].resolve(filename);
465
+ if (cache && (cache.hasParseResult(tsconfig) || cache.hasParseResult(filename))) return tsconfig;
466
+ return __WEBPACK_EXTERNAL_MODULE_node_fs__.promises.stat(tsconfig).then((stat)=>{
467
+ if (stat.isFile() || stat.isFIFO()) return tsconfig;
468
+ throw new Error(`${filename} exists but is not a regular file.`);
469
+ });
470
+ }
471
+ /**
472
+ *
473
+ * @param {string} dir an absolute directory path
474
+ * @returns {boolean} if dir path includes a node_modules segment
475
+ */ const util_isInNodeModules = IS_POSIX ? (dir)=>dir.includes('/node_modules/') : (dir)=>dir.match(/[/\\]node_modules[/\\]/);
476
+ /**
477
+ * convert posix separator to native separator
478
+ *
479
+ * eg.
480
+ * windows: C:/foo/bar -> c:\foo\bar
481
+ * linux: /foo/bar -> /foo/bar
482
+ *
483
+ * @param {string} filename with posix separators
484
+ * @returns {string} filename with native separators
485
+ */ const posix2native = IS_POSIX ? (filename)=>filename : (filename)=>filename.replace(POSIX_SEP_RE, __WEBPACK_EXTERNAL_MODULE_node_path__["default"].sep);
486
+ /**
487
+ * convert native separator to posix separator
488
+ *
489
+ * eg.
490
+ * windows: C:\foo\bar -> c:/foo/bar
491
+ * linux: /foo/bar -> /foo/bar
492
+ *
493
+ * @param {string} filename - filename with native separators
494
+ * @returns {string} filename with posix separators
495
+ */ const util_native2posix = IS_POSIX ? (filename)=>filename : (filename)=>filename.replace(NATIVE_SEP_RE, __WEBPACK_EXTERNAL_MODULE_node_path__["default"].posix.sep);
496
+ /**
497
+ * converts params to native separator, resolves path and converts native back to posix
498
+ *
499
+ * needed on windows to handle posix paths in tsconfig
500
+ *
501
+ * @param dir {string|null} directory to resolve from
502
+ * @param filename {string} filename or pattern to resolve
503
+ * @returns string
504
+ */ const resolve2posix = IS_POSIX ? (dir, filename)=>dir ? __WEBPACK_EXTERNAL_MODULE_node_path__["default"].resolve(dir, filename) : __WEBPACK_EXTERNAL_MODULE_node_path__["default"].resolve(filename) : (dir, filename)=>util_native2posix(dir ? __WEBPACK_EXTERNAL_MODULE_node_path__["default"].resolve(posix2native(dir), posix2native(filename)) : __WEBPACK_EXTERNAL_MODULE_node_path__["default"].resolve(posix2native(filename)));
505
+ /**
506
+ *
507
+ * @param {import('./public.d.ts').TSConfckParseResult} result
508
+ * @param {import('./public.d.ts').TSConfckParseOptions} [options]
509
+ * @returns {string[]}
510
+ */ function util_resolveReferencedTSConfigFiles(result, options) {
511
+ const dir = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(result.tsconfigFile);
512
+ return result.tsconfig.references.map((ref)=>{
513
+ const refPath = ref.path.endsWith('.json') ? ref.path : __WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(ref.path, options?.configName ?? 'tsconfig.json');
514
+ return resolve2posix(dir, refPath);
515
+ });
516
+ }
517
+ /**
518
+ * @param {string} filename
519
+ * @param {import('./public.d.ts').TSConfckParseResult} result
520
+ * @returns {import('./public.d.ts').TSConfckParseResult}
521
+ */ function util_resolveSolutionTSConfig(filename, result) {
522
+ const allowJs = result.tsconfig.compilerOptions?.allowJs;
523
+ const extensions = allowJs ? TSJS_EXTENSIONS : TS_EXTENSIONS;
524
+ if (result.referenced && extensions.some((ext)=>filename.endsWith(ext)) && !util_isIncluded(filename, result)) {
525
+ const solutionTSConfig = result.referenced.find((referenced)=>util_isIncluded(filename, referenced));
526
+ if (solutionTSConfig) return solutionTSConfig;
527
+ }
528
+ return result;
529
+ }
530
+ /**
531
+ *
532
+ * @param {string} filename
533
+ * @param {import('./public.d.ts').TSConfckParseResult} result
534
+ * @returns {boolean}
535
+ */ function util_isIncluded(filename, result) {
536
+ const dir = util_native2posix(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(result.tsconfigFile));
537
+ const files = (result.tsconfig.files || []).map((file)=>resolve2posix(dir, file));
538
+ const absoluteFilename = resolve2posix(null, filename);
539
+ if (files.includes(filename)) return true;
540
+ const allowJs = result.tsconfig.compilerOptions?.allowJs;
541
+ const isIncluded = isGlobMatch(absoluteFilename, dir, result.tsconfig.include || (result.tsconfig.files ? [] : [
542
+ GLOB_ALL_PATTERN
543
+ ]), allowJs);
544
+ if (isIncluded) {
545
+ const isExcluded = isGlobMatch(absoluteFilename, dir, result.tsconfig.exclude || [], allowJs);
546
+ return !isExcluded;
547
+ }
548
+ return false;
549
+ }
550
+ /**
551
+ * test filenames agains glob patterns in tsconfig
552
+ *
553
+ * @param filename {string} posix style abolute path to filename to test
554
+ * @param dir {string} posix style absolute path to directory of tsconfig containing patterns
555
+ * @param patterns {string[]} glob patterns to match against
556
+ * @param allowJs {boolean} allowJs setting in tsconfig to include js extensions in checks
557
+ * @returns {boolean} true when at least one pattern matches filename
558
+ */ function isGlobMatch(filename, dir, patterns, allowJs) {
559
+ const extensions = allowJs ? TSJS_EXTENSIONS : TS_EXTENSIONS;
560
+ return patterns.some((pattern)=>{
561
+ // filename must end with part of pattern that comes after last wildcard
562
+ let lastWildcardIndex = pattern.length;
563
+ let hasWildcard = false;
564
+ for(let i = pattern.length - 1; i > -1; i--)if ('*' === pattern[i] || '?' === pattern[i]) {
565
+ lastWildcardIndex = i;
566
+ hasWildcard = true;
567
+ break;
568
+ }
569
+ // if pattern does not end with wildcard, filename must end with pattern after last wildcard
570
+ if (lastWildcardIndex < pattern.length - 1 && !filename.endsWith(pattern.slice(lastWildcardIndex + 1))) return false;
571
+ // if pattern ends with *, filename must end with a default extension
572
+ if (pattern.endsWith('*') && !extensions.some((ext)=>filename.endsWith(ext))) return false;
573
+ // for **/* , filename must start with the dir
574
+ if (pattern === GLOB_ALL_PATTERN) return filename.startsWith(`${dir}/`);
575
+ const resolvedPattern = resolve2posix(dir, pattern);
576
+ // filename must start with part of pattern that comes before first wildcard
577
+ let firstWildcardIndex = -1;
578
+ for(let i = 0; i < resolvedPattern.length; i++)if ('*' === resolvedPattern[i] || '?' === resolvedPattern[i]) {
579
+ firstWildcardIndex = i;
580
+ hasWildcard = true;
581
+ break;
582
+ }
583
+ if (firstWildcardIndex > 1 && !filename.startsWith(resolvedPattern.slice(0, firstWildcardIndex - 1))) return false;
584
+ // if no wildcard in pattern, filename must be equal to resolved pattern
585
+ if (!hasWildcard) return filename === resolvedPattern;
586
+ // complex pattern, use regex to check it
587
+ if (PATTERN_REGEX_CACHE.has(resolvedPattern)) return PATTERN_REGEX_CACHE.get(resolvedPattern).test(filename);
588
+ const regex = pattern2regex(resolvedPattern, allowJs);
589
+ PATTERN_REGEX_CACHE.set(resolvedPattern, regex);
590
+ return regex.test(filename);
591
+ });
592
+ }
593
+ /**
594
+ * @param {string} resolvedPattern
595
+ * @param {boolean} allowJs
596
+ * @returns {RegExp}
597
+ */ function pattern2regex(resolvedPattern, allowJs) {
598
+ let regexStr = '^';
599
+ for(let i = 0; i < resolvedPattern.length; i++){
600
+ const char = resolvedPattern[i];
601
+ if ('?' === char) {
602
+ regexStr += '[^\\/]';
603
+ continue;
604
+ }
605
+ if ('*' === char) {
606
+ if ('*' === resolvedPattern[i + 1] && '/' === resolvedPattern[i + 2]) {
607
+ i += 2;
608
+ regexStr += '(?:[^\\/]*\\/)*'; // zero or more path segments
609
+ continue;
610
+ }
611
+ regexStr += '[^\\/]*';
612
+ continue;
613
+ }
614
+ if ('/.+^${}()|[]\\'.includes(char)) regexStr += "\\";
615
+ regexStr += char;
616
+ }
617
+ // add known file endings if pattern ends on *
618
+ if (resolvedPattern.endsWith('*')) regexStr += allowJs ? TSJS_EXTENSIONS_RE_GROUP : TS_EXTENSIONS_RE_GROUP;
619
+ regexStr += '$';
620
+ return new RegExp(regexStr);
621
+ }
622
+ /**
623
+ * replace tokens like ${configDir}
624
+ * @param {any} tsconfig
625
+ * @param {string} configDir
626
+ * @returns {any}
627
+ */ function util_replaceTokens(tsconfig, configDir) {
628
+ return JSON.parse(JSON.stringify(tsconfig)// replace ${configDir}
629
+ .replaceAll(/"\${configDir}/g, `"${util_native2posix(configDir)}`));
630
+ }
631
+ /**
632
+ * find the closest tsconfig.json file
633
+ *
634
+ * @param {string} filename - path to file to find tsconfig for (absolute or relative to cwd)
635
+ * @param {import('./public.d.ts').TSConfckFindOptions} [options] - options
636
+ * @returns {Promise<string|null>} absolute path to closest tsconfig.json or null if not found
637
+ */ async function find(filename, options) {
638
+ let dir = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].resolve(filename));
639
+ if (options?.ignoreNodeModules && util_isInNodeModules(dir)) return null;
640
+ const cache = options?.cache;
641
+ const configName = options?.configName ?? 'tsconfig.json';
642
+ if (cache?.hasConfigPath(dir, configName)) return cache.getConfigPath(dir, configName);
643
+ const { promise, resolve, reject } = util_makePromise();
644
+ if (options?.root && !__WEBPACK_EXTERNAL_MODULE_node_path__["default"].isAbsolute(options.root)) options.root = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].resolve(options.root);
645
+ findUp(dir, {
646
+ promise,
647
+ resolve,
648
+ reject
649
+ }, options);
650
+ return promise;
651
+ }
652
+ /**
653
+ *
654
+ * @param {string} dir
655
+ * @param {{promise:Promise<string|null>,resolve:(result:string|null)=>void,reject:(err:any)=>void}} madePromise
656
+ * @param {import('./public.d.ts').TSConfckFindOptions} [options] - options
657
+ */ function findUp(dir, { resolve, reject, promise }, options) {
658
+ const { cache, root, configName } = options ?? {};
659
+ if (cache) {
660
+ if (cache.hasConfigPath(dir, configName)) {
661
+ let cached;
662
+ try {
663
+ cached = cache.getConfigPath(dir, configName);
664
+ } catch (e) {
665
+ reject(e);
666
+ return;
667
+ }
668
+ if (cached?.then) cached.then(resolve).catch(reject);
669
+ else resolve(cached);
670
+ } else cache.setConfigPath(dir, promise, configName);
671
+ }
672
+ const tsconfig = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(dir, options?.configName ?? 'tsconfig.json');
673
+ __WEBPACK_EXTERNAL_MODULE_node_fs__["default"].stat(tsconfig, (err, stats)=>{
674
+ if (stats && (stats.isFile() || stats.isFIFO())) resolve(tsconfig);
675
+ else if (err?.code !== 'ENOENT') reject(err);
676
+ else {
677
+ let parent;
678
+ if (root === dir || (parent = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(dir)) === dir) resolve(null);
679
+ else findUp(parent, {
680
+ promise,
681
+ resolve,
682
+ reject
683
+ }, options);
684
+ }
685
+ });
686
+ }
687
+ /**
688
+ * @typedef WalkState
689
+ * @interface
690
+ * @property {string[]} files - files
691
+ * @property {number} calls - number of ongoing calls
692
+ * @property {(dir: string)=>boolean} skip - function to skip dirs
693
+ * @property {boolean} err - error flag
694
+ * @property {string[]} configNames - config file names
695
+ */ __WEBPACK_EXTERNAL_MODULE_node_path__["default"].sep;
696
+ /*
697
+ this file contains code from strip-bom and strip-json-comments by Sindre Sorhus
698
+ https://github.com/sindresorhus/strip-json-comments/blob/v4.0.0/index.js
699
+ https://github.com/sindresorhus/strip-bom/blob/v5.0.0/index.js
700
+ licensed under MIT, see ../LICENSE
701
+ */ /**
702
+ * convert content of tsconfig.json to regular json
703
+ *
704
+ * @param {string} tsconfigJson - content of tsconfig.json
705
+ * @returns {string} content as regular json, comments and dangling commas have been replaced with whitespace
706
+ */ function toJson(tsconfigJson) {
707
+ const stripped = stripDanglingComma(stripJsonComments(stripBom(tsconfigJson)));
708
+ if ('' === stripped.trim()) // only whitespace left after stripping, return empty object so that JSON.parse still works
709
+ return '{}';
710
+ return stripped;
711
+ }
712
+ /**
713
+ * replace dangling commas from pseudo-json string with single space
714
+ * implementation heavily inspired by strip-json-comments
715
+ *
716
+ * @param {string} pseudoJson
717
+ * @returns {string}
718
+ */ function stripDanglingComma(pseudoJson) {
719
+ let insideString = false;
720
+ let offset = 0;
721
+ let result = '';
722
+ let danglingCommaPos = null;
723
+ for(let i = 0; i < pseudoJson.length; i++){
724
+ const currentCharacter = pseudoJson[i];
725
+ if ('"' === currentCharacter) {
726
+ const escaped = isEscaped(pseudoJson, i);
727
+ if (!escaped) insideString = !insideString;
728
+ }
729
+ if (insideString) {
730
+ danglingCommaPos = null;
731
+ continue;
732
+ }
733
+ if (',' === currentCharacter) {
734
+ danglingCommaPos = i;
735
+ continue;
736
+ }
737
+ if (danglingCommaPos) {
738
+ if ('}' === currentCharacter || ']' === currentCharacter) {
739
+ result += pseudoJson.slice(offset, danglingCommaPos) + ' ';
740
+ offset = danglingCommaPos + 1;
741
+ danglingCommaPos = null;
742
+ } else if (!currentCharacter.match(/\s/)) danglingCommaPos = null;
743
+ }
744
+ }
745
+ return result + pseudoJson.substring(offset);
746
+ }
747
+ // start strip-json-comments
748
+ /**
749
+ *
750
+ * @param {string} jsonString
751
+ * @param {number} quotePosition
752
+ * @returns {boolean}
753
+ */ function isEscaped(jsonString, quotePosition) {
754
+ let index = quotePosition - 1;
755
+ let backslashCount = 0;
756
+ while('\\' === jsonString[index]){
757
+ index -= 1;
758
+ backslashCount += 1;
759
+ }
760
+ return Boolean(backslashCount % 2);
761
+ }
762
+ /**
763
+ *
764
+ * @param {string} string
765
+ * @param {number?} start
766
+ * @param {number?} end
767
+ */ function strip(string, start, end) {
768
+ return string.slice(start, end).replace(/\S/g, ' ');
769
+ }
770
+ const singleComment = Symbol('singleComment');
771
+ const multiComment = Symbol('multiComment');
772
+ /**
773
+ * @param {string} jsonString
774
+ * @returns {string}
775
+ */ function stripJsonComments(jsonString) {
776
+ let isInsideString = false;
777
+ /** @type {false | symbol} */ let isInsideComment = false;
778
+ let offset = 0;
779
+ let result = '';
780
+ for(let index = 0; index < jsonString.length; index++){
781
+ const currentCharacter = jsonString[index];
782
+ const nextCharacter = jsonString[index + 1];
783
+ if (!isInsideComment && '"' === currentCharacter) {
784
+ const escaped = isEscaped(jsonString, index);
785
+ if (!escaped) isInsideString = !isInsideString;
786
+ }
787
+ if (!isInsideString) if (isInsideComment || currentCharacter + nextCharacter !== '//') {
788
+ if (isInsideComment === singleComment && currentCharacter + nextCharacter === '\r\n') {
789
+ index++;
790
+ isInsideComment = false;
791
+ result += strip(jsonString, offset, index);
792
+ offset = index;
793
+ } else if (isInsideComment === singleComment && '\n' === currentCharacter) {
794
+ isInsideComment = false;
795
+ result += strip(jsonString, offset, index);
796
+ offset = index;
797
+ } else if (isInsideComment || currentCharacter + nextCharacter !== '/*') {
798
+ if (isInsideComment === multiComment && currentCharacter + nextCharacter === '*/') {
799
+ index++;
800
+ isInsideComment = false;
801
+ result += strip(jsonString, offset, index + 1);
802
+ offset = index + 1;
803
+ }
804
+ } else {
805
+ result += jsonString.slice(offset, index);
806
+ offset = index;
807
+ isInsideComment = multiComment;
808
+ index++;
809
+ }
810
+ } else {
811
+ result += jsonString.slice(offset, index);
812
+ offset = index;
813
+ isInsideComment = singleComment;
814
+ index++;
815
+ }
816
+ }
817
+ return result + (isInsideComment ? strip(jsonString.slice(offset)) : jsonString.slice(offset));
818
+ }
819
+ // end strip-json-comments
820
+ // start strip-bom
821
+ /**
822
+ * @param {string} string
823
+ * @returns {string}
824
+ */ function stripBom(string) {
825
+ // Catches EFBBBF (UTF-8 BOM) because the buffer-to-string
826
+ // conversion translates it to FEFF (UTF-16 BOM).
827
+ if (0xfeff === string.charCodeAt(0)) return string.slice(1);
828
+ return string;
829
+ }
830
+ const not_found_result = {
831
+ tsconfigFile: null,
832
+ tsconfig: {}
833
+ };
834
+ /**
835
+ * parse the closest tsconfig.json file
836
+ *
837
+ * @param {string} filename - path to a tsconfig .json or a source file or directory (absolute or relative to cwd)
838
+ * @param {import('./public.d.ts').TSConfckParseOptions} [options] - options
839
+ * @returns {Promise<import('./public.d.ts').TSConfckParseResult>}
840
+ * @throws {TSConfckParseError}
841
+ */ async function parse(filename, options) {
842
+ /** @type {import('./cache.js').TSConfckCache} */ const cache = options?.cache;
843
+ if (cache?.hasParseResult(filename)) return getParsedDeep(filename, cache, options);
844
+ const { resolve, reject, /** @type {Promise<import('./public.d.ts').TSConfckParseResult>}*/ promise } = util_makePromise();
845
+ cache?.setParseResult(filename, promise, true);
846
+ try {
847
+ let tsconfigFile = await util_resolveTSConfigJson(filename, cache) || await find(filename, options);
848
+ if (!tsconfigFile) {
849
+ resolve(not_found_result);
850
+ return promise;
851
+ }
852
+ let result;
853
+ if (filename !== tsconfigFile && cache?.hasParseResult(tsconfigFile)) result = await getParsedDeep(tsconfigFile, cache, options);
854
+ else {
855
+ result = await parseFile(tsconfigFile, cache, filename === tsconfigFile);
856
+ await Promise.all([
857
+ parseExtends(result, cache),
858
+ parseReferences(result, options)
859
+ ]);
860
+ }
861
+ result.tsconfig = util_replaceTokens(result.tsconfig, __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(tsconfigFile));
862
+ resolve(util_resolveSolutionTSConfig(filename, result));
863
+ } catch (e) {
864
+ reject(e);
865
+ }
866
+ return promise;
867
+ }
868
+ /**
869
+ * ensure extends and references are parsed
870
+ *
871
+ * @param {string} filename - cached file
872
+ * @param {import('./cache.js').TSConfckCache} cache - cache
873
+ * @param {import('./public.d.ts').TSConfckParseOptions} options - options
874
+ */ async function getParsedDeep(filename, cache, options) {
875
+ const result = await cache.getParseResult(filename);
876
+ if (result.tsconfig.extends && !result.extended || result.tsconfig.references && !result.referenced) {
877
+ const promise = Promise.all([
878
+ parseExtends(result, cache),
879
+ parseReferences(result, options)
880
+ ]).then(()=>result);
881
+ cache.setParseResult(filename, promise, true);
882
+ return promise;
883
+ }
884
+ return result;
885
+ }
886
+ /**
887
+ *
888
+ * @param {string} tsconfigFile - path to tsconfig file
889
+ * @param {import('./cache.js').TSConfckCache} [cache] - cache
890
+ * @param {boolean} [skipCache] - skip cache
891
+ * @returns {Promise<import('./public.d.ts').TSConfckParseResult>}
892
+ */ async function parseFile(tsconfigFile, cache, skipCache) {
893
+ if (!skipCache && cache?.hasParseResult(tsconfigFile) && !cache.getParseResult(tsconfigFile)._isRootFile_) return cache.getParseResult(tsconfigFile);
894
+ const promise = __WEBPACK_EXTERNAL_MODULE_node_fs__.promises.readFile(tsconfigFile, 'utf-8').then(toJson).then((json)=>{
895
+ const parsed = JSON.parse(json);
896
+ applyDefaults(parsed, tsconfigFile);
897
+ return {
898
+ tsconfigFile,
899
+ tsconfig: normalizeTSConfig(parsed, __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(tsconfigFile))
900
+ };
901
+ }).catch((e)=>{
902
+ throw new TSConfckParseError(`parsing ${tsconfigFile} failed: ${e}`, 'PARSE_FILE', tsconfigFile, e);
903
+ });
904
+ if (!skipCache && (!cache?.hasParseResult(tsconfigFile) || !cache.getParseResult(tsconfigFile)._isRootFile_)) cache?.setParseResult(tsconfigFile, promise);
905
+ return promise;
906
+ }
907
+ /**
908
+ * normalize to match the output of ts.parseJsonConfigFileContent
909
+ *
910
+ * @param {any} tsconfig - typescript tsconfig output
911
+ * @param {string} dir - directory
912
+ */ function normalizeTSConfig(tsconfig, dir) {
913
+ // set baseUrl to absolute path
914
+ const baseUrl = tsconfig.compilerOptions?.baseUrl;
915
+ if (baseUrl && !baseUrl.startsWith('${') && !__WEBPACK_EXTERNAL_MODULE_node_path__["default"].isAbsolute(baseUrl)) tsconfig.compilerOptions.baseUrl = resolve2posix(dir, baseUrl);
916
+ return tsconfig;
917
+ }
918
+ /**
919
+ *
920
+ * @param {import('./public.d.ts').TSConfckParseResult} result
921
+ * @param {import('./public.d.ts').TSConfckParseOptions} [options]
922
+ * @returns {Promise<void>}
923
+ */ async function parseReferences(result, options) {
924
+ if (!result.tsconfig.references) return;
925
+ const referencedFiles = util_resolveReferencedTSConfigFiles(result, options);
926
+ const referenced = await Promise.all(referencedFiles.map((file)=>parseFile(file, options?.cache)));
927
+ await Promise.all(referenced.map((ref)=>parseExtends(ref, options?.cache)));
928
+ referenced.forEach((ref)=>{
929
+ ref.solution = result;
930
+ });
931
+ result.referenced = referenced;
932
+ }
933
+ /**
934
+ * @param {import('./public.d.ts').TSConfckParseResult} result
935
+ * @param {import('./cache.js').TSConfckCache}[cache]
936
+ * @returns {Promise<void>}
937
+ */ async function parseExtends(result, cache) {
938
+ if (!result.tsconfig.extends) return;
939
+ // use result as first element in extended
940
+ // but dereference tsconfig so that mergeExtended can modify the original without affecting extended[0]
941
+ /** @type {import('./public.d.ts').TSConfckParseResult[]} */ const extended = [
942
+ {
943
+ tsconfigFile: result.tsconfigFile,
944
+ tsconfig: JSON.parse(JSON.stringify(result.tsconfig))
945
+ }
946
+ ];
947
+ // flatten extends graph into extended
948
+ let pos = 0;
949
+ /** @type {string[]} */ const extendsPath = [];
950
+ let currentBranchDepth = 0;
951
+ while(pos < extended.length){
952
+ const extending = extended[pos];
953
+ extendsPath.push(extending.tsconfigFile);
954
+ if (extending.tsconfig.extends) {
955
+ // keep following this branch
956
+ currentBranchDepth += 1;
957
+ /** @type {string[]} */ let resolvedExtends;
958
+ // reverse because typescript 5.0 treats ['a','b','c'] as c extends b extends a
959
+ resolvedExtends = Array.isArray(extending.tsconfig.extends) ? extending.tsconfig.extends.reverse().map((ex)=>resolveExtends(ex, extending.tsconfigFile)) : [
960
+ resolveExtends(extending.tsconfig.extends, extending.tsconfigFile)
961
+ ];
962
+ const circularExtends = resolvedExtends.find((tsconfigFile)=>extendsPath.includes(tsconfigFile));
963
+ if (circularExtends) {
964
+ const circle = extendsPath.concat([
965
+ circularExtends
966
+ ]).join(' -> ');
967
+ throw new TSConfckParseError(`Circular dependency in "extends": ${circle}`, 'EXTENDS_CIRCULAR', result.tsconfigFile);
968
+ }
969
+ // add new extends to the list directly after current
970
+ extended.splice(pos + 1, 0, ...await Promise.all(resolvedExtends.map((file)=>parseFile(file, cache))));
971
+ } else {
972
+ // reached a leaf, backtrack to the last branching point and continue
973
+ extendsPath.splice(-currentBranchDepth);
974
+ currentBranchDepth = 0;
975
+ }
976
+ pos += 1;
977
+ }
978
+ result.extended = extended;
979
+ // skip first as it is the original config
980
+ for (const ext of result.extended.slice(1))extendTSConfig(result, ext);
981
+ }
982
+ /**
983
+ *
984
+ * @param {string} extended
985
+ * @param {string} from
986
+ * @returns {string}
987
+ */ function resolveExtends(extended, from) {
988
+ if ('..' === extended) // see #149
989
+ extended = '../tsconfig.json';
990
+ const req = (0, __WEBPACK_EXTERNAL_MODULE_module__.createRequire)(from);
991
+ let error;
992
+ try {
993
+ return req.resolve(extended);
994
+ } catch (e) {
995
+ error = e;
996
+ }
997
+ if ('.' !== extended[0] && !__WEBPACK_EXTERNAL_MODULE_node_path__["default"].isAbsolute(extended)) try {
998
+ return req.resolve(`${extended}/tsconfig.json`);
999
+ } catch (e) {
1000
+ error = e;
1001
+ }
1002
+ throw new TSConfckParseError(`failed to resolve "extends":"${extended}" in ${from}`, 'EXTENDS_RESOLVE', from, error);
1003
+ }
1004
+ // references, extends and custom keys are not carried over
1005
+ const EXTENDABLE_KEYS = [
1006
+ 'compilerOptions',
1007
+ 'files',
1008
+ 'include',
1009
+ 'exclude',
1010
+ 'watchOptions',
1011
+ 'compileOnSave',
1012
+ 'typeAcquisition',
1013
+ 'buildOptions'
1014
+ ];
1015
+ /**
1016
+ *
1017
+ * @param {import('./public.d.ts').TSConfckParseResult} extending
1018
+ * @param {import('./public.d.ts').TSConfckParseResult} extended
1019
+ * @returns void
1020
+ */ function extendTSConfig(extending, extended) {
1021
+ const extendingConfig = extending.tsconfig;
1022
+ const extendedConfig = extended.tsconfig;
1023
+ const relativePath = util_native2posix(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].relative(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(extending.tsconfigFile), __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(extended.tsconfigFile)));
1024
+ for (const key of Object.keys(extendedConfig).filter((key)=>EXTENDABLE_KEYS.includes(key)))if ('compilerOptions' === key) {
1025
+ if (!extendingConfig.compilerOptions) extendingConfig.compilerOptions = {};
1026
+ for (const option of Object.keys(extendedConfig.compilerOptions)){
1027
+ if (!Object.prototype.hasOwnProperty.call(extendingConfig.compilerOptions, option)) extendingConfig.compilerOptions[option] = rebaseRelative(option, extendedConfig.compilerOptions[option], relativePath);
1028
+ }
1029
+ } else if (void 0 === extendingConfig[key]) {
1030
+ if ('watchOptions' === key) {
1031
+ extendingConfig.watchOptions = {};
1032
+ for (const option of Object.keys(extendedConfig.watchOptions))extendingConfig.watchOptions[option] = rebaseRelative(option, extendedConfig.watchOptions[option], relativePath);
1033
+ } else extendingConfig[key] = rebaseRelative(key, extendedConfig[key], relativePath);
1034
+ }
1035
+ }
1036
+ const REBASE_KEYS = [
1037
+ // root
1038
+ 'files',
1039
+ 'include',
1040
+ 'exclude',
1041
+ // compilerOptions
1042
+ 'baseUrl',
1043
+ 'rootDir',
1044
+ 'rootDirs',
1045
+ 'typeRoots',
1046
+ 'outDir',
1047
+ 'outFile',
1048
+ 'declarationDir',
1049
+ // watchOptions
1050
+ 'excludeDirectories',
1051
+ 'excludeFiles'
1052
+ ];
1053
+ /** @typedef {string | string[]} PathValue */ /**
1054
+ *
1055
+ * @param {string} key
1056
+ * @param {PathValue} value
1057
+ * @param {string} prependPath
1058
+ * @returns {PathValue}
1059
+ */ function rebaseRelative(key, value, prependPath) {
1060
+ if (!REBASE_KEYS.includes(key)) return value;
1061
+ if (Array.isArray(value)) return value.map((x)=>rebasePath(x, prependPath));
1062
+ return rebasePath(value, prependPath);
1063
+ }
1064
+ /**
1065
+ *
1066
+ * @param {string} value
1067
+ * @param {string} prependPath
1068
+ * @returns {string}
1069
+ */ function rebasePath(value, prependPath) {
1070
+ if (__WEBPACK_EXTERNAL_MODULE_node_path__["default"].isAbsolute(value) || value.startsWith('${configDir}')) return value;
1071
+ // relative paths use posix syntax in tsconfig
1072
+ return __WEBPACK_EXTERNAL_MODULE_node_path__["default"].posix.normalize(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].posix.join(prependPath, value));
1073
+ }
1074
+ class TSConfckParseError extends Error {
1075
+ /**
1076
+ * error code
1077
+ * @type {string}
1078
+ */ code;
1079
+ /**
1080
+ * error cause
1081
+ * @type { Error | undefined}
1082
+ */ cause;
1083
+ /**
1084
+ * absolute path of tsconfig file where the error happened
1085
+ * @type {string}
1086
+ */ tsconfigFile;
1087
+ /**
1088
+ *
1089
+ * @param {string} message - error message
1090
+ * @param {string} code - error code
1091
+ * @param {string} tsconfigFile - path to tsconfig file
1092
+ * @param {Error?} cause - cause of this error
1093
+ */ constructor(message, code, tsconfigFile, cause){
1094
+ super(message);
1095
+ // Set the prototype explicitly.
1096
+ Object.setPrototypeOf(this, TSConfckParseError.prototype);
1097
+ this.name = TSConfckParseError.name;
1098
+ this.code = code;
1099
+ this.cause = cause;
1100
+ this.tsconfigFile = tsconfigFile;
1101
+ }
1102
+ }
1103
+ /**
1104
+ *
1105
+ * @param {any} tsconfig
1106
+ * @param {string} tsconfigFile
1107
+ */ function applyDefaults(tsconfig, tsconfigFile) {
1108
+ if (isJSConfig(tsconfigFile)) tsconfig.compilerOptions = {
1109
+ ...DEFAULT_JSCONFIG_COMPILER_OPTIONS,
1110
+ ...tsconfig.compilerOptions
1111
+ };
1112
+ }
1113
+ const DEFAULT_JSCONFIG_COMPILER_OPTIONS = {
1114
+ allowJs: true,
1115
+ maxNodeModuleJsDepth: 2,
1116
+ allowSyntheticDefaultImports: true,
1117
+ skipLibCheck: true,
1118
+ noEmit: true
1119
+ };
1120
+ /**
1121
+ * @param {string} configFileName
1122
+ */ function isJSConfig(configFileName) {
1123
+ return 'jsconfig.json' === __WEBPACK_EXTERNAL_MODULE_node_path__["default"].basename(configFileName);
1124
+ }
1125
+ async function loadTsconfig(root, tsconfigPath = 'tsconfig.json') {
1126
+ const tsconfigFileName = await find((0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(root, tsconfigPath), {
1127
+ root,
1128
+ configName: (0, __WEBPACK_EXTERNAL_MODULE_node_path__.basename)(tsconfigPath)
1129
+ });
1130
+ if (tsconfigFileName) {
1131
+ const { tsconfig } = await parse(tsconfigFileName);
1132
+ return tsconfig;
1133
+ }
1134
+ return {};
1135
+ }
1136
+ function defineConfig(config) {
1137
+ return config;
1138
+ }
1139
+ const findConfig = (basePath)=>DEFAULT_CONFIG_EXTENSIONS.map((ext)=>basePath + ext).find(__WEBPACK_EXTERNAL_MODULE_node_fs__["default"].existsSync);
1140
+ const resolveConfigPath = (root, customConfig)=>{
1141
+ if (customConfig) {
1142
+ const customConfigPath = (0, __WEBPACK_EXTERNAL_MODULE_node_path__.isAbsolute)(customConfig) ? customConfig : (0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(root, customConfig);
1143
+ if (__WEBPACK_EXTERNAL_MODULE_node_fs__["default"].existsSync(customConfigPath)) return customConfigPath;
1144
+ __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.warn(`Cannot find config file: ${__WEBPACK_EXTERNAL_MODULE__compiled_picocolors_index_js__["default"].dim(customConfigPath)}\n`);
1145
+ }
1146
+ const configFilePath = findConfig((0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(root, DEFAULT_CONFIG_NAME));
1147
+ if (configFilePath) return configFilePath;
1148
+ throw new Error(`${DEFAULT_CONFIG_NAME} not found in ${root}`);
1149
+ };
1150
+ async function loadConfig({ cwd = process.cwd(), path, envMode }) {
1151
+ const configFilePath = resolveConfigPath(cwd, path);
1152
+ const { content } = await (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.loadConfig)({
1153
+ cwd: (0, __WEBPACK_EXTERNAL_MODULE_node_path__.dirname)(configFilePath),
1154
+ path: configFilePath,
1155
+ envMode
1156
+ });
1157
+ return content;
1158
+ }
1159
+ const composeExternalsWarnConfig = (format, ...externalsArray)=>{
1160
+ if ('esm' !== format) return {};
1161
+ const externals = [];
1162
+ for (const e of externalsArray.filter(Boolean))if (Array.isArray(e)) externals.push(...e);
1163
+ else // @ts-ignore
1164
+ externals.push(e);
1165
+ // Match logic is derived from https://github.com/webpack/webpack/blob/94aba382eccf3de1004d235045d4462918dfdbb7/lib/ExternalModuleFactoryPlugin.js#L166-L293.
1166
+ const matchUserExternals = (externals, request, callback)=>{
1167
+ if ('string' == typeof externals) {
1168
+ if (externals === request) {
1169
+ callback(true);
1170
+ return;
1171
+ }
1172
+ } else if (Array.isArray(externals)) {
1173
+ let i = 0;
1174
+ const next = ()=>{
1175
+ let asyncFlag;
1176
+ const handleExternalsAndCallback = (matched)=>{
1177
+ if (!matched) {
1178
+ if (asyncFlag) {
1179
+ asyncFlag = false;
1180
+ return;
1181
+ }
1182
+ return next();
1183
+ }
1184
+ callback(matched);
1185
+ };
1186
+ do {
1187
+ asyncFlag = true;
1188
+ if (i >= externals.length) return callback();
1189
+ matchUserExternals(externals[i++], request, handleExternalsAndCallback);
1190
+ }while (!asyncFlag);
1191
+ asyncFlag = false;
1192
+ };
1193
+ next();
1194
+ return;
1195
+ } else if (externals instanceof RegExp) {
1196
+ if (externals.test(request)) {
1197
+ callback(true);
1198
+ return;
1199
+ }
1200
+ } else if ('function' == typeof externals) ;
1201
+ else if ('object' == typeof externals) {
1202
+ if (Object.prototype.hasOwnProperty.call(externals, request)) {
1203
+ callback(true);
1204
+ return;
1205
+ }
1206
+ }
1207
+ callback();
1208
+ };
1209
+ return {
1210
+ output: {
1211
+ externals: [
1212
+ ({ request, dependencyType, contextInfo }, callback)=>{
1213
+ let externalized = false;
1214
+ const _callback = (matched)=>{
1215
+ if (matched) externalized = true;
1216
+ };
1217
+ if (contextInfo.issuer && 'commonjs' === dependencyType) {
1218
+ matchUserExternals(externals, request, _callback);
1219
+ if (externalized) __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.warn(composeModuleImportWarn(request));
1220
+ }
1221
+ callback();
1222
+ }
1223
+ ]
1224
+ }
1225
+ };
1226
+ };
1227
+ const composeAutoExternalConfig = (options)=>{
1228
+ const { autoExternal, pkgJson, userExternals } = options;
1229
+ if (!autoExternal) return {};
1230
+ if (!pkgJson) {
1231
+ __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.warn('autoExternal configuration will not be applied due to read package.json failed');
1232
+ return {};
1233
+ }
1234
+ const externalOptions = {
1235
+ dependencies: true,
1236
+ peerDependencies: true,
1237
+ devDependencies: false,
1238
+ ...true === autoExternal ? {} : autoExternal
1239
+ };
1240
+ // User externals configuration has higher priority than autoExternal
1241
+ // eg: autoExternal: ['react'], user: output: { externals: { react: 'react-1' } }
1242
+ // Only handle the case where the externals type is object, string / string[] does not need to be processed, other types are too complex.
1243
+ const userExternalKeys = userExternals && isObject(userExternals) ? Object.keys(userExternals) : [];
1244
+ const externals = [
1245
+ 'dependencies',
1246
+ 'peerDependencies',
1247
+ 'devDependencies'
1248
+ ].reduce((prev, type)=>{
1249
+ if (externalOptions[type]) return pkgJson[type] ? prev.concat(Object.keys(pkgJson[type])) : prev;
1250
+ return prev;
1251
+ }, []).filter((name)=>!userExternalKeys.includes(name));
1252
+ const uniqueExternals = Array.from(new Set(externals));
1253
+ return externals.length ? {
1254
+ output: {
1255
+ externals: [
1256
+ // Exclude dependencies, e.g. `react`, `react/jsx-runtime`
1257
+ ...uniqueExternals.map((dep)=>new RegExp(`^${dep}($|\\/|\\\\)`)),
1258
+ ...uniqueExternals
1259
+ ]
1260
+ }
1261
+ } : {};
1262
+ };
1263
+ function composeMinifyConfig(minify) {
1264
+ if (void 0 !== minify) // User's minify configuration will be merged afterwards.
1265
+ return {};
1266
+ // When minify is not specified, Rslib will use a sane default for minify options.
1267
+ // The default options will only perform dead code elimination and unused code elimination.
1268
+ return {
1269
+ output: {
1270
+ minify: {
1271
+ js: true,
1272
+ css: false,
1273
+ jsOptions: {
1274
+ minimizerOptions: {
1275
+ mangle: false,
1276
+ minify: false,
1277
+ compress: {
1278
+ defaults: false,
1279
+ unused: true,
1280
+ dead_code: true,
1281
+ toplevel: true
1282
+ },
1283
+ format: {
1284
+ comments: 'all'
1285
+ }
1286
+ }
1287
+ }
1288
+ }
1289
+ }
1290
+ };
1291
+ }
1292
+ function composeBannerFooterConfig(banner, footer) {
1293
+ const bannerConfig = pick(banner, [
1294
+ 'js',
1295
+ 'css'
1296
+ ]);
1297
+ const footerConfig = pick(footer, [
1298
+ 'js',
1299
+ 'css'
1300
+ ]);
1301
+ if (isEmptyObject(bannerConfig) && isEmptyObject(footerConfig)) return {};
1302
+ const plugins = [];
1303
+ if (!isEmptyObject(bannerConfig)) {
1304
+ if (bannerConfig.js) plugins.push(new __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.rspack.BannerPlugin({
1305
+ banner: bannerConfig.js,
1306
+ stage: __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE + 1,
1307
+ raw: true,
1308
+ include: /\.(js|mjs|cjs)$/
1309
+ }));
1310
+ if (bannerConfig.css) plugins.push(new __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.rspack.BannerPlugin({
1311
+ banner: bannerConfig.css,
1312
+ stage: __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE + 1,
1313
+ raw: true,
1314
+ include: /\.(css)$/
1315
+ }));
1316
+ }
1317
+ if (!isEmptyObject(footerConfig)) {
1318
+ if (footerConfig.js) plugins.push(new __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.rspack.BannerPlugin({
1319
+ banner: footerConfig.js,
1320
+ stage: __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE + 1,
1321
+ raw: true,
1322
+ footer: true,
1323
+ include: /\.(js|mjs|cjs)$/
1324
+ }));
1325
+ if (footerConfig.css) plugins.push(new __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.rspack.BannerPlugin({
1326
+ banner: footerConfig.css,
1327
+ stage: __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE + 1,
1328
+ raw: true,
1329
+ footer: true,
1330
+ include: /\.(css)$/
1331
+ }));
1332
+ }
1333
+ return {
1334
+ tools: {
1335
+ rspack: {
1336
+ plugins
1337
+ }
1338
+ }
1339
+ };
1340
+ }
1341
+ function composeDecoratorsConfig(compilerOptions, version) {
1342
+ if (version || !compilerOptions?.experimentalDecorators) return {};
1343
+ return {
1344
+ source: {
1345
+ decorators: {
1346
+ version: 'legacy'
1347
+ }
1348
+ }
1349
+ };
1350
+ }
1351
+ async function createConstantRsbuildConfig() {
1352
+ return (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.defineConfig)({
1353
+ mode: 'production',
1354
+ dev: {
1355
+ progressBar: false
1356
+ },
1357
+ tools: {
1358
+ htmlPlugin: false,
1359
+ rspack: {
1360
+ optimization: {
1361
+ moduleIds: 'named',
1362
+ nodeEnv: false
1363
+ },
1364
+ experiments: {
1365
+ rspackFuture: {
1366
+ bundlerInfo: {
1367
+ force: false
1368
+ }
1369
+ }
1370
+ },
1371
+ // TypeScript-specific behavior: if the extension is ".js" or ".jsx", try replacing it with ".ts" or ".tsx"
1372
+ // see https://github.com/web-infra-dev/rslib/issues/41
1373
+ resolve: {
1374
+ extensionAlias: {
1375
+ '.js': [
1376
+ '.ts',
1377
+ '.tsx',
1378
+ '.js',
1379
+ '.jsx'
1380
+ ],
1381
+ '.jsx': [
1382
+ '.tsx',
1383
+ '.jsx'
1384
+ ],
1385
+ '.mjs': [
1386
+ '.mts',
1387
+ '.mjs'
1388
+ ],
1389
+ '.cjs': [
1390
+ '.cts',
1391
+ '.cjs'
1392
+ ]
1393
+ }
1394
+ }
1395
+ }
1396
+ },
1397
+ output: {
1398
+ filenameHash: false,
1399
+ distPath: {
1400
+ js: './'
1401
+ }
1402
+ }
1403
+ });
1404
+ }
1405
+ const composeFormatConfig = (format, pkgJson)=>{
1406
+ switch(format){
1407
+ case 'esm':
1408
+ return {
1409
+ tools: {
1410
+ rspack: {
1411
+ output: {
1412
+ module: true,
1413
+ chunkFormat: 'module',
1414
+ library: {
1415
+ type: 'modern-module'
1416
+ }
1417
+ },
1418
+ module: {
1419
+ parser: {
1420
+ javascript: {
1421
+ importMeta: false
1422
+ }
1423
+ }
1424
+ },
1425
+ optimization: {
1426
+ concatenateModules: true
1427
+ },
1428
+ experiments: {
1429
+ outputModule: true
1430
+ }
1431
+ }
1432
+ }
1433
+ };
1434
+ case 'cjs':
1435
+ return {
1436
+ plugins: [
1437
+ pluginCjsShim()
1438
+ ],
1439
+ tools: {
1440
+ rspack: {
1441
+ module: {
1442
+ parser: {
1443
+ javascript: {
1444
+ importMeta: false
1445
+ }
1446
+ }
1447
+ },
1448
+ output: {
1449
+ iife: false,
1450
+ chunkFormat: 'commonjs',
1451
+ library: {
1452
+ type: 'commonjs'
1453
+ }
1454
+ }
1455
+ }
1456
+ }
1457
+ };
1458
+ case 'umd':
1459
+ return {
1460
+ tools: {
1461
+ rspack: {
1462
+ module: {
1463
+ parser: {
1464
+ javascript: {
1465
+ importMeta: false
1466
+ }
1467
+ }
1468
+ },
1469
+ output: {
1470
+ library: {
1471
+ type: 'umd'
1472
+ }
1473
+ }
1474
+ }
1475
+ }
1476
+ };
1477
+ case 'mf':
1478
+ return {
1479
+ tools: {
1480
+ rspack: {
1481
+ output: {
1482
+ uniqueName: pkgJson.name
1483
+ }
1484
+ }
1485
+ }
1486
+ };
1487
+ default:
1488
+ throw new Error(`Unsupported format: ${format}`);
1489
+ }
1490
+ };
1491
+ const composeModuleImportWarn = (request)=>`The externalized commonjs request ${__WEBPACK_EXTERNAL_MODULE__compiled_picocolors_index_js__["default"].green(`"${request}"`)} will use ${__WEBPACK_EXTERNAL_MODULE__compiled_picocolors_index_js__["default"].blue('"module"')} external type in ESM format. If you want to specify other external type, considering set the request and type with ${__WEBPACK_EXTERNAL_MODULE__compiled_picocolors_index_js__["default"].blue('"output.externals"')}.`;
1492
+ const composeExternalsConfig = (format, externals)=>{
1493
+ // TODO: Define the internal externals config in Rsbuild's externals instead
1494
+ // Rspack's externals as they will not be merged from different fields. All externals
1495
+ // should to be unified and merged together in the future.
1496
+ const externalsTypeMap = {
1497
+ esm: 'module-import',
1498
+ cjs: 'commonjs',
1499
+ umd: 'umd'
1500
+ };
1501
+ switch(format){
1502
+ case 'esm':
1503
+ case 'cjs':
1504
+ case 'umd':
1505
+ return {
1506
+ output: externals ? {
1507
+ externals
1508
+ } : {},
1509
+ tools: {
1510
+ rspack: {
1511
+ externalsType: externalsTypeMap[format]
1512
+ }
1513
+ }
1514
+ };
1515
+ case 'mf':
1516
+ return {
1517
+ output: externals ? {
1518
+ externals
1519
+ } : {}
1520
+ };
1521
+ default:
1522
+ throw new Error(`Unsupported format: ${format}`);
1523
+ }
1524
+ };
1525
+ const composeAutoExtensionConfig = (config, autoExtension, pkgJson)=>{
1526
+ const { jsExtension, dtsExtension } = getDefaultExtension({
1527
+ format: config.format,
1528
+ pkgJson,
1529
+ autoExtension
1530
+ });
1531
+ return {
1532
+ config: {
1533
+ output: {
1534
+ filename: {
1535
+ js: `[name]${jsExtension}`,
1536
+ ...config.output?.filename
1537
+ }
1538
+ }
1539
+ },
1540
+ jsExtension,
1541
+ dtsExtension
1542
+ };
1543
+ };
1544
+ const composeSyntaxConfig = (syntax, target)=>{
1545
+ // Defaults to ESNext, Rslib will assume all of the latest JavaScript and CSS features are supported.
1546
+ if (syntax) return {
1547
+ tools: {
1548
+ rspack: (config)=>{
1549
+ config.target = transformSyntaxToRspackTarget(syntax);
1550
+ }
1551
+ },
1552
+ output: {
1553
+ overrideBrowserslist: transformSyntaxToBrowserslist(syntax, target)
1554
+ }
1555
+ };
1556
+ return {
1557
+ tools: {
1558
+ rspack: (config)=>{
1559
+ config.target = [
1560
+ 'es2022'
1561
+ ];
1562
+ return config;
1563
+ }
1564
+ },
1565
+ output: {
1566
+ // If `syntax` is not defined, Rslib will try to determine by the `target`, with the last version of the target.
1567
+ overrideBrowserslist: ESX_TO_BROWSERSLIST.esnext(target)
1568
+ }
1569
+ };
1570
+ };
1571
+ const composeEntryConfig = async (entries, bundle, root)=>{
1572
+ if (!entries) return {};
1573
+ if (false !== bundle) return {
1574
+ source: {
1575
+ entry: entries
1576
+ }
1577
+ };
1578
+ // In bundleless mode, resolve glob patterns and convert them to entry object.
1579
+ const resolvedEntries = {};
1580
+ for (const key of Object.keys(entries)){
1581
+ const entry = entries[key];
1582
+ // Entries in bundleless mode could be:
1583
+ // 1. A string of glob pattern: { entry: { index: 'src/*.ts' } }
1584
+ // 2. An array of glob patterns: { entry: { index: ['src/*.ts', 'src/*.tsx'] } }
1585
+ // Not supported for now: entry description object
1586
+ const entryFiles = Array.isArray(entry) ? entry : 'string' == typeof entry ? [
1587
+ entry
1588
+ ] : null;
1589
+ if (!entryFiles) throw new Error('Entry can only be a string or an array of strings for now');
1590
+ // Turn entries in array into each separate entry.
1591
+ const globEntryFiles = await (0, __WEBPACK_EXTERNAL_MODULE__compiled_fast_glob_index_js__["default"])(entryFiles, {
1592
+ cwd: root
1593
+ });
1594
+ // Filter the glob resolved entry files based on the allowed extensions
1595
+ const resolvedEntryFiles = globEntryFiles.filter((file)=>ENTRY_EXTENSIONS_PATTERN.test(file));
1596
+ if (0 === resolvedEntryFiles.length) throw new Error(`Cannot find ${resolvedEntryFiles}`);
1597
+ // Similar to `rootDir` in tsconfig and `outbase` in esbuild.
1598
+ const lcp = await calcLongestCommonPath(resolvedEntryFiles);
1599
+ // Using the longest common path of all non-declaration input files by default.
1600
+ const outBase = null === lcp ? root : lcp;
1601
+ for (const file of resolvedEntryFiles){
1602
+ const { dir, name } = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].parse(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].relative(outBase, file));
1603
+ // Entry filename contains nested path to preserve source directory structure.
1604
+ const entryFileName = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(dir, name);
1605
+ resolvedEntries[entryFileName] = file;
1606
+ }
1607
+ }
1608
+ return {
1609
+ source: {
1610
+ entry: resolvedEntries
1611
+ }
1612
+ };
1613
+ };
1614
+ const composeBundleConfig = (jsExtension, bundle = true)=>{
1615
+ if (bundle) return {};
1616
+ return {
1617
+ output: {
1618
+ externals: [
1619
+ (data, callback)=>{
1620
+ // Issuer is not empty string when the module is imported by another module.
1621
+ // Prevent from externalizing entry modules here.
1622
+ if (data.contextInfo.issuer) {
1623
+ // Node.js ECMAScript module loader does no extension searching.
1624
+ // Add a file extension according to autoExtension config
1625
+ // when data.request is a relative path and do not have an extension.
1626
+ // If data.request already have an extension, we replace it with new extension
1627
+ // This may result in a change in semantics,
1628
+ // user should use copy to keep origin file or use another separate entry to deal this
1629
+ let request = data.request;
1630
+ if ('.' === request[0]) {
1631
+ if ((0, __WEBPACK_EXTERNAL_MODULE_node_path__.extname)(request)) {
1632
+ if (!JS_EXTENSIONS_PATTERN.test(request)) // If it does not match jsExtensionsPattern, we should do nothing, eg: ./foo.png
1633
+ return callback();
1634
+ request = request.replace(/\.[^.]+$/, jsExtension);
1635
+ } else request = `${request}${jsExtension}`;
1636
+ }
1637
+ return callback(null, request);
1638
+ }
1639
+ callback();
1640
+ }
1641
+ ]
1642
+ }
1643
+ };
1644
+ };
1645
+ const composeDtsConfig = async (libConfig, dtsExtension)=>{
1646
+ const { dts, bundle, output, autoExternal, banner, footer } = libConfig;
1647
+ if (false === dts || void 0 === dts) return {};
1648
+ const { pluginDts } = await import("rsbuild-plugin-dts");
1649
+ return {
1650
+ plugins: [
1651
+ pluginDts({
1652
+ bundle: dts?.bundle ?? bundle,
1653
+ distPath: dts?.distPath ?? output?.distPath?.root ?? './dist',
1654
+ abortOnError: dts?.abortOnError ?? true,
1655
+ dtsExtension,
1656
+ autoExternal,
1657
+ banner: banner?.dts,
1658
+ footer: footer?.dts
1659
+ })
1660
+ ]
1661
+ };
1662
+ };
1663
+ const composeTargetConfig = (target = 'web')=>{
1664
+ switch(target){
1665
+ case 'web':
1666
+ return {
1667
+ tools: {
1668
+ rspack: {
1669
+ target: [
1670
+ 'web'
1671
+ ],
1672
+ output: {
1673
+ chunkLoading: 'import',
1674
+ workerChunkLoading: 'import',
1675
+ wasmLoading: 'fetch'
1676
+ }
1677
+ }
1678
+ }
1679
+ };
1680
+ case 'node':
1681
+ return {
1682
+ tools: {
1683
+ rspack: {
1684
+ target: [
1685
+ 'node'
1686
+ ],
1687
+ // "__dirname" and "__filename" shims will automatically be enabled when `output.module` is `true`,
1688
+ // and leave them as-is in the rest of the cases.
1689
+ // { node: { __dirname: ..., __filename: ... } }
1690
+ output: {
1691
+ chunkLoading: 'require',
1692
+ workerChunkLoading: 'async-node',
1693
+ wasmLoading: 'async-node'
1694
+ }
1695
+ }
1696
+ },
1697
+ output: {
1698
+ // When output.target is 'node', Node.js's built-in will be treated as externals of type `node-commonjs`.
1699
+ // Simply override the built-in modules to make them external.
1700
+ // https://github.com/webpack/webpack/blob/dd44b206a9c50f4b4cb4d134e1a0bd0387b159a3/lib/node/NodeTargetPlugin.js#L81
1701
+ externals: nodeBuiltInModules,
1702
+ target: 'node'
1703
+ }
1704
+ };
1705
+ // TODO: Support `neutral` target, however Rsbuild don't list it as an option in the target field.
1706
+ // case 'neutral':
1707
+ // return {
1708
+ // tools: {
1709
+ // rspack: {
1710
+ // target: ['web', 'node'],
1711
+ // },
1712
+ // },
1713
+ // };
1714
+ default:
1715
+ throw new Error(`Unsupported platform: ${target}`);
1716
+ }
1717
+ };
1718
+ const composeExternalHelpersConfig = (externalHelpers, pkgJson)=>{
1719
+ let defaultConfig = {
1720
+ tools: {
1721
+ swc: {
1722
+ jsc: {
1723
+ externalHelpers: false
1724
+ }
1725
+ }
1726
+ }
1727
+ };
1728
+ if (externalHelpers) {
1729
+ const deps = [
1730
+ ...Object.keys(pkgJson?.dependencies ?? []),
1731
+ ...Object.keys(pkgJson?.devDependencies ?? [])
1732
+ ];
1733
+ if (!deps.includes(SWC_HELPERS)) {
1734
+ __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.error(`${__WEBPACK_EXTERNAL_MODULE__compiled_picocolors_index_js__["default"].green('externalHelpers')} is enabled, but the ${__WEBPACK_EXTERNAL_MODULE__compiled_picocolors_index_js__["default"].blue(SWC_HELPERS)} dependency declaration was not found in package.json.`);
1735
+ process.exit(1);
1736
+ }
1737
+ defaultConfig = Object.assign(defaultConfig, {
1738
+ output: {
1739
+ externals: new RegExp(`^${SWC_HELPERS}($|\\/|\\\\)`)
1740
+ }
1741
+ });
1742
+ defaultConfig.tools.swc.jsc.externalHelpers = true;
1743
+ }
1744
+ return defaultConfig;
1745
+ };
1746
+ async function composeLibRsbuildConfig(config, configPath) {
1747
+ const rootPath = (0, __WEBPACK_EXTERNAL_MODULE_node_path__.dirname)(configPath);
1748
+ const pkgJson = readPackageJson(rootPath);
1749
+ const { compilerOptions } = await loadTsconfig(rootPath, config.source?.tsconfigPath);
1750
+ const { format, banner = {}, footer = {}, autoExtension = true, autoExternal = true, externalHelpers = false } = config;
1751
+ const formatConfig = composeFormatConfig(format, pkgJson);
1752
+ const externalHelpersConfig = composeExternalHelpersConfig(externalHelpers, pkgJson);
1753
+ const externalsConfig = composeExternalsConfig(format, config.output?.externals);
1754
+ const { config: autoExtensionConfig, jsExtension, dtsExtension } = composeAutoExtensionConfig(config, autoExtension, pkgJson);
1755
+ const bundleConfig = composeBundleConfig(jsExtension, config.bundle);
1756
+ const targetConfig = composeTargetConfig(config.output?.target);
1757
+ const syntaxConfig = composeSyntaxConfig(config?.syntax, config.output?.target);
1758
+ const autoExternalConfig = composeAutoExternalConfig({
1759
+ autoExternal,
1760
+ pkgJson,
1761
+ userExternals: config.output?.externals
1762
+ });
1763
+ const entryConfig = await composeEntryConfig(config.source?.entry, config.bundle, (0, __WEBPACK_EXTERNAL_MODULE_node_path__.dirname)(configPath));
1764
+ const dtsConfig = await composeDtsConfig(config, dtsExtension);
1765
+ const externalsWarnConfig = composeExternalsWarnConfig(format, autoExternalConfig?.output?.externals, externalsConfig?.output?.externals);
1766
+ const minifyConfig = composeMinifyConfig(config.output?.minify);
1767
+ const bannerFooterConfig = composeBannerFooterConfig(banner, footer);
1768
+ const decoratorsConfig = composeDecoratorsConfig(compilerOptions, config.source?.decorators?.version);
1769
+ return (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.mergeRsbuildConfig)(formatConfig, externalHelpersConfig, externalsWarnConfig, externalsConfig, autoExternalConfig, autoExtensionConfig, syntaxConfig, bundleConfig, targetConfig, entryConfig, minifyConfig, dtsConfig, bannerFooterConfig, decoratorsConfig);
1770
+ }
1771
+ async function composeCreateRsbuildConfig(rslibConfig, path) {
1772
+ const constantRsbuildConfig = await createConstantRsbuildConfig();
1773
+ const configPath = path ?? rslibConfig._privateMeta?.configFilePath;
1774
+ const { lib: libConfigsArray, ...sharedRsbuildConfig } = rslibConfig;
1775
+ if (!libConfigsArray) throw new Error(`Expect lib field to be an array, but got ${libConfigsArray}.`);
1776
+ const libConfigPromises = libConfigsArray.map(async (libConfig)=>{
1777
+ const userConfig = (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.mergeRsbuildConfig)(sharedRsbuildConfig, libConfig);
1778
+ // Merge the configuration of each environment based on the shared Rsbuild
1779
+ // configuration and Lib configuration in the settings.
1780
+ const libRsbuildConfig = await composeLibRsbuildConfig(userConfig, configPath);
1781
+ // Reset certain fields because they will be completely overridden by the upcoming merge.
1782
+ // We don't want to retain them in the final configuration.
1783
+ // The reset process should occur after merging the library configuration.
1784
+ userConfig.source ??= {};
1785
+ userConfig.source.entry = {};
1786
+ // Already manually sort and merge the externals configuration.
1787
+ userConfig.output ??= {};
1788
+ delete userConfig.output.externals;
1789
+ return {
1790
+ format: libConfig.format,
1791
+ // The merge order represents the priority of the configuration
1792
+ // The priorities from high to low are as follows:
1793
+ // 1 - userConfig: users can configure any Rsbuild and Rspack config
1794
+ // 2 - libRsbuildConfig: the configuration that we compose from Rslib unique config and userConfig from 1
1795
+ // 3 - constantRsbuildConfig: the built-in best practice Rsbuild configuration we provide in Rslib
1796
+ // We should state in the document that the built-in configuration should not be changed optionally
1797
+ // In compose process of 2, we may read some config from 1, and reassemble the related config,
1798
+ // so before final mergeRsbuildConfig, we reset some specified fields
1799
+ config: (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.mergeRsbuildConfig)(constantRsbuildConfig, libRsbuildConfig, omit(userConfig, [
1800
+ 'bundle',
1801
+ 'format',
1802
+ 'autoExtension',
1803
+ 'autoExternal',
1804
+ 'syntax',
1805
+ 'externalHelpers',
1806
+ 'banner',
1807
+ 'footer',
1808
+ 'dts'
1809
+ ]))
1810
+ };
1811
+ });
1812
+ const composedRsbuildConfig = await Promise.all(libConfigPromises);
1813
+ return composedRsbuildConfig;
1814
+ }
1815
+ async function initRsbuild(rslibConfig) {
1816
+ const rsbuildConfigObject = await composeCreateRsbuildConfig(rslibConfig);
1817
+ const environments = {};
1818
+ const formatCount = rsbuildConfigObject.reduce((acc, { format })=>{
1819
+ acc[format] = (acc[format] ?? 0) + 1;
1820
+ return acc;
1821
+ }, {});
1822
+ const formatIndex = {
1823
+ esm: 0,
1824
+ cjs: 0,
1825
+ umd: 0,
1826
+ mf: 0
1827
+ };
1828
+ for (const { format, config } of rsbuildConfigObject){
1829
+ const currentFormatCount = formatCount[format];
1830
+ const currentFormatIndex = formatIndex[format]++;
1831
+ environments[1 === currentFormatCount ? format : `${format}${currentFormatIndex}`] = config;
1832
+ }
1833
+ return (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.createRsbuild)({
1834
+ rsbuildConfig: {
1835
+ environments
1836
+ }
1837
+ });
1838
+ }
1839
+ async function build(config, options) {
1840
+ const rsbuildInstance = await initRsbuild(config);
1841
+ await rsbuildInstance.build({
1842
+ watch: options?.watch
1843
+ });
1844
+ return rsbuildInstance;
1845
+ }
1846
+ const applyCommonOptions = (command)=>{
1847
+ command.option('-c --config <config>', 'specify the configuration file, can be a relative or absolute path').option('--env-mode <mode>', 'specify the env mode to load the `.env.[mode]` file');
1848
+ };
1849
+ function runCli() {
1850
+ __WEBPACK_EXTERNAL_MODULE__compiled_commander_index_js__.program.name('rslib').usage('<command> [options]').version("0.0.7");
1851
+ const buildCommand = __WEBPACK_EXTERNAL_MODULE__compiled_commander_index_js__.program.command('build');
1852
+ const inspectCommand = __WEBPACK_EXTERNAL_MODULE__compiled_commander_index_js__.program.command('inspect');
1853
+ [
1854
+ buildCommand,
1855
+ inspectCommand
1856
+ ].forEach(applyCommonOptions);
1857
+ buildCommand.option('-w --watch', 'turn on watch mode, watch for changes and rebuild').description('build the library for production').action(async (options)=>{
1858
+ try {
1859
+ const rslibConfig = await loadConfig({
1860
+ path: options.config,
1861
+ envMode: options.envMode
1862
+ });
1863
+ await build(rslibConfig, options);
1864
+ } catch (err) {
1865
+ __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.error('Failed to build.');
1866
+ __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.error(err);
1867
+ process.exit(1);
1868
+ }
1869
+ });
1870
+ inspectCommand.description('inspect the Rslib / Rsbuild / Rspack configs').option('--env <env>', 'specify env mode', 'development').option('--output <output>', 'specify inspect content output path', './').option('--verbose', 'show full function definitions in output').action(async (options)=>{
1871
+ try {
1872
+ // TODO: inspect should output Rslib's config
1873
+ const rslibConfig = await loadConfig({
1874
+ path: options.config,
1875
+ envMode: options.envMode
1876
+ });
1877
+ const rsbuildInstance = await initRsbuild(rslibConfig);
1878
+ await rsbuildInstance.inspectConfig({
1879
+ mode: options.mode,
1880
+ verbose: options.verbose,
1881
+ outputPath: options.output,
1882
+ writeToDisk: true
1883
+ });
1884
+ } catch (err) {
1885
+ __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.error('Failed to inspect config.');
1886
+ __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.error(err);
1887
+ process.exit(1);
1888
+ }
1889
+ });
1890
+ __WEBPACK_EXTERNAL_MODULE__compiled_commander_index_js__.program.parse();
1891
+ }
1892
+ const src_version = "0.0.7";
1893
+ var __webpack_exports__logger = __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger;
1894
+ export { build, defineConfig, loadConfig, prepareCli, runCli, src_version as version, __webpack_exports__logger as logger };