@rsbuild/plugin-babel 2.0.0-alpha.3 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/compiled/babel-loader/index.js +35 -24
- package/compiled/babel-loader/package.json +1 -1
- package/dist/helper.d.ts +4 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.js +29 -14
- package/dist/plugin.d.ts +1 -1
- package/dist/types.d.ts +34 -33
- package/package.json +32 -32
- package/dist/index.cjs +0 -181
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
(() => {
|
|
2
2
|
var __webpack_modules__ = {
|
|
3
|
-
|
|
3
|
+
633: (module) => {
|
|
4
4
|
const STRIP_FILENAME_RE = /^[^:]+: /;
|
|
5
5
|
const format = (err) => {
|
|
6
6
|
if (err instanceof SyntaxError) {
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
}
|
|
27
27
|
module.exports = LoaderError;
|
|
28
28
|
},
|
|
29
|
-
|
|
29
|
+
261: (module, __unused_webpack_exports, __nccwpck_require__) => {
|
|
30
30
|
const os = __nccwpck_require__(857);
|
|
31
31
|
const path = __nccwpck_require__(928);
|
|
32
32
|
const zlib = __nccwpck_require__(106);
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
const { readFile, writeFile, mkdir } = __nccwpck_require__(943);
|
|
35
35
|
const { sync: findUpSync } = __nccwpck_require__(48);
|
|
36
36
|
const { env } = process;
|
|
37
|
-
const transform = __nccwpck_require__(
|
|
38
|
-
const serialize = __nccwpck_require__(
|
|
37
|
+
const transform = __nccwpck_require__(555);
|
|
38
|
+
const serialize = __nccwpck_require__(9);
|
|
39
39
|
let defaultCacheDirectory = null;
|
|
40
40
|
const gunzip = promisify(zlib.gunzip);
|
|
41
41
|
const gzip = promisify(zlib.gzip);
|
|
@@ -129,6 +129,10 @@
|
|
|
129
129
|
}
|
|
130
130
|
logger.debug(`applying Babel transform`);
|
|
131
131
|
const result = await transform(source, options);
|
|
132
|
+
if (!result) {
|
|
133
|
+
logger.debug(`no result from Babel transform, skipping cache write`);
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
132
136
|
await addTimestamps(result.externalDependencies, getFileTimestamp);
|
|
133
137
|
try {
|
|
134
138
|
logger.debug(`writing result to cache file '${file}'`);
|
|
@@ -141,7 +145,7 @@
|
|
|
141
145
|
}
|
|
142
146
|
return result;
|
|
143
147
|
};
|
|
144
|
-
module.exports = async function (params) {
|
|
148
|
+
module.exports = async function cache(params) {
|
|
145
149
|
let directory;
|
|
146
150
|
if (typeof params.cacheDirectory === "string") {
|
|
147
151
|
directory = params.cacheDirectory;
|
|
@@ -158,14 +162,19 @@
|
|
|
158
162
|
) {
|
|
159
163
|
return path.join(env.CACHE_DIR, name);
|
|
160
164
|
}
|
|
161
|
-
const rootPkgJSONPath =
|
|
165
|
+
const rootPkgJSONPath = findUpSync("package.json");
|
|
162
166
|
if (rootPkgJSONPath) {
|
|
163
|
-
return path.join(
|
|
167
|
+
return path.join(
|
|
168
|
+
path.dirname(rootPkgJSONPath),
|
|
169
|
+
"node_modules",
|
|
170
|
+
".cache",
|
|
171
|
+
name,
|
|
172
|
+
);
|
|
164
173
|
}
|
|
165
174
|
return os.tmpdir();
|
|
166
175
|
}
|
|
167
176
|
},
|
|
168
|
-
|
|
177
|
+
811: (module, __unused_webpack_exports, __nccwpck_require__) => {
|
|
169
178
|
let babel;
|
|
170
179
|
try {
|
|
171
180
|
babel = __nccwpck_require__(571);
|
|
@@ -184,10 +193,10 @@
|
|
|
184
193
|
);
|
|
185
194
|
}
|
|
186
195
|
const { version } = __nccwpck_require__(344);
|
|
187
|
-
const cache = __nccwpck_require__(
|
|
188
|
-
const transform = __nccwpck_require__(
|
|
189
|
-
const injectCaller = __nccwpck_require__(
|
|
190
|
-
const schema = __nccwpck_require__(
|
|
196
|
+
const cache = __nccwpck_require__(261);
|
|
197
|
+
const transform = __nccwpck_require__(555);
|
|
198
|
+
const injectCaller = __nccwpck_require__(161);
|
|
199
|
+
const schema = __nccwpck_require__(814);
|
|
191
200
|
const { isAbsolute } = __nccwpck_require__(928);
|
|
192
201
|
const { promisify } = __nccwpck_require__(23);
|
|
193
202
|
function subscribe(subscriber, metadata, context) {
|
|
@@ -199,13 +208,14 @@
|
|
|
199
208
|
module.exports.custom = makeLoader;
|
|
200
209
|
function makeLoader(callback) {
|
|
201
210
|
const overrides = callback ? callback(babel) : undefined;
|
|
202
|
-
|
|
211
|
+
const webpackLoader = function (source, inputSourceMap) {
|
|
203
212
|
const callback = this.async();
|
|
204
213
|
loader.call(this, source, inputSourceMap, overrides).then(
|
|
205
214
|
(args) => callback(null, ...args),
|
|
206
215
|
(err) => callback(err),
|
|
207
216
|
);
|
|
208
217
|
};
|
|
218
|
+
return webpackLoader;
|
|
209
219
|
}
|
|
210
220
|
async function loader(source, inputSourceMap, overrides) {
|
|
211
221
|
const filename = this.resourcePath;
|
|
@@ -373,9 +383,10 @@
|
|
|
373
383
|
return [source, inputSourceMap];
|
|
374
384
|
}
|
|
375
385
|
},
|
|
376
|
-
|
|
386
|
+
161: (module) => {
|
|
377
387
|
module.exports = function injectCaller(opts, target) {
|
|
378
|
-
return
|
|
388
|
+
return {
|
|
389
|
+
...opts,
|
|
379
390
|
caller: Object.assign(
|
|
380
391
|
{
|
|
381
392
|
name: "babel-loader",
|
|
@@ -386,10 +397,10 @@
|
|
|
386
397
|
},
|
|
387
398
|
opts.caller,
|
|
388
399
|
),
|
|
389
|
-
}
|
|
400
|
+
};
|
|
390
401
|
};
|
|
391
402
|
},
|
|
392
|
-
|
|
403
|
+
9: (module) => {
|
|
393
404
|
var objToString = Object.prototype.toString;
|
|
394
405
|
var objKeys = Object.getOwnPropertyNames;
|
|
395
406
|
function serialize(val, isArrayProp) {
|
|
@@ -455,15 +466,15 @@
|
|
|
455
466
|
}
|
|
456
467
|
};
|
|
457
468
|
},
|
|
458
|
-
|
|
469
|
+
555: (module, __unused_webpack_exports, __nccwpck_require__) => {
|
|
459
470
|
const babel = __nccwpck_require__(571);
|
|
460
471
|
const { promisify } = __nccwpck_require__(23);
|
|
461
|
-
const LoaderError = __nccwpck_require__(
|
|
462
|
-
const
|
|
463
|
-
module.exports = async function (source, options) {
|
|
472
|
+
const LoaderError = __nccwpck_require__(633);
|
|
473
|
+
const babelTransform = babel.transformAsync ?? promisify(babel.transform);
|
|
474
|
+
module.exports = async function transform(source, options) {
|
|
464
475
|
let result;
|
|
465
476
|
try {
|
|
466
|
-
result = await
|
|
477
|
+
result = await babelTransform(source, options);
|
|
467
478
|
} catch (err) {
|
|
468
479
|
throw err.message && err.codeFrame ? new LoaderError(err) : err;
|
|
469
480
|
}
|
|
@@ -809,7 +820,7 @@
|
|
|
809
820
|
"use strict";
|
|
810
821
|
module.exports = require("zlib");
|
|
811
822
|
},
|
|
812
|
-
|
|
823
|
+
814: (module) => {
|
|
813
824
|
"use strict";
|
|
814
825
|
module.exports = JSON.parse(
|
|
815
826
|
'{"title":"Babel Loader options","type":"object","properties":{"cacheDirectory":{"anyOf":[{"type":"boolean"},{"type":"string"}],"default":false},"cacheIdentifier":{"type":"string"},"cacheCompression":{"type":"boolean","default":true},"customize":{"anyOf":[{"type":"null"},{"type":"string"}],"default":null},"metadataSubscribers":{"type":"array"}},"additionalProperties":true}',
|
|
@@ -838,6 +849,6 @@
|
|
|
838
849
|
}
|
|
839
850
|
if (typeof __nccwpck_require__ !== "undefined")
|
|
840
851
|
__nccwpck_require__.ab = __dirname + "/";
|
|
841
|
-
var __webpack_exports__ = __nccwpck_require__(
|
|
852
|
+
var __webpack_exports__ = __nccwpck_require__(811);
|
|
842
853
|
module.exports = __webpack_exports__;
|
|
843
854
|
})();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"babel-loader","author":"Luis Couto <hello@luiscouto.pt>","version":"10.
|
|
1
|
+
{"name":"babel-loader","author":"Luis Couto <hello@luiscouto.pt>","version":"10.1.1","license":"MIT","types":"index.d.ts","type":"commonjs"}
|
package/dist/helper.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { ChainIdentifier, RspackChain } from '@rsbuild/core';
|
|
2
2
|
import type { BabelConfigUtils, BabelLoaderOptions, BabelTransformOptions, PluginBabelOptions } from './types.js';
|
|
3
|
-
export declare const BABEL_JS_RULE =
|
|
3
|
+
export declare const BABEL_JS_RULE = 'babel-js';
|
|
4
|
+
export declare const getBabelRuleId: (chain: RspackChain) => string;
|
|
4
5
|
export declare const castArray: <T>(arr?: T | T[]) => T[];
|
|
5
6
|
export declare const getBabelUtils: (config: BabelTransformOptions) => BabelConfigUtils;
|
|
6
|
-
export declare const applyUserBabelConfig: (defaultOptions: BabelLoaderOptions, userBabelConfig?: PluginBabelOptions[
|
|
7
|
-
export declare const modifyBabelLoaderOptions: ({ chain, CHAIN_ID, modifier
|
|
7
|
+
export declare const applyUserBabelConfig: (defaultOptions: BabelLoaderOptions, userBabelConfig?: PluginBabelOptions['babelLoaderOptions'], extraBabelUtils?: Partial<BabelConfigUtils>) => BabelLoaderOptions;
|
|
8
|
+
export declare const modifyBabelLoaderOptions: ({ chain, CHAIN_ID, modifier }: {
|
|
8
9
|
chain: RspackChain;
|
|
9
10
|
CHAIN_ID: ChainIdentifier;
|
|
10
11
|
modifier: (config: BabelTransformOptions) => BabelTransformOptions;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { getBabelUtils, modifyBabelLoaderOptions } from './helper.js';
|
|
2
|
-
export { getDefaultBabelOptions, PLUGIN_BABEL_NAME, pluginBabel
|
|
3
|
-
export type { BabelConfigUtils, BabelTransformOptions, PluginBabelOptions, PresetEnvBuiltIns, PresetEnvOptions, PresetEnvTargets
|
|
2
|
+
export { getDefaultBabelOptions, PLUGIN_BABEL_NAME, pluginBabel } from './plugin.js';
|
|
3
|
+
export type { BabelConfigUtils, BabelTransformOptions, PluginBabelOptions, PresetEnvBuiltIns, PresetEnvOptions, PresetEnvTargets } from './types.js';
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import node_path, { isAbsolute, join, normalize, sep } from "node:path";
|
|
2
2
|
import { reduceConfigsWithContext } from "reduce-configs";
|
|
3
|
-
import upath from "upath";
|
|
4
3
|
import node_fs from "node:fs";
|
|
5
4
|
import { createRequire } from "node:module";
|
|
6
5
|
import { fileURLToPath } from "node:url";
|
|
7
|
-
|
|
8
|
-
let BABEL_JS_RULE = 'babel-js', castArray = (arr)=>void 0 === arr ? [] : Array.isArray(arr) ? arr : [
|
|
6
|
+
let BABEL_JS_RULE = 'babel-js', BABEL_JS_RULE_REGEXP = /^babel-js(?:-\d+)?$/, isBabelRuleId = (id)=>BABEL_JS_RULE_REGEXP.test(id), castArray = (arr)=>void 0 === arr ? [] : Array.isArray(arr) ? arr : [
|
|
9
7
|
arr
|
|
10
|
-
], normalizeToPosixPath = (p)=>
|
|
8
|
+
], normalizeToPosixPath = (p)=>normalize(p || '').replace(/\\/g, '/').replace(/^([a-zA-Z]+):/, (_, m)=>`/${m.toLowerCase()}`), formatPath = (originPath)=>isAbsolute(originPath) ? originPath.split(sep).join('/') : originPath, getPluginItemName = (item)=>'string' == typeof item ? formatPath(item) : Array.isArray(item) && 'string' == typeof item[0] ? formatPath(item[0]) : null, modifyPresetOptions = (presetName, options, presets = [])=>{
|
|
11
9
|
presets.forEach((preset, index)=>{
|
|
12
10
|
Array.isArray(preset) ? 'string' == typeof preset[0] && normalizeToPosixPath(preset[0]).includes(presetName) && (preset[1] = {
|
|
13
11
|
...preset[1] || {},
|
|
@@ -59,9 +57,13 @@ let BABEL_JS_RULE = 'babel-js', castArray = (arr)=>void 0 === arr ? [] : Array.i
|
|
|
59
57
|
for (let rule of [
|
|
60
58
|
chain.module.rules.get(CHAIN_ID.RULE.JS).oneOfs.get(CHAIN_ID.ONE_OF.JS_MAIN),
|
|
61
59
|
chain.module.rules.get(CHAIN_ID.RULE.JS_DATA_URI),
|
|
62
|
-
chain.module.rules.get(
|
|
60
|
+
...Object.keys(chain.module.rules.entries()).filter(isBabelRuleId).map((id)=>chain.module.rules.get(id))
|
|
63
61
|
].filter(Boolean))rule.uses.has(CHAIN_ID.USE.BABEL) && rule.use(CHAIN_ID.USE.BABEL).tap(modifier);
|
|
64
|
-
}, plugin_dirname = node_path.dirname(fileURLToPath(import.meta.url)), plugin_require = createRequire(import.meta.url), PLUGIN_BABEL_NAME = 'rsbuild:babel', SCRIPT_REGEX = /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)
|
|
62
|
+
}, plugin_dirname = node_path.dirname(fileURLToPath(import.meta.url)), plugin_require = createRequire(import.meta.url), PLUGIN_BABEL_NAME = 'rsbuild:babel', SCRIPT_REGEX = /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/;
|
|
63
|
+
function assertCoreVersion(version) {
|
|
64
|
+
if ('1' === version.split('.')[0]) throw Error('"@rsbuild/plugin-babel" v2 requires "@rsbuild/core" >= 2.0. Please upgrade "@rsbuild/core" or use "@rsbuild/plugin-babel" v1.');
|
|
65
|
+
}
|
|
66
|
+
let DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
|
|
65
67
|
allowNamespaces: !0,
|
|
66
68
|
allExtensions: !0,
|
|
67
69
|
allowDeclareFields: !0,
|
|
@@ -83,7 +85,9 @@ function getDefaultBabelOptions(config, context) {
|
|
|
83
85
|
plugins: [
|
|
84
86
|
[
|
|
85
87
|
plugin_require.resolve('@babel/plugin-proposal-decorators'),
|
|
86
|
-
|
|
88
|
+
{
|
|
89
|
+
...config.source.decorators
|
|
90
|
+
}
|
|
87
91
|
],
|
|
88
92
|
...isLegacyDecorators ? [
|
|
89
93
|
plugin_require.resolve('@babel/plugin-transform-class-properties')
|
|
@@ -92,7 +96,9 @@ function getDefaultBabelOptions(config, context) {
|
|
|
92
96
|
presets: [
|
|
93
97
|
[
|
|
94
98
|
plugin_require.resolve("@babel/preset-typescript"),
|
|
95
|
-
|
|
99
|
+
{
|
|
100
|
+
...DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS
|
|
101
|
+
}
|
|
96
102
|
]
|
|
97
103
|
]
|
|
98
104
|
}, { buildCache = !0 } = config.performance;
|
|
@@ -105,8 +111,9 @@ function getDefaultBabelOptions(config, context) {
|
|
|
105
111
|
let pluginBabel = (options = {})=>({
|
|
106
112
|
name: PLUGIN_BABEL_NAME,
|
|
107
113
|
setup (api) {
|
|
114
|
+
assertCoreVersion(api.context.version);
|
|
108
115
|
let getBabelOptions = async (environment)=>{
|
|
109
|
-
let { config } = environment, mergedOptions = ((defaultOptions, userBabelConfig
|
|
116
|
+
let { config } = environment, mergedOptions = ((defaultOptions, userBabelConfig)=>{
|
|
110
117
|
if (userBabelConfig) {
|
|
111
118
|
let babelUtils = {
|
|
112
119
|
...getBabelUtils(defaultOptions),
|
|
@@ -119,19 +126,27 @@ let pluginBabel = (options = {})=>({
|
|
|
119
126
|
});
|
|
120
127
|
}
|
|
121
128
|
return defaultOptions;
|
|
122
|
-
})(
|
|
129
|
+
})(getDefaultBabelOptions(config, api.context), options.babelLoaderOptions);
|
|
123
130
|
return mergedOptions.cacheDirectory && !mergedOptions.cacheIdentifier && (mergedOptions.cacheIdentifier = await getCacheIdentifier(mergedOptions)), mergedOptions;
|
|
124
131
|
};
|
|
125
132
|
api.modifyBundlerChain({
|
|
126
133
|
order: 'pre',
|
|
127
134
|
handler: async (chain, { CHAIN_ID, environment })=>{
|
|
128
|
-
let babelOptions = await getBabelOptions(environment), babelLoader = node_path.resolve(plugin_dirname, '../compiled/babel-loader/index.js'), { include, exclude } = options;
|
|
135
|
+
let babelOptions = await getBabelOptions(environment), babelLoader = node_path.resolve(plugin_dirname, '../compiled/babel-loader/index.js'), { include, exclude, parallel = !1 } = options;
|
|
129
136
|
if (include || exclude) {
|
|
130
|
-
let rule = chain.module.rule(
|
|
137
|
+
let rule = chain.module.rule(((chain)=>{
|
|
138
|
+
let id = BABEL_JS_RULE, index = 0;
|
|
139
|
+
for(; chain.module.rules.has(id);)id = `${BABEL_JS_RULE}-${++index}`;
|
|
140
|
+
return id;
|
|
141
|
+
})(chain)).after(CHAIN_ID.RULE.JS);
|
|
131
142
|
if (include) for (let condition of castArray(include))rule.include.add(condition);
|
|
132
143
|
if (exclude) for (let condition of castArray(exclude))rule.exclude.add(condition);
|
|
133
|
-
rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).loader(babelLoader).options(babelOptions);
|
|
134
|
-
|
|
144
|
+
let loader = rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).loader(babelLoader).options(babelOptions);
|
|
145
|
+
parallel && loader.parallel(!0);
|
|
146
|
+
} else {
|
|
147
|
+
let loader = chain.module.rule(CHAIN_ID.RULE.JS).test(SCRIPT_REGEX).oneOfs.get(CHAIN_ID.ONE_OF.JS_MAIN).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(babelLoader).options(babelOptions);
|
|
148
|
+
parallel && loader.parallel(!0);
|
|
149
|
+
}
|
|
135
150
|
}
|
|
136
151
|
});
|
|
137
152
|
}
|
package/dist/plugin.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { NormalizedEnvironmentConfig, RsbuildContext, RsbuildPlugin } from '@rsbuild/core';
|
|
2
2
|
import type { BabelLoaderOptions, PluginBabelOptions } from './types.js';
|
|
3
|
-
export declare const PLUGIN_BABEL_NAME =
|
|
3
|
+
export declare const PLUGIN_BABEL_NAME = 'rsbuild:babel';
|
|
4
4
|
export declare function getDefaultBabelOptions(config: NormalizedEnvironmentConfig, context: RsbuildContext): BabelLoaderOptions;
|
|
5
5
|
export declare const pluginBabel: (options?: PluginBabelOptions) => RsbuildPlugin;
|
package/dist/types.d.ts
CHANGED
|
@@ -48,50 +48,51 @@ export type BabelConfigUtils = {
|
|
|
48
48
|
modifyPresetEnvOptions: (options: PresetEnvOptions) => void;
|
|
49
49
|
modifyPresetReactOptions: (options: PresetReactOptions) => void;
|
|
50
50
|
/**
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
addIncludes: (includes: RuleCondition) => void;
|
|
51
|
+
* use `source.include` instead
|
|
52
|
+
* @deprecated
|
|
53
|
+
*/ addIncludes: (includes: RuleCondition) => void;
|
|
55
54
|
/**
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
addExcludes: (excludes: RuleCondition) => void;
|
|
55
|
+
* use `source.exclude` instead
|
|
56
|
+
* @deprecated
|
|
57
|
+
*/ addExcludes: (excludes: RuleCondition) => void;
|
|
60
58
|
};
|
|
61
59
|
export type BabelLoaderOptions = BabelTransformOptions & {
|
|
62
60
|
/**
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
cacheDirectory?: string | boolean;
|
|
61
|
+
* When set, the given directory will be used to cache the results of the loader.
|
|
62
|
+
*/ cacheDirectory?: string | boolean;
|
|
66
63
|
/**
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
cacheIdentifier?: string;
|
|
64
|
+
* Can be set to a custom value to force cache busting if the identifier changes.
|
|
65
|
+
*/ cacheIdentifier?: string;
|
|
70
66
|
/**
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
cacheCompression?: boolean;
|
|
67
|
+
* When set, each Babel transform output will be compressed with Gzip.
|
|
68
|
+
*/ cacheCompression?: boolean;
|
|
74
69
|
/**
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
customize?: string | null;
|
|
70
|
+
* The path of a module that exports a custom callback.
|
|
71
|
+
*/ customize?: string | null;
|
|
78
72
|
/**
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
metadataSubscribers?: string[];
|
|
73
|
+
* Takes an array of context function names. E.g.
|
|
74
|
+
*/ metadataSubscribers?: string[];
|
|
82
75
|
};
|
|
83
76
|
export type PluginBabelOptions = {
|
|
84
77
|
/**
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
include?: RuleCondition;
|
|
78
|
+
* Used to specify the files that need to be compiled by Babel.
|
|
79
|
+
*/ include?: RuleCondition;
|
|
88
80
|
/**
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
exclude?: RuleCondition;
|
|
81
|
+
* Used to specify the files that do not need to be compiled by Babel.
|
|
82
|
+
*/ exclude?: RuleCondition;
|
|
92
83
|
/**
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
84
|
+
* Options passed to `babel-loader`.
|
|
85
|
+
* @see https://github.com/babel/babel-loader
|
|
86
|
+
*/ babelLoaderOptions?: ConfigChainWithContext<BabelLoaderOptions, BabelConfigUtils>;
|
|
87
|
+
/**
|
|
88
|
+
* Whether to run Babel transformations in parallel using worker threads. When
|
|
89
|
+
* enabled, JavaScript modules are processed across multiple worker threads,
|
|
90
|
+
* reducing pressure on the main thread and improving overall build performance
|
|
91
|
+
* when compiling large numbers of modules.
|
|
92
|
+
*
|
|
93
|
+
* Options transferred to worker threads must comply with the HTML structured clone
|
|
94
|
+
* algorithm. For example, functions cannot be passed as options.
|
|
95
|
+
* @see https://nodejs.org/api/worker_threads.html#portpostmessagevalue-transferlist
|
|
96
|
+
* @default false
|
|
97
|
+
*/ parallel?: boolean;
|
|
97
98
|
};
|
package/package.json
CHANGED
|
@@ -1,57 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsbuild/plugin-babel",
|
|
3
|
-
"version": "2.0.0
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Babel plugin for Rsbuild",
|
|
5
|
+
"license": "MIT",
|
|
5
6
|
"repository": {
|
|
6
7
|
"type": "git",
|
|
7
8
|
"url": "https://github.com/web-infra-dev/rsbuild",
|
|
8
9
|
"directory": "packages/plugin-babel"
|
|
9
10
|
},
|
|
10
|
-
"
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"compiled"
|
|
14
|
+
],
|
|
11
15
|
"type": "module",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
12
17
|
"exports": {
|
|
13
18
|
".": {
|
|
14
19
|
"types": "./dist/index.d.ts",
|
|
15
|
-
"
|
|
16
|
-
"require": "./dist/index.cjs"
|
|
20
|
+
"default": "./dist/index.js"
|
|
17
21
|
}
|
|
18
22
|
},
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"compiled"
|
|
24
|
-
],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public",
|
|
25
|
+
"registry": "https://registry.npmjs.org/"
|
|
26
|
+
},
|
|
25
27
|
"dependencies": {
|
|
26
|
-
"@babel/core": "^7.
|
|
27
|
-
"@babel/plugin-proposal-decorators": "^7.
|
|
28
|
-
"@babel/plugin-transform-class-properties": "^7.
|
|
29
|
-
"@babel/preset-typescript": "^7.
|
|
28
|
+
"@babel/core": "^7.29.7",
|
|
29
|
+
"@babel/plugin-proposal-decorators": "^7.29.7",
|
|
30
|
+
"@babel/plugin-transform-class-properties": "^7.29.7",
|
|
31
|
+
"@babel/preset-typescript": "^7.29.7",
|
|
30
32
|
"@types/babel__core": "^7.20.5",
|
|
31
|
-
"
|
|
32
|
-
"reduce-configs": "^1.1.1",
|
|
33
|
-
"upath": "2.0.1"
|
|
33
|
+
"reduce-configs": "^1.1.2"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@rslib/core": "0.
|
|
37
|
-
"@types/node": "^24.
|
|
38
|
-
"babel-loader": "10.
|
|
39
|
-
"prebundle": "1.6.
|
|
40
|
-
"typescript": "^
|
|
41
|
-
"@rsbuild/core": "2.0.
|
|
42
|
-
"@scripts/test-helper": "1.0.
|
|
36
|
+
"@rslib/core": "0.22.1",
|
|
37
|
+
"@types/node": "^24.13.2",
|
|
38
|
+
"babel-loader": "10.1.1",
|
|
39
|
+
"prebundle": "1.6.5",
|
|
40
|
+
"typescript": "^6.0.3",
|
|
41
|
+
"@rsbuild/core": "2.0.15",
|
|
42
|
+
"@scripts/test-helper": "1.0.0"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"@rsbuild/core": "^2.0.0
|
|
45
|
+
"@rsbuild/core": "^2.0.0"
|
|
46
46
|
},
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
|
|
47
|
+
"peerDependenciesMeta": {
|
|
48
|
+
"@rsbuild/core": {
|
|
49
|
+
"optional": true
|
|
50
|
+
}
|
|
50
51
|
},
|
|
51
52
|
"scripts": {
|
|
52
|
-
"build": "rslib
|
|
53
|
-
"dev": "rslib
|
|
54
|
-
"prebundle": "prebundle"
|
|
55
|
-
"bump": "pnpx bumpp --no-tag"
|
|
53
|
+
"build": "rslib",
|
|
54
|
+
"dev": "rslib -w",
|
|
55
|
+
"prebundle": "prebundle"
|
|
56
56
|
}
|
|
57
57
|
}
|
package/dist/index.cjs
DELETED
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const __rslib_import_meta_url__ = "u" < typeof document ? new (require('url'.replace('', ''))).URL('file:' + __filename).href : document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href;
|
|
3
|
-
var __webpack_require__ = {};
|
|
4
|
-
__webpack_require__.n = (module)=>{
|
|
5
|
-
var getter = module && module.__esModule ? ()=>module.default : ()=>module;
|
|
6
|
-
return __webpack_require__.d(getter, {
|
|
7
|
-
a: getter
|
|
8
|
-
}), getter;
|
|
9
|
-
}, __webpack_require__.d = (exports1, definition)=>{
|
|
10
|
-
for(var key in definition)__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key) && Object.defineProperty(exports1, key, {
|
|
11
|
-
enumerable: !0,
|
|
12
|
-
get: definition[key]
|
|
13
|
-
});
|
|
14
|
-
}, __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop), __webpack_require__.r = (exports1)=>{
|
|
15
|
-
"u" > typeof Symbol && Symbol.toStringTag && Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
16
|
-
value: 'Module'
|
|
17
|
-
}), Object.defineProperty(exports1, '__esModule', {
|
|
18
|
-
value: !0
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
var __webpack_exports__ = {};
|
|
22
|
-
__webpack_require__.r(__webpack_exports__), __webpack_require__.d(__webpack_exports__, {
|
|
23
|
-
getDefaultBabelOptions: ()=>getDefaultBabelOptions,
|
|
24
|
-
getBabelUtils: ()=>getBabelUtils,
|
|
25
|
-
modifyBabelLoaderOptions: ()=>modifyBabelLoaderOptions,
|
|
26
|
-
PLUGIN_BABEL_NAME: ()=>PLUGIN_BABEL_NAME,
|
|
27
|
-
pluginBabel: ()=>pluginBabel
|
|
28
|
-
});
|
|
29
|
-
const external_node_path_namespaceObject = require("node:path");
|
|
30
|
-
var external_node_path_default = __webpack_require__.n(external_node_path_namespaceObject);
|
|
31
|
-
const external_reduce_configs_namespaceObject = require("reduce-configs"), external_upath_namespaceObject = require("upath");
|
|
32
|
-
var external_upath_default = __webpack_require__.n(external_upath_namespaceObject);
|
|
33
|
-
const BABEL_JS_RULE = 'babel-js', castArray = (arr)=>void 0 === arr ? [] : Array.isArray(arr) ? arr : [
|
|
34
|
-
arr
|
|
35
|
-
], normalizeToPosixPath = (p)=>external_upath_default().normalizeSafe((0, external_node_path_namespaceObject.normalize)(p || '')).replace(/^([a-zA-Z]+):/, (_, m)=>`/${m.toLowerCase()}`), formatPath = (originPath)=>(0, external_node_path_namespaceObject.isAbsolute)(originPath) ? originPath.split(external_node_path_namespaceObject.sep).join('/') : originPath, getPluginItemName = (item)=>'string' == typeof item ? formatPath(item) : Array.isArray(item) && 'string' == typeof item[0] ? formatPath(item[0]) : null, addPlugins = (plugins, config)=>{
|
|
36
|
-
config.plugins ? config.plugins.push(...plugins) : config.plugins = plugins;
|
|
37
|
-
}, addPresets = (presets, config)=>{
|
|
38
|
-
config.presets ? config.presets.push(...presets) : config.presets = presets;
|
|
39
|
-
}, removePlugins = (plugins, config)=>{
|
|
40
|
-
if (!config.plugins) return;
|
|
41
|
-
let removeList = castArray(plugins);
|
|
42
|
-
config.plugins = config.plugins.filter((item)=>{
|
|
43
|
-
let name = getPluginItemName(item);
|
|
44
|
-
return !name || !removeList.find((removeItem)=>name.includes(removeItem));
|
|
45
|
-
});
|
|
46
|
-
}, removePresets = (presets, config)=>{
|
|
47
|
-
if (!config.presets) return;
|
|
48
|
-
let removeList = castArray(presets);
|
|
49
|
-
config.presets = config.presets.filter((item)=>{
|
|
50
|
-
let name = getPluginItemName(item);
|
|
51
|
-
return !name || !removeList.find((removeItem)=>name.includes(removeItem));
|
|
52
|
-
});
|
|
53
|
-
}, modifyPresetOptions = (presetName, options, presets = [])=>{
|
|
54
|
-
presets.forEach((preset, index)=>{
|
|
55
|
-
Array.isArray(preset) ? 'string' == typeof preset[0] && normalizeToPosixPath(preset[0]).includes(presetName) && (preset[1] = {
|
|
56
|
-
...preset[1] || {},
|
|
57
|
-
...options
|
|
58
|
-
}) : 'string' == typeof preset && normalizeToPosixPath(preset).includes(presetName) && (presets[index] = [
|
|
59
|
-
preset,
|
|
60
|
-
options
|
|
61
|
-
]);
|
|
62
|
-
});
|
|
63
|
-
}, getBabelUtils = (config)=>{
|
|
64
|
-
let noop = ()=>{};
|
|
65
|
-
return {
|
|
66
|
-
addPlugins: (plugins)=>{
|
|
67
|
-
addPlugins(plugins, config);
|
|
68
|
-
},
|
|
69
|
-
addPresets: (presets)=>{
|
|
70
|
-
addPresets(presets, config);
|
|
71
|
-
},
|
|
72
|
-
removePlugins: (plugins)=>{
|
|
73
|
-
removePlugins(plugins, config);
|
|
74
|
-
},
|
|
75
|
-
removePresets: (presets)=>{
|
|
76
|
-
removePresets(presets, config);
|
|
77
|
-
},
|
|
78
|
-
addIncludes: noop,
|
|
79
|
-
addExcludes: noop,
|
|
80
|
-
modifyPresetEnvOptions: (options)=>{
|
|
81
|
-
modifyPresetOptions('@babel/preset-env', options, config.presets || []);
|
|
82
|
-
},
|
|
83
|
-
modifyPresetReactOptions: (options)=>{
|
|
84
|
-
modifyPresetOptions('@babel/preset-react', options, config.presets || []);
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
}, applyUserBabelConfig = (defaultOptions, userBabelConfig, extraBabelUtils)=>{
|
|
88
|
-
if (userBabelConfig) {
|
|
89
|
-
let babelUtils = {
|
|
90
|
-
...getBabelUtils(defaultOptions),
|
|
91
|
-
...extraBabelUtils
|
|
92
|
-
};
|
|
93
|
-
return (0, external_reduce_configs_namespaceObject.reduceConfigsWithContext)({
|
|
94
|
-
initial: defaultOptions,
|
|
95
|
-
config: userBabelConfig,
|
|
96
|
-
ctx: babelUtils
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
return defaultOptions;
|
|
100
|
-
}, modifyBabelLoaderOptions = ({ chain, CHAIN_ID, modifier })=>{
|
|
101
|
-
for (let rule of [
|
|
102
|
-
chain.module.rules.get(CHAIN_ID.RULE.JS).oneOfs.get(CHAIN_ID.ONE_OF.JS_MAIN),
|
|
103
|
-
chain.module.rules.get(CHAIN_ID.RULE.JS_DATA_URI),
|
|
104
|
-
chain.module.rules.get(BABEL_JS_RULE)
|
|
105
|
-
].filter(Boolean))rule.uses.has(CHAIN_ID.USE.BABEL) && rule.use(CHAIN_ID.USE.BABEL).tap(modifier);
|
|
106
|
-
}, external_node_fs_namespaceObject = require("node:fs");
|
|
107
|
-
var external_node_fs_default = __webpack_require__.n(external_node_fs_namespaceObject);
|
|
108
|
-
const external_node_module_namespaceObject = require("node:module"), external_node_url_namespaceObject = require("node:url"), external_deepmerge_namespaceObject = require("deepmerge");
|
|
109
|
-
var external_deepmerge_default = __webpack_require__.n(external_deepmerge_namespaceObject);
|
|
110
|
-
const plugin_dirname = external_node_path_default().dirname((0, external_node_url_namespaceObject.fileURLToPath)(__rslib_import_meta_url__)), plugin_require = (0, external_node_module_namespaceObject.createRequire)(__rslib_import_meta_url__), PLUGIN_BABEL_NAME = 'rsbuild:babel', SCRIPT_REGEX = /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/, DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
|
|
111
|
-
allowNamespaces: !0,
|
|
112
|
-
allExtensions: !0,
|
|
113
|
-
allowDeclareFields: !0,
|
|
114
|
-
optimizeConstEnums: !0,
|
|
115
|
-
isTSX: !0
|
|
116
|
-
};
|
|
117
|
-
function getCacheDirectory(context, cacheDirectory) {
|
|
118
|
-
return cacheDirectory ? (0, external_node_path_namespaceObject.isAbsolute)(cacheDirectory) ? cacheDirectory : (0, external_node_path_namespaceObject.join)(context.rootPath, cacheDirectory) : (0, external_node_path_namespaceObject.join)(context.cachePath);
|
|
119
|
-
}
|
|
120
|
-
async function getCacheIdentifier(options) {
|
|
121
|
-
let identifier = `${process.env.NODE_ENV}${JSON.stringify(options)}`, { version: coreVersion } = await import("@babel/core"), loaderVersion = JSON.parse(await external_node_fs_default().promises.readFile((0, external_node_path_namespaceObject.join)(plugin_dirname, '../compiled/babel-loader/package.json'), 'utf-8')).version ?? '';
|
|
122
|
-
return identifier + `@babel/core@${coreVersion}babel-loader@${loaderVersion}`;
|
|
123
|
-
}
|
|
124
|
-
function getDefaultBabelOptions(config, context) {
|
|
125
|
-
let isLegacyDecorators = 'legacy' === config.source.decorators.version, options = {
|
|
126
|
-
babelrc: !1,
|
|
127
|
-
configFile: !1,
|
|
128
|
-
compact: 'production' === config.mode,
|
|
129
|
-
plugins: [
|
|
130
|
-
[
|
|
131
|
-
plugin_require.resolve('@babel/plugin-proposal-decorators'),
|
|
132
|
-
config.source.decorators
|
|
133
|
-
],
|
|
134
|
-
...isLegacyDecorators ? [
|
|
135
|
-
plugin_require.resolve('@babel/plugin-transform-class-properties')
|
|
136
|
-
] : []
|
|
137
|
-
],
|
|
138
|
-
presets: [
|
|
139
|
-
[
|
|
140
|
-
plugin_require.resolve("@babel/preset-typescript"),
|
|
141
|
-
DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS
|
|
142
|
-
]
|
|
143
|
-
]
|
|
144
|
-
}, { buildCache = !0 } = config.performance;
|
|
145
|
-
if (buildCache && 'rspack' === context.bundlerType) {
|
|
146
|
-
let cacheDirectory = getCacheDirectory(context, 'boolean' == typeof buildCache ? void 0 : buildCache.cacheDirectory);
|
|
147
|
-
options.cacheCompression = !1, options.cacheDirectory = (0, external_node_path_namespaceObject.join)(cacheDirectory, 'babel-loader');
|
|
148
|
-
}
|
|
149
|
-
return options;
|
|
150
|
-
}
|
|
151
|
-
const pluginBabel = (options = {})=>({
|
|
152
|
-
name: PLUGIN_BABEL_NAME,
|
|
153
|
-
setup (api) {
|
|
154
|
-
let getBabelOptions = async (environment)=>{
|
|
155
|
-
let { config } = environment, baseOptions = getDefaultBabelOptions(config, api.context), mergedOptions = applyUserBabelConfig(external_deepmerge_default()({}, baseOptions), options.babelLoaderOptions);
|
|
156
|
-
return mergedOptions.cacheDirectory && !mergedOptions.cacheIdentifier && (mergedOptions.cacheIdentifier = await getCacheIdentifier(mergedOptions)), mergedOptions;
|
|
157
|
-
};
|
|
158
|
-
api.modifyBundlerChain({
|
|
159
|
-
order: 'pre',
|
|
160
|
-
handler: async (chain, { CHAIN_ID, environment })=>{
|
|
161
|
-
let babelOptions = await getBabelOptions(environment), babelLoader = external_node_path_default().resolve(plugin_dirname, '../compiled/babel-loader/index.js'), { include, exclude } = options;
|
|
162
|
-
if (include || exclude) {
|
|
163
|
-
let rule = chain.module.rule(BABEL_JS_RULE).after(CHAIN_ID.RULE.JS);
|
|
164
|
-
if (include) for (let condition of castArray(include))rule.include.add(condition);
|
|
165
|
-
if (exclude) for (let condition of castArray(exclude))rule.exclude.add(condition);
|
|
166
|
-
rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).loader(babelLoader).options(babelOptions);
|
|
167
|
-
} else chain.module.rule(CHAIN_ID.RULE.JS).test(SCRIPT_REGEX).oneOfs.get(CHAIN_ID.ONE_OF.JS_MAIN).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(babelLoader).options(babelOptions);
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
for(var __rspack_i in exports.PLUGIN_BABEL_NAME = __webpack_exports__.PLUGIN_BABEL_NAME, exports.getBabelUtils = __webpack_exports__.getBabelUtils, exports.getDefaultBabelOptions = __webpack_exports__.getDefaultBabelOptions, exports.modifyBabelLoaderOptions = __webpack_exports__.modifyBabelLoaderOptions, exports.pluginBabel = __webpack_exports__.pluginBabel, __webpack_exports__)-1 === [
|
|
173
|
-
"PLUGIN_BABEL_NAME",
|
|
174
|
-
"getBabelUtils",
|
|
175
|
-
"getDefaultBabelOptions",
|
|
176
|
-
"modifyBabelLoaderOptions",
|
|
177
|
-
"pluginBabel"
|
|
178
|
-
].indexOf(__rspack_i) && (exports[__rspack_i] = __webpack_exports__[__rspack_i]);
|
|
179
|
-
Object.defineProperty(exports, '__esModule', {
|
|
180
|
-
value: !0
|
|
181
|
-
});
|