@rslib/core 0.10.0 → 0.10.2
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.
- package/compiled/rslog/index.d.ts +2 -2
- package/compiled/rslog/index.js +260 -309
- package/compiled/rslog/package.json +1 -1
- package/dist/index.js +1736 -1735
- package/dist/libCssExtractLoader.js +23 -27
- package/dist-types/css/const.d.ts +5 -0
- package/dist-types/css/cssConfig.d.ts +2 -12
- package/dist-types/css/libCssExtractLoader.d.ts +1 -6
- package/dist-types/css/utils.d.ts +13 -2
- package/dist-types/index.d.ts +5 -5
- package/dist-types/utils/color.d.ts +2 -0
- package/dist-types/utils/helper.d.ts +0 -2
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,1196 +1,698 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import promises_default from "node:fs/promises";
|
|
4
|
-
import external_node_path_default, { basename, dirname, extname as external_node_path_extname, isAbsolute, join } from "node:path";
|
|
5
|
-
import index_js_default from "../compiled/picocolors/index.js";
|
|
6
|
-
import { EventEmitter } from "events";
|
|
1
|
+
import node_fs, { chmodSync, promises } from "node:fs";
|
|
2
|
+
import node_path, { basename, dirname, extname, isAbsolute, join } from "node:path";
|
|
7
3
|
import { glob } from "tinyglobby";
|
|
8
4
|
import { createRequire } from "node:module";
|
|
5
|
+
import picocolors from "../compiled/picocolors/index.js";
|
|
6
|
+
import { logger } from "../compiled/rslog/index.js";
|
|
7
|
+
import node_fs_promises from "node:fs/promises";
|
|
9
8
|
import { createRequire as external_module_createRequire } from "module";
|
|
9
|
+
import { EventEmitter } from "events";
|
|
10
10
|
import * as __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__ from "@rsbuild/core";
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
'
|
|
14
|
-
'
|
|
15
|
-
'
|
|
16
|
-
'
|
|
17
|
-
'
|
|
18
|
-
'
|
|
19
|
-
'constants',
|
|
20
|
-
'crypto',
|
|
21
|
-
'dgram',
|
|
22
|
-
'diagnostics_channel',
|
|
23
|
-
'dns',
|
|
24
|
-
'dns/promises',
|
|
25
|
-
'domain',
|
|
26
|
-
'events',
|
|
27
|
-
'fs',
|
|
28
|
-
'fs/promises',
|
|
29
|
-
'http',
|
|
30
|
-
'http2',
|
|
31
|
-
'https',
|
|
32
|
-
'inspector',
|
|
33
|
-
'inspector/promises',
|
|
34
|
-
'module',
|
|
35
|
-
'net',
|
|
36
|
-
'os',
|
|
37
|
-
'path',
|
|
38
|
-
'path/posix',
|
|
39
|
-
'path/win32',
|
|
40
|
-
'perf_hooks',
|
|
41
|
-
'process',
|
|
42
|
-
'punycode',
|
|
43
|
-
'querystring',
|
|
44
|
-
'readline',
|
|
45
|
-
'readline/promises',
|
|
46
|
-
'repl',
|
|
47
|
-
'stream',
|
|
48
|
-
'stream/consumers',
|
|
49
|
-
'stream/promises',
|
|
50
|
-
'stream/web',
|
|
51
|
-
'string_decoder',
|
|
52
|
-
'sys',
|
|
53
|
-
'timers',
|
|
54
|
-
'timers/promises',
|
|
55
|
-
'tls',
|
|
56
|
-
'trace_events',
|
|
57
|
-
'tty',
|
|
58
|
-
'url',
|
|
59
|
-
'util',
|
|
60
|
-
'util/types',
|
|
61
|
-
'v8',
|
|
62
|
-
'vm',
|
|
63
|
-
'wasi',
|
|
64
|
-
'worker_threads',
|
|
65
|
-
'zlib',
|
|
66
|
-
/^node:/,
|
|
67
|
-
'pnpapi'
|
|
11
|
+
const DEFAULT_CONFIG_NAME = 'rslib.config';
|
|
12
|
+
const DEFAULT_CONFIG_EXTENSIONS = [
|
|
13
|
+
'.js',
|
|
14
|
+
'.ts',
|
|
15
|
+
'.mjs',
|
|
16
|
+
'.mts',
|
|
17
|
+
'.cjs',
|
|
18
|
+
'.cts'
|
|
68
19
|
];
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
20
|
+
const SWC_HELPERS = '@swc/helpers';
|
|
21
|
+
const SHEBANG_PREFIX = '#!';
|
|
22
|
+
const SHEBANG_REGEX = /#!.*[\s\n\r]*$/;
|
|
23
|
+
const REACT_DIRECTIVE_REGEX = /^['"]use (client|server)['"](;?)[\s\n\r]*$/;
|
|
24
|
+
const DTS_EXTENSIONS = [
|
|
25
|
+
'd.ts',
|
|
26
|
+
'd.mts',
|
|
27
|
+
'd.cts'
|
|
28
|
+
];
|
|
29
|
+
const JS_EXTENSIONS = [
|
|
30
|
+
'js',
|
|
31
|
+
'mjs',
|
|
32
|
+
'jsx',
|
|
33
|
+
'(?<!\\.d\\.)ts',
|
|
34
|
+
'(?<!\\.d\\.)mts',
|
|
35
|
+
'(?<!\\.d\\.)cts',
|
|
36
|
+
'tsx',
|
|
37
|
+
'cjs',
|
|
38
|
+
'cjsx',
|
|
39
|
+
'mjsx',
|
|
40
|
+
'mtsx',
|
|
41
|
+
'ctsx'
|
|
42
|
+
];
|
|
43
|
+
const CSS_EXTENSIONS = [
|
|
44
|
+
'css',
|
|
45
|
+
'sass',
|
|
46
|
+
'scss',
|
|
47
|
+
'less',
|
|
48
|
+
'styl',
|
|
49
|
+
'stylus'
|
|
50
|
+
];
|
|
51
|
+
const JS_EXTENSIONS_PATTERN = new RegExp(`\\.(${JS_EXTENSIONS.join('|')})$`);
|
|
52
|
+
const CSS_EXTENSIONS_PATTERN = new RegExp(`\\.(${CSS_EXTENSIONS.join('|')})$`);
|
|
53
|
+
const DTS_EXTENSIONS_PATTERN = new RegExp(`\\.(${DTS_EXTENSIONS.join('|')})$`);
|
|
54
|
+
function getUndoPath(filename, outputPathArg, enforceRelative) {
|
|
55
|
+
let depth = -1;
|
|
56
|
+
let append = '';
|
|
57
|
+
let outputPath = outputPathArg.replace(/[\\/]$/, '');
|
|
58
|
+
for (const part of filename.split(/[/\\]+/))if ('..' === part) if (depth > -1) depth--;
|
|
59
|
+
else {
|
|
60
|
+
const i = outputPath.lastIndexOf('/');
|
|
61
|
+
const j = outputPath.lastIndexOf('\\');
|
|
62
|
+
const pos = i < 0 ? j : j < 0 ? i : Math.max(i, j);
|
|
63
|
+
if (pos < 0) return `${outputPath}/`;
|
|
64
|
+
append = `${outputPath.slice(pos + 1)}/${append}`;
|
|
65
|
+
outputPath = outputPath.slice(0, pos);
|
|
97
66
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const isEmptyObject = (obj)=>0 === Object.keys(obj).length;
|
|
101
|
-
function pick(obj, keys) {
|
|
102
|
-
return keys.reduce((ret, key)=>{
|
|
103
|
-
if (void 0 !== obj[key]) ret[key] = obj[key];
|
|
104
|
-
return ret;
|
|
105
|
-
}, {});
|
|
106
|
-
}
|
|
107
|
-
function omit(obj, keysObj) {
|
|
108
|
-
return Object.keys(obj).reduce((ret, key)=>{
|
|
109
|
-
if (true !== keysObj[key]) ret[key] = obj[key];
|
|
110
|
-
return ret;
|
|
111
|
-
}, {});
|
|
112
|
-
}
|
|
113
|
-
function isPluginIncluded(pluginName, plugins) {
|
|
114
|
-
return Boolean(plugins?.some((plugin)=>{
|
|
115
|
-
if (Array.isArray(plugin)) return isPluginIncluded(pluginName, plugin);
|
|
116
|
-
if ('object' == typeof plugin && null !== plugin && 'name' in plugin) return plugin.name === pluginName;
|
|
117
|
-
return false;
|
|
118
|
-
}));
|
|
67
|
+
else if ('.' !== part) depth++;
|
|
68
|
+
return depth > 0 ? `${'../'.repeat(depth)}${append}` : enforceRelative ? `./${append}` : append;
|
|
119
69
|
}
|
|
120
|
-
function
|
|
121
|
-
|
|
122
|
-
const added = isPluginIncluded('rsbuild:module-federation-enhanced', [
|
|
123
|
-
...sharedPlugins || [],
|
|
124
|
-
...config.plugins || []
|
|
125
|
-
]);
|
|
126
|
-
if (!added) {
|
|
127
|
-
logger.warn(`${index_js_default.green('format: "mf"')} should be used with ${index_js_default.blue('@module-federation/rsbuild-plugin')}", consider installing and adding it to plugins. Check the documentation (https://module-federation.io/guide/basic/rsbuild.html#rslib-module) to get started with "mf" output.`);
|
|
128
|
-
process.exit(1);
|
|
129
|
-
}
|
|
130
|
-
return added;
|
|
70
|
+
function isCssFile(filepath) {
|
|
71
|
+
return CSS_EXTENSIONS_PATTERN.test(filepath);
|
|
131
72
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
73
|
+
const CSS_MODULE_REG = /\.module\.\w+$/i;
|
|
74
|
+
const PATH_QUERY_FRAGMENT_REGEXP = /^((?:\u200b.|[^?#\u200b])*)(\?(?:\u200b.|[^#\u200b])*)?(#.*)?$/;
|
|
75
|
+
function parsePathQueryFragment(str) {
|
|
76
|
+
const match = PATH_QUERY_FRAGMENT_REGEXP.exec(str);
|
|
77
|
+
return {
|
|
78
|
+
path: match?.[1]?.replace(/\u200b(.)/g, '$1') || '',
|
|
79
|
+
query: match?.[2] ? match[2].replace(/\u200b(.)/g, '$1') : '',
|
|
80
|
+
fragment: match?.[3] || ''
|
|
139
81
|
};
|
|
140
82
|
}
|
|
141
|
-
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (!process.env.DEBUG) return false;
|
|
149
|
-
const values = process.env.DEBUG.toLocaleLowerCase().split(',');
|
|
150
|
-
return [
|
|
151
|
-
'rslib',
|
|
152
|
-
'rs*',
|
|
153
|
-
'rstack',
|
|
154
|
-
'*'
|
|
155
|
-
].some((key)=>values.includes(key));
|
|
156
|
-
};
|
|
157
|
-
if (isDebug()) logger.level = 'verbose';
|
|
158
|
-
function getTime() {
|
|
159
|
-
const now = new Date();
|
|
160
|
-
const hours = String(now.getHours()).padStart(2, '0');
|
|
161
|
-
const minutes = String(now.getMinutes()).padStart(2, '0');
|
|
162
|
-
const seconds = String(now.getSeconds()).padStart(2, '0');
|
|
163
|
-
return `${hours}:${minutes}:${seconds}`;
|
|
164
|
-
}
|
|
165
|
-
logger.override({
|
|
166
|
-
debug: (message, ...args)=>{
|
|
167
|
-
if ('verbose' !== logger.level) return;
|
|
168
|
-
const time = index_js_default.gray(`${getTime()}`);
|
|
169
|
-
console.log(` ${index_js_default.green('rslib')} ${time} ${message}`, ...args);
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
function initNodeEnv() {
|
|
173
|
-
if (!process.env.NODE_ENV) {
|
|
174
|
-
const command = process.argv[2] ?? '';
|
|
175
|
-
process.env.NODE_ENV = [
|
|
176
|
-
'build'
|
|
177
|
-
].includes(command) ? 'production' : 'development';
|
|
83
|
+
function isCssModulesFile(filepath, auto) {
|
|
84
|
+
const filename = node_path.basename(filepath);
|
|
85
|
+
if (true === auto) return CSS_MODULE_REG.test(filename);
|
|
86
|
+
if (auto instanceof RegExp) return auto.test(filepath);
|
|
87
|
+
if ('function' == typeof auto) {
|
|
88
|
+
const { path, query, fragment } = parsePathQueryFragment(filepath);
|
|
89
|
+
return auto(path, query, fragment);
|
|
178
90
|
}
|
|
91
|
+
return false;
|
|
179
92
|
}
|
|
180
|
-
function
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
function toArr(any) {
|
|
187
|
-
return null == any ? [] : Array.isArray(any) ? any : [
|
|
188
|
-
any
|
|
189
|
-
];
|
|
190
|
-
}
|
|
191
|
-
function toVal(out, key, val, opts) {
|
|
192
|
-
var x, old = out[key], nxt = ~opts.string.indexOf(key) ? null == val || true === val ? '' : String(val) : 'boolean' == typeof val ? val : ~opts.boolean.indexOf(key) ? 'false' === val ? false : 'true' === val || (out._.push((x = +val, 0 * x === 0) ? x : val), !!val) : (x = +val, 0 * x === 0) ? x : val;
|
|
193
|
-
out[key] = null == old ? nxt : Array.isArray(old) ? old.concat(nxt) : [
|
|
194
|
-
old,
|
|
195
|
-
nxt
|
|
196
|
-
];
|
|
93
|
+
function isCssGlobalFile(filepath, auto) {
|
|
94
|
+
const isCss = isCssFile(filepath);
|
|
95
|
+
if (!isCss) return false;
|
|
96
|
+
const isCssModules = isCssModulesFile(filepath, auto);
|
|
97
|
+
return !isCssModules;
|
|
197
98
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
const keys = strict ? Object.keys(opts.alias) : [];
|
|
232
|
-
for(i = 0; i < len; i++){
|
|
233
|
-
arg = args[i];
|
|
234
|
-
if ('--' === arg) {
|
|
235
|
-
out._ = out._.concat(args.slice(++i));
|
|
236
|
-
break;
|
|
237
|
-
}
|
|
238
|
-
for(j = 0; j < arg.length && 45 === arg.charCodeAt(j); j++);
|
|
239
|
-
if (0 === j) out._.push(arg);
|
|
240
|
-
else if ('no-' === arg.substring(j, j + 3)) {
|
|
241
|
-
name = arg.substring(j + 3);
|
|
242
|
-
if (strict && !~keys.indexOf(name)) return opts.unknown(arg);
|
|
243
|
-
out[name] = false;
|
|
244
|
-
} else {
|
|
245
|
-
for(idx = j + 1; idx < arg.length && 61 !== arg.charCodeAt(idx); idx++);
|
|
246
|
-
name = arg.substring(j, idx);
|
|
247
|
-
val = arg.substring(++idx) || i + 1 === len || 45 === ('' + args[i + 1]).charCodeAt(0) || args[++i];
|
|
248
|
-
arr = 2 === j ? [
|
|
249
|
-
name
|
|
250
|
-
] : name;
|
|
251
|
-
for(idx = 0; idx < arr.length; idx++){
|
|
252
|
-
name = arr[idx];
|
|
253
|
-
if (strict && !~keys.indexOf(name)) return opts.unknown('-'.repeat(j) + name);
|
|
254
|
-
toVal(out, name, idx + 1 < arr.length || val, opts);
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
if (defaults) {
|
|
259
|
-
for(k in opts.default)if (void 0 === out[k]) out[k] = opts.default[k];
|
|
260
|
-
}
|
|
261
|
-
if (alibi) for(k in out){
|
|
262
|
-
arr = opts.alias[k] || [];
|
|
263
|
-
while(arr.length > 0)out[arr.shift()] = out[k];
|
|
99
|
+
const LibSvgrPatchPlugin_pluginName = 'LIB_SVGR_PATCH_PLUGIN';
|
|
100
|
+
const PUBLIC_PATH_PLACEHOLDER = '__RSLIB_SVGR_AUTO_PUBLIC_PATH__';
|
|
101
|
+
class LibSvgrPatchPlugin {
|
|
102
|
+
name = LibSvgrPatchPlugin_pluginName;
|
|
103
|
+
apply(compiler) {
|
|
104
|
+
compiler.hooks.make.tap(this.name, (compilation)=>{
|
|
105
|
+
compilation.hooks.processAssets.tap(this.name, (assets)=>{
|
|
106
|
+
const isEsm = Boolean(compilation.options.output.module);
|
|
107
|
+
const chunkAsset = Object.keys(assets).filter((name)=>/js$/.test(name));
|
|
108
|
+
for (const name of chunkAsset)compilation.updateAsset(name, (old)=>{
|
|
109
|
+
const oldSource = old.source().toString();
|
|
110
|
+
const newSource = new __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.rspack.sources.ReplaceSource(old);
|
|
111
|
+
const pattern = new RegExp(`\\(?['"]${PUBLIC_PATH_PLACEHOLDER}(.*)['"]\\)?`, 'g');
|
|
112
|
+
const matches = [
|
|
113
|
+
...oldSource.matchAll(pattern)
|
|
114
|
+
];
|
|
115
|
+
const len = matches.length;
|
|
116
|
+
if (0 === len) return old;
|
|
117
|
+
const undoPath = getUndoPath(name, compilation.outputOptions.path, true);
|
|
118
|
+
for(let i = 0; i < len; i++){
|
|
119
|
+
const match = matches[i];
|
|
120
|
+
const filename = match[1];
|
|
121
|
+
const requirePath = `${undoPath}${filename}`;
|
|
122
|
+
let replaced = '';
|
|
123
|
+
replaced = isEsm ? `__rslib_svgr_url__${i}__` : `require("${requirePath}")`;
|
|
124
|
+
newSource.replace(match.index, match.index + match[0].length - 1, replaced);
|
|
125
|
+
if (isEsm) newSource.insert(0, `import __rslib_svgr_url__${i}__ from "${requirePath}";\n`);
|
|
126
|
+
}
|
|
127
|
+
return newSource;
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
});
|
|
264
131
|
}
|
|
265
|
-
return out;
|
|
266
132
|
}
|
|
267
|
-
const
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
133
|
+
const PLUGIN_NAME = 'rsbuild:lib-asset';
|
|
134
|
+
const RSBUILD_SVGR_PLUGIN_NAME = 'rsbuild:svgr';
|
|
135
|
+
const pluginLibAsset = ({ bundle })=>({
|
|
136
|
+
name: PLUGIN_NAME,
|
|
137
|
+
pre: [
|
|
138
|
+
RSBUILD_SVGR_PLUGIN_NAME
|
|
139
|
+
],
|
|
140
|
+
setup (api) {
|
|
141
|
+
api.modifyBundlerChain((config, { CHAIN_ID })=>{
|
|
142
|
+
const svgAssetRule = config.module.rules.get(CHAIN_ID.RULE.SVG).oneOfs.get(CHAIN_ID.ONE_OF.SVG_ASSET);
|
|
143
|
+
const originalTypeOptions = svgAssetRule.get('type');
|
|
144
|
+
const originalParserOptions = svgAssetRule.get('parser');
|
|
145
|
+
const originalGeneratorOptions = svgAssetRule.get('generator');
|
|
146
|
+
const isUserSetPublicPath = 'auto' !== config.output.get('publicPath');
|
|
147
|
+
const generatorOptions = isUserSetPublicPath ? originalGeneratorOptions : {
|
|
148
|
+
...originalGeneratorOptions,
|
|
149
|
+
importMode: 'preserve'
|
|
150
|
+
};
|
|
151
|
+
const rule = config.module.rule(CHAIN_ID.RULE.SVG);
|
|
152
|
+
rule.oneOf(CHAIN_ID.ONE_OF.SVG_ASSET).generator(generatorOptions).issuer({
|
|
153
|
+
not: CSS_EXTENSIONS_PATTERN
|
|
154
|
+
});
|
|
155
|
+
rule.oneOf(`${CHAIN_ID.ONE_OF.SVG_ASSET}-for-css`).type(originalTypeOptions).parser(originalParserOptions).generator(originalGeneratorOptions).issuer(CSS_EXTENSIONS_PATTERN);
|
|
156
|
+
const ruleIds = [
|
|
157
|
+
CHAIN_ID.RULE.FONT,
|
|
158
|
+
CHAIN_ID.RULE.MEDIA,
|
|
159
|
+
CHAIN_ID.RULE.IMAGE,
|
|
160
|
+
CHAIN_ID.RULE.ADDITIONAL_ASSETS
|
|
161
|
+
];
|
|
162
|
+
for (const ruleId of ruleIds){
|
|
163
|
+
const oneOfId = `${ruleId}-asset`;
|
|
164
|
+
const assetRule = config.module.rules.get(ruleId);
|
|
165
|
+
if (!assetRule) continue;
|
|
166
|
+
const assetRuleOneOf = assetRule.oneOfs.get(oneOfId);
|
|
167
|
+
const originalTypeOptions = assetRuleOneOf.get('type');
|
|
168
|
+
const originalParserOptions = assetRuleOneOf.get('parser');
|
|
169
|
+
const originalGeneratorOptions = assetRuleOneOf.get('generator');
|
|
170
|
+
const generatorOptions = isUserSetPublicPath ? originalGeneratorOptions : {
|
|
171
|
+
...originalGeneratorOptions,
|
|
172
|
+
importMode: 'preserve'
|
|
173
|
+
};
|
|
174
|
+
const rule = config.module.rule(ruleId);
|
|
175
|
+
rule.oneOf(oneOfId).generator(generatorOptions).issuer({
|
|
176
|
+
not: CSS_EXTENSIONS_PATTERN
|
|
177
|
+
});
|
|
178
|
+
rule.oneOf(`${oneOfId}-for-css`).type(originalTypeOptions).parser(originalParserOptions).generator(originalGeneratorOptions).issuer(CSS_EXTENSIONS_PATTERN);
|
|
179
|
+
}
|
|
180
|
+
const isUsingSvgr = config.module.rule(CHAIN_ID.RULE.SVG).oneOf(CHAIN_ID.RULE.SVG).uses.has(CHAIN_ID.USE.SVGR);
|
|
181
|
+
const isUsingSvgrUrlLoader = config.module.rule(CHAIN_ID.RULE.SVG).oneOf(CHAIN_ID.ONE_OF.SVG).uses.has(CHAIN_ID.USE.URL);
|
|
182
|
+
if (isUsingSvgr && isUsingSvgrUrlLoader) {
|
|
183
|
+
const urlLoaderRule = config.module.rule(CHAIN_ID.RULE.SVG).oneOf(CHAIN_ID.ONE_OF.SVG).use(CHAIN_ID.USE.URL);
|
|
184
|
+
const originalOptions = urlLoaderRule.get('options');
|
|
185
|
+
urlLoaderRule.options({
|
|
186
|
+
...originalOptions,
|
|
187
|
+
publicPath: (url)=>`${PUBLIC_PATH_PLACEHOLDER}${url}`
|
|
188
|
+
});
|
|
189
|
+
config.plugin(LibSvgrPatchPlugin.name).use(LibSvgrPatchPlugin, []);
|
|
190
|
+
}
|
|
191
|
+
if (isUsingSvgr) if (bundle) {
|
|
192
|
+
const rule = config.module.rule(CHAIN_ID.RULE.SVG).oneOf(CHAIN_ID.ONE_OF.SVG_URL);
|
|
193
|
+
const originalGeneratorOptions = rule.get('generator');
|
|
194
|
+
const generatorOptions = isUserSetPublicPath ? originalGeneratorOptions : {
|
|
195
|
+
...originalGeneratorOptions,
|
|
196
|
+
importMode: 'preserve'
|
|
197
|
+
};
|
|
198
|
+
rule.generator(generatorOptions);
|
|
199
|
+
} else {
|
|
200
|
+
const rule = config.module.rule(CHAIN_ID.RULE.SVG).oneOf(CHAIN_ID.ONE_OF.SVG);
|
|
201
|
+
rule.issuer([]);
|
|
202
|
+
}
|
|
203
|
+
if (bundle) config.plugins.get(CHAIN_ID.PLUGIN.MINI_CSS_EXTRACT)?.tap((options)=>[
|
|
204
|
+
{
|
|
205
|
+
...options[0],
|
|
206
|
+
enforceRelative: true
|
|
207
|
+
}
|
|
208
|
+
]);
|
|
209
|
+
});
|
|
278
210
|
}
|
|
211
|
+
});
|
|
212
|
+
const composeAssetConfig = (bundle, format)=>{
|
|
213
|
+
if ('esm' === format || 'cjs' === format) {
|
|
214
|
+
if (bundle) return {
|
|
215
|
+
output: {
|
|
216
|
+
dataUriLimit: 0,
|
|
217
|
+
assetPrefix: 'auto'
|
|
218
|
+
},
|
|
219
|
+
plugins: [
|
|
220
|
+
pluginLibAsset({
|
|
221
|
+
bundle: true
|
|
222
|
+
})
|
|
223
|
+
]
|
|
224
|
+
};
|
|
279
225
|
return {
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
226
|
+
output: {
|
|
227
|
+
dataUriLimit: 0,
|
|
228
|
+
assetPrefix: 'auto'
|
|
229
|
+
},
|
|
230
|
+
plugins: [
|
|
231
|
+
pluginLibAsset({
|
|
232
|
+
bundle: false
|
|
233
|
+
})
|
|
234
|
+
]
|
|
283
235
|
};
|
|
284
|
-
};
|
|
285
|
-
let angledMatch;
|
|
286
|
-
while(angledMatch = ANGLED_BRACKET_RE_GLOBAL.exec(v))res.push(parse(angledMatch));
|
|
287
|
-
let squareMatch;
|
|
288
|
-
while(squareMatch = SQUARE_BRACKET_RE_GLOBAL.exec(v))res.push(parse(squareMatch));
|
|
289
|
-
return res;
|
|
290
|
-
};
|
|
291
|
-
const getMriOptions = (options)=>{
|
|
292
|
-
const result = {
|
|
293
|
-
alias: {},
|
|
294
|
-
boolean: []
|
|
295
|
-
};
|
|
296
|
-
for (const [index, option] of options.entries()){
|
|
297
|
-
if (option.names.length > 1) result.alias[option.names[0]] = option.names.slice(1);
|
|
298
|
-
if (option.isBoolean) if (option.negated) {
|
|
299
|
-
const hasStringTypeOption = options.some((o, i)=>i !== index && o.names.some((name)=>option.names.includes(name)) && "boolean" == typeof o.required);
|
|
300
|
-
if (!hasStringTypeOption) result.boolean.push(option.names[0]);
|
|
301
|
-
} else result.boolean.push(option.names[0]);
|
|
302
|
-
}
|
|
303
|
-
return result;
|
|
304
|
-
};
|
|
305
|
-
const findLongest = (arr)=>arr.sort((a, b)=>a.length > b.length ? -1 : 1)[0];
|
|
306
|
-
const padRight = (str, length)=>str.length >= length ? str : `${str}${" ".repeat(length - str.length)}`;
|
|
307
|
-
const camelcase = (input)=>input.replace(/([a-z])-([a-z])/g, (_, p1, p2)=>p1 + p2.toUpperCase());
|
|
308
|
-
const setDotProp = (obj, keys, val)=>{
|
|
309
|
-
let i = 0;
|
|
310
|
-
let length = keys.length;
|
|
311
|
-
let t = obj;
|
|
312
|
-
let x;
|
|
313
|
-
for(; i < length; ++i){
|
|
314
|
-
x = t[keys[i]];
|
|
315
|
-
t = t[keys[i]] = i === length - 1 ? val : null != x ? x : !~keys[i + 1].indexOf(".") && +keys[i + 1] > -1 ? [] : {};
|
|
316
236
|
}
|
|
237
|
+
return {};
|
|
317
238
|
};
|
|
318
|
-
const
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
239
|
+
const BASE_URI = 'webpack://';
|
|
240
|
+
const AUTO_PUBLIC_PATH = '__mini_css_extract_plugin_public_path_auto__';
|
|
241
|
+
const ABSOLUTE_PUBLIC_PATH = `${BASE_URI}/mini-css-extract-plugin/`;
|
|
242
|
+
const SINGLE_DOT_PATH_SEGMENT = '__mini_css_extract_plugin_single_dot_path_segment__';
|
|
243
|
+
const LibCssExtractPlugin_pluginName = 'LIB_CSS_EXTRACT_PLUGIN';
|
|
244
|
+
class LibCssExtractPlugin {
|
|
245
|
+
name = LibCssExtractPlugin_pluginName;
|
|
246
|
+
options;
|
|
247
|
+
constructor(options){
|
|
248
|
+
this.options = options ?? {};
|
|
325
249
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
const
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
if (name.startsWith("no-")) {
|
|
350
|
-
this.negated = true;
|
|
351
|
-
name = name.replace(/^no-/, "");
|
|
352
|
-
}
|
|
353
|
-
return camelcaseOptionName(name);
|
|
354
|
-
}).sort((a, b)=>a.length > b.length ? 1 : -1);
|
|
355
|
-
this.name = this.names[this.names.length - 1];
|
|
356
|
-
if (this.negated && null == this.config.default) this.config.default = true;
|
|
357
|
-
if (rawName.includes("<")) this.required = true;
|
|
358
|
-
else if (rawName.includes("[")) this.required = false;
|
|
359
|
-
else this.isBoolean = true;
|
|
250
|
+
apply(compiler) {
|
|
251
|
+
compiler.hooks.make.tap(LibCssExtractPlugin_pluginName, (compilation)=>{
|
|
252
|
+
compilation.hooks.processAssets.tap(LibCssExtractPlugin_pluginName, (assets)=>{
|
|
253
|
+
const chunkAsset = Object.keys(assets).filter((name)=>/\.css/.test(name));
|
|
254
|
+
for (const name of chunkAsset)compilation.updateAsset(name, (old)=>{
|
|
255
|
+
const oldSource = old.source().toString();
|
|
256
|
+
const replaceSource = new __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.rspack.sources.ReplaceSource(old);
|
|
257
|
+
function replace(searchValue, replaceValue) {
|
|
258
|
+
let start = oldSource.indexOf(searchValue);
|
|
259
|
+
while(-1 !== start){
|
|
260
|
+
replaceSource.replace(start, start + searchValue.length - 1, replaceValue);
|
|
261
|
+
start = oldSource.indexOf(searchValue, start + 1);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
replace(SINGLE_DOT_PATH_SEGMENT, '.');
|
|
265
|
+
const undoPath = getUndoPath(name, compilation.outputOptions.path, true);
|
|
266
|
+
replace(`${ABSOLUTE_PUBLIC_PATH}${AUTO_PUBLIC_PATH}`, undoPath);
|
|
267
|
+
replace(ABSOLUTE_PUBLIC_PATH, '');
|
|
268
|
+
replace(`${BASE_URI}/`, '');
|
|
269
|
+
return replaceSource;
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
});
|
|
360
273
|
}
|
|
361
274
|
}
|
|
362
|
-
const
|
|
363
|
-
const
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
this.aliasNames = [];
|
|
372
|
-
this.name = removeBrackets(rawName);
|
|
373
|
-
this.args = findAllBrackets(rawName);
|
|
374
|
-
this.examples = [];
|
|
375
|
-
}
|
|
376
|
-
usage(text) {
|
|
377
|
-
this.usageText = text;
|
|
378
|
-
return this;
|
|
379
|
-
}
|
|
380
|
-
allowUnknownOptions() {
|
|
381
|
-
this.config.allowUnknownOptions = true;
|
|
382
|
-
return this;
|
|
383
|
-
}
|
|
384
|
-
ignoreOptionDefaultValue() {
|
|
385
|
-
this.config.ignoreOptionDefaultValue = true;
|
|
386
|
-
return this;
|
|
387
|
-
}
|
|
388
|
-
version(version, customFlags = "-v, --version") {
|
|
389
|
-
this.versionNumber = version;
|
|
390
|
-
this.option(customFlags, "Display version number");
|
|
391
|
-
return this;
|
|
392
|
-
}
|
|
393
|
-
example(example) {
|
|
394
|
-
this.examples.push(example);
|
|
395
|
-
return this;
|
|
396
|
-
}
|
|
397
|
-
option(rawName, description, config) {
|
|
398
|
-
const option = new Option(rawName, description, config);
|
|
399
|
-
this.options.push(option);
|
|
400
|
-
return this;
|
|
401
|
-
}
|
|
402
|
-
alias(name) {
|
|
403
|
-
this.aliasNames.push(name);
|
|
404
|
-
return this;
|
|
405
|
-
}
|
|
406
|
-
action(callback) {
|
|
407
|
-
this.commandAction = callback;
|
|
408
|
-
return this;
|
|
409
|
-
}
|
|
410
|
-
isMatched(name) {
|
|
411
|
-
return this.name === name || this.aliasNames.includes(name);
|
|
412
|
-
}
|
|
413
|
-
get isDefaultCommand() {
|
|
414
|
-
return "" === this.name || this.aliasNames.includes("!");
|
|
275
|
+
const cssConfig_require = createRequire(import.meta.url);
|
|
276
|
+
const RSLIB_CSS_ENTRY_FLAG = '__rslib_css__';
|
|
277
|
+
async function cssExternalHandler(request, callback, jsExtension, auto, styleRedirectPath, styleRedirectExtension, redirectPath, issuer) {
|
|
278
|
+
if (/compiled\/css-loader\//.test(request)) return callback();
|
|
279
|
+
let resolvedRequest = request;
|
|
280
|
+
if (styleRedirectPath) {
|
|
281
|
+
const redirectedPath = await redirectPath(resolvedRequest);
|
|
282
|
+
if (void 0 === redirectedPath) return callback(void 0, request);
|
|
283
|
+
resolvedRequest = redirectedPath;
|
|
415
284
|
}
|
|
416
|
-
|
|
417
|
-
|
|
285
|
+
if (!isCssFile(resolvedRequest)) {
|
|
286
|
+
if (isCssFile(issuer)) return callback();
|
|
287
|
+
return false;
|
|
418
288
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
return
|
|
289
|
+
if (styleRedirectExtension) {
|
|
290
|
+
const isCssModulesRequest = isCssModulesFile(resolvedRequest, auto);
|
|
291
|
+
if (isCssModulesRequest) return callback(void 0, resolvedRequest.replace(/\.[^.]+$/, jsExtension));
|
|
292
|
+
return callback(void 0, resolvedRequest.replace(/\.[^.]+$/, '.css'));
|
|
422
293
|
}
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
body: ` $ ${name} ${this.usageText || this.rawName}`
|
|
434
|
-
});
|
|
435
|
-
const showCommands = (this.isGlobalCommand || this.isDefaultCommand) && commands.length > 0;
|
|
436
|
-
if (showCommands) {
|
|
437
|
-
const longestCommandName = findLongest(commands.map((command)=>command.rawName));
|
|
438
|
-
sections.push({
|
|
439
|
-
title: "Commands",
|
|
440
|
-
body: commands.map((command)=>` ${padRight(command.rawName, longestCommandName.length)} ${command.description}`).join("\n")
|
|
441
|
-
});
|
|
442
|
-
sections.push({
|
|
443
|
-
title: "For more info, run any command with the `--help` flag",
|
|
444
|
-
body: commands.map((command)=>` $ ${name}${"" === command.name ? "" : ` ${command.name}`} --help`).join("\n")
|
|
294
|
+
return callback(void 0, resolvedRequest);
|
|
295
|
+
}
|
|
296
|
+
const cssConfig_PLUGIN_NAME = 'rsbuild:lib-css';
|
|
297
|
+
const pluginLibCss = (rootDir, auto, banner, footer)=>({
|
|
298
|
+
name: cssConfig_PLUGIN_NAME,
|
|
299
|
+
setup (api) {
|
|
300
|
+
api.processAssets({
|
|
301
|
+
stage: 'additional'
|
|
302
|
+
}, ({ assets, compilation })=>{
|
|
303
|
+
for (const key of Object.keys(assets))if (key.match(RSLIB_CSS_ENTRY_FLAG)) compilation.deleteAsset(key);
|
|
445
304
|
});
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
305
|
+
api.modifyBundlerChain((config, { CHAIN_ID })=>{
|
|
306
|
+
let isUsingCssExtract = false;
|
|
307
|
+
for (const ruleId of [
|
|
308
|
+
CHAIN_ID.RULE.CSS,
|
|
309
|
+
CHAIN_ID.RULE.SASS,
|
|
310
|
+
CHAIN_ID.RULE.LESS,
|
|
311
|
+
CHAIN_ID.RULE.STYLUS
|
|
312
|
+
]){
|
|
313
|
+
const rule = config.module.rule(ruleId);
|
|
314
|
+
if (rule.uses.has(CHAIN_ID.USE.MINI_CSS_EXTRACT)) {
|
|
315
|
+
isUsingCssExtract = true;
|
|
316
|
+
rule.use(CHAIN_ID.USE.MINI_CSS_EXTRACT).loader(cssConfig_require.resolve('./libCssExtractLoader.js')).options({
|
|
317
|
+
rootDir,
|
|
318
|
+
auto,
|
|
319
|
+
banner,
|
|
320
|
+
footer
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (isUsingCssExtract) {
|
|
325
|
+
const cssExtract = CHAIN_ID.PLUGIN.MINI_CSS_EXTRACT;
|
|
326
|
+
config.plugins.delete(cssExtract);
|
|
327
|
+
config.plugin(LibCssExtractPlugin.name).use(LibCssExtractPlugin);
|
|
328
|
+
}
|
|
457
329
|
});
|
|
458
330
|
}
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
}
|
|
470
|
-
outputVersion() {
|
|
471
|
-
const { name } = this.cli;
|
|
472
|
-
const { versionNumber } = this.cli.globalCommand;
|
|
473
|
-
if (versionNumber) console.log(`${name}/${versionNumber} ${platformInfo}`);
|
|
474
|
-
}
|
|
475
|
-
checkRequiredArgs() {
|
|
476
|
-
const minimalArgsCount = this.args.filter((arg)=>arg.required).length;
|
|
477
|
-
if (this.cli.args.length < minimalArgsCount) throw new CACError(`missing required args for command \`${this.rawName}\``);
|
|
478
|
-
}
|
|
479
|
-
checkUnknownOptions() {
|
|
480
|
-
const { options, globalCommand } = this.cli;
|
|
481
|
-
if (!this.config.allowUnknownOptions) {
|
|
482
|
-
for (const name of Object.keys(options))if ("--" !== name && !this.hasOption(name) && !globalCommand.hasOption(name)) throw new CACError(`Unknown option \`${name.length > 1 ? `--${name}` : `-${name}`}\``);
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
checkOptionValue() {
|
|
486
|
-
const { options: parsedOptions, globalCommand } = this.cli;
|
|
487
|
-
const options = [
|
|
488
|
-
...globalCommand.options,
|
|
489
|
-
...this.options
|
|
490
|
-
];
|
|
491
|
-
for (const option of options){
|
|
492
|
-
const value = parsedOptions[option.name.split(".")[0]];
|
|
493
|
-
if (option.required) {
|
|
494
|
-
const hasNegated = options.some((o)=>o.negated && o.names.includes(option.name));
|
|
495
|
-
if (true === value || false === value && !hasNegated) throw new CACError(`option \`${option.rawName}\` value is missing`);
|
|
331
|
+
});
|
|
332
|
+
const composeCssConfig = (rootDir, auto, bundle = true, banner, footer)=>{
|
|
333
|
+
if (bundle || null === rootDir) return {};
|
|
334
|
+
return {
|
|
335
|
+
plugins: [
|
|
336
|
+
pluginLibCss(rootDir, auto, banner, footer)
|
|
337
|
+
],
|
|
338
|
+
tools: {
|
|
339
|
+
cssLoader: {
|
|
340
|
+
import: false
|
|
496
341
|
}
|
|
497
342
|
}
|
|
343
|
+
};
|
|
344
|
+
};
|
|
345
|
+
const EntryChunkPlugin_require = createRequire(import.meta.url);
|
|
346
|
+
const EntryChunkPlugin_PLUGIN_NAME = 'rsbuild:lib-entry-chunk';
|
|
347
|
+
const LOADER_NAME = 'rsbuild:lib-entry-module';
|
|
348
|
+
const IMPORT_META_URL_SHIM = `const __rslib_import_meta_url__ = /*#__PURE__*/ (function () {
|
|
349
|
+
return typeof document === 'undefined'
|
|
350
|
+
? new (require('url'.replace('', '')).URL)('file:' + __filename).href
|
|
351
|
+
: (document.currentScript && document.currentScript.src) ||
|
|
352
|
+
new URL('main.js', document.baseURI).href;
|
|
353
|
+
})();
|
|
354
|
+
`;
|
|
355
|
+
const matchFirstLine = (source, regex)=>{
|
|
356
|
+
const lineBreakPos = source.match(/(\r\n|\n)/);
|
|
357
|
+
const firstLineContent = source.slice(0, lineBreakPos?.index);
|
|
358
|
+
const matched = regex.exec(firstLineContent);
|
|
359
|
+
if (!matched) return false;
|
|
360
|
+
return matched[0];
|
|
361
|
+
};
|
|
362
|
+
class EntryChunkPlugin {
|
|
363
|
+
reactDirectives = {};
|
|
364
|
+
shimsInjectedAssets = new Set();
|
|
365
|
+
shebangChmod = 493;
|
|
366
|
+
shebangEntries = {};
|
|
367
|
+
shebangInjectedAssets = new Set();
|
|
368
|
+
enabledImportMetaUrlShim;
|
|
369
|
+
contextToWatch = null;
|
|
370
|
+
constructor({ enabledImportMetaUrlShim = true, contextToWatch }){
|
|
371
|
+
this.enabledImportMetaUrlShim = enabledImportMetaUrlShim;
|
|
372
|
+
this.contextToWatch = contextToWatch;
|
|
498
373
|
}
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
this.commands = [];
|
|
511
|
-
this.rawArgs = [];
|
|
512
|
-
this.args = [];
|
|
513
|
-
this.options = {};
|
|
514
|
-
this.globalCommand = new GlobalCommand(this);
|
|
515
|
-
this.globalCommand.usage("<command> [options]");
|
|
516
|
-
}
|
|
517
|
-
usage(text) {
|
|
518
|
-
this.globalCommand.usage(text);
|
|
519
|
-
return this;
|
|
520
|
-
}
|
|
521
|
-
command(rawName, description, config) {
|
|
522
|
-
const command = new Command(rawName, description || "", config, this);
|
|
523
|
-
command.globalCommand = this.globalCommand;
|
|
524
|
-
this.commands.push(command);
|
|
525
|
-
return command;
|
|
526
|
-
}
|
|
527
|
-
option(rawName, description, config) {
|
|
528
|
-
this.globalCommand.option(rawName, description, config);
|
|
529
|
-
return this;
|
|
530
|
-
}
|
|
531
|
-
help(callback) {
|
|
532
|
-
this.globalCommand.option("-h, --help", "Display this message");
|
|
533
|
-
this.globalCommand.helpCallback = callback;
|
|
534
|
-
this.showHelpOnExit = true;
|
|
535
|
-
return this;
|
|
536
|
-
}
|
|
537
|
-
version(version, customFlags = "-v, --version") {
|
|
538
|
-
this.globalCommand.version(version, customFlags);
|
|
539
|
-
this.showVersionOnExit = true;
|
|
540
|
-
return this;
|
|
541
|
-
}
|
|
542
|
-
example(example) {
|
|
543
|
-
this.globalCommand.example(example);
|
|
544
|
-
return this;
|
|
545
|
-
}
|
|
546
|
-
outputHelp() {
|
|
547
|
-
if (this.matchedCommand) this.matchedCommand.outputHelp();
|
|
548
|
-
else this.globalCommand.outputHelp();
|
|
549
|
-
}
|
|
550
|
-
outputVersion() {
|
|
551
|
-
this.globalCommand.outputVersion();
|
|
552
|
-
}
|
|
553
|
-
setParsedInfo({ args, options }, matchedCommand, matchedCommandName) {
|
|
554
|
-
this.args = args;
|
|
555
|
-
this.options = options;
|
|
556
|
-
if (matchedCommand) this.matchedCommand = matchedCommand;
|
|
557
|
-
if (matchedCommandName) this.matchedCommandName = matchedCommandName;
|
|
558
|
-
return this;
|
|
559
|
-
}
|
|
560
|
-
unsetMatchedCommand() {
|
|
561
|
-
this.matchedCommand = void 0;
|
|
562
|
-
this.matchedCommandName = void 0;
|
|
563
|
-
}
|
|
564
|
-
parse(argv = processArgs, { run = true } = {}) {
|
|
565
|
-
this.rawArgs = argv;
|
|
566
|
-
if (!this.name) this.name = argv[1] ? getFileName(argv[1]) : "cli";
|
|
567
|
-
let shouldParse = true;
|
|
568
|
-
for (const command of this.commands){
|
|
569
|
-
const parsed = this.mri(argv.slice(2), command);
|
|
570
|
-
const commandName = parsed.args[0];
|
|
571
|
-
if (command.isMatched(commandName)) {
|
|
572
|
-
shouldParse = false;
|
|
573
|
-
const parsedInfo = __assign(__assign({}, parsed), {
|
|
574
|
-
args: parsed.args.slice(1)
|
|
575
|
-
});
|
|
576
|
-
this.setParsedInfo(parsedInfo, command, commandName);
|
|
577
|
-
this.emit(`command:${commandName}`, command);
|
|
374
|
+
apply(compiler) {
|
|
375
|
+
compiler.hooks.afterCompile.tap(EntryChunkPlugin_PLUGIN_NAME, (compilation)=>{
|
|
376
|
+
if (null === this.contextToWatch) return;
|
|
377
|
+
const contextDep = compilation.contextDependencies;
|
|
378
|
+
if (!contextDep.has(this.contextToWatch)) contextDep.add(this.contextToWatch);
|
|
379
|
+
});
|
|
380
|
+
compiler.hooks.make.tap(EntryChunkPlugin_PLUGIN_NAME, (compilation)=>{
|
|
381
|
+
const entries = {};
|
|
382
|
+
for (const [key, value] of compilation.entries){
|
|
383
|
+
const firstDep = value.dependencies[0];
|
|
384
|
+
if (firstDep?.request) entries[key] = firstDep.request;
|
|
578
385
|
}
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
const
|
|
584
|
-
|
|
585
|
-
|
|
386
|
+
for(const name in entries){
|
|
387
|
+
const first = entries[name];
|
|
388
|
+
if (!first) continue;
|
|
389
|
+
const filename = first.split('?')[0];
|
|
390
|
+
const isJs = JS_EXTENSIONS_PATTERN.test(filename);
|
|
391
|
+
if (!isJs) continue;
|
|
392
|
+
const content = compiler.inputFileSystem.readFileSync(filename, {
|
|
393
|
+
encoding: 'utf-8'
|
|
394
|
+
});
|
|
395
|
+
if (content.startsWith(SHEBANG_PREFIX)) {
|
|
396
|
+
const shebangMatch = matchFirstLine(content, SHEBANG_REGEX);
|
|
397
|
+
if (shebangMatch) this.shebangEntries[name] = shebangMatch;
|
|
398
|
+
}
|
|
399
|
+
const reactDirective = matchFirstLine(content, REACT_DIRECTIVE_REGEX);
|
|
400
|
+
if (reactDirective) this.reactDirectives[name] = reactDirective;
|
|
586
401
|
}
|
|
587
|
-
}
|
|
588
|
-
if (shouldParse) {
|
|
589
|
-
const parsed = this.mri(argv.slice(2));
|
|
590
|
-
this.setParsedInfo(parsed);
|
|
591
|
-
}
|
|
592
|
-
if (this.options.help && this.showHelpOnExit) {
|
|
593
|
-
this.outputHelp();
|
|
594
|
-
run = false;
|
|
595
|
-
this.unsetMatchedCommand();
|
|
596
|
-
}
|
|
597
|
-
if (this.options.version && this.showVersionOnExit && null == this.matchedCommandName) {
|
|
598
|
-
this.outputVersion();
|
|
599
|
-
run = false;
|
|
600
|
-
this.unsetMatchedCommand();
|
|
601
|
-
}
|
|
602
|
-
const parsedArgv = {
|
|
603
|
-
args: this.args,
|
|
604
|
-
options: this.options
|
|
605
|
-
};
|
|
606
|
-
if (run) this.runMatchedCommand();
|
|
607
|
-
if (!this.matchedCommand && this.args[0]) this.emit("command:*");
|
|
608
|
-
return parsedArgv;
|
|
609
|
-
}
|
|
610
|
-
mri(argv, command) {
|
|
611
|
-
const cliOptions = [
|
|
612
|
-
...this.globalCommand.options,
|
|
613
|
-
...command ? command.options : []
|
|
614
|
-
];
|
|
615
|
-
const mriOptions = getMriOptions(cliOptions);
|
|
616
|
-
let argsAfterDoubleDashes = [];
|
|
617
|
-
const doubleDashesIndex = argv.indexOf("--");
|
|
618
|
-
if (doubleDashesIndex > -1) {
|
|
619
|
-
argsAfterDoubleDashes = argv.slice(doubleDashesIndex + 1);
|
|
620
|
-
argv = argv.slice(0, doubleDashesIndex);
|
|
621
|
-
}
|
|
622
|
-
let parsed = mri2(argv, mriOptions);
|
|
623
|
-
parsed = Object.keys(parsed).reduce((res, name)=>__assign(__assign({}, res), {
|
|
624
|
-
[camelcaseOptionName(name)]: parsed[name]
|
|
625
|
-
}), {
|
|
626
|
-
_: []
|
|
627
402
|
});
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
403
|
+
compiler.hooks.make.tap(EntryChunkPlugin_PLUGIN_NAME, (compilation)=>{
|
|
404
|
+
compilation.hooks.chunkAsset.tap(EntryChunkPlugin_PLUGIN_NAME, (chunk, filename)=>{
|
|
405
|
+
const isJs = JS_EXTENSIONS_PATTERN.test(filename);
|
|
406
|
+
if (!isJs) return;
|
|
407
|
+
const name = chunk.name;
|
|
408
|
+
if (!name) return;
|
|
409
|
+
this.shimsInjectedAssets.add(filename);
|
|
410
|
+
const shebangEntry = this.shebangEntries[name];
|
|
411
|
+
if (shebangEntry) this.shebangEntries[filename] = shebangEntry;
|
|
412
|
+
const reactDirective = this.reactDirectives[name];
|
|
413
|
+
if (reactDirective) this.reactDirectives[filename] = reactDirective;
|
|
414
|
+
});
|
|
415
|
+
});
|
|
416
|
+
compiler.hooks.make.tap(EntryChunkPlugin_PLUGIN_NAME, (compilation)=>{
|
|
417
|
+
compilation.hooks.processAssets.tap(EntryChunkPlugin_PLUGIN_NAME, (assets)=>{
|
|
418
|
+
if (!this.enabledImportMetaUrlShim) return;
|
|
419
|
+
const chunkAsset = Object.keys(assets).filter((name)=>JS_EXTENSIONS_PATTERN.test(name) && this.shimsInjectedAssets.has(name));
|
|
420
|
+
for (const name of chunkAsset)compilation.updateAsset(name, (old)=>{
|
|
421
|
+
const oldSource = old.source().toString();
|
|
422
|
+
const replaceSource = new __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.rspack.sources.ReplaceSource(old);
|
|
423
|
+
if (oldSource.startsWith('use strict;') || oldSource.startsWith('"use strict";')) replaceSource.replace(0, 11, `"use strict";\n${IMPORT_META_URL_SHIM}`);
|
|
424
|
+
else replaceSource.insert(0, IMPORT_META_URL_SHIM);
|
|
425
|
+
return replaceSource;
|
|
426
|
+
});
|
|
427
|
+
});
|
|
428
|
+
compilation.hooks.processAssets.tap({
|
|
429
|
+
name: EntryChunkPlugin_PLUGIN_NAME,
|
|
430
|
+
stage: __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.rspack.Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING - 1
|
|
431
|
+
}, (assets)=>{
|
|
432
|
+
const chunkAsset = Object.keys(assets);
|
|
433
|
+
for (const name of chunkAsset){
|
|
434
|
+
const shebangValue = this.shebangEntries[name];
|
|
435
|
+
const reactDirectiveValue = this.reactDirectives[name];
|
|
436
|
+
if (shebangValue || reactDirectiveValue) compilation.updateAsset(name, (old)=>{
|
|
437
|
+
const replaceSource = new __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.rspack.sources.ReplaceSource(old);
|
|
438
|
+
if (shebangValue) {
|
|
439
|
+
replaceSource.insert(0, `${shebangValue}\n`);
|
|
440
|
+
this.shebangInjectedAssets.add(name);
|
|
441
|
+
}
|
|
442
|
+
if (reactDirectiveValue) replaceSource.insert(0, `${reactDirectiveValue}\n`);
|
|
443
|
+
return replaceSource;
|
|
444
|
+
});
|
|
641
445
|
}
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
setDotProp(options, keys, parsed[key]);
|
|
647
|
-
setByType(options, transforms);
|
|
648
|
-
}
|
|
649
|
-
return {
|
|
650
|
-
args,
|
|
651
|
-
options
|
|
652
|
-
};
|
|
653
|
-
}
|
|
654
|
-
runMatchedCommand() {
|
|
655
|
-
const { args, options, matchedCommand: command } = this;
|
|
656
|
-
if (!command || !command.commandAction) return;
|
|
657
|
-
command.checkUnknownOptions();
|
|
658
|
-
command.checkOptionValue();
|
|
659
|
-
command.checkRequiredArgs();
|
|
660
|
-
const actionArgs = [];
|
|
661
|
-
command.args.forEach((arg, index)=>{
|
|
662
|
-
if (arg.variadic) actionArgs.push(args.slice(index));
|
|
663
|
-
else actionArgs.push(args[index]);
|
|
446
|
+
});
|
|
447
|
+
});
|
|
448
|
+
compiler.hooks.assetEmitted.tap(EntryChunkPlugin_PLUGIN_NAME, (file, { targetPath })=>{
|
|
449
|
+
if (this.shebangInjectedAssets.has(file)) chmodSync(targetPath, this.shebangChmod);
|
|
664
450
|
});
|
|
665
|
-
actionArgs.push(options);
|
|
666
|
-
return command.commandAction.apply(this, actionArgs);
|
|
667
451
|
}
|
|
668
452
|
}
|
|
669
|
-
const
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
'.ts',
|
|
675
|
-
'.mjs',
|
|
676
|
-
'.mts',
|
|
677
|
-
'.cjs',
|
|
678
|
-
'.cts'
|
|
679
|
-
];
|
|
680
|
-
const SWC_HELPERS = '@swc/helpers';
|
|
681
|
-
const SHEBANG_PREFIX = '#!';
|
|
682
|
-
const SHEBANG_REGEX = /#!.*[\s\n\r]*$/;
|
|
683
|
-
const REACT_DIRECTIVE_REGEX = /^['"]use (client|server)['"](;?)[\s\n\r]*$/;
|
|
684
|
-
const DTS_EXTENSIONS = [
|
|
685
|
-
'd.ts',
|
|
686
|
-
'd.mts',
|
|
687
|
-
'd.cts'
|
|
688
|
-
];
|
|
689
|
-
const JS_EXTENSIONS = [
|
|
690
|
-
'js',
|
|
691
|
-
'mjs',
|
|
692
|
-
'jsx',
|
|
693
|
-
'(?<!\\.d\\.)ts',
|
|
694
|
-
'(?<!\\.d\\.)mts',
|
|
695
|
-
'(?<!\\.d\\.)cts',
|
|
696
|
-
'tsx',
|
|
697
|
-
'cjs',
|
|
698
|
-
'cjsx',
|
|
699
|
-
'mjsx',
|
|
700
|
-
'mtsx',
|
|
701
|
-
'ctsx'
|
|
702
|
-
];
|
|
703
|
-
const CSS_EXTENSIONS = [
|
|
704
|
-
'css',
|
|
705
|
-
'sass',
|
|
706
|
-
'scss',
|
|
707
|
-
'less',
|
|
708
|
-
'styl',
|
|
709
|
-
'stylus'
|
|
710
|
-
];
|
|
711
|
-
const JS_EXTENSIONS_PATTERN = new RegExp(`\\.(${JS_EXTENSIONS.join('|')})$`);
|
|
712
|
-
const CSS_EXTENSIONS_PATTERN = new RegExp(`\\.(${CSS_EXTENSIONS.join('|')})$`);
|
|
713
|
-
const DTS_EXTENSIONS_PATTERN = new RegExp(`\\.(${DTS_EXTENSIONS.join('|')})$`);
|
|
714
|
-
function getUndoPath(filename, outputPathArg, enforceRelative) {
|
|
715
|
-
let depth = -1;
|
|
716
|
-
let append = '';
|
|
717
|
-
let outputPath = outputPathArg.replace(/[\\/]$/, '');
|
|
718
|
-
for (const part of filename.split(/[/\\]+/))if ('..' === part) if (depth > -1) depth--;
|
|
719
|
-
else {
|
|
720
|
-
const i = outputPath.lastIndexOf('/');
|
|
721
|
-
const j = outputPath.lastIndexOf('\\');
|
|
722
|
-
const pos = i < 0 ? j : j < 0 ? i : Math.max(i, j);
|
|
723
|
-
if (pos < 0) return `${outputPath}/`;
|
|
724
|
-
append = `${outputPath.slice(pos + 1)}/${append}`;
|
|
725
|
-
outputPath = outputPath.slice(0, pos);
|
|
726
|
-
}
|
|
727
|
-
else if ('.' !== part) depth++;
|
|
728
|
-
return depth > 0 ? `${'../'.repeat(depth)}${append}` : enforceRelative ? `./${append}` : append;
|
|
729
|
-
}
|
|
730
|
-
const LibSvgrPatchPlugin_pluginName = 'LIB_SVGR_PATCH_PLUGIN';
|
|
731
|
-
const PUBLIC_PATH_PLACEHOLDER = '__RSLIB_SVGR_AUTO_PUBLIC_PATH__';
|
|
732
|
-
class LibSvgrPatchPlugin {
|
|
733
|
-
name = LibSvgrPatchPlugin_pluginName;
|
|
734
|
-
apply(compiler) {
|
|
735
|
-
compiler.hooks.make.tap(this.name, (compilation)=>{
|
|
736
|
-
compilation.hooks.processAssets.tap(this.name, (assets)=>{
|
|
737
|
-
const isEsm = Boolean(compilation.options.output.module);
|
|
738
|
-
const chunkAsset = Object.keys(assets).filter((name)=>/js$/.test(name));
|
|
739
|
-
for (const name of chunkAsset)compilation.updateAsset(name, (old)=>{
|
|
740
|
-
const oldSource = old.source().toString();
|
|
741
|
-
const newSource = new __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.rspack.sources.ReplaceSource(old);
|
|
742
|
-
const pattern = new RegExp(`\\(?['"]${PUBLIC_PATH_PLACEHOLDER}(.*)['"]\\)?`, 'g');
|
|
743
|
-
const matches = [
|
|
744
|
-
...oldSource.matchAll(pattern)
|
|
745
|
-
];
|
|
746
|
-
const len = matches.length;
|
|
747
|
-
if (0 === len) return old;
|
|
748
|
-
const undoPath = getUndoPath(name, compilation.outputOptions.path, true);
|
|
749
|
-
for(let i = 0; i < len; i++){
|
|
750
|
-
const match = matches[i];
|
|
751
|
-
const filename = match[1];
|
|
752
|
-
const requirePath = `${undoPath}${filename}`;
|
|
753
|
-
let replaced = '';
|
|
754
|
-
replaced = isEsm ? `__rslib_svgr_url__${i}__` : `require("${requirePath}")`;
|
|
755
|
-
newSource.replace(match.index, match.index + match[0].length - 1, replaced);
|
|
756
|
-
if (isEsm) newSource.insert(0, `import __rslib_svgr_url__${i}__ from "${requirePath}";\n`);
|
|
757
|
-
}
|
|
758
|
-
return newSource;
|
|
759
|
-
});
|
|
453
|
+
const entryModuleLoaderRsbuildPlugin = ()=>({
|
|
454
|
+
name: EntryChunkPlugin_PLUGIN_NAME,
|
|
455
|
+
setup (api) {
|
|
456
|
+
api.modifyBundlerChain((config, { CHAIN_ID })=>{
|
|
457
|
+
config.module.rule(`Rslib:${CHAIN_ID.RULE.JS}-entry-loader`).test(config.module.rule(CHAIN_ID.RULE.JS).get('test')).issuer(/^$/).use(LOADER_NAME).loader(EntryChunkPlugin_require.resolve('./entryModuleLoader.js'));
|
|
760
458
|
});
|
|
761
|
-
}
|
|
762
|
-
}
|
|
763
|
-
}
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
const pluginLibAsset = ({ bundle })=>({
|
|
767
|
-
name: PLUGIN_NAME,
|
|
768
|
-
pre: [
|
|
769
|
-
RSBUILD_SVGR_PLUGIN_NAME
|
|
459
|
+
}
|
|
460
|
+
});
|
|
461
|
+
const composeEntryChunkConfig = ({ enabledImportMetaUrlShim, contextToWatch = null })=>({
|
|
462
|
+
plugins: [
|
|
463
|
+
entryModuleLoaderRsbuildPlugin()
|
|
770
464
|
],
|
|
465
|
+
tools: {
|
|
466
|
+
rspack: {
|
|
467
|
+
plugins: [
|
|
468
|
+
new EntryChunkPlugin({
|
|
469
|
+
enabledImportMetaUrlShim,
|
|
470
|
+
contextToWatch
|
|
471
|
+
})
|
|
472
|
+
]
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
});
|
|
476
|
+
const pluginCjsImportMetaUrlShim = ()=>({
|
|
477
|
+
name: 'rsbuild:cjs-import-meta-url-shim',
|
|
771
478
|
setup (api) {
|
|
772
|
-
api.
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
const originalGeneratorOptions = svgAssetRule.get('generator');
|
|
777
|
-
const isUserSetPublicPath = 'auto' !== config.output.get('publicPath');
|
|
778
|
-
const generatorOptions = isUserSetPublicPath ? originalGeneratorOptions : {
|
|
779
|
-
...originalGeneratorOptions,
|
|
780
|
-
importMode: 'preserve'
|
|
479
|
+
api.modifyEnvironmentConfig((config)=>{
|
|
480
|
+
config.source.define = {
|
|
481
|
+
...config.source.define,
|
|
482
|
+
'import.meta.url': '__rslib_import_meta_url__'
|
|
781
483
|
};
|
|
782
|
-
const rule = config.module.rule(CHAIN_ID.RULE.SVG);
|
|
783
|
-
rule.oneOf(CHAIN_ID.ONE_OF.SVG_ASSET).generator(generatorOptions).issuer({
|
|
784
|
-
not: CSS_EXTENSIONS_PATTERN
|
|
785
|
-
});
|
|
786
|
-
rule.oneOf(`${CHAIN_ID.ONE_OF.SVG_ASSET}-for-css`).type(originalTypeOptions).parser(originalParserOptions).generator(originalGeneratorOptions).issuer(CSS_EXTENSIONS_PATTERN);
|
|
787
|
-
const ruleIds = [
|
|
788
|
-
CHAIN_ID.RULE.FONT,
|
|
789
|
-
CHAIN_ID.RULE.MEDIA,
|
|
790
|
-
CHAIN_ID.RULE.IMAGE,
|
|
791
|
-
CHAIN_ID.RULE.ADDITIONAL_ASSETS
|
|
792
|
-
];
|
|
793
|
-
for (const ruleId of ruleIds){
|
|
794
|
-
const oneOfId = `${ruleId}-asset`;
|
|
795
|
-
const assetRule = config.module.rules.get(ruleId);
|
|
796
|
-
if (!assetRule) continue;
|
|
797
|
-
const assetRuleOneOf = assetRule.oneOfs.get(oneOfId);
|
|
798
|
-
const originalTypeOptions = assetRuleOneOf.get('type');
|
|
799
|
-
const originalParserOptions = assetRuleOneOf.get('parser');
|
|
800
|
-
const originalGeneratorOptions = assetRuleOneOf.get('generator');
|
|
801
|
-
const generatorOptions = isUserSetPublicPath ? originalGeneratorOptions : {
|
|
802
|
-
...originalGeneratorOptions,
|
|
803
|
-
importMode: 'preserve'
|
|
804
|
-
};
|
|
805
|
-
const rule = config.module.rule(ruleId);
|
|
806
|
-
rule.oneOf(oneOfId).generator(generatorOptions).issuer({
|
|
807
|
-
not: CSS_EXTENSIONS_PATTERN
|
|
808
|
-
});
|
|
809
|
-
rule.oneOf(`${oneOfId}-for-css`).type(originalTypeOptions).parser(originalParserOptions).generator(originalGeneratorOptions).issuer(CSS_EXTENSIONS_PATTERN);
|
|
810
|
-
}
|
|
811
|
-
const isUsingSvgr = config.module.rule(CHAIN_ID.RULE.SVG).oneOf(CHAIN_ID.RULE.SVG).uses.has(CHAIN_ID.USE.SVGR);
|
|
812
|
-
const isUsingSvgrUrlLoader = config.module.rule(CHAIN_ID.RULE.SVG).oneOf(CHAIN_ID.ONE_OF.SVG).uses.has(CHAIN_ID.USE.URL);
|
|
813
|
-
if (isUsingSvgr && isUsingSvgrUrlLoader) {
|
|
814
|
-
const urlLoaderRule = config.module.rule(CHAIN_ID.RULE.SVG).oneOf(CHAIN_ID.ONE_OF.SVG).use(CHAIN_ID.USE.URL);
|
|
815
|
-
const originalOptions = urlLoaderRule.get('options');
|
|
816
|
-
urlLoaderRule.options({
|
|
817
|
-
...originalOptions,
|
|
818
|
-
publicPath: (url)=>`${PUBLIC_PATH_PLACEHOLDER}${url}`
|
|
819
|
-
});
|
|
820
|
-
config.plugin(LibSvgrPatchPlugin.name).use(LibSvgrPatchPlugin, []);
|
|
821
|
-
}
|
|
822
|
-
if (isUsingSvgr) if (bundle) {
|
|
823
|
-
const rule = config.module.rule(CHAIN_ID.RULE.SVG).oneOf(CHAIN_ID.ONE_OF.SVG_URL);
|
|
824
|
-
const originalGeneratorOptions = rule.get('generator');
|
|
825
|
-
const generatorOptions = isUserSetPublicPath ? originalGeneratorOptions : {
|
|
826
|
-
...originalGeneratorOptions,
|
|
827
|
-
importMode: 'preserve'
|
|
828
|
-
};
|
|
829
|
-
rule.generator(generatorOptions);
|
|
830
|
-
} else {
|
|
831
|
-
const rule = config.module.rule(CHAIN_ID.RULE.SVG).oneOf(CHAIN_ID.ONE_OF.SVG);
|
|
832
|
-
rule.issuer([]);
|
|
833
|
-
}
|
|
834
|
-
if (bundle) config.plugins.get(CHAIN_ID.PLUGIN.MINI_CSS_EXTRACT)?.tap((options)=>[
|
|
835
|
-
{
|
|
836
|
-
...options[0],
|
|
837
|
-
enforceRelative: true
|
|
838
|
-
}
|
|
839
|
-
]);
|
|
840
484
|
});
|
|
841
485
|
}
|
|
842
486
|
});
|
|
843
|
-
const
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
487
|
+
const requireShim = `// Rslib ESM shims
|
|
488
|
+
import __rslib_shim_module__ from 'module';
|
|
489
|
+
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(import.meta.url);
|
|
490
|
+
`;
|
|
491
|
+
const pluginEsmRequireShim = ()=>({
|
|
492
|
+
name: 'rsbuild:esm-require-shim',
|
|
493
|
+
setup (api) {
|
|
494
|
+
api.modifyRspackConfig((config)=>{
|
|
495
|
+
config.plugins ??= [];
|
|
496
|
+
config.plugins.push(new __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.rspack.BannerPlugin({
|
|
497
|
+
banner: requireShim,
|
|
498
|
+
stage: __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE - 1,
|
|
499
|
+
raw: true,
|
|
500
|
+
include: /\.(js|mjs)$/
|
|
501
|
+
}));
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
const isDebug = ()=>{
|
|
506
|
+
if (!process.env.DEBUG) return false;
|
|
507
|
+
const values = process.env.DEBUG.toLocaleLowerCase().split(',');
|
|
508
|
+
return [
|
|
509
|
+
'rslib',
|
|
510
|
+
'rs*',
|
|
511
|
+
'rstack',
|
|
512
|
+
'*'
|
|
513
|
+
].some((key)=>values.includes(key));
|
|
869
514
|
};
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
const
|
|
873
|
-
const
|
|
874
|
-
const
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
options;
|
|
878
|
-
constructor(options){
|
|
879
|
-
this.options = options ?? {};
|
|
880
|
-
}
|
|
881
|
-
apply(compiler) {
|
|
882
|
-
compiler.hooks.make.tap(LibCssExtractPlugin_pluginName, (compilation)=>{
|
|
883
|
-
compilation.hooks.processAssets.tap(LibCssExtractPlugin_pluginName, (assets)=>{
|
|
884
|
-
const chunkAsset = Object.keys(assets).filter((name)=>/\.css/.test(name));
|
|
885
|
-
for (const name of chunkAsset)compilation.updateAsset(name, (old)=>{
|
|
886
|
-
const oldSource = old.source().toString();
|
|
887
|
-
const replaceSource = new __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.rspack.sources.ReplaceSource(old);
|
|
888
|
-
function replace(searchValue, replaceValue) {
|
|
889
|
-
let start = oldSource.indexOf(searchValue);
|
|
890
|
-
while(-1 !== start){
|
|
891
|
-
replaceSource.replace(start, start + searchValue.length - 1, replaceValue);
|
|
892
|
-
start = oldSource.indexOf(searchValue, start + 1);
|
|
893
|
-
}
|
|
894
|
-
}
|
|
895
|
-
replace(SINGLE_DOT_PATH_SEGMENT, '.');
|
|
896
|
-
const undoPath = getUndoPath(name, compilation.outputOptions.path, true);
|
|
897
|
-
replace(`${ABSOLUTE_PUBLIC_PATH}${AUTO_PUBLIC_PATH}`, undoPath);
|
|
898
|
-
replace(ABSOLUTE_PUBLIC_PATH, '');
|
|
899
|
-
replace(`${BASE_URI}/`, '');
|
|
900
|
-
return replaceSource;
|
|
901
|
-
});
|
|
902
|
-
});
|
|
903
|
-
});
|
|
904
|
-
}
|
|
905
|
-
}
|
|
906
|
-
const cssConfig_require = createRequire(import.meta.url);
|
|
907
|
-
const RSLIB_CSS_ENTRY_FLAG = '__rslib_css__';
|
|
908
|
-
function isCssFile(filepath) {
|
|
909
|
-
return CSS_EXTENSIONS_PATTERN.test(filepath);
|
|
515
|
+
if (isDebug()) logger.level = 'verbose';
|
|
516
|
+
function getTime() {
|
|
517
|
+
const now = new Date();
|
|
518
|
+
const hours = String(now.getHours()).padStart(2, '0');
|
|
519
|
+
const minutes = String(now.getMinutes()).padStart(2, '0');
|
|
520
|
+
const seconds = String(now.getSeconds()).padStart(2, '0');
|
|
521
|
+
return `${hours}:${minutes}:${seconds}`;
|
|
910
522
|
}
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
523
|
+
logger.override({
|
|
524
|
+
debug: (message, ...args)=>{
|
|
525
|
+
if ('verbose' !== logger.level) return;
|
|
526
|
+
const time = picocolors.gray(`${getTime()}`);
|
|
527
|
+
console.log(` ${picocolors.green('rslib')} ${time} ${message}`, ...args);
|
|
528
|
+
}
|
|
529
|
+
});
|
|
530
|
+
const getDefaultExtension = (options)=>{
|
|
531
|
+
const { format, pkgJson, autoExtension } = options;
|
|
532
|
+
let jsExtension = '.js';
|
|
533
|
+
let dtsExtension = '.d.ts';
|
|
534
|
+
if (!autoExtension) return {
|
|
535
|
+
jsExtension,
|
|
536
|
+
dtsExtension
|
|
537
|
+
};
|
|
538
|
+
if (!pkgJson) {
|
|
539
|
+
logger.warn('The `autoExtension` configuration will not be applied due to read package.json failed');
|
|
540
|
+
return {
|
|
541
|
+
jsExtension,
|
|
542
|
+
dtsExtension
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
const isModule = 'module' === pkgJson.type;
|
|
546
|
+
if (isModule && 'cjs' === format) {
|
|
547
|
+
jsExtension = '.cjs';
|
|
548
|
+
dtsExtension = '.d.cts';
|
|
549
|
+
}
|
|
550
|
+
if (!isModule && 'esm' === format) {
|
|
551
|
+
jsExtension = '.mjs';
|
|
552
|
+
dtsExtension = '.d.mts';
|
|
553
|
+
}
|
|
915
554
|
return {
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
555
|
+
jsExtension,
|
|
556
|
+
dtsExtension,
|
|
557
|
+
isModule
|
|
919
558
|
};
|
|
920
|
-
}
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
559
|
+
};
|
|
560
|
+
const nodeBuiltInModules = [
|
|
561
|
+
'assert',
|
|
562
|
+
'assert/strict',
|
|
563
|
+
'async_hooks',
|
|
564
|
+
'buffer',
|
|
565
|
+
'child_process',
|
|
566
|
+
'cluster',
|
|
567
|
+
'console',
|
|
568
|
+
'constants',
|
|
569
|
+
'crypto',
|
|
570
|
+
'dgram',
|
|
571
|
+
'diagnostics_channel',
|
|
572
|
+
'dns',
|
|
573
|
+
'dns/promises',
|
|
574
|
+
'domain',
|
|
575
|
+
'events',
|
|
576
|
+
'fs',
|
|
577
|
+
'fs/promises',
|
|
578
|
+
'http',
|
|
579
|
+
'http2',
|
|
580
|
+
'https',
|
|
581
|
+
'inspector',
|
|
582
|
+
'inspector/promises',
|
|
583
|
+
'module',
|
|
584
|
+
'net',
|
|
585
|
+
'os',
|
|
586
|
+
'path',
|
|
587
|
+
'path/posix',
|
|
588
|
+
'path/win32',
|
|
589
|
+
'perf_hooks',
|
|
590
|
+
'process',
|
|
591
|
+
'punycode',
|
|
592
|
+
'querystring',
|
|
593
|
+
'readline',
|
|
594
|
+
'readline/promises',
|
|
595
|
+
'repl',
|
|
596
|
+
'stream',
|
|
597
|
+
'stream/consumers',
|
|
598
|
+
'stream/promises',
|
|
599
|
+
'stream/web',
|
|
600
|
+
'string_decoder',
|
|
601
|
+
'sys',
|
|
602
|
+
'timers',
|
|
603
|
+
'timers/promises',
|
|
604
|
+
'tls',
|
|
605
|
+
'trace_events',
|
|
606
|
+
'tty',
|
|
607
|
+
'url',
|
|
608
|
+
'util',
|
|
609
|
+
'util/types',
|
|
610
|
+
'v8',
|
|
611
|
+
'vm',
|
|
612
|
+
'wasi',
|
|
613
|
+
'worker_threads',
|
|
614
|
+
'zlib',
|
|
615
|
+
/^node:/,
|
|
616
|
+
'pnpapi'
|
|
617
|
+
];
|
|
618
|
+
async function calcLongestCommonPath(absPaths) {
|
|
619
|
+
if (0 === absPaths.length) return null;
|
|
620
|
+
const sep = node_path.posix.sep;
|
|
621
|
+
const splitPaths = absPaths.map((p)=>p.split(sep));
|
|
622
|
+
let lcaFragments = splitPaths[0];
|
|
623
|
+
for(let i = 1; i < splitPaths.length; i++){
|
|
624
|
+
const currentPath = splitPaths[i];
|
|
625
|
+
const minLength = Math.min(lcaFragments.length, currentPath.length);
|
|
626
|
+
let j = 0;
|
|
627
|
+
while(j < minLength && lcaFragments[j] === currentPath[j])j++;
|
|
628
|
+
lcaFragments = lcaFragments.slice(0, j);
|
|
928
629
|
}
|
|
929
|
-
|
|
630
|
+
let lca = lcaFragments.length > 0 ? lcaFragments.join(sep) : sep;
|
|
631
|
+
const stats = await node_fs_promises.stat(lca);
|
|
632
|
+
if (stats?.isFile()) lca = node_path.dirname(lca);
|
|
633
|
+
return lca;
|
|
930
634
|
}
|
|
931
|
-
function
|
|
932
|
-
|
|
933
|
-
if (!isCss) return false;
|
|
934
|
-
const isCssModules = cssConfig_isCssModulesFile(filepath, auto);
|
|
935
|
-
return !isCssModules;
|
|
635
|
+
function getAbsolutePath(base, filepath) {
|
|
636
|
+
return isAbsolute(filepath) ? filepath : join(base, filepath);
|
|
936
637
|
}
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
638
|
+
const readPackageJson = (rootPath)=>{
|
|
639
|
+
const pkgJsonPath = node_path.join(rootPath, './package.json');
|
|
640
|
+
if (!node_fs.existsSync(pkgJsonPath)) return void logger.warn(`The \`package.json\` file does not exist in the ${rootPath} directory`);
|
|
641
|
+
try {
|
|
642
|
+
return JSON.parse(node_fs.readFileSync(pkgJsonPath, 'utf8'));
|
|
643
|
+
} catch (_err) {
|
|
644
|
+
logger.warn(`Failed to parse ${pkgJsonPath}, it might not be valid JSON`);
|
|
645
|
+
return;
|
|
943
646
|
}
|
|
944
|
-
|
|
945
|
-
|
|
647
|
+
};
|
|
648
|
+
const isObject = (obj)=>'[object Object]' === Object.prototype.toString.call(obj);
|
|
649
|
+
const isEmptyObject = (obj)=>0 === Object.keys(obj).length;
|
|
650
|
+
function pick(obj, keys) {
|
|
651
|
+
return keys.reduce((ret, key)=>{
|
|
652
|
+
if (void 0 !== obj[key]) ret[key] = obj[key];
|
|
653
|
+
return ret;
|
|
654
|
+
}, {});
|
|
655
|
+
}
|
|
656
|
+
function omit(obj, keysObj) {
|
|
657
|
+
return Object.keys(obj).reduce((ret, key)=>{
|
|
658
|
+
if (true !== keysObj[key]) ret[key] = obj[key];
|
|
659
|
+
return ret;
|
|
660
|
+
}, {});
|
|
661
|
+
}
|
|
662
|
+
function isPluginIncluded(pluginName, plugins) {
|
|
663
|
+
return Boolean(plugins?.some((plugin)=>{
|
|
664
|
+
if (Array.isArray(plugin)) return isPluginIncluded(pluginName, plugin);
|
|
665
|
+
if ('object' == typeof plugin && null !== plugin && 'name' in plugin) return plugin.name === pluginName;
|
|
946
666
|
return false;
|
|
667
|
+
}));
|
|
668
|
+
}
|
|
669
|
+
function checkMFPlugin(config, sharedPlugins) {
|
|
670
|
+
if ('mf' !== config.format) return true;
|
|
671
|
+
const added = isPluginIncluded('rsbuild:module-federation-enhanced', [
|
|
672
|
+
...sharedPlugins || [],
|
|
673
|
+
...config.plugins || []
|
|
674
|
+
]);
|
|
675
|
+
if (!added) {
|
|
676
|
+
logger.warn(`${picocolors.green('format: "mf"')} should be used with ${picocolors.blue('@module-federation/rsbuild-plugin')}", consider installing and adding it to plugins. Check the documentation (https://module-federation.io/guide/basic/rsbuild.html#rslib-module) to get started with "mf" output.`);
|
|
677
|
+
process.exit(1);
|
|
947
678
|
}
|
|
948
|
-
|
|
949
|
-
const isCssModulesRequest = cssConfig_isCssModulesFile(resolvedRequest, auto);
|
|
950
|
-
if (isCssModulesRequest) return callback(void 0, resolvedRequest.replace(/\.[^.]+$/, jsExtension));
|
|
951
|
-
return callback(void 0, resolvedRequest.replace(/\.[^.]+$/, '.css'));
|
|
952
|
-
}
|
|
953
|
-
return callback(void 0, resolvedRequest);
|
|
679
|
+
return added;
|
|
954
680
|
}
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
for (const key of Object.keys(assets))if (key.match(RSLIB_CSS_ENTRY_FLAG)) compilation.deleteAsset(key);
|
|
963
|
-
});
|
|
964
|
-
api.modifyBundlerChain((config, { CHAIN_ID })=>{
|
|
965
|
-
let isUsingCssExtract = false;
|
|
966
|
-
for (const ruleId of [
|
|
967
|
-
CHAIN_ID.RULE.CSS,
|
|
968
|
-
CHAIN_ID.RULE.SASS,
|
|
969
|
-
CHAIN_ID.RULE.LESS,
|
|
970
|
-
CHAIN_ID.RULE.STYLUS
|
|
971
|
-
]){
|
|
972
|
-
const rule = config.module.rule(ruleId);
|
|
973
|
-
if (rule.uses.has(CHAIN_ID.USE.MINI_CSS_EXTRACT)) {
|
|
974
|
-
isUsingCssExtract = true;
|
|
975
|
-
rule.use(CHAIN_ID.USE.MINI_CSS_EXTRACT).loader(cssConfig_require.resolve('./libCssExtractLoader.js')).options({
|
|
976
|
-
rootDir,
|
|
977
|
-
auto,
|
|
978
|
-
banner,
|
|
979
|
-
footer
|
|
980
|
-
});
|
|
981
|
-
}
|
|
982
|
-
}
|
|
983
|
-
if (isUsingCssExtract) {
|
|
984
|
-
const cssExtract = CHAIN_ID.PLUGIN.MINI_CSS_EXTRACT;
|
|
985
|
-
config.plugins.delete(cssExtract);
|
|
986
|
-
config.plugin(LibCssExtractPlugin.name).use(LibCssExtractPlugin);
|
|
987
|
-
}
|
|
988
|
-
});
|
|
989
|
-
}
|
|
990
|
-
});
|
|
991
|
-
const composeCssConfig = (rootDir, auto, bundle = true, banner, footer)=>{
|
|
992
|
-
if (bundle || null === rootDir) return {};
|
|
993
|
-
return {
|
|
994
|
-
plugins: [
|
|
995
|
-
pluginLibCss(rootDir, auto, banner, footer)
|
|
996
|
-
],
|
|
997
|
-
tools: {
|
|
998
|
-
cssLoader: {
|
|
999
|
-
import: false
|
|
1000
|
-
}
|
|
1001
|
-
}
|
|
681
|
+
function debounce(func, wait) {
|
|
682
|
+
let timeoutId = null;
|
|
683
|
+
return (...args)=>{
|
|
684
|
+
if (null !== timeoutId) clearTimeout(timeoutId);
|
|
685
|
+
timeoutId = setTimeout(()=>{
|
|
686
|
+
func(...args);
|
|
687
|
+
}, wait);
|
|
1002
688
|
};
|
|
1003
|
-
};
|
|
1004
|
-
const EntryChunkPlugin_require = createRequire(import.meta.url);
|
|
1005
|
-
const EntryChunkPlugin_PLUGIN_NAME = 'rsbuild:lib-entry-chunk';
|
|
1006
|
-
const EntryChunkPlugin_LOADER_NAME = 'rsbuild:lib-entry-module';
|
|
1007
|
-
const IMPORT_META_URL_SHIM = `const __rslib_import_meta_url__ = /*#__PURE__*/ (function () {
|
|
1008
|
-
return typeof document === 'undefined'
|
|
1009
|
-
? new (require('url'.replace('', '')).URL)('file:' + __filename).href
|
|
1010
|
-
: (document.currentScript && document.currentScript.src) ||
|
|
1011
|
-
new URL('main.js', document.baseURI).href;
|
|
1012
|
-
})();
|
|
1013
|
-
`;
|
|
1014
|
-
const matchFirstLine = (source, regex)=>{
|
|
1015
|
-
const lineBreakPos = source.match(/(\r\n|\n)/);
|
|
1016
|
-
const firstLineContent = source.slice(0, lineBreakPos?.index);
|
|
1017
|
-
const matched = regex.exec(firstLineContent);
|
|
1018
|
-
if (!matched) return false;
|
|
1019
|
-
return matched[0];
|
|
1020
|
-
};
|
|
1021
|
-
class EntryChunkPlugin {
|
|
1022
|
-
reactDirectives = {};
|
|
1023
|
-
shimsInjectedAssets = new Set();
|
|
1024
|
-
shebangChmod = 493;
|
|
1025
|
-
shebangEntries = {};
|
|
1026
|
-
shebangInjectedAssets = new Set();
|
|
1027
|
-
enabledImportMetaUrlShim;
|
|
1028
|
-
contextToWatch = null;
|
|
1029
|
-
constructor({ enabledImportMetaUrlShim = true, contextToWatch }){
|
|
1030
|
-
this.enabledImportMetaUrlShim = enabledImportMetaUrlShim;
|
|
1031
|
-
this.contextToWatch = contextToWatch;
|
|
1032
|
-
}
|
|
1033
|
-
apply(compiler) {
|
|
1034
|
-
compiler.hooks.afterCompile.tap(EntryChunkPlugin_PLUGIN_NAME, (compilation)=>{
|
|
1035
|
-
if (null === this.contextToWatch) return;
|
|
1036
|
-
const contextDep = compilation.contextDependencies;
|
|
1037
|
-
if (!contextDep.has(this.contextToWatch)) contextDep.add(this.contextToWatch);
|
|
1038
|
-
});
|
|
1039
|
-
compiler.hooks.make.tap(EntryChunkPlugin_PLUGIN_NAME, (compilation)=>{
|
|
1040
|
-
const entries = {};
|
|
1041
|
-
for (const [key, value] of compilation.entries){
|
|
1042
|
-
const firstDep = value.dependencies[0];
|
|
1043
|
-
if (firstDep?.request) entries[key] = firstDep.request;
|
|
1044
|
-
}
|
|
1045
|
-
for(const name in entries){
|
|
1046
|
-
const first = entries[name];
|
|
1047
|
-
if (!first) continue;
|
|
1048
|
-
const filename = first.split('?')[0];
|
|
1049
|
-
const isJs = JS_EXTENSIONS_PATTERN.test(filename);
|
|
1050
|
-
if (!isJs) continue;
|
|
1051
|
-
const content = compiler.inputFileSystem.readFileSync(filename, {
|
|
1052
|
-
encoding: 'utf-8'
|
|
1053
|
-
});
|
|
1054
|
-
if (content.startsWith(SHEBANG_PREFIX)) {
|
|
1055
|
-
const shebangMatch = matchFirstLine(content, SHEBANG_REGEX);
|
|
1056
|
-
if (shebangMatch) this.shebangEntries[name] = shebangMatch;
|
|
1057
|
-
}
|
|
1058
|
-
const reactDirective = matchFirstLine(content, REACT_DIRECTIVE_REGEX);
|
|
1059
|
-
if (reactDirective) this.reactDirectives[name] = reactDirective;
|
|
1060
|
-
}
|
|
1061
|
-
});
|
|
1062
|
-
compiler.hooks.make.tap(EntryChunkPlugin_PLUGIN_NAME, (compilation)=>{
|
|
1063
|
-
compilation.hooks.chunkAsset.tap(EntryChunkPlugin_PLUGIN_NAME, (chunk, filename)=>{
|
|
1064
|
-
const isJs = JS_EXTENSIONS_PATTERN.test(filename);
|
|
1065
|
-
if (!isJs) return;
|
|
1066
|
-
const name = chunk.name;
|
|
1067
|
-
if (!name) return;
|
|
1068
|
-
this.shimsInjectedAssets.add(filename);
|
|
1069
|
-
const shebangEntry = this.shebangEntries[name];
|
|
1070
|
-
if (shebangEntry) this.shebangEntries[filename] = shebangEntry;
|
|
1071
|
-
const reactDirective = this.reactDirectives[name];
|
|
1072
|
-
if (reactDirective) this.reactDirectives[filename] = reactDirective;
|
|
1073
|
-
});
|
|
1074
|
-
});
|
|
1075
|
-
compiler.hooks.make.tap(EntryChunkPlugin_PLUGIN_NAME, (compilation)=>{
|
|
1076
|
-
compilation.hooks.processAssets.tap(EntryChunkPlugin_PLUGIN_NAME, (assets)=>{
|
|
1077
|
-
if (!this.enabledImportMetaUrlShim) return;
|
|
1078
|
-
const chunkAsset = Object.keys(assets).filter((name)=>JS_EXTENSIONS_PATTERN.test(name) && this.shimsInjectedAssets.has(name));
|
|
1079
|
-
for (const name of chunkAsset)compilation.updateAsset(name, (old)=>{
|
|
1080
|
-
const oldSource = old.source().toString();
|
|
1081
|
-
const replaceSource = new __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.rspack.sources.ReplaceSource(old);
|
|
1082
|
-
if (oldSource.startsWith('use strict;') || oldSource.startsWith('"use strict";')) replaceSource.replace(0, 11, `"use strict";\n${IMPORT_META_URL_SHIM}`);
|
|
1083
|
-
else replaceSource.insert(0, IMPORT_META_URL_SHIM);
|
|
1084
|
-
return replaceSource;
|
|
1085
|
-
});
|
|
1086
|
-
});
|
|
1087
|
-
compilation.hooks.processAssets.tap({
|
|
1088
|
-
name: EntryChunkPlugin_PLUGIN_NAME,
|
|
1089
|
-
stage: __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.rspack.Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING - 1
|
|
1090
|
-
}, (assets)=>{
|
|
1091
|
-
const chunkAsset = Object.keys(assets);
|
|
1092
|
-
for (const name of chunkAsset){
|
|
1093
|
-
const shebangValue = this.shebangEntries[name];
|
|
1094
|
-
const reactDirectiveValue = this.reactDirectives[name];
|
|
1095
|
-
if (shebangValue || reactDirectiveValue) compilation.updateAsset(name, (old)=>{
|
|
1096
|
-
const replaceSource = new __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.rspack.sources.ReplaceSource(old);
|
|
1097
|
-
if (shebangValue) {
|
|
1098
|
-
replaceSource.insert(0, `${shebangValue}\n`);
|
|
1099
|
-
this.shebangInjectedAssets.add(name);
|
|
1100
|
-
}
|
|
1101
|
-
if (reactDirectiveValue) replaceSource.insert(0, `${reactDirectiveValue}\n`);
|
|
1102
|
-
return replaceSource;
|
|
1103
|
-
});
|
|
1104
|
-
}
|
|
1105
|
-
});
|
|
1106
|
-
});
|
|
1107
|
-
compiler.hooks.assetEmitted.tap(EntryChunkPlugin_PLUGIN_NAME, (file, { targetPath })=>{
|
|
1108
|
-
if (this.shebangInjectedAssets.has(file)) chmodSync(targetPath, this.shebangChmod);
|
|
1109
|
-
});
|
|
1110
|
-
}
|
|
1111
689
|
}
|
|
1112
|
-
const
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
}
|
|
1119
|
-
});
|
|
1120
|
-
const composeEntryChunkConfig = ({ enabledImportMetaUrlShim, contextToWatch = null })=>({
|
|
1121
|
-
plugins: [
|
|
1122
|
-
entryModuleLoaderRsbuildPlugin()
|
|
1123
|
-
],
|
|
1124
|
-
tools: {
|
|
1125
|
-
rspack: {
|
|
1126
|
-
plugins: [
|
|
1127
|
-
new EntryChunkPlugin({
|
|
1128
|
-
enabledImportMetaUrlShim,
|
|
1129
|
-
contextToWatch
|
|
1130
|
-
})
|
|
1131
|
-
]
|
|
1132
|
-
}
|
|
1133
|
-
}
|
|
1134
|
-
});
|
|
1135
|
-
const pluginCjsImportMetaUrlShim = ()=>({
|
|
1136
|
-
name: 'rsbuild:cjs-import-meta-url-shim',
|
|
1137
|
-
setup (api) {
|
|
1138
|
-
api.modifyEnvironmentConfig((config)=>{
|
|
1139
|
-
config.source.define = {
|
|
1140
|
-
...config.source.define,
|
|
1141
|
-
'import.meta.url': '__rslib_import_meta_url__'
|
|
1142
|
-
};
|
|
1143
|
-
});
|
|
1144
|
-
}
|
|
1145
|
-
});
|
|
1146
|
-
const requireShim = `// Rslib ESM shims
|
|
1147
|
-
import __rslib_shim_module__ from 'module';
|
|
1148
|
-
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(import.meta.url);
|
|
1149
|
-
`;
|
|
1150
|
-
const pluginEsmRequireShim = ()=>({
|
|
1151
|
-
name: 'rsbuild:esm-require-shim',
|
|
1152
|
-
setup (api) {
|
|
1153
|
-
api.modifyRspackConfig((config)=>{
|
|
1154
|
-
config.plugins ??= [];
|
|
1155
|
-
config.plugins.push(new __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.rspack.BannerPlugin({
|
|
1156
|
-
banner: requireShim,
|
|
1157
|
-
stage: __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE - 1,
|
|
1158
|
-
raw: true,
|
|
1159
|
-
include: /\.(js|mjs)$/
|
|
1160
|
-
}));
|
|
1161
|
-
});
|
|
1162
|
-
}
|
|
1163
|
-
});
|
|
1164
|
-
const getDefaultExtension = (options)=>{
|
|
1165
|
-
const { format, pkgJson, autoExtension } = options;
|
|
1166
|
-
let jsExtension = '.js';
|
|
1167
|
-
let dtsExtension = '.d.ts';
|
|
1168
|
-
if (!autoExtension) return {
|
|
1169
|
-
jsExtension,
|
|
1170
|
-
dtsExtension
|
|
1171
|
-
};
|
|
1172
|
-
if (!pkgJson) {
|
|
1173
|
-
logger.warn('The `autoExtension` configuration will not be applied due to read package.json failed');
|
|
1174
|
-
return {
|
|
1175
|
-
jsExtension,
|
|
1176
|
-
dtsExtension
|
|
1177
|
-
};
|
|
1178
|
-
}
|
|
1179
|
-
const isModule = 'module' === pkgJson.type;
|
|
1180
|
-
if (isModule && 'cjs' === format) {
|
|
1181
|
-
jsExtension = '.cjs';
|
|
1182
|
-
dtsExtension = '.d.cts';
|
|
1183
|
-
}
|
|
1184
|
-
if (!isModule && 'esm' === format) {
|
|
1185
|
-
jsExtension = '.mjs';
|
|
1186
|
-
dtsExtension = '.d.mts';
|
|
1187
|
-
}
|
|
1188
|
-
return {
|
|
1189
|
-
jsExtension,
|
|
1190
|
-
dtsExtension,
|
|
1191
|
-
isModule
|
|
1192
|
-
};
|
|
1193
|
-
};
|
|
690
|
+
const isTTY = (type = 'stdout')=>('stdin' === type ? process.stdin.isTTY : process.stdout.isTTY) && !process.env.CI;
|
|
691
|
+
const isIntermediateOutputFormat = (format)=>'cjs' === format || 'esm' === format;
|
|
692
|
+
const windowsSlashRegex = /\\/g;
|
|
693
|
+
function normalizeSlash(p) {
|
|
694
|
+
return p.replace(windowsSlashRegex, '/');
|
|
695
|
+
}
|
|
1194
696
|
const LATEST_TARGET_VERSIONS = {
|
|
1195
697
|
node: [
|
|
1196
698
|
'last 1 node versions'
|
|
@@ -1355,8 +857,8 @@ function transformSyntaxToBrowserslist(syntax, target) {
|
|
|
1355
857
|
if (Array.isArray(syntax)) return syntax.flatMap(handleSyntaxItem);
|
|
1356
858
|
return handleSyntaxItem(syntax);
|
|
1357
859
|
}
|
|
1358
|
-
const POSIX_SEP_RE = new RegExp('\\' +
|
|
1359
|
-
const NATIVE_SEP_RE = new RegExp('\\' +
|
|
860
|
+
const POSIX_SEP_RE = new RegExp('\\' + node_path.posix.sep, 'g');
|
|
861
|
+
const NATIVE_SEP_RE = new RegExp('\\' + node_path.sep, 'g');
|
|
1360
862
|
const PATTERN_REGEX_CACHE = new Map();
|
|
1361
863
|
const GLOB_ALL_PATTERN = "**/*";
|
|
1362
864
|
const TS_EXTENSIONS = [
|
|
@@ -1374,7 +876,7 @@ const util_JS_EXTENSIONS = [
|
|
|
1374
876
|
const TSJS_EXTENSIONS = TS_EXTENSIONS.concat(util_JS_EXTENSIONS);
|
|
1375
877
|
const TS_EXTENSIONS_RE_GROUP = `\\.(?:${TS_EXTENSIONS.map((ext)=>ext.substring(1)).join('|')})`;
|
|
1376
878
|
const TSJS_EXTENSIONS_RE_GROUP = `\\.(?:${TSJS_EXTENSIONS.map((ext)=>ext.substring(1)).join('|')})`;
|
|
1377
|
-
const IS_POSIX =
|
|
879
|
+
const IS_POSIX = node_path.posix.sep === node_path.sep;
|
|
1378
880
|
function util_makePromise() {
|
|
1379
881
|
let resolve, reject;
|
|
1380
882
|
const promise = new Promise((res, rej)=>{
|
|
@@ -1388,8 +890,8 @@ function util_makePromise() {
|
|
|
1388
890
|
};
|
|
1389
891
|
}
|
|
1390
892
|
async function util_resolveTSConfigJson(filename, cache) {
|
|
1391
|
-
if ('.json' !==
|
|
1392
|
-
const tsconfig =
|
|
893
|
+
if ('.json' !== node_path.extname(filename)) return;
|
|
894
|
+
const tsconfig = node_path.resolve(filename);
|
|
1393
895
|
if (cache && (cache.hasParseResult(tsconfig) || cache.hasParseResult(filename))) return tsconfig;
|
|
1394
896
|
return promises.stat(tsconfig).then((stat)=>{
|
|
1395
897
|
if (stat.isFile() || stat.isFIFO()) return tsconfig;
|
|
@@ -1397,13 +899,13 @@ async function util_resolveTSConfigJson(filename, cache) {
|
|
|
1397
899
|
});
|
|
1398
900
|
}
|
|
1399
901
|
const util_isInNodeModules = IS_POSIX ? (dir)=>dir.includes('/node_modules/') : (dir)=>dir.match(/[/\\]node_modules[/\\]/);
|
|
1400
|
-
const posix2native = IS_POSIX ? (filename)=>filename : (filename)=>filename.replace(POSIX_SEP_RE,
|
|
1401
|
-
const util_native2posix = IS_POSIX ? (filename)=>filename : (filename)=>filename.replace(NATIVE_SEP_RE,
|
|
1402
|
-
const resolve2posix = IS_POSIX ? (dir, filename)=>dir ?
|
|
902
|
+
const posix2native = IS_POSIX ? (filename)=>filename : (filename)=>filename.replace(POSIX_SEP_RE, node_path.sep);
|
|
903
|
+
const util_native2posix = IS_POSIX ? (filename)=>filename : (filename)=>filename.replace(NATIVE_SEP_RE, node_path.posix.sep);
|
|
904
|
+
const resolve2posix = IS_POSIX ? (dir, filename)=>dir ? node_path.resolve(dir, filename) : node_path.resolve(filename) : (dir, filename)=>util_native2posix(dir ? node_path.resolve(posix2native(dir), posix2native(filename)) : node_path.resolve(posix2native(filename)));
|
|
1403
905
|
function util_resolveReferencedTSConfigFiles(result, options) {
|
|
1404
|
-
const dir =
|
|
906
|
+
const dir = node_path.dirname(result.tsconfigFile);
|
|
1405
907
|
return result.tsconfig.references.map((ref)=>{
|
|
1406
|
-
const refPath = ref.path.endsWith('.json') ? ref.path :
|
|
908
|
+
const refPath = ref.path.endsWith('.json') ? ref.path : node_path.join(ref.path, options?.configName ?? 'tsconfig.json');
|
|
1407
909
|
return resolve2posix(dir, refPath);
|
|
1408
910
|
});
|
|
1409
911
|
}
|
|
@@ -1417,7 +919,7 @@ function util_resolveSolutionTSConfig(filename, result) {
|
|
|
1417
919
|
return result;
|
|
1418
920
|
}
|
|
1419
921
|
function util_isIncluded(filename, result) {
|
|
1420
|
-
const dir = util_native2posix(
|
|
922
|
+
const dir = util_native2posix(node_path.dirname(result.tsconfigFile));
|
|
1421
923
|
const files = (result.tsconfig.files || []).map((file)=>resolve2posix(dir, file));
|
|
1422
924
|
const absoluteFilename = resolve2posix(null, filename);
|
|
1423
925
|
if (files.includes(filename)) return true;
|
|
@@ -1505,16 +1007,16 @@ function pattern2regex(resolvedPattern, allowJs) {
|
|
|
1505
1007
|
return new RegExp(regexStr);
|
|
1506
1008
|
}
|
|
1507
1009
|
function util_replaceTokens(result) {
|
|
1508
|
-
if (result.tsconfig) result.tsconfig = JSON.parse(JSON.stringify(result.tsconfig).replaceAll(/"\${configDir}/g, `"${util_native2posix(
|
|
1010
|
+
if (result.tsconfig) result.tsconfig = JSON.parse(JSON.stringify(result.tsconfig).replaceAll(/"\${configDir}/g, `"${util_native2posix(node_path.dirname(result.tsconfigFile))}`));
|
|
1509
1011
|
}
|
|
1510
1012
|
async function find(filename, options) {
|
|
1511
|
-
let dir =
|
|
1013
|
+
let dir = node_path.dirname(node_path.resolve(filename));
|
|
1512
1014
|
if (options?.ignoreNodeModules && util_isInNodeModules(dir)) return null;
|
|
1513
1015
|
const cache = options?.cache;
|
|
1514
1016
|
const configName = options?.configName ?? 'tsconfig.json';
|
|
1515
1017
|
if (cache?.hasConfigPath(dir, configName)) return cache.getConfigPath(dir, configName);
|
|
1516
1018
|
const { promise, resolve, reject } = util_makePromise();
|
|
1517
|
-
if (options?.root && !
|
|
1019
|
+
if (options?.root && !node_path.isAbsolute(options.root)) options.root = node_path.resolve(options.root);
|
|
1518
1020
|
findUp(dir, {
|
|
1519
1021
|
promise,
|
|
1520
1022
|
resolve,
|
|
@@ -1535,13 +1037,13 @@ function findUp(dir, { resolve, reject, promise }, options) {
|
|
|
1535
1037
|
if (cached?.then) cached.then(resolve).catch(reject);
|
|
1536
1038
|
else resolve(cached);
|
|
1537
1039
|
} else cache.setConfigPath(dir, promise, configName);
|
|
1538
|
-
const tsconfig =
|
|
1539
|
-
|
|
1040
|
+
const tsconfig = node_path.join(dir, options?.configName ?? 'tsconfig.json');
|
|
1041
|
+
node_fs.stat(tsconfig, (err, stats)=>{
|
|
1540
1042
|
if (stats && (stats.isFile() || stats.isFIFO())) resolve(tsconfig);
|
|
1541
1043
|
else if (err?.code !== 'ENOENT') reject(err);
|
|
1542
1044
|
else {
|
|
1543
1045
|
let parent;
|
|
1544
|
-
if (root === dir || (parent =
|
|
1046
|
+
if (root === dir || (parent = node_path.dirname(dir)) === dir) resolve(null);
|
|
1545
1047
|
else findUp(parent, {
|
|
1546
1048
|
promise,
|
|
1547
1049
|
resolve,
|
|
@@ -1550,7 +1052,7 @@ function findUp(dir, { resolve, reject, promise }, options) {
|
|
|
1550
1052
|
}
|
|
1551
1053
|
});
|
|
1552
1054
|
}
|
|
1553
|
-
|
|
1055
|
+
node_path.sep;
|
|
1554
1056
|
function toJson(tsconfigJson) {
|
|
1555
1057
|
const stripped = stripDanglingComma(stripJsonComments(stripBom(tsconfigJson)));
|
|
1556
1058
|
if ('' === stripped.trim()) return '{}';
|
|
@@ -1696,7 +1198,7 @@ async function parseFile(tsconfigFile, cache, skipCache) {
|
|
|
1696
1198
|
applyDefaults(parsed, tsconfigFile);
|
|
1697
1199
|
return {
|
|
1698
1200
|
tsconfigFile,
|
|
1699
|
-
tsconfig: normalizeTSConfig(parsed,
|
|
1201
|
+
tsconfig: normalizeTSConfig(parsed, node_path.dirname(tsconfigFile))
|
|
1700
1202
|
};
|
|
1701
1203
|
}).catch((e)=>{
|
|
1702
1204
|
throw new TSConfckParseError(`parsing ${tsconfigFile} failed: ${e}`, 'PARSE_FILE', tsconfigFile, e);
|
|
@@ -1706,7 +1208,7 @@ async function parseFile(tsconfigFile, cache, skipCache) {
|
|
|
1706
1208
|
}
|
|
1707
1209
|
function normalizeTSConfig(tsconfig, dir) {
|
|
1708
1210
|
const baseUrl = tsconfig.compilerOptions?.baseUrl;
|
|
1709
|
-
if (baseUrl && !baseUrl.startsWith('${') && !
|
|
1211
|
+
if (baseUrl && !baseUrl.startsWith('${') && !node_path.isAbsolute(baseUrl)) tsconfig.compilerOptions.baseUrl = resolve2posix(dir, baseUrl);
|
|
1710
1212
|
return tsconfig;
|
|
1711
1213
|
}
|
|
1712
1214
|
async function parseReferences(result, options) {
|
|
@@ -1769,7 +1271,7 @@ function resolveExtends(extended, from) {
|
|
|
1769
1271
|
} catch (e) {
|
|
1770
1272
|
error = e;
|
|
1771
1273
|
}
|
|
1772
|
-
if ('.' !== extended[0] && !
|
|
1274
|
+
if ('.' !== extended[0] && !node_path.isAbsolute(extended)) try {
|
|
1773
1275
|
return req.resolve(`${extended}/tsconfig.json`);
|
|
1774
1276
|
} catch (e) {
|
|
1775
1277
|
error = e;
|
|
@@ -1789,7 +1291,7 @@ const EXTENDABLE_KEYS = [
|
|
|
1789
1291
|
function extendTSConfig(extending, extended) {
|
|
1790
1292
|
const extendingConfig = extending.tsconfig;
|
|
1791
1293
|
const extendedConfig = extended.tsconfig;
|
|
1792
|
-
const relativePath = util_native2posix(
|
|
1294
|
+
const relativePath = util_native2posix(node_path.relative(node_path.dirname(extending.tsconfigFile), node_path.dirname(extended.tsconfigFile)));
|
|
1793
1295
|
for (const key of Object.keys(extendedConfig).filter((key)=>EXTENDABLE_KEYS.includes(key)))if ('compilerOptions' === key) {
|
|
1794
1296
|
if (!extendingConfig.compilerOptions) extendingConfig.compilerOptions = {};
|
|
1795
1297
|
for (const option of Object.keys(extendedConfig.compilerOptions))if (!Object.prototype.hasOwnProperty.call(extendingConfig.compilerOptions, option)) extendingConfig.compilerOptions[option] = rebaseRelative(option, extendedConfig.compilerOptions[option], relativePath);
|
|
@@ -1818,8 +1320,8 @@ function rebaseRelative(key, value, prependPath) {
|
|
|
1818
1320
|
return rebasePath(value, prependPath);
|
|
1819
1321
|
}
|
|
1820
1322
|
function rebasePath(value, prependPath) {
|
|
1821
|
-
if (
|
|
1822
|
-
return
|
|
1323
|
+
if (node_path.isAbsolute(value) || value.startsWith('${configDir}')) return value;
|
|
1324
|
+
return node_path.posix.normalize(node_path.posix.join(prependPath, value));
|
|
1823
1325
|
}
|
|
1824
1326
|
class TSConfckParseError extends Error {
|
|
1825
1327
|
code;
|
|
@@ -1848,7 +1350,7 @@ const DEFAULT_JSCONFIG_COMPILER_OPTIONS = {
|
|
|
1848
1350
|
noEmit: true
|
|
1849
1351
|
};
|
|
1850
1352
|
function isJSConfig(configFileName) {
|
|
1851
|
-
return 'jsconfig.json' ===
|
|
1353
|
+
return 'jsconfig.json' === node_path.basename(configFileName);
|
|
1852
1354
|
}
|
|
1853
1355
|
async function loadTsconfig(root, tsconfigPath = 'tsconfig.json') {
|
|
1854
1356
|
const tsconfigFileName = await find(join(root, tsconfigPath), {
|
|
@@ -1864,12 +1366,12 @@ async function loadTsconfig(root, tsconfigPath = 'tsconfig.json') {
|
|
|
1864
1366
|
function defineConfig(config) {
|
|
1865
1367
|
return config;
|
|
1866
1368
|
}
|
|
1867
|
-
const findConfig = (basePath)=>DEFAULT_CONFIG_EXTENSIONS.map((ext)=>basePath + ext).find(
|
|
1369
|
+
const findConfig = (basePath)=>DEFAULT_CONFIG_EXTENSIONS.map((ext)=>basePath + ext).find(node_fs.existsSync);
|
|
1868
1370
|
const resolveConfigPath = (root, customConfig)=>{
|
|
1869
1371
|
if (customConfig) {
|
|
1870
1372
|
const customConfigPath = isAbsolute(customConfig) ? customConfig : join(root, customConfig);
|
|
1871
|
-
if (
|
|
1872
|
-
logger.warn(`Cannot find config file: ${
|
|
1373
|
+
if (node_fs.existsSync(customConfigPath)) return customConfigPath;
|
|
1374
|
+
logger.warn(`Cannot find config file: ${picocolors.dim(customConfigPath)}\n`);
|
|
1873
1375
|
}
|
|
1874
1376
|
const configFilePath = findConfig(join(root, DEFAULT_CONFIG_NAME));
|
|
1875
1377
|
if (configFilePath) return configFilePath;
|
|
@@ -1936,7 +1438,7 @@ const composeExternalsWarnConfig = (format, ...externalsArray)=>{
|
|
|
1936
1438
|
if (externals.test(request)) return void callback(true, true);
|
|
1937
1439
|
} else if ('function' == typeof externals) ;
|
|
1938
1440
|
else if ('object' == typeof externals) {
|
|
1939
|
-
if (Object.
|
|
1441
|
+
if (Object.hasOwn(externals, request)) return void (handleMatchedExternal(externals[request], request) ? callback(true, true) : callback(true));
|
|
1940
1442
|
}
|
|
1941
1443
|
callback(false);
|
|
1942
1444
|
};
|
|
@@ -2316,601 +1818,1086 @@ const disableUrlParseRsbuildPlugin = ()=>({
|
|
|
2316
1818
|
});
|
|
2317
1819
|
});
|
|
2318
1820
|
}
|
|
2319
|
-
});
|
|
2320
|
-
const composeShimsConfig = (format, shims)=>{
|
|
2321
|
-
const resolvedShims = {
|
|
2322
|
-
cjs: {
|
|
2323
|
-
'import.meta.url': shims?.cjs?.['import.meta.url'] ?? true
|
|
2324
|
-
},
|
|
2325
|
-
esm: {
|
|
2326
|
-
__filename: shims?.esm?.__filename ?? false,
|
|
2327
|
-
__dirname: shims?.esm?.__dirname ?? false,
|
|
2328
|
-
require: shims?.esm?.require ?? false
|
|
2329
|
-
}
|
|
2330
|
-
};
|
|
2331
|
-
const enabledShims = {
|
|
2332
|
-
cjs: 'cjs' === format ? resolvedShims.cjs : {
|
|
2333
|
-
'import.meta.url': false
|
|
2334
|
-
},
|
|
2335
|
-
esm: 'esm' === format ? resolvedShims.esm : {
|
|
2336
|
-
__filename: false,
|
|
2337
|
-
__dirname: false,
|
|
2338
|
-
require: false
|
|
1821
|
+
});
|
|
1822
|
+
const composeShimsConfig = (format, shims)=>{
|
|
1823
|
+
const resolvedShims = {
|
|
1824
|
+
cjs: {
|
|
1825
|
+
'import.meta.url': shims?.cjs?.['import.meta.url'] ?? true
|
|
1826
|
+
},
|
|
1827
|
+
esm: {
|
|
1828
|
+
__filename: shims?.esm?.__filename ?? false,
|
|
1829
|
+
__dirname: shims?.esm?.__dirname ?? false,
|
|
1830
|
+
require: shims?.esm?.require ?? false
|
|
1831
|
+
}
|
|
1832
|
+
};
|
|
1833
|
+
const enabledShims = {
|
|
1834
|
+
cjs: 'cjs' === format ? resolvedShims.cjs : {
|
|
1835
|
+
'import.meta.url': false
|
|
1836
|
+
},
|
|
1837
|
+
esm: 'esm' === format ? resolvedShims.esm : {
|
|
1838
|
+
__filename: false,
|
|
1839
|
+
__dirname: false,
|
|
1840
|
+
require: false
|
|
1841
|
+
}
|
|
1842
|
+
};
|
|
1843
|
+
let rsbuildConfig = {};
|
|
1844
|
+
switch(format){
|
|
1845
|
+
case 'esm':
|
|
1846
|
+
rsbuildConfig = {
|
|
1847
|
+
tools: {
|
|
1848
|
+
rspack: {
|
|
1849
|
+
node: {
|
|
1850
|
+
__dirname: resolvedShims.esm.__dirname ? 'node-module' : false,
|
|
1851
|
+
__filename: resolvedShims.esm.__filename ? 'node-module' : false
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
},
|
|
1855
|
+
plugins: [
|
|
1856
|
+
resolvedShims.esm.require && pluginEsmRequireShim(),
|
|
1857
|
+
disableUrlParseRsbuildPlugin()
|
|
1858
|
+
].filter(Boolean)
|
|
1859
|
+
};
|
|
1860
|
+
break;
|
|
1861
|
+
case 'cjs':
|
|
1862
|
+
rsbuildConfig = {
|
|
1863
|
+
plugins: [
|
|
1864
|
+
resolvedShims.cjs['import.meta.url'] && pluginCjsImportMetaUrlShim(),
|
|
1865
|
+
disableUrlParseRsbuildPlugin()
|
|
1866
|
+
].filter(Boolean)
|
|
1867
|
+
};
|
|
1868
|
+
break;
|
|
1869
|
+
case 'umd':
|
|
1870
|
+
case 'iife':
|
|
1871
|
+
case 'mf':
|
|
1872
|
+
break;
|
|
1873
|
+
default:
|
|
1874
|
+
throw new Error(`Unsupported format: ${format}`);
|
|
1875
|
+
}
|
|
1876
|
+
return {
|
|
1877
|
+
rsbuildConfig,
|
|
1878
|
+
enabledShims
|
|
1879
|
+
};
|
|
1880
|
+
};
|
|
1881
|
+
const composeModuleImportWarn = (request, issuer)=>`The externalized commonjs request ${picocolors.green(`"${request}"`)} from ${picocolors.green(issuer)} will use ${picocolors.blue('"module"')} external type in ESM format. If you want to specify other external type, consider setting the request and type with ${picocolors.blue('"output.externals"')}.`;
|
|
1882
|
+
const composeExternalsConfig = (format, externals)=>{
|
|
1883
|
+
const externalsTypeMap = {
|
|
1884
|
+
esm: 'module-import',
|
|
1885
|
+
cjs: 'commonjs-import',
|
|
1886
|
+
umd: 'umd',
|
|
1887
|
+
mf: 'global',
|
|
1888
|
+
iife: 'global'
|
|
1889
|
+
};
|
|
1890
|
+
const globalObjectMap = {
|
|
1891
|
+
esm: void 0,
|
|
1892
|
+
cjs: void 0,
|
|
1893
|
+
umd: void 0,
|
|
1894
|
+
mf: void 0,
|
|
1895
|
+
iife: 'globalThis'
|
|
1896
|
+
};
|
|
1897
|
+
const rspackConfig = {};
|
|
1898
|
+
const rsbuildConfig = {};
|
|
1899
|
+
switch(format){
|
|
1900
|
+
case 'esm':
|
|
1901
|
+
case 'cjs':
|
|
1902
|
+
case 'umd':
|
|
1903
|
+
case 'mf':
|
|
1904
|
+
case 'iife':
|
|
1905
|
+
rsbuildConfig.output = externals ? {
|
|
1906
|
+
externals
|
|
1907
|
+
} : {};
|
|
1908
|
+
rspackConfig.externalsType = externalsTypeMap[format];
|
|
1909
|
+
if (globalObjectMap[format]) rspackConfig.output = {
|
|
1910
|
+
globalObject: globalObjectMap[format]
|
|
1911
|
+
};
|
|
1912
|
+
break;
|
|
1913
|
+
default:
|
|
1914
|
+
throw new Error(`Unsupported format: ${format}`);
|
|
1915
|
+
}
|
|
1916
|
+
return {
|
|
1917
|
+
...rsbuildConfig,
|
|
1918
|
+
tools: {
|
|
1919
|
+
rspack: rspackConfig
|
|
1920
|
+
}
|
|
1921
|
+
};
|
|
1922
|
+
};
|
|
1923
|
+
const composeAutoExtensionConfig = (config, format, autoExtension, pkgJson)=>{
|
|
1924
|
+
const { jsExtension, dtsExtension } = getDefaultExtension({
|
|
1925
|
+
format,
|
|
1926
|
+
pkgJson,
|
|
1927
|
+
autoExtension
|
|
1928
|
+
});
|
|
1929
|
+
const filenameHash = config.output?.filenameHash ?? false;
|
|
1930
|
+
const getHash = ()=>{
|
|
1931
|
+
if ('string' == typeof filenameHash) return filenameHash ? `.[${filenameHash}]` : '';
|
|
1932
|
+
return filenameHash ? '.[contenthash:8]' : '';
|
|
1933
|
+
};
|
|
1934
|
+
const hash = getHash();
|
|
1935
|
+
const defaultJsFilename = `[name]${hash}${jsExtension}`;
|
|
1936
|
+
const userJsFilename = config.output?.filename?.js;
|
|
1937
|
+
const finalJsExtension = 'string' == typeof userJsFilename && userJsFilename ? extname(userJsFilename) : jsExtension;
|
|
1938
|
+
const finalConfig = userJsFilename ? {} : {
|
|
1939
|
+
output: {
|
|
1940
|
+
filename: {
|
|
1941
|
+
js: defaultJsFilename
|
|
1942
|
+
}
|
|
1943
|
+
}
|
|
1944
|
+
};
|
|
1945
|
+
return {
|
|
1946
|
+
config: finalConfig,
|
|
1947
|
+
jsExtension: finalJsExtension,
|
|
1948
|
+
dtsExtension
|
|
1949
|
+
};
|
|
1950
|
+
};
|
|
1951
|
+
const composeSyntaxConfig = (target, syntax)=>{
|
|
1952
|
+
if (syntax) return {
|
|
1953
|
+
tools: {
|
|
1954
|
+
rspack: (config)=>{
|
|
1955
|
+
config.target = transformSyntaxToRspackTarget(syntax);
|
|
1956
|
+
}
|
|
1957
|
+
},
|
|
1958
|
+
output: {
|
|
1959
|
+
overrideBrowserslist: transformSyntaxToBrowserslist(syntax, target)
|
|
1960
|
+
}
|
|
1961
|
+
};
|
|
1962
|
+
return {
|
|
1963
|
+
tools: {
|
|
1964
|
+
rspack: (config)=>{
|
|
1965
|
+
config.target = [
|
|
1966
|
+
'es2022'
|
|
1967
|
+
];
|
|
1968
|
+
return config;
|
|
1969
|
+
}
|
|
1970
|
+
},
|
|
1971
|
+
output: {
|
|
1972
|
+
overrideBrowserslist: ESX_TO_BROWSERSLIST.esnext(target)
|
|
1973
|
+
}
|
|
1974
|
+
};
|
|
1975
|
+
};
|
|
1976
|
+
const traverseEntryQuery = (entry, callback)=>{
|
|
1977
|
+
const newEntry = {};
|
|
1978
|
+
for (const [key, value] of Object.entries(entry)){
|
|
1979
|
+
let result = value;
|
|
1980
|
+
result = 'string' == typeof value ? callback(value) : Array.isArray(value) ? value.map(callback) : {
|
|
1981
|
+
...value,
|
|
1982
|
+
import: 'string' == typeof value.import ? callback(value.import) : value.import.map(callback)
|
|
1983
|
+
};
|
|
1984
|
+
newEntry[key] = result;
|
|
1985
|
+
}
|
|
1986
|
+
return newEntry;
|
|
1987
|
+
};
|
|
1988
|
+
const resolveEntryPath = (entries, root)=>traverseEntryQuery(entries, (item)=>node_path.resolve(root, item));
|
|
1989
|
+
const composeEntryConfig = async (rawEntry, bundle, root, cssModulesAuto, userOutBase)=>{
|
|
1990
|
+
let entries = rawEntry;
|
|
1991
|
+
if (!entries) {
|
|
1992
|
+
if (false !== bundle) return {
|
|
1993
|
+
entryConfig: {},
|
|
1994
|
+
outBase: null
|
|
1995
|
+
};
|
|
1996
|
+
entries = {
|
|
1997
|
+
index: 'src/**'
|
|
1998
|
+
};
|
|
1999
|
+
}
|
|
2000
|
+
if ('object' != typeof entries) throw new Error(`The ${picocolors.cyan('source.entry')} configuration should be an object, but received ${typeof entries}: ${picocolors.cyan(entries)}. Checkout ${picocolors.green('https://rslib.rs/config/rsbuild/source#sourceentry')} for more details.`);
|
|
2001
|
+
if (false !== bundle) {
|
|
2002
|
+
const entryErrorReasons = [];
|
|
2003
|
+
traverseEntryQuery(entries, (entry)=>{
|
|
2004
|
+
const entryAbsPath = node_path.isAbsolute(entry) ? entry : node_path.resolve(root, entry);
|
|
2005
|
+
const isDirLike = '' === node_path.extname(entryAbsPath);
|
|
2006
|
+
const dirError = `Glob pattern ${picocolors.cyan(`"${entry}"`)} is not supported when "bundle" is "true", considering "bundle" to "false" to use bundleless mode, or specify a file entry to bundle. See ${picocolors.green('https://rslib.rs/guide/basic/output-structure')} for more details.`;
|
|
2007
|
+
if (node_fs.existsSync(entryAbsPath)) {
|
|
2008
|
+
const stats = node_fs.statSync(entryAbsPath);
|
|
2009
|
+
if (!stats.isFile()) entryErrorReasons.push(dirError);
|
|
2010
|
+
} else if (isDirLike) entryErrorReasons.push(dirError);
|
|
2011
|
+
else entryErrorReasons.push(`Can't resolve the entry ${picocolors.cyan(`"${entry}"`)} at the location ${picocolors.cyan(`${entryAbsPath}`)}. Please ensure that the file exists.`);
|
|
2012
|
+
return entry;
|
|
2013
|
+
});
|
|
2014
|
+
if (entryErrorReasons.length) throw new AggregateError(entryErrorReasons.map((reason)=>new Error(reason)));
|
|
2015
|
+
return {
|
|
2016
|
+
entryConfig: {
|
|
2017
|
+
source: {
|
|
2018
|
+
entry: resolveEntryPath(entries, root)
|
|
2019
|
+
}
|
|
2020
|
+
},
|
|
2021
|
+
outBase: null
|
|
2022
|
+
};
|
|
2023
|
+
}
|
|
2024
|
+
const scanGlobEntries = async (tryResolveOutBase)=>{
|
|
2025
|
+
const resolvedEntries = {};
|
|
2026
|
+
const resolveOutBase = async (resolvedEntryFiles)=>{
|
|
2027
|
+
if (void 0 !== userOutBase) return node_path.isAbsolute(userOutBase) ? userOutBase : node_path.resolve(root, userOutBase);
|
|
2028
|
+
const lcp = await calcLongestCommonPath(resolvedEntryFiles) ?? root;
|
|
2029
|
+
return lcp;
|
|
2030
|
+
};
|
|
2031
|
+
for (const key of Object.keys(entries)){
|
|
2032
|
+
const entry = entries[key];
|
|
2033
|
+
const entryFiles = Array.isArray(entry) ? entry : 'string' == typeof entry ? [
|
|
2034
|
+
entry
|
|
2035
|
+
] : null;
|
|
2036
|
+
if (!entryFiles) throw new Error('Entry can only be a string or an array of strings for now');
|
|
2037
|
+
const globEntryFiles = await glob(entryFiles, {
|
|
2038
|
+
cwd: root,
|
|
2039
|
+
absolute: true
|
|
2040
|
+
});
|
|
2041
|
+
const resolvedEntryFiles = globEntryFiles.filter((i)=>!DTS_EXTENSIONS_PATTERN.test(i));
|
|
2042
|
+
if (0 === resolvedEntryFiles.length) throw new Error(`Cannot find ${resolvedEntryFiles}`);
|
|
2043
|
+
const outBase = await resolveOutBase(resolvedEntryFiles);
|
|
2044
|
+
function getEntryName(file) {
|
|
2045
|
+
const { dir, name } = node_path.parse(node_path.relative(outBase, file));
|
|
2046
|
+
const entryFileName = node_path.join(dir, name);
|
|
2047
|
+
if (isCssGlobalFile(file, cssModulesAuto)) return `${RSLIB_CSS_ENTRY_FLAG}/${entryFileName}`;
|
|
2048
|
+
return entryFileName;
|
|
2049
|
+
}
|
|
2050
|
+
for (const file of resolvedEntryFiles){
|
|
2051
|
+
const entryName = getEntryName(file);
|
|
2052
|
+
if (resolvedEntries[entryName]) tryResolveOutBase && logger.warn(`Duplicate entry ${picocolors.cyan(entryName)} from ${picocolors.cyan(node_path.relative(root, file))} and ${picocolors.cyan(node_path.relative(root, resolvedEntries[entryName]))}, which may lead to the incorrect output, please rename the file.`);
|
|
2053
|
+
resolvedEntries[entryName] = file;
|
|
2054
|
+
}
|
|
2055
|
+
}
|
|
2056
|
+
if (tryResolveOutBase) {
|
|
2057
|
+
const outBase = await resolveOutBase(Object.values(resolvedEntries));
|
|
2058
|
+
return {
|
|
2059
|
+
resolvedEntries,
|
|
2060
|
+
outBase
|
|
2061
|
+
};
|
|
2062
|
+
}
|
|
2063
|
+
return {
|
|
2064
|
+
resolvedEntries,
|
|
2065
|
+
outBase: null
|
|
2066
|
+
};
|
|
2067
|
+
};
|
|
2068
|
+
const { outBase } = await scanGlobEntries(true);
|
|
2069
|
+
const entryConfig = {
|
|
2070
|
+
tools: {
|
|
2071
|
+
rspack: {
|
|
2072
|
+
entry: async ()=>{
|
|
2073
|
+
const { resolvedEntries } = await scanGlobEntries(false);
|
|
2074
|
+
return resolvedEntries;
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
2077
|
+
}
|
|
2078
|
+
};
|
|
2079
|
+
return {
|
|
2080
|
+
entryConfig,
|
|
2081
|
+
outBase
|
|
2082
|
+
};
|
|
2083
|
+
};
|
|
2084
|
+
const composeBundlelessExternalConfig = (jsExtension, redirect, cssModulesAuto, bundle, outBase)=>{
|
|
2085
|
+
if (bundle) return {
|
|
2086
|
+
config: {}
|
|
2087
|
+
};
|
|
2088
|
+
const styleRedirectPath = redirect.style?.path ?? true;
|
|
2089
|
+
const styleRedirectExtension = redirect.style?.extension ?? true;
|
|
2090
|
+
const jsRedirectPath = redirect.js?.path ?? true;
|
|
2091
|
+
const jsRedirectExtension = redirect.js?.extension ?? true;
|
|
2092
|
+
const assetRedirect = redirect.asset ?? true;
|
|
2093
|
+
let resolver;
|
|
2094
|
+
return {
|
|
2095
|
+
resolvedJsRedirect: {
|
|
2096
|
+
path: jsRedirectPath,
|
|
2097
|
+
extension: jsRedirectExtension
|
|
2098
|
+
},
|
|
2099
|
+
config: {
|
|
2100
|
+
output: {
|
|
2101
|
+
externals: [
|
|
2102
|
+
async (data, callback)=>{
|
|
2103
|
+
const { request, getResolve, context, contextInfo } = data;
|
|
2104
|
+
if (!request || !getResolve || !context || !contextInfo) return callback();
|
|
2105
|
+
const { issuer } = contextInfo;
|
|
2106
|
+
if (!resolver) resolver = await getResolve();
|
|
2107
|
+
async function redirectPath(request) {
|
|
2108
|
+
try {
|
|
2109
|
+
let resolvedRequest = request;
|
|
2110
|
+
resolvedRequest = await resolver(context, resolvedRequest);
|
|
2111
|
+
if ('string' != typeof outBase) throw new Error(`outBase expect to be a string in bundleless mode, but got ${outBase}`);
|
|
2112
|
+
const isSubpath = normalizeSlash(resolvedRequest).startsWith(`${normalizeSlash(outBase)}/`);
|
|
2113
|
+
if (isSubpath) {
|
|
2114
|
+
resolvedRequest = normalizeSlash(node_path.relative(node_path.dirname(issuer), resolvedRequest));
|
|
2115
|
+
if ('.' !== resolvedRequest[0]) resolvedRequest = `./${resolvedRequest}`;
|
|
2116
|
+
return resolvedRequest;
|
|
2117
|
+
}
|
|
2118
|
+
return;
|
|
2119
|
+
} catch (_e) {
|
|
2120
|
+
logger.debug(`Failed to resolve module ${picocolors.green(`"${request}"`)} from ${picocolors.green(issuer)}. If it's an npm package, consider adding it to dependencies or peerDependencies in package.json to make it externalized.`);
|
|
2121
|
+
return request;
|
|
2122
|
+
}
|
|
2123
|
+
}
|
|
2124
|
+
if (issuer) {
|
|
2125
|
+
let resolvedRequest = request;
|
|
2126
|
+
const cssExternal = await cssExternalHandler(resolvedRequest, callback, jsExtension, cssModulesAuto, styleRedirectPath, styleRedirectExtension, redirectPath, issuer);
|
|
2127
|
+
if (false !== cssExternal) return cssExternal;
|
|
2128
|
+
if (jsRedirectPath) {
|
|
2129
|
+
const redirectedPath = await redirectPath(resolvedRequest);
|
|
2130
|
+
if (void 0 === redirectedPath) return callback(void 0, request);
|
|
2131
|
+
resolvedRequest = redirectedPath;
|
|
2132
|
+
}
|
|
2133
|
+
if (resolvedRequest.startsWith('.')) {
|
|
2134
|
+
const ext = extname(resolvedRequest);
|
|
2135
|
+
if (ext) {
|
|
2136
|
+
if (JS_EXTENSIONS_PATTERN.test(resolvedRequest)) {
|
|
2137
|
+
if (jsRedirectExtension) resolvedRequest = resolvedRequest.replace(/\.[^.]+$/, jsExtension);
|
|
2138
|
+
} else if (assetRedirect) resolvedRequest = resolvedRequest.replace(/\.[^.]+$/, jsExtension);
|
|
2139
|
+
} else if (jsRedirectExtension) resolvedRequest = `${resolvedRequest}${jsExtension}`;
|
|
2140
|
+
}
|
|
2141
|
+
return callback(void 0, resolvedRequest);
|
|
2142
|
+
}
|
|
2143
|
+
callback();
|
|
2144
|
+
}
|
|
2145
|
+
]
|
|
2146
|
+
}
|
|
2147
|
+
}
|
|
2148
|
+
};
|
|
2149
|
+
};
|
|
2150
|
+
const composeDtsConfig = async (libConfig, format, dtsExtension)=>{
|
|
2151
|
+
const { autoExternal, banner, footer, redirect } = libConfig;
|
|
2152
|
+
let { dts } = libConfig;
|
|
2153
|
+
if (false === dts || void 0 === dts) return {};
|
|
2154
|
+
if (true === dts) dts = {
|
|
2155
|
+
bundle: false
|
|
2156
|
+
};
|
|
2157
|
+
const { pluginDts } = await import("rsbuild-plugin-dts");
|
|
2158
|
+
return {
|
|
2159
|
+
plugins: [
|
|
2160
|
+
pluginDts({
|
|
2161
|
+
bundle: dts?.bundle,
|
|
2162
|
+
distPath: dts?.distPath,
|
|
2163
|
+
build: dts?.build,
|
|
2164
|
+
abortOnError: dts?.abortOnError,
|
|
2165
|
+
dtsExtension: dts?.autoExtension ? dtsExtension : '.d.ts',
|
|
2166
|
+
autoExternal: getAutoExternalDefaultValue(format, autoExternal),
|
|
2167
|
+
banner: banner?.dts,
|
|
2168
|
+
footer: footer?.dts,
|
|
2169
|
+
redirect: redirect?.dts
|
|
2170
|
+
})
|
|
2171
|
+
]
|
|
2172
|
+
};
|
|
2173
|
+
};
|
|
2174
|
+
const composeTargetConfig = (userTarget, format)=>{
|
|
2175
|
+
const target = userTarget ?? ('mf' === format ? 'web' : 'node');
|
|
2176
|
+
switch(target){
|
|
2177
|
+
case 'web':
|
|
2178
|
+
return {
|
|
2179
|
+
config: {
|
|
2180
|
+
tools: {
|
|
2181
|
+
rspack: {
|
|
2182
|
+
target: [
|
|
2183
|
+
'web'
|
|
2184
|
+
]
|
|
2185
|
+
}
|
|
2186
|
+
}
|
|
2187
|
+
},
|
|
2188
|
+
target: 'web',
|
|
2189
|
+
externalsConfig: {}
|
|
2190
|
+
};
|
|
2191
|
+
case 'node':
|
|
2192
|
+
return {
|
|
2193
|
+
config: {
|
|
2194
|
+
tools: {
|
|
2195
|
+
rspack: {
|
|
2196
|
+
target: [
|
|
2197
|
+
'node'
|
|
2198
|
+
]
|
|
2199
|
+
}
|
|
2200
|
+
},
|
|
2201
|
+
output: {
|
|
2202
|
+
target: 'node'
|
|
2203
|
+
}
|
|
2204
|
+
},
|
|
2205
|
+
target: 'node',
|
|
2206
|
+
externalsConfig: {
|
|
2207
|
+
output: {
|
|
2208
|
+
externals: nodeBuiltInModules
|
|
2209
|
+
}
|
|
2210
|
+
}
|
|
2211
|
+
};
|
|
2212
|
+
default:
|
|
2213
|
+
throw new Error(`Unsupported platform: ${target}`);
|
|
2214
|
+
}
|
|
2215
|
+
};
|
|
2216
|
+
const composeExternalHelpersConfig = (externalHelpers, pkgJson)=>{
|
|
2217
|
+
let defaultConfig = {
|
|
2218
|
+
tools: {
|
|
2219
|
+
swc: {
|
|
2220
|
+
jsc: {
|
|
2221
|
+
externalHelpers: false
|
|
2222
|
+
}
|
|
2223
|
+
}
|
|
2224
|
+
}
|
|
2225
|
+
};
|
|
2226
|
+
if (externalHelpers) {
|
|
2227
|
+
const deps = [
|
|
2228
|
+
...Object.keys(pkgJson?.dependencies ?? []),
|
|
2229
|
+
...Object.keys(pkgJson?.devDependencies ?? [])
|
|
2230
|
+
];
|
|
2231
|
+
if (!deps.includes(SWC_HELPERS)) {
|
|
2232
|
+
logger.error(`${picocolors.green('externalHelpers')} is enabled, but the ${picocolors.blue(SWC_HELPERS)} dependency declaration was not found in package.json.`);
|
|
2233
|
+
process.exit(1);
|
|
2234
|
+
}
|
|
2235
|
+
defaultConfig = Object.assign(defaultConfig, {
|
|
2236
|
+
output: {
|
|
2237
|
+
externals: new RegExp(`^${SWC_HELPERS}($|\\/|\\\\)`)
|
|
2238
|
+
}
|
|
2239
|
+
});
|
|
2240
|
+
defaultConfig.tools.swc.jsc.externalHelpers = true;
|
|
2241
|
+
}
|
|
2242
|
+
return defaultConfig;
|
|
2243
|
+
};
|
|
2244
|
+
async function composeLibRsbuildConfig(config, root, sharedPlugins) {
|
|
2245
|
+
checkMFPlugin(config, sharedPlugins);
|
|
2246
|
+
const rootPath = root ? getAbsolutePath(process.cwd(), root) : process.cwd();
|
|
2247
|
+
const pkgJson = readPackageJson(rootPath);
|
|
2248
|
+
const { compilerOptions } = await loadTsconfig(rootPath, config.source?.tsconfigPath);
|
|
2249
|
+
const cssModulesAuto = config.output?.cssModules?.auto ?? true;
|
|
2250
|
+
const { format = 'esm', shims, bundle = true, banner = {}, footer = {}, autoExtension = true, autoExternal, externalHelpers = false, redirect = {}, umdName } = config;
|
|
2251
|
+
const { rsbuildConfig: shimsConfig, enabledShims } = composeShimsConfig(format, shims);
|
|
2252
|
+
const formatConfig = composeFormatConfig({
|
|
2253
|
+
format,
|
|
2254
|
+
pkgJson: pkgJson,
|
|
2255
|
+
bundle,
|
|
2256
|
+
umdName
|
|
2257
|
+
});
|
|
2258
|
+
const externalHelpersConfig = composeExternalHelpersConfig(externalHelpers, pkgJson);
|
|
2259
|
+
const userExternalsConfig = composeExternalsConfig(format, config.output?.externals);
|
|
2260
|
+
const { config: autoExtensionConfig, jsExtension, dtsExtension } = composeAutoExtensionConfig(config, format, autoExtension, pkgJson);
|
|
2261
|
+
const { entryConfig, outBase } = await composeEntryConfig(config.source?.entry, config.bundle, rootPath, cssModulesAuto, config.outBase);
|
|
2262
|
+
const { config: bundlelessExternalConfig } = composeBundlelessExternalConfig(jsExtension, redirect, cssModulesAuto, bundle, outBase);
|
|
2263
|
+
const { config: targetConfig, externalsConfig: targetExternalsConfig, target } = composeTargetConfig(config.output?.target, format);
|
|
2264
|
+
const syntaxConfig = composeSyntaxConfig(target, config?.syntax);
|
|
2265
|
+
const autoExternalConfig = composeAutoExternalConfig({
|
|
2266
|
+
bundle,
|
|
2267
|
+
format,
|
|
2268
|
+
autoExternal,
|
|
2269
|
+
pkgJson,
|
|
2270
|
+
userExternals: config.output?.externals
|
|
2271
|
+
});
|
|
2272
|
+
const cssConfig = composeCssConfig(outBase, cssModulesAuto, config.bundle, banner?.css, footer?.css);
|
|
2273
|
+
const assetConfig = composeAssetConfig(bundle, format);
|
|
2274
|
+
const entryChunkConfig = composeEntryChunkConfig({
|
|
2275
|
+
enabledImportMetaUrlShim: enabledShims.cjs['import.meta.url'],
|
|
2276
|
+
contextToWatch: outBase
|
|
2277
|
+
});
|
|
2278
|
+
const dtsConfig = await composeDtsConfig(config, format, dtsExtension);
|
|
2279
|
+
const externalsWarnConfig = composeExternalsWarnConfig(format, userExternalsConfig?.output?.externals, autoExternalConfig?.output?.externals);
|
|
2280
|
+
const minifyConfig = composeMinifyConfig(config);
|
|
2281
|
+
const bannerFooterConfig = composeBannerFooterConfig(banner, footer);
|
|
2282
|
+
const decoratorsConfig = composeDecoratorsConfig(compilerOptions, config.source?.decorators?.version);
|
|
2283
|
+
return (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.mergeRsbuildConfig)(formatConfig, shimsConfig, syntaxConfig, externalHelpersConfig, autoExtensionConfig, targetConfig, externalsWarnConfig, userExternalsConfig, autoExternalConfig, targetExternalsConfig, bundlelessExternalConfig, entryConfig, cssConfig, assetConfig, entryChunkConfig, minifyConfig, dtsConfig, bannerFooterConfig, decoratorsConfig);
|
|
2284
|
+
}
|
|
2285
|
+
async function composeCreateRsbuildConfig(rslibConfig) {
|
|
2286
|
+
const constantRsbuildConfig = await createConstantRsbuildConfig();
|
|
2287
|
+
const { lib: libConfigsArray, mode: _mode, root, plugins: sharedPlugins, dev: _dev, server: _server, ...sharedRsbuildConfig } = rslibConfig;
|
|
2288
|
+
if (!Array.isArray(libConfigsArray) || 0 === libConfigsArray.length) throw new Error(`Expect "lib" field to be a non-empty array, but got: ${picocolors.cyan(JSON.stringify(libConfigsArray))}.`);
|
|
2289
|
+
const libConfigPromises = libConfigsArray.map(async (libConfig)=>{
|
|
2290
|
+
const userConfig = (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.mergeRsbuildConfig)(sharedRsbuildConfig, libConfig);
|
|
2291
|
+
const libRsbuildConfig = await composeLibRsbuildConfig(userConfig, root, sharedPlugins);
|
|
2292
|
+
userConfig.source ??= {};
|
|
2293
|
+
userConfig.source.entry = {};
|
|
2294
|
+
userConfig.output ??= {};
|
|
2295
|
+
delete userConfig.output.externals;
|
|
2296
|
+
const config = {
|
|
2297
|
+
format: libConfig.format ?? 'esm',
|
|
2298
|
+
config: (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.mergeRsbuildConfig)(constantRsbuildConfig, libRsbuildConfig, omit(userConfig, {
|
|
2299
|
+
id: true,
|
|
2300
|
+
bundle: true,
|
|
2301
|
+
format: true,
|
|
2302
|
+
autoExtension: true,
|
|
2303
|
+
autoExternal: true,
|
|
2304
|
+
redirect: true,
|
|
2305
|
+
syntax: true,
|
|
2306
|
+
externalHelpers: true,
|
|
2307
|
+
banner: true,
|
|
2308
|
+
footer: true,
|
|
2309
|
+
dts: true,
|
|
2310
|
+
shims: true,
|
|
2311
|
+
umdName: true,
|
|
2312
|
+
outBase: true
|
|
2313
|
+
}))
|
|
2314
|
+
};
|
|
2315
|
+
if ('string' == typeof libConfig.id) config.id = libConfig.id;
|
|
2316
|
+
return config;
|
|
2317
|
+
});
|
|
2318
|
+
const composedRsbuildConfig = await Promise.all(libConfigPromises);
|
|
2319
|
+
return composedRsbuildConfig;
|
|
2320
|
+
}
|
|
2321
|
+
async function composeRsbuildEnvironments(rslibConfig) {
|
|
2322
|
+
const rsbuildConfigWithLibInfo = await composeCreateRsbuildConfig(rslibConfig);
|
|
2323
|
+
const environmentWithInfos = [];
|
|
2324
|
+
const usedIds = rsbuildConfigWithLibInfo.map(({ id })=>id).filter(Boolean);
|
|
2325
|
+
const environments = {};
|
|
2326
|
+
const formatCount = rsbuildConfigWithLibInfo.reduce((acc, { format })=>{
|
|
2327
|
+
acc[format] = (acc[format] ?? 0) + 1;
|
|
2328
|
+
return acc;
|
|
2329
|
+
}, {});
|
|
2330
|
+
const composeDefaultId = (format)=>{
|
|
2331
|
+
const nextDefaultId = (format, index)=>`${format}${1 === formatCount[format] && 0 === index ? '' : index}`;
|
|
2332
|
+
let index = 0;
|
|
2333
|
+
let candidateId = nextDefaultId(format, index);
|
|
2334
|
+
while(-1 !== usedIds.indexOf(candidateId))candidateId = nextDefaultId(format, ++index);
|
|
2335
|
+
usedIds.push(candidateId);
|
|
2336
|
+
return candidateId;
|
|
2337
|
+
};
|
|
2338
|
+
for (const { format, id, config } of rsbuildConfigWithLibInfo){
|
|
2339
|
+
const libId = 'string' == typeof id ? id : composeDefaultId(format);
|
|
2340
|
+
environments[libId] = config;
|
|
2341
|
+
environmentWithInfos.push({
|
|
2342
|
+
id: libId,
|
|
2343
|
+
format,
|
|
2344
|
+
config
|
|
2345
|
+
});
|
|
2346
|
+
}
|
|
2347
|
+
const conflictIds = usedIds.filter((id, index)=>usedIds.indexOf(id) !== index);
|
|
2348
|
+
if (conflictIds.length) throw new Error(`The following ids are duplicated: ${conflictIds.map((id)=>`"${id}"`).join(', ')}. Please change the "lib.id" to be unique.`);
|
|
2349
|
+
return {
|
|
2350
|
+
environments,
|
|
2351
|
+
environmentWithInfos
|
|
2352
|
+
};
|
|
2353
|
+
}
|
|
2354
|
+
const pruneEnvironments = (environments, libs)=>{
|
|
2355
|
+
if (!libs || 0 === libs.length) return environments;
|
|
2356
|
+
const filteredEnvironments = Object.fromEntries(Object.entries(environments).filter(([name])=>libs.includes(name)));
|
|
2357
|
+
if (0 === Object.keys(filteredEnvironments).length) throw new Error(`The following libs are not found: ${libs.map((lib)=>`"${lib}"`).join(', ')}.`);
|
|
2358
|
+
return filteredEnvironments;
|
|
2359
|
+
};
|
|
2360
|
+
async function watchFilesForRestart(files, restart) {
|
|
2361
|
+
if (!files.length) return;
|
|
2362
|
+
const chokidar = await import("../compiled/chokidar/index.js");
|
|
2363
|
+
const watcher = chokidar.watch(files, {
|
|
2364
|
+
ignoreInitial: true,
|
|
2365
|
+
ignorePermissionErrors: true
|
|
2366
|
+
});
|
|
2367
|
+
const callback = debounce(async (filePath)=>{
|
|
2368
|
+
watcher.close();
|
|
2369
|
+
await beforeRestart({
|
|
2370
|
+
filePath
|
|
2371
|
+
});
|
|
2372
|
+
await restart();
|
|
2373
|
+
}, 300);
|
|
2374
|
+
watcher.on('add', callback);
|
|
2375
|
+
watcher.on('change', callback);
|
|
2376
|
+
watcher.on('unlink', callback);
|
|
2377
|
+
}
|
|
2378
|
+
let cleaners = [];
|
|
2379
|
+
const onBeforeRestart = (cleaner)=>{
|
|
2380
|
+
cleaners.push(cleaner);
|
|
2381
|
+
};
|
|
2382
|
+
const clearConsole = ()=>{
|
|
2383
|
+
if (isTTY() && !process.env.DEBUG) process.stdout.write('\x1B[H\x1B[2J');
|
|
2384
|
+
};
|
|
2385
|
+
const beforeRestart = async ({ filePath, clear = true } = {})=>{
|
|
2386
|
+
if (clear) clearConsole();
|
|
2387
|
+
if (filePath) {
|
|
2388
|
+
const filename = node_path.basename(filePath);
|
|
2389
|
+
logger.info(`restart because ${picocolors.yellow(filename)} is changed.\n`);
|
|
2390
|
+
} else logger.info('restarting...\n');
|
|
2391
|
+
for (const cleaner of cleaners)await cleaner();
|
|
2392
|
+
cleaners = [];
|
|
2393
|
+
};
|
|
2394
|
+
async function build(config, options = {}) {
|
|
2395
|
+
const { environments } = await composeRsbuildEnvironments(config);
|
|
2396
|
+
const rsbuildInstance = await (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.createRsbuild)({
|
|
2397
|
+
callerName: 'rslib',
|
|
2398
|
+
rsbuildConfig: {
|
|
2399
|
+
mode: 'production',
|
|
2400
|
+
root: config.root,
|
|
2401
|
+
plugins: config.plugins,
|
|
2402
|
+
dev: config.dev,
|
|
2403
|
+
server: config.server,
|
|
2404
|
+
environments: pruneEnvironments(environments, options.lib)
|
|
2405
|
+
}
|
|
2406
|
+
});
|
|
2407
|
+
const buildInstance = await rsbuildInstance.build({
|
|
2408
|
+
watch: options.watch
|
|
2409
|
+
});
|
|
2410
|
+
if (options.watch) onBeforeRestart(buildInstance.close);
|
|
2411
|
+
else await buildInstance.close();
|
|
2412
|
+
return rsbuildInstance;
|
|
2413
|
+
}
|
|
2414
|
+
function toArr(any) {
|
|
2415
|
+
return null == any ? [] : Array.isArray(any) ? any : [
|
|
2416
|
+
any
|
|
2417
|
+
];
|
|
2418
|
+
}
|
|
2419
|
+
function toVal(out, key, val, opts) {
|
|
2420
|
+
var x, old = out[key], nxt = ~opts.string.indexOf(key) ? null == val || true === val ? '' : String(val) : 'boolean' == typeof val ? val : ~opts.boolean.indexOf(key) ? 'false' === val ? false : 'true' === val || (out._.push((x = +val, 0 * x === 0) ? x : val), !!val) : (x = +val, 0 * x === 0) ? x : val;
|
|
2421
|
+
out[key] = null == old ? nxt : Array.isArray(old) ? old.concat(nxt) : [
|
|
2422
|
+
old,
|
|
2423
|
+
nxt
|
|
2424
|
+
];
|
|
2425
|
+
}
|
|
2426
|
+
function mri2(args, opts) {
|
|
2427
|
+
args = args || [];
|
|
2428
|
+
opts = opts || {};
|
|
2429
|
+
var k, arr, arg, name, val, out = {
|
|
2430
|
+
_: []
|
|
2431
|
+
};
|
|
2432
|
+
var i = 0, j = 0, idx = 0, len = args.length;
|
|
2433
|
+
const alibi = void 0 !== opts.alias;
|
|
2434
|
+
const strict = void 0 !== opts.unknown;
|
|
2435
|
+
const defaults = void 0 !== opts.default;
|
|
2436
|
+
opts.alias = opts.alias || {};
|
|
2437
|
+
opts.string = toArr(opts.string);
|
|
2438
|
+
opts.boolean = toArr(opts.boolean);
|
|
2439
|
+
if (alibi) for(k in opts.alias){
|
|
2440
|
+
arr = opts.alias[k] = toArr(opts.alias[k]);
|
|
2441
|
+
for(i = 0; i < arr.length; i++)(opts.alias[arr[i]] = arr.concat(k)).splice(i, 1);
|
|
2442
|
+
}
|
|
2443
|
+
for(i = opts.boolean.length; i-- > 0;){
|
|
2444
|
+
arr = opts.alias[opts.boolean[i]] || [];
|
|
2445
|
+
for(j = arr.length; j-- > 0;)opts.boolean.push(arr[j]);
|
|
2446
|
+
}
|
|
2447
|
+
for(i = opts.string.length; i-- > 0;){
|
|
2448
|
+
arr = opts.alias[opts.string[i]] || [];
|
|
2449
|
+
for(j = arr.length; j-- > 0;)opts.string.push(arr[j]);
|
|
2450
|
+
}
|
|
2451
|
+
if (defaults) for(k in opts.default){
|
|
2452
|
+
name = typeof opts.default[k];
|
|
2453
|
+
arr = opts.alias[k] = opts.alias[k] || [];
|
|
2454
|
+
if (void 0 !== opts[name]) {
|
|
2455
|
+
opts[name].push(k);
|
|
2456
|
+
for(i = 0; i < arr.length; i++)opts[name].push(arr[i]);
|
|
2457
|
+
}
|
|
2458
|
+
}
|
|
2459
|
+
const keys = strict ? Object.keys(opts.alias) : [];
|
|
2460
|
+
for(i = 0; i < len; i++){
|
|
2461
|
+
arg = args[i];
|
|
2462
|
+
if ('--' === arg) {
|
|
2463
|
+
out._ = out._.concat(args.slice(++i));
|
|
2464
|
+
break;
|
|
2465
|
+
}
|
|
2466
|
+
for(j = 0; j < arg.length && 45 === arg.charCodeAt(j); j++);
|
|
2467
|
+
if (0 === j) out._.push(arg);
|
|
2468
|
+
else if ('no-' === arg.substring(j, j + 3)) {
|
|
2469
|
+
name = arg.substring(j + 3);
|
|
2470
|
+
if (strict && !~keys.indexOf(name)) return opts.unknown(arg);
|
|
2471
|
+
out[name] = false;
|
|
2472
|
+
} else {
|
|
2473
|
+
for(idx = j + 1; idx < arg.length && 61 !== arg.charCodeAt(idx); idx++);
|
|
2474
|
+
name = arg.substring(j, idx);
|
|
2475
|
+
val = arg.substring(++idx) || i + 1 === len || 45 === ('' + args[i + 1]).charCodeAt(0) || args[++i];
|
|
2476
|
+
arr = 2 === j ? [
|
|
2477
|
+
name
|
|
2478
|
+
] : name;
|
|
2479
|
+
for(idx = 0; idx < arr.length; idx++){
|
|
2480
|
+
name = arr[idx];
|
|
2481
|
+
if (strict && !~keys.indexOf(name)) return opts.unknown('-'.repeat(j) + name);
|
|
2482
|
+
toVal(out, name, idx + 1 < arr.length || val, opts);
|
|
2483
|
+
}
|
|
2484
|
+
}
|
|
2485
|
+
}
|
|
2486
|
+
if (defaults) {
|
|
2487
|
+
for(k in opts.default)if (void 0 === out[k]) out[k] = opts.default[k];
|
|
2488
|
+
}
|
|
2489
|
+
if (alibi) for(k in out){
|
|
2490
|
+
arr = opts.alias[k] || [];
|
|
2491
|
+
while(arr.length > 0)out[arr.shift()] = out[k];
|
|
2492
|
+
}
|
|
2493
|
+
return out;
|
|
2494
|
+
}
|
|
2495
|
+
const removeBrackets = (v)=>v.replace(/[<[].+/, "").trim();
|
|
2496
|
+
const findAllBrackets = (v)=>{
|
|
2497
|
+
const ANGLED_BRACKET_RE_GLOBAL = /<([^>]+)>/g;
|
|
2498
|
+
const SQUARE_BRACKET_RE_GLOBAL = /\[([^\]]+)\]/g;
|
|
2499
|
+
const res = [];
|
|
2500
|
+
const parse = (match)=>{
|
|
2501
|
+
let variadic = false;
|
|
2502
|
+
let value = match[1];
|
|
2503
|
+
if (value.startsWith("...")) {
|
|
2504
|
+
value = value.slice(3);
|
|
2505
|
+
variadic = true;
|
|
2506
|
+
}
|
|
2507
|
+
return {
|
|
2508
|
+
required: match[0].startsWith("<"),
|
|
2509
|
+
value,
|
|
2510
|
+
variadic
|
|
2511
|
+
};
|
|
2512
|
+
};
|
|
2513
|
+
let angledMatch;
|
|
2514
|
+
while(angledMatch = ANGLED_BRACKET_RE_GLOBAL.exec(v))res.push(parse(angledMatch));
|
|
2515
|
+
let squareMatch;
|
|
2516
|
+
while(squareMatch = SQUARE_BRACKET_RE_GLOBAL.exec(v))res.push(parse(squareMatch));
|
|
2517
|
+
return res;
|
|
2518
|
+
};
|
|
2519
|
+
const getMriOptions = (options)=>{
|
|
2520
|
+
const result = {
|
|
2521
|
+
alias: {},
|
|
2522
|
+
boolean: []
|
|
2523
|
+
};
|
|
2524
|
+
for (const [index, option] of options.entries()){
|
|
2525
|
+
if (option.names.length > 1) result.alias[option.names[0]] = option.names.slice(1);
|
|
2526
|
+
if (option.isBoolean) if (option.negated) {
|
|
2527
|
+
const hasStringTypeOption = options.some((o, i)=>i !== index && o.names.some((name)=>option.names.includes(name)) && "boolean" == typeof o.required);
|
|
2528
|
+
if (!hasStringTypeOption) result.boolean.push(option.names[0]);
|
|
2529
|
+
} else result.boolean.push(option.names[0]);
|
|
2530
|
+
}
|
|
2531
|
+
return result;
|
|
2532
|
+
};
|
|
2533
|
+
const findLongest = (arr)=>arr.sort((a, b)=>a.length > b.length ? -1 : 1)[0];
|
|
2534
|
+
const padRight = (str, length)=>str.length >= length ? str : `${str}${" ".repeat(length - str.length)}`;
|
|
2535
|
+
const camelcase = (input)=>input.replace(/([a-z])-([a-z])/g, (_, p1, p2)=>p1 + p2.toUpperCase());
|
|
2536
|
+
const setDotProp = (obj, keys, val)=>{
|
|
2537
|
+
let i = 0;
|
|
2538
|
+
let length = keys.length;
|
|
2539
|
+
let t = obj;
|
|
2540
|
+
let x;
|
|
2541
|
+
for(; i < length; ++i){
|
|
2542
|
+
x = t[keys[i]];
|
|
2543
|
+
t = t[keys[i]] = i === length - 1 ? val : null != x ? x : !~keys[i + 1].indexOf(".") && +keys[i + 1] > -1 ? [] : {};
|
|
2544
|
+
}
|
|
2545
|
+
};
|
|
2546
|
+
const setByType = (obj, transforms)=>{
|
|
2547
|
+
for (const key of Object.keys(transforms)){
|
|
2548
|
+
const transform = transforms[key];
|
|
2549
|
+
if (transform.shouldTransform) {
|
|
2550
|
+
obj[key] = Array.prototype.concat.call([], obj[key]);
|
|
2551
|
+
if ("function" == typeof transform.transformFunction) obj[key] = obj[key].map(transform.transformFunction);
|
|
2552
|
+
}
|
|
2553
|
+
}
|
|
2554
|
+
};
|
|
2555
|
+
const getFileName = (input)=>{
|
|
2556
|
+
const m = /([^\\\/]+)$/.exec(input);
|
|
2557
|
+
return m ? m[1] : "";
|
|
2558
|
+
};
|
|
2559
|
+
const camelcaseOptionName = (name)=>name.split(".").map((v, i)=>0 === i ? camelcase(v) : v).join(".");
|
|
2560
|
+
class CACError extends Error {
|
|
2561
|
+
constructor(message){
|
|
2562
|
+
super(message);
|
|
2563
|
+
this.name = this.constructor.name;
|
|
2564
|
+
if ("function" == typeof Error.captureStackTrace) Error.captureStackTrace(this, this.constructor);
|
|
2565
|
+
else this.stack = new Error(message).stack;
|
|
2566
|
+
}
|
|
2567
|
+
}
|
|
2568
|
+
class Option {
|
|
2569
|
+
constructor(rawName, description, config){
|
|
2570
|
+
this.rawName = rawName;
|
|
2571
|
+
this.description = description;
|
|
2572
|
+
this.config = Object.assign({}, config);
|
|
2573
|
+
rawName = rawName.replace(/\.\*/g, "");
|
|
2574
|
+
this.negated = false;
|
|
2575
|
+
this.names = removeBrackets(rawName).split(",").map((v)=>{
|
|
2576
|
+
let name = v.trim().replace(/^-{1,2}/, "");
|
|
2577
|
+
if (name.startsWith("no-")) {
|
|
2578
|
+
this.negated = true;
|
|
2579
|
+
name = name.replace(/^no-/, "");
|
|
2580
|
+
}
|
|
2581
|
+
return camelcaseOptionName(name);
|
|
2582
|
+
}).sort((a, b)=>a.length > b.length ? 1 : -1);
|
|
2583
|
+
this.name = this.names[this.names.length - 1];
|
|
2584
|
+
if (this.negated && null == this.config.default) this.config.default = true;
|
|
2585
|
+
if (rawName.includes("<")) this.required = true;
|
|
2586
|
+
else if (rawName.includes("[")) this.required = false;
|
|
2587
|
+
else this.isBoolean = true;
|
|
2588
|
+
}
|
|
2589
|
+
}
|
|
2590
|
+
const processArgs = process.argv;
|
|
2591
|
+
const platformInfo = `${process.platform}-${process.arch} node-${process.version}`;
|
|
2592
|
+
class Command {
|
|
2593
|
+
constructor(rawName, description, config = {}, cli){
|
|
2594
|
+
this.rawName = rawName;
|
|
2595
|
+
this.description = description;
|
|
2596
|
+
this.config = config;
|
|
2597
|
+
this.cli = cli;
|
|
2598
|
+
this.options = [];
|
|
2599
|
+
this.aliasNames = [];
|
|
2600
|
+
this.name = removeBrackets(rawName);
|
|
2601
|
+
this.args = findAllBrackets(rawName);
|
|
2602
|
+
this.examples = [];
|
|
2603
|
+
}
|
|
2604
|
+
usage(text) {
|
|
2605
|
+
this.usageText = text;
|
|
2606
|
+
return this;
|
|
2607
|
+
}
|
|
2608
|
+
allowUnknownOptions() {
|
|
2609
|
+
this.config.allowUnknownOptions = true;
|
|
2610
|
+
return this;
|
|
2611
|
+
}
|
|
2612
|
+
ignoreOptionDefaultValue() {
|
|
2613
|
+
this.config.ignoreOptionDefaultValue = true;
|
|
2614
|
+
return this;
|
|
2615
|
+
}
|
|
2616
|
+
version(version, customFlags = "-v, --version") {
|
|
2617
|
+
this.versionNumber = version;
|
|
2618
|
+
this.option(customFlags, "Display version number");
|
|
2619
|
+
return this;
|
|
2620
|
+
}
|
|
2621
|
+
example(example) {
|
|
2622
|
+
this.examples.push(example);
|
|
2623
|
+
return this;
|
|
2624
|
+
}
|
|
2625
|
+
option(rawName, description, config) {
|
|
2626
|
+
const option = new Option(rawName, description, config);
|
|
2627
|
+
this.options.push(option);
|
|
2628
|
+
return this;
|
|
2629
|
+
}
|
|
2630
|
+
alias(name) {
|
|
2631
|
+
this.aliasNames.push(name);
|
|
2632
|
+
return this;
|
|
2633
|
+
}
|
|
2634
|
+
action(callback) {
|
|
2635
|
+
this.commandAction = callback;
|
|
2636
|
+
return this;
|
|
2637
|
+
}
|
|
2638
|
+
isMatched(name) {
|
|
2639
|
+
return this.name === name || this.aliasNames.includes(name);
|
|
2640
|
+
}
|
|
2641
|
+
get isDefaultCommand() {
|
|
2642
|
+
return "" === this.name || this.aliasNames.includes("!");
|
|
2643
|
+
}
|
|
2644
|
+
get isGlobalCommand() {
|
|
2645
|
+
return this instanceof GlobalCommand;
|
|
2646
|
+
}
|
|
2647
|
+
hasOption(name) {
|
|
2648
|
+
name = name.split(".")[0];
|
|
2649
|
+
return this.options.find((option)=>option.names.includes(name));
|
|
2650
|
+
}
|
|
2651
|
+
outputHelp() {
|
|
2652
|
+
const { name, commands } = this.cli;
|
|
2653
|
+
const { versionNumber, options: globalOptions, helpCallback } = this.cli.globalCommand;
|
|
2654
|
+
let sections = [
|
|
2655
|
+
{
|
|
2656
|
+
body: `${name}${versionNumber ? `/${versionNumber}` : ""}`
|
|
2657
|
+
}
|
|
2658
|
+
];
|
|
2659
|
+
sections.push({
|
|
2660
|
+
title: "Usage",
|
|
2661
|
+
body: ` $ ${name} ${this.usageText || this.rawName}`
|
|
2662
|
+
});
|
|
2663
|
+
const showCommands = (this.isGlobalCommand || this.isDefaultCommand) && commands.length > 0;
|
|
2664
|
+
if (showCommands) {
|
|
2665
|
+
const longestCommandName = findLongest(commands.map((command)=>command.rawName));
|
|
2666
|
+
sections.push({
|
|
2667
|
+
title: "Commands",
|
|
2668
|
+
body: commands.map((command)=>` ${padRight(command.rawName, longestCommandName.length)} ${command.description}`).join("\n")
|
|
2669
|
+
});
|
|
2670
|
+
sections.push({
|
|
2671
|
+
title: "For more info, run any command with the `--help` flag",
|
|
2672
|
+
body: commands.map((command)=>` $ ${name}${"" === command.name ? "" : ` ${command.name}`} --help`).join("\n")
|
|
2673
|
+
});
|
|
2674
|
+
}
|
|
2675
|
+
let options = this.isGlobalCommand ? globalOptions : [
|
|
2676
|
+
...this.options,
|
|
2677
|
+
...globalOptions || []
|
|
2678
|
+
];
|
|
2679
|
+
if (!this.isGlobalCommand && !this.isDefaultCommand) options = options.filter((option)=>"version" !== option.name);
|
|
2680
|
+
if (options.length > 0) {
|
|
2681
|
+
const longestOptionName = findLongest(options.map((option)=>option.rawName));
|
|
2682
|
+
sections.push({
|
|
2683
|
+
title: "Options",
|
|
2684
|
+
body: options.map((option)=>` ${padRight(option.rawName, longestOptionName.length)} ${option.description} ${void 0 === option.config.default ? "" : `(default: ${option.config.default})`}`).join("\n")
|
|
2685
|
+
});
|
|
2339
2686
|
}
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
}
|
|
2351
|
-
}
|
|
2352
|
-
},
|
|
2353
|
-
plugins: [
|
|
2354
|
-
resolvedShims.esm.require && pluginEsmRequireShim(),
|
|
2355
|
-
disableUrlParseRsbuildPlugin()
|
|
2356
|
-
].filter(Boolean)
|
|
2357
|
-
};
|
|
2358
|
-
break;
|
|
2359
|
-
case 'cjs':
|
|
2360
|
-
rsbuildConfig = {
|
|
2361
|
-
plugins: [
|
|
2362
|
-
resolvedShims.cjs['import.meta.url'] && pluginCjsImportMetaUrlShim(),
|
|
2363
|
-
disableUrlParseRsbuildPlugin()
|
|
2364
|
-
].filter(Boolean)
|
|
2365
|
-
};
|
|
2366
|
-
break;
|
|
2367
|
-
case 'umd':
|
|
2368
|
-
case 'iife':
|
|
2369
|
-
case 'mf':
|
|
2370
|
-
break;
|
|
2371
|
-
default:
|
|
2372
|
-
throw new Error(`Unsupported format: ${format}`);
|
|
2687
|
+
if (this.examples.length > 0) sections.push({
|
|
2688
|
+
title: "Examples",
|
|
2689
|
+
body: this.examples.map((example)=>{
|
|
2690
|
+
if ("function" == typeof example) return example(name);
|
|
2691
|
+
return example;
|
|
2692
|
+
}).join("\n")
|
|
2693
|
+
});
|
|
2694
|
+
if (helpCallback) sections = helpCallback(sections) || sections;
|
|
2695
|
+
console.log(sections.map((section)=>section.title ? `${section.title}:
|
|
2696
|
+
${section.body}` : section.body).join("\n\n"));
|
|
2373
2697
|
}
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
};
|
|
2379
|
-
const composeModuleImportWarn = (request, issuer)=>`The externalized commonjs request ${index_js_default.green(`"${request}"`)} from ${index_js_default.green(issuer)} will use ${index_js_default.blue('"module"')} external type in ESM format. If you want to specify other external type, consider setting the request and type with ${index_js_default.blue('"output.externals"')}.`;
|
|
2380
|
-
const composeExternalsConfig = (format, externals)=>{
|
|
2381
|
-
const externalsTypeMap = {
|
|
2382
|
-
esm: 'module-import',
|
|
2383
|
-
cjs: 'commonjs-import',
|
|
2384
|
-
umd: 'umd',
|
|
2385
|
-
mf: 'global',
|
|
2386
|
-
iife: 'global'
|
|
2387
|
-
};
|
|
2388
|
-
const globalObjectMap = {
|
|
2389
|
-
esm: void 0,
|
|
2390
|
-
cjs: void 0,
|
|
2391
|
-
umd: void 0,
|
|
2392
|
-
mf: void 0,
|
|
2393
|
-
iife: 'globalThis'
|
|
2394
|
-
};
|
|
2395
|
-
const rspackConfig = {};
|
|
2396
|
-
const rsbuildConfig = {};
|
|
2397
|
-
switch(format){
|
|
2398
|
-
case 'esm':
|
|
2399
|
-
case 'cjs':
|
|
2400
|
-
case 'umd':
|
|
2401
|
-
case 'mf':
|
|
2402
|
-
case 'iife':
|
|
2403
|
-
rsbuildConfig.output = externals ? {
|
|
2404
|
-
externals
|
|
2405
|
-
} : {};
|
|
2406
|
-
rspackConfig.externalsType = externalsTypeMap[format];
|
|
2407
|
-
if (globalObjectMap[format]) rspackConfig.output = {
|
|
2408
|
-
globalObject: globalObjectMap[format]
|
|
2409
|
-
};
|
|
2410
|
-
break;
|
|
2411
|
-
default:
|
|
2412
|
-
throw new Error(`Unsupported format: ${format}`);
|
|
2698
|
+
outputVersion() {
|
|
2699
|
+
const { name } = this.cli;
|
|
2700
|
+
const { versionNumber } = this.cli.globalCommand;
|
|
2701
|
+
if (versionNumber) console.log(`${name}/${versionNumber} ${platformInfo}`);
|
|
2413
2702
|
}
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
const
|
|
2422
|
-
const { jsExtension, dtsExtension } = getDefaultExtension({
|
|
2423
|
-
format,
|
|
2424
|
-
pkgJson,
|
|
2425
|
-
autoExtension
|
|
2426
|
-
});
|
|
2427
|
-
const filenameHash = config.output?.filenameHash ?? false;
|
|
2428
|
-
const getHash = ()=>{
|
|
2429
|
-
if ('string' == typeof filenameHash) return filenameHash ? `.[${filenameHash}]` : '';
|
|
2430
|
-
return filenameHash ? '.[contenthash:8]' : '';
|
|
2431
|
-
};
|
|
2432
|
-
const hash = getHash();
|
|
2433
|
-
const defaultJsFilename = `[name]${hash}${jsExtension}`;
|
|
2434
|
-
const userJsFilename = config.output?.filename?.js;
|
|
2435
|
-
const finalJsExtension = 'string' == typeof userJsFilename && userJsFilename ? external_node_path_extname(userJsFilename) : jsExtension;
|
|
2436
|
-
const finalConfig = userJsFilename ? {} : {
|
|
2437
|
-
output: {
|
|
2438
|
-
filename: {
|
|
2439
|
-
js: defaultJsFilename
|
|
2440
|
-
}
|
|
2441
|
-
}
|
|
2442
|
-
};
|
|
2443
|
-
return {
|
|
2444
|
-
config: finalConfig,
|
|
2445
|
-
jsExtension: finalJsExtension,
|
|
2446
|
-
dtsExtension
|
|
2447
|
-
};
|
|
2448
|
-
};
|
|
2449
|
-
const composeSyntaxConfig = (target, syntax)=>{
|
|
2450
|
-
if (syntax) return {
|
|
2451
|
-
tools: {
|
|
2452
|
-
rspack: (config)=>{
|
|
2453
|
-
config.target = transformSyntaxToRspackTarget(syntax);
|
|
2454
|
-
}
|
|
2455
|
-
},
|
|
2456
|
-
output: {
|
|
2457
|
-
overrideBrowserslist: transformSyntaxToBrowserslist(syntax, target)
|
|
2703
|
+
checkRequiredArgs() {
|
|
2704
|
+
const minimalArgsCount = this.args.filter((arg)=>arg.required).length;
|
|
2705
|
+
if (this.cli.args.length < minimalArgsCount) throw new CACError(`missing required args for command \`${this.rawName}\``);
|
|
2706
|
+
}
|
|
2707
|
+
checkUnknownOptions() {
|
|
2708
|
+
const { options, globalCommand } = this.cli;
|
|
2709
|
+
if (!this.config.allowUnknownOptions) {
|
|
2710
|
+
for (const name of Object.keys(options))if ("--" !== name && !this.hasOption(name) && !globalCommand.hasOption(name)) throw new CACError(`Unknown option \`${name.length > 1 ? `--${name}` : `-${name}`}\``);
|
|
2458
2711
|
}
|
|
2459
|
-
}
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2712
|
+
}
|
|
2713
|
+
checkOptionValue() {
|
|
2714
|
+
const { options: parsedOptions, globalCommand } = this.cli;
|
|
2715
|
+
const options = [
|
|
2716
|
+
...globalCommand.options,
|
|
2717
|
+
...this.options
|
|
2718
|
+
];
|
|
2719
|
+
for (const option of options){
|
|
2720
|
+
const value = parsedOptions[option.name.split(".")[0]];
|
|
2721
|
+
if (option.required) {
|
|
2722
|
+
const hasNegated = options.some((o)=>o.negated && o.names.includes(option.name));
|
|
2723
|
+
if (true === value || false === value && !hasNegated) throw new CACError(`option \`${option.rawName}\` value is missing`);
|
|
2467
2724
|
}
|
|
2468
|
-
},
|
|
2469
|
-
output: {
|
|
2470
|
-
overrideBrowserslist: ESX_TO_BROWSERSLIST.esnext(target)
|
|
2471
2725
|
}
|
|
2472
|
-
};
|
|
2473
|
-
};
|
|
2474
|
-
const traverseEntryQuery = (entry, callback)=>{
|
|
2475
|
-
const newEntry = {};
|
|
2476
|
-
for (const [key, value] of Object.entries(entry)){
|
|
2477
|
-
let result = value;
|
|
2478
|
-
result = 'string' == typeof value ? callback(value) : Array.isArray(value) ? value.map(callback) : {
|
|
2479
|
-
...value,
|
|
2480
|
-
import: 'string' == typeof value.import ? callback(value.import) : value.import.map(callback)
|
|
2481
|
-
};
|
|
2482
|
-
newEntry[key] = result;
|
|
2483
2726
|
}
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2727
|
+
}
|
|
2728
|
+
class GlobalCommand extends Command {
|
|
2729
|
+
constructor(cli){
|
|
2730
|
+
super("@@global@@", "", {}, cli);
|
|
2731
|
+
}
|
|
2732
|
+
}
|
|
2733
|
+
var __assign = Object.assign;
|
|
2734
|
+
class CAC extends EventEmitter {
|
|
2735
|
+
constructor(name = ""){
|
|
2736
|
+
super();
|
|
2737
|
+
this.name = name;
|
|
2738
|
+
this.commands = [];
|
|
2739
|
+
this.rawArgs = [];
|
|
2740
|
+
this.args = [];
|
|
2741
|
+
this.options = {};
|
|
2742
|
+
this.globalCommand = new GlobalCommand(this);
|
|
2743
|
+
this.globalCommand.usage("<command> [options]");
|
|
2744
|
+
}
|
|
2745
|
+
usage(text) {
|
|
2746
|
+
this.globalCommand.usage(text);
|
|
2747
|
+
return this;
|
|
2748
|
+
}
|
|
2749
|
+
command(rawName, description, config) {
|
|
2750
|
+
const command = new Command(rawName, description || "", config, this);
|
|
2751
|
+
command.globalCommand = this.globalCommand;
|
|
2752
|
+
this.commands.push(command);
|
|
2753
|
+
return command;
|
|
2754
|
+
}
|
|
2755
|
+
option(rawName, description, config) {
|
|
2756
|
+
this.globalCommand.option(rawName, description, config);
|
|
2757
|
+
return this;
|
|
2758
|
+
}
|
|
2759
|
+
help(callback) {
|
|
2760
|
+
this.globalCommand.option("-h, --help", "Display this message");
|
|
2761
|
+
this.globalCommand.helpCallback = callback;
|
|
2762
|
+
this.showHelpOnExit = true;
|
|
2763
|
+
return this;
|
|
2764
|
+
}
|
|
2765
|
+
version(version, customFlags = "-v, --version") {
|
|
2766
|
+
this.globalCommand.version(version, customFlags);
|
|
2767
|
+
this.showVersionOnExit = true;
|
|
2768
|
+
return this;
|
|
2769
|
+
}
|
|
2770
|
+
example(example) {
|
|
2771
|
+
this.globalCommand.example(example);
|
|
2772
|
+
return this;
|
|
2773
|
+
}
|
|
2774
|
+
outputHelp() {
|
|
2775
|
+
if (this.matchedCommand) this.matchedCommand.outputHelp();
|
|
2776
|
+
else this.globalCommand.outputHelp();
|
|
2497
2777
|
}
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
const entryErrorReasons = [];
|
|
2501
|
-
traverseEntryQuery(entries, (entry)=>{
|
|
2502
|
-
const entryAbsPath = external_node_path_default.isAbsolute(entry) ? entry : external_node_path_default.resolve(root, entry);
|
|
2503
|
-
const isDirLike = '' === external_node_path_default.extname(entryAbsPath);
|
|
2504
|
-
const dirError = `Glob pattern ${index_js_default.cyan(`"${entry}"`)} is not supported when "bundle" is "true", considering "bundle" to "false" to use bundleless mode, or specify a file entry to bundle. See ${index_js_default.green('https://rslib.rs/guide/basic/output-structure')} for more details.`;
|
|
2505
|
-
if (external_node_fs_default.existsSync(entryAbsPath)) {
|
|
2506
|
-
const stats = external_node_fs_default.statSync(entryAbsPath);
|
|
2507
|
-
if (!stats.isFile()) entryErrorReasons.push(dirError);
|
|
2508
|
-
} else if (isDirLike) entryErrorReasons.push(dirError);
|
|
2509
|
-
else entryErrorReasons.push(`Can't resolve the entry ${index_js_default.cyan(`"${entry}"`)} at the location ${index_js_default.cyan(`${entryAbsPath}`)}. Please ensure that the file exists.`);
|
|
2510
|
-
return entry;
|
|
2511
|
-
});
|
|
2512
|
-
if (entryErrorReasons.length) throw new AggregateError(entryErrorReasons.map((reason)=>new Error(reason)));
|
|
2513
|
-
return {
|
|
2514
|
-
entryConfig: {
|
|
2515
|
-
source: {
|
|
2516
|
-
entry: resolveEntryPath(entries, root)
|
|
2517
|
-
}
|
|
2518
|
-
},
|
|
2519
|
-
outBase: null
|
|
2520
|
-
};
|
|
2778
|
+
outputVersion() {
|
|
2779
|
+
this.globalCommand.outputVersion();
|
|
2521
2780
|
}
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
const
|
|
2540
|
-
if (
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2781
|
+
setParsedInfo({ args, options }, matchedCommand, matchedCommandName) {
|
|
2782
|
+
this.args = args;
|
|
2783
|
+
this.options = options;
|
|
2784
|
+
if (matchedCommand) this.matchedCommand = matchedCommand;
|
|
2785
|
+
if (matchedCommandName) this.matchedCommandName = matchedCommandName;
|
|
2786
|
+
return this;
|
|
2787
|
+
}
|
|
2788
|
+
unsetMatchedCommand() {
|
|
2789
|
+
this.matchedCommand = void 0;
|
|
2790
|
+
this.matchedCommandName = void 0;
|
|
2791
|
+
}
|
|
2792
|
+
parse(argv = processArgs, { run = true } = {}) {
|
|
2793
|
+
this.rawArgs = argv;
|
|
2794
|
+
if (!this.name) this.name = argv[1] ? getFileName(argv[1]) : "cli";
|
|
2795
|
+
let shouldParse = true;
|
|
2796
|
+
for (const command of this.commands){
|
|
2797
|
+
const parsed = this.mri(argv.slice(2), command);
|
|
2798
|
+
const commandName = parsed.args[0];
|
|
2799
|
+
if (command.isMatched(commandName)) {
|
|
2800
|
+
shouldParse = false;
|
|
2801
|
+
const parsedInfo = __assign(__assign({}, parsed), {
|
|
2802
|
+
args: parsed.args.slice(1)
|
|
2803
|
+
});
|
|
2804
|
+
this.setParsedInfo(parsedInfo, command, commandName);
|
|
2805
|
+
this.emit(`command:${commandName}`, command);
|
|
2547
2806
|
}
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2807
|
+
}
|
|
2808
|
+
if (shouldParse) {
|
|
2809
|
+
for (const command of this.commands)if ("" === command.name) {
|
|
2810
|
+
shouldParse = false;
|
|
2811
|
+
const parsed = this.mri(argv.slice(2), command);
|
|
2812
|
+
this.setParsedInfo(parsed, command);
|
|
2813
|
+
this.emit("command:!", command);
|
|
2552
2814
|
}
|
|
2553
2815
|
}
|
|
2554
|
-
if (
|
|
2555
|
-
const
|
|
2556
|
-
|
|
2557
|
-
resolvedEntries,
|
|
2558
|
-
outBase
|
|
2559
|
-
};
|
|
2816
|
+
if (shouldParse) {
|
|
2817
|
+
const parsed = this.mri(argv.slice(2));
|
|
2818
|
+
this.setParsedInfo(parsed);
|
|
2560
2819
|
}
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
};
|
|
2566
|
-
const { outBase } = await scanGlobEntries(true);
|
|
2567
|
-
const entryConfig = {
|
|
2568
|
-
tools: {
|
|
2569
|
-
rspack: {
|
|
2570
|
-
entry: async ()=>{
|
|
2571
|
-
const { resolvedEntries } = await scanGlobEntries(false);
|
|
2572
|
-
return resolvedEntries;
|
|
2573
|
-
}
|
|
2574
|
-
}
|
|
2820
|
+
if (this.options.help && this.showHelpOnExit) {
|
|
2821
|
+
this.outputHelp();
|
|
2822
|
+
run = false;
|
|
2823
|
+
this.unsetMatchedCommand();
|
|
2575
2824
|
}
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
};
|
|
2581
|
-
};
|
|
2582
|
-
const composeBundlelessExternalConfig = (jsExtension, redirect, cssModulesAuto, bundle, outBase)=>{
|
|
2583
|
-
if (bundle) return {
|
|
2584
|
-
config: {}
|
|
2585
|
-
};
|
|
2586
|
-
const styleRedirectPath = redirect.style?.path ?? true;
|
|
2587
|
-
const styleRedirectExtension = redirect.style?.extension ?? true;
|
|
2588
|
-
const jsRedirectPath = redirect.js?.path ?? true;
|
|
2589
|
-
const jsRedirectExtension = redirect.js?.extension ?? true;
|
|
2590
|
-
const assetRedirect = redirect.asset ?? true;
|
|
2591
|
-
let resolver;
|
|
2592
|
-
return {
|
|
2593
|
-
resolvedJsRedirect: {
|
|
2594
|
-
path: jsRedirectPath,
|
|
2595
|
-
extension: jsRedirectExtension
|
|
2596
|
-
},
|
|
2597
|
-
config: {
|
|
2598
|
-
output: {
|
|
2599
|
-
externals: [
|
|
2600
|
-
async (data, callback)=>{
|
|
2601
|
-
const { request, getResolve, context, contextInfo } = data;
|
|
2602
|
-
if (!request || !getResolve || !context || !contextInfo) return callback();
|
|
2603
|
-
const { issuer } = contextInfo;
|
|
2604
|
-
if (!resolver) resolver = await getResolve();
|
|
2605
|
-
async function redirectPath(request) {
|
|
2606
|
-
try {
|
|
2607
|
-
let resolvedRequest = request;
|
|
2608
|
-
resolvedRequest = await resolver(context, resolvedRequest);
|
|
2609
|
-
if ('string' != typeof outBase) throw new Error(`outBase expect to be a string in bundleless mode, but got ${outBase}`);
|
|
2610
|
-
const isSubpath = normalizeSlash(resolvedRequest).startsWith(`${normalizeSlash(outBase)}/`);
|
|
2611
|
-
if (isSubpath) {
|
|
2612
|
-
resolvedRequest = normalizeSlash(external_node_path_default.relative(external_node_path_default.dirname(issuer), resolvedRequest));
|
|
2613
|
-
if ('.' !== resolvedRequest[0]) resolvedRequest = `./${resolvedRequest}`;
|
|
2614
|
-
return resolvedRequest;
|
|
2615
|
-
}
|
|
2616
|
-
return;
|
|
2617
|
-
} catch (e) {
|
|
2618
|
-
logger.debug(`Failed to resolve module ${index_js_default.green(`"${request}"`)} from ${index_js_default.green(issuer)}. If it's an npm package, consider adding it to dependencies or peerDependencies in package.json to make it externalized.`);
|
|
2619
|
-
return request;
|
|
2620
|
-
}
|
|
2621
|
-
}
|
|
2622
|
-
if (issuer) {
|
|
2623
|
-
let resolvedRequest = request;
|
|
2624
|
-
const cssExternal = await cssExternalHandler(resolvedRequest, callback, jsExtension, cssModulesAuto, styleRedirectPath, styleRedirectExtension, redirectPath, issuer);
|
|
2625
|
-
if (false !== cssExternal) return cssExternal;
|
|
2626
|
-
if (jsRedirectPath) {
|
|
2627
|
-
const redirectedPath = await redirectPath(resolvedRequest);
|
|
2628
|
-
if (void 0 === redirectedPath) return callback(void 0, request);
|
|
2629
|
-
resolvedRequest = redirectedPath;
|
|
2630
|
-
}
|
|
2631
|
-
if (resolvedRequest.startsWith('.')) {
|
|
2632
|
-
const ext = external_node_path_extname(resolvedRequest);
|
|
2633
|
-
if (ext) {
|
|
2634
|
-
if (JS_EXTENSIONS_PATTERN.test(resolvedRequest)) {
|
|
2635
|
-
if (jsRedirectExtension) resolvedRequest = resolvedRequest.replace(/\.[^.]+$/, jsExtension);
|
|
2636
|
-
} else if (assetRedirect) resolvedRequest = resolvedRequest.replace(/\.[^.]+$/, jsExtension);
|
|
2637
|
-
} else if (jsRedirectExtension) resolvedRequest = `${resolvedRequest}${jsExtension}`;
|
|
2638
|
-
}
|
|
2639
|
-
return callback(void 0, resolvedRequest);
|
|
2640
|
-
}
|
|
2641
|
-
callback();
|
|
2642
|
-
}
|
|
2643
|
-
]
|
|
2644
|
-
}
|
|
2825
|
+
if (this.options.version && this.showVersionOnExit && null == this.matchedCommandName) {
|
|
2826
|
+
this.outputVersion();
|
|
2827
|
+
run = false;
|
|
2828
|
+
this.unsetMatchedCommand();
|
|
2645
2829
|
}
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
bundle: false
|
|
2654
|
-
};
|
|
2655
|
-
const { pluginDts } = await import("rsbuild-plugin-dts");
|
|
2656
|
-
return {
|
|
2657
|
-
plugins: [
|
|
2658
|
-
pluginDts({
|
|
2659
|
-
bundle: dts?.bundle,
|
|
2660
|
-
distPath: dts?.distPath,
|
|
2661
|
-
build: dts?.build,
|
|
2662
|
-
abortOnError: dts?.abortOnError,
|
|
2663
|
-
dtsExtension: dts?.autoExtension ? dtsExtension : '.d.ts',
|
|
2664
|
-
autoExternal: getAutoExternalDefaultValue(format, autoExternal),
|
|
2665
|
-
banner: banner?.dts,
|
|
2666
|
-
footer: footer?.dts,
|
|
2667
|
-
redirect: redirect?.dts
|
|
2668
|
-
})
|
|
2669
|
-
]
|
|
2670
|
-
};
|
|
2671
|
-
};
|
|
2672
|
-
const composeTargetConfig = (userTarget, format)=>{
|
|
2673
|
-
const target = userTarget ?? ('mf' === format ? 'web' : 'node');
|
|
2674
|
-
switch(target){
|
|
2675
|
-
case 'web':
|
|
2676
|
-
return {
|
|
2677
|
-
config: {
|
|
2678
|
-
tools: {
|
|
2679
|
-
rspack: {
|
|
2680
|
-
target: [
|
|
2681
|
-
'web'
|
|
2682
|
-
]
|
|
2683
|
-
}
|
|
2684
|
-
}
|
|
2685
|
-
},
|
|
2686
|
-
target: 'web',
|
|
2687
|
-
externalsConfig: {}
|
|
2688
|
-
};
|
|
2689
|
-
case 'node':
|
|
2690
|
-
return {
|
|
2691
|
-
config: {
|
|
2692
|
-
tools: {
|
|
2693
|
-
rspack: {
|
|
2694
|
-
target: [
|
|
2695
|
-
'node'
|
|
2696
|
-
]
|
|
2697
|
-
}
|
|
2698
|
-
},
|
|
2699
|
-
output: {
|
|
2700
|
-
target: 'node'
|
|
2701
|
-
}
|
|
2702
|
-
},
|
|
2703
|
-
target: 'node',
|
|
2704
|
-
externalsConfig: {
|
|
2705
|
-
output: {
|
|
2706
|
-
externals: nodeBuiltInModules
|
|
2707
|
-
}
|
|
2708
|
-
}
|
|
2709
|
-
};
|
|
2710
|
-
default:
|
|
2711
|
-
throw new Error(`Unsupported platform: ${target}`);
|
|
2830
|
+
const parsedArgv = {
|
|
2831
|
+
args: this.args,
|
|
2832
|
+
options: this.options
|
|
2833
|
+
};
|
|
2834
|
+
if (run) this.runMatchedCommand();
|
|
2835
|
+
if (!this.matchedCommand && this.args[0]) this.emit("command:*");
|
|
2836
|
+
return parsedArgv;
|
|
2712
2837
|
}
|
|
2713
|
-
|
|
2714
|
-
const
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2838
|
+
mri(argv, command) {
|
|
2839
|
+
const cliOptions = [
|
|
2840
|
+
...this.globalCommand.options,
|
|
2841
|
+
...command ? command.options : []
|
|
2842
|
+
];
|
|
2843
|
+
const mriOptions = getMriOptions(cliOptions);
|
|
2844
|
+
let argsAfterDoubleDashes = [];
|
|
2845
|
+
const doubleDashesIndex = argv.indexOf("--");
|
|
2846
|
+
if (doubleDashesIndex > -1) {
|
|
2847
|
+
argsAfterDoubleDashes = argv.slice(doubleDashesIndex + 1);
|
|
2848
|
+
argv = argv.slice(0, doubleDashesIndex);
|
|
2849
|
+
}
|
|
2850
|
+
let parsed = mri2(argv, mriOptions);
|
|
2851
|
+
parsed = Object.keys(parsed).reduce((res, name)=>__assign(__assign({}, res), {
|
|
2852
|
+
[camelcaseOptionName(name)]: parsed[name]
|
|
2853
|
+
}), {
|
|
2854
|
+
_: []
|
|
2855
|
+
});
|
|
2856
|
+
const args = parsed._;
|
|
2857
|
+
const options = {
|
|
2858
|
+
"--": argsAfterDoubleDashes
|
|
2859
|
+
};
|
|
2860
|
+
const ignoreDefault = command && command.config.ignoreOptionDefaultValue ? command.config.ignoreOptionDefaultValue : this.globalCommand.config.ignoreOptionDefaultValue;
|
|
2861
|
+
let transforms = Object.create(null);
|
|
2862
|
+
for (const cliOption of cliOptions){
|
|
2863
|
+
if (!ignoreDefault && void 0 !== cliOption.config.default) for (const name of cliOption.names)options[name] = cliOption.config.default;
|
|
2864
|
+
if (Array.isArray(cliOption.config.type)) {
|
|
2865
|
+
if (void 0 === transforms[cliOption.name]) {
|
|
2866
|
+
transforms[cliOption.name] = Object.create(null);
|
|
2867
|
+
transforms[cliOption.name]["shouldTransform"] = true;
|
|
2868
|
+
transforms[cliOption.name]["transformFunction"] = cliOption.config.type[0];
|
|
2720
2869
|
}
|
|
2721
2870
|
}
|
|
2722
2871
|
}
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
...Object.keys(pkgJson?.devDependencies ?? [])
|
|
2728
|
-
];
|
|
2729
|
-
if (!deps.includes(SWC_HELPERS)) {
|
|
2730
|
-
logger.error(`${index_js_default.green('externalHelpers')} is enabled, but the ${index_js_default.blue(SWC_HELPERS)} dependency declaration was not found in package.json.`);
|
|
2731
|
-
process.exit(1);
|
|
2872
|
+
for (const key of Object.keys(parsed))if ("_" !== key) {
|
|
2873
|
+
const keys = key.split(".");
|
|
2874
|
+
setDotProp(options, keys, parsed[key]);
|
|
2875
|
+
setByType(options, transforms);
|
|
2732
2876
|
}
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
}
|
|
2737
|
-
});
|
|
2738
|
-
defaultConfig.tools.swc.jsc.externalHelpers = true;
|
|
2739
|
-
}
|
|
2740
|
-
return defaultConfig;
|
|
2741
|
-
};
|
|
2742
|
-
async function composeLibRsbuildConfig(config, root, sharedPlugins) {
|
|
2743
|
-
checkMFPlugin(config, sharedPlugins);
|
|
2744
|
-
const rootPath = root ? getAbsolutePath(process.cwd(), root) : process.cwd();
|
|
2745
|
-
const pkgJson = readPackageJson(rootPath);
|
|
2746
|
-
const { compilerOptions } = await loadTsconfig(rootPath, config.source?.tsconfigPath);
|
|
2747
|
-
const cssModulesAuto = config.output?.cssModules?.auto ?? true;
|
|
2748
|
-
const { format = 'esm', shims, bundle = true, banner = {}, footer = {}, autoExtension = true, autoExternal, externalHelpers = false, redirect = {}, umdName } = config;
|
|
2749
|
-
const { rsbuildConfig: shimsConfig, enabledShims } = composeShimsConfig(format, shims);
|
|
2750
|
-
const formatConfig = composeFormatConfig({
|
|
2751
|
-
format,
|
|
2752
|
-
pkgJson: pkgJson,
|
|
2753
|
-
bundle,
|
|
2754
|
-
umdName
|
|
2755
|
-
});
|
|
2756
|
-
const externalHelpersConfig = composeExternalHelpersConfig(externalHelpers, pkgJson);
|
|
2757
|
-
const userExternalsConfig = composeExternalsConfig(format, config.output?.externals);
|
|
2758
|
-
const { config: autoExtensionConfig, jsExtension, dtsExtension } = composeAutoExtensionConfig(config, format, autoExtension, pkgJson);
|
|
2759
|
-
const { entryConfig, outBase } = await composeEntryConfig(config.source?.entry, config.bundle, rootPath, cssModulesAuto, config.outBase);
|
|
2760
|
-
const { config: bundlelessExternalConfig } = composeBundlelessExternalConfig(jsExtension, redirect, cssModulesAuto, bundle, outBase);
|
|
2761
|
-
const { config: targetConfig, externalsConfig: targetExternalsConfig, target } = composeTargetConfig(config.output?.target, format);
|
|
2762
|
-
const syntaxConfig = composeSyntaxConfig(target, config?.syntax);
|
|
2763
|
-
const autoExternalConfig = composeAutoExternalConfig({
|
|
2764
|
-
bundle,
|
|
2765
|
-
format,
|
|
2766
|
-
autoExternal,
|
|
2767
|
-
pkgJson,
|
|
2768
|
-
userExternals: config.output?.externals
|
|
2769
|
-
});
|
|
2770
|
-
const cssConfig = composeCssConfig(outBase, cssModulesAuto, config.bundle, banner?.css, footer?.css);
|
|
2771
|
-
const assetConfig = composeAssetConfig(bundle, format);
|
|
2772
|
-
const entryChunkConfig = composeEntryChunkConfig({
|
|
2773
|
-
enabledImportMetaUrlShim: enabledShims.cjs['import.meta.url'],
|
|
2774
|
-
contextToWatch: outBase
|
|
2775
|
-
});
|
|
2776
|
-
const dtsConfig = await composeDtsConfig(config, format, dtsExtension);
|
|
2777
|
-
const externalsWarnConfig = composeExternalsWarnConfig(format, userExternalsConfig?.output?.externals, autoExternalConfig?.output?.externals);
|
|
2778
|
-
const minifyConfig = composeMinifyConfig(config);
|
|
2779
|
-
const bannerFooterConfig = composeBannerFooterConfig(banner, footer);
|
|
2780
|
-
const decoratorsConfig = composeDecoratorsConfig(compilerOptions, config.source?.decorators?.version);
|
|
2781
|
-
return (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.mergeRsbuildConfig)(formatConfig, shimsConfig, syntaxConfig, externalHelpersConfig, autoExtensionConfig, targetConfig, externalsWarnConfig, userExternalsConfig, autoExternalConfig, targetExternalsConfig, bundlelessExternalConfig, entryConfig, cssConfig, assetConfig, entryChunkConfig, minifyConfig, dtsConfig, bannerFooterConfig, decoratorsConfig);
|
|
2782
|
-
}
|
|
2783
|
-
async function composeCreateRsbuildConfig(rslibConfig) {
|
|
2784
|
-
const constantRsbuildConfig = await createConstantRsbuildConfig();
|
|
2785
|
-
const { lib: libConfigsArray, mode, root, plugins: sharedPlugins, dev, server, ...sharedRsbuildConfig } = rslibConfig;
|
|
2786
|
-
if (!Array.isArray(libConfigsArray) || 0 === libConfigsArray.length) throw new Error(`Expect "lib" field to be a non-empty array, but got: ${index_js_default.cyan(JSON.stringify(libConfigsArray))}.`);
|
|
2787
|
-
const libConfigPromises = libConfigsArray.map(async (libConfig)=>{
|
|
2788
|
-
const userConfig = (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.mergeRsbuildConfig)(sharedRsbuildConfig, libConfig);
|
|
2789
|
-
const libRsbuildConfig = await composeLibRsbuildConfig(userConfig, root, sharedPlugins);
|
|
2790
|
-
userConfig.source ??= {};
|
|
2791
|
-
userConfig.source.entry = {};
|
|
2792
|
-
userConfig.output ??= {};
|
|
2793
|
-
delete userConfig.output.externals;
|
|
2794
|
-
const config = {
|
|
2795
|
-
format: libConfig.format ?? 'esm',
|
|
2796
|
-
config: (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.mergeRsbuildConfig)(constantRsbuildConfig, libRsbuildConfig, omit(userConfig, {
|
|
2797
|
-
id: true,
|
|
2798
|
-
bundle: true,
|
|
2799
|
-
format: true,
|
|
2800
|
-
autoExtension: true,
|
|
2801
|
-
autoExternal: true,
|
|
2802
|
-
redirect: true,
|
|
2803
|
-
syntax: true,
|
|
2804
|
-
externalHelpers: true,
|
|
2805
|
-
banner: true,
|
|
2806
|
-
footer: true,
|
|
2807
|
-
dts: true,
|
|
2808
|
-
shims: true,
|
|
2809
|
-
umdName: true,
|
|
2810
|
-
outBase: true
|
|
2811
|
-
}))
|
|
2877
|
+
return {
|
|
2878
|
+
args,
|
|
2879
|
+
options
|
|
2812
2880
|
};
|
|
2813
|
-
if ('string' == typeof libConfig.id) config.id = libConfig.id;
|
|
2814
|
-
return config;
|
|
2815
|
-
});
|
|
2816
|
-
const composedRsbuildConfig = await Promise.all(libConfigPromises);
|
|
2817
|
-
return composedRsbuildConfig;
|
|
2818
|
-
}
|
|
2819
|
-
async function composeRsbuildEnvironments(rslibConfig) {
|
|
2820
|
-
const rsbuildConfigWithLibInfo = await composeCreateRsbuildConfig(rslibConfig);
|
|
2821
|
-
const environmentWithInfos = [];
|
|
2822
|
-
const usedIds = rsbuildConfigWithLibInfo.map(({ id })=>id).filter(Boolean);
|
|
2823
|
-
const environments = {};
|
|
2824
|
-
const formatCount = rsbuildConfigWithLibInfo.reduce((acc, { format })=>{
|
|
2825
|
-
acc[format] = (acc[format] ?? 0) + 1;
|
|
2826
|
-
return acc;
|
|
2827
|
-
}, {});
|
|
2828
|
-
const composeDefaultId = (format)=>{
|
|
2829
|
-
const nextDefaultId = (format, index)=>`${format}${1 === formatCount[format] && 0 === index ? '' : index}`;
|
|
2830
|
-
let index = 0;
|
|
2831
|
-
let candidateId = nextDefaultId(format, index);
|
|
2832
|
-
while(-1 !== usedIds.indexOf(candidateId))candidateId = nextDefaultId(format, ++index);
|
|
2833
|
-
usedIds.push(candidateId);
|
|
2834
|
-
return candidateId;
|
|
2835
|
-
};
|
|
2836
|
-
for (const { format, id, config } of rsbuildConfigWithLibInfo){
|
|
2837
|
-
const libId = 'string' == typeof id ? id : composeDefaultId(format);
|
|
2838
|
-
environments[libId] = config;
|
|
2839
|
-
environmentWithInfos.push({
|
|
2840
|
-
id: libId,
|
|
2841
|
-
format,
|
|
2842
|
-
config
|
|
2843
|
-
});
|
|
2844
2881
|
}
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
if (0 === Object.keys(filteredEnvironments).length) throw new Error(`The following libs are not found: ${libs.map((lib)=>`"${lib}"`).join(', ')}.`);
|
|
2856
|
-
return filteredEnvironments;
|
|
2857
|
-
};
|
|
2858
|
-
async function watchFilesForRestart(files, restart) {
|
|
2859
|
-
if (!files.length) return;
|
|
2860
|
-
const chokidar = await import("../compiled/chokidar/index.js");
|
|
2861
|
-
const watcher = chokidar.watch(files, {
|
|
2862
|
-
ignoreInitial: true,
|
|
2863
|
-
ignorePermissionErrors: true
|
|
2864
|
-
});
|
|
2865
|
-
const callback = debounce(async (filePath)=>{
|
|
2866
|
-
watcher.close();
|
|
2867
|
-
await beforeRestart({
|
|
2868
|
-
filePath
|
|
2882
|
+
runMatchedCommand() {
|
|
2883
|
+
const { args, options, matchedCommand: command } = this;
|
|
2884
|
+
if (!command || !command.commandAction) return;
|
|
2885
|
+
command.checkUnknownOptions();
|
|
2886
|
+
command.checkOptionValue();
|
|
2887
|
+
command.checkRequiredArgs();
|
|
2888
|
+
const actionArgs = [];
|
|
2889
|
+
command.args.forEach((arg, index)=>{
|
|
2890
|
+
if (arg.variadic) actionArgs.push(args.slice(index));
|
|
2891
|
+
else actionArgs.push(args[index]);
|
|
2869
2892
|
});
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
watcher.on('change', callback);
|
|
2874
|
-
watcher.on('unlink', callback);
|
|
2875
|
-
}
|
|
2876
|
-
let cleaners = [];
|
|
2877
|
-
const onBeforeRestart = (cleaner)=>{
|
|
2878
|
-
cleaners.push(cleaner);
|
|
2879
|
-
};
|
|
2880
|
-
const clearConsole = ()=>{
|
|
2881
|
-
if (isTTY() && !process.env.DEBUG) process.stdout.write('\x1B[H\x1B[2J');
|
|
2882
|
-
};
|
|
2883
|
-
const beforeRestart = async ({ filePath, clear = true } = {})=>{
|
|
2884
|
-
if (clear) clearConsole();
|
|
2885
|
-
if (filePath) {
|
|
2886
|
-
const filename = external_node_path_default.basename(filePath);
|
|
2887
|
-
logger.info(`restart because ${index_js_default.yellow(filename)} is changed.\n`);
|
|
2888
|
-
} else logger.info('restarting...\n');
|
|
2889
|
-
for (const cleaner of cleaners)await cleaner();
|
|
2890
|
-
cleaners = [];
|
|
2891
|
-
};
|
|
2892
|
-
async function build(config, options = {}) {
|
|
2893
|
-
const { environments } = await composeRsbuildEnvironments(config);
|
|
2894
|
-
const rsbuildInstance = await (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.createRsbuild)({
|
|
2895
|
-
callerName: 'rslib',
|
|
2896
|
-
rsbuildConfig: {
|
|
2897
|
-
mode: 'production',
|
|
2898
|
-
root: config.root,
|
|
2899
|
-
plugins: config.plugins,
|
|
2900
|
-
dev: config.dev,
|
|
2901
|
-
server: config.server,
|
|
2902
|
-
environments: pruneEnvironments(environments, options.lib)
|
|
2903
|
-
}
|
|
2904
|
-
});
|
|
2905
|
-
const buildInstance = await rsbuildInstance.build({
|
|
2906
|
-
watch: options.watch
|
|
2907
|
-
});
|
|
2908
|
-
if (options.watch) onBeforeRestart(buildInstance.close);
|
|
2909
|
-
else await buildInstance.close();
|
|
2910
|
-
return rsbuildInstance;
|
|
2893
|
+
actionArgs.push(options);
|
|
2894
|
+
return command.commandAction.apply(this, actionArgs);
|
|
2895
|
+
}
|
|
2911
2896
|
}
|
|
2897
|
+
const cac = (name = "")=>new CAC(name);
|
|
2898
|
+
const dist = cac;
|
|
2912
2899
|
const getEnvDir = (cwd, envDir)=>{
|
|
2913
|
-
if (envDir) return
|
|
2900
|
+
if (envDir) return node_path.isAbsolute(envDir) ? envDir : node_path.resolve(cwd, envDir);
|
|
2914
2901
|
return cwd;
|
|
2915
2902
|
};
|
|
2916
2903
|
async function init(options) {
|
|
@@ -3001,7 +2988,7 @@ const applyCommonOptions = (cli)=>{
|
|
|
3001
2988
|
function runCli() {
|
|
3002
2989
|
const cli = dist('rslib');
|
|
3003
2990
|
cli.help();
|
|
3004
|
-
cli.version("0.10.
|
|
2991
|
+
cli.version("0.10.2");
|
|
3005
2992
|
applyCommonOptions(cli);
|
|
3006
2993
|
const buildCommand = cli.command('build', 'build the library for production');
|
|
3007
2994
|
const inspectCommand = cli.command('inspect', 'inspect the Rsbuild / Rspack configs of Rslib projects');
|
|
@@ -3060,6 +3047,20 @@ function runCli() {
|
|
|
3060
3047
|
});
|
|
3061
3048
|
cli.parse();
|
|
3062
3049
|
}
|
|
3063
|
-
|
|
3050
|
+
function initNodeEnv() {
|
|
3051
|
+
if (!process.env.NODE_ENV) {
|
|
3052
|
+
const command = process.argv[2] ?? '';
|
|
3053
|
+
process.env.NODE_ENV = [
|
|
3054
|
+
'build'
|
|
3055
|
+
].includes(command) ? 'production' : 'development';
|
|
3056
|
+
}
|
|
3057
|
+
}
|
|
3058
|
+
function prepareCli() {
|
|
3059
|
+
initNodeEnv();
|
|
3060
|
+
const { npm_execpath } = process.env;
|
|
3061
|
+
if (!npm_execpath || npm_execpath.includes('npx-cli.js') || npm_execpath.includes('.bun')) console.log();
|
|
3062
|
+
logger.greet(` Rslib v0.10.2\n`);
|
|
3063
|
+
}
|
|
3064
|
+
const src_version = "0.10.2";
|
|
3064
3065
|
var __webpack_exports__rspack = __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.rspack;
|
|
3065
3066
|
export { build, defineConfig, inspect, loadConfig, logger, prepareCli, __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__ as rsbuild, runCli, startMFDevServer, composeCreateRsbuildConfig as unstable_composeCreateRsbuildConfig, src_version as version, __webpack_exports__rspack as rspack };
|