@rsbuild/plugin-babel 0.2.0 → 0.2.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/dist/index.d.ts +12 -4
- package/dist/index.js +47 -36
- package/dist/index.mjs +47 -37
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { RsbuildPlugin } from '@rsbuild/core';
|
|
2
|
-
import { ChainedConfigWithUtils, NormalizedConfig } from '@rsbuild/shared';
|
|
2
|
+
import { ChainedConfigWithUtils, NormalizedConfig, BundlerChain, ChainIdentifier } from '@rsbuild/shared';
|
|
3
3
|
import { PluginItem, TransformOptions } from '@babel/core';
|
|
4
4
|
export { TransformOptions as BabelTransformOptions } from '@babel/core';
|
|
5
5
|
|
|
@@ -41,6 +41,7 @@ interface ClassicRuntimePresetReactOptions extends SharedBabelPresetReactOptions
|
|
|
41
41
|
useSpread?: boolean;
|
|
42
42
|
}
|
|
43
43
|
type PresetReactOptions = AutomaticRuntimePresetReactOptions | ClassicRuntimePresetReactOptions;
|
|
44
|
+
type RuleCondition = string | RegExp | (string | RegExp)[];
|
|
44
45
|
type BabelConfigUtils = {
|
|
45
46
|
addPlugins: (plugins: PluginItem[]) => void;
|
|
46
47
|
addPresets: (presets: PluginItem[]) => void;
|
|
@@ -52,14 +53,16 @@ type BabelConfigUtils = {
|
|
|
52
53
|
* use `source.include` instead
|
|
53
54
|
* @deprecated
|
|
54
55
|
*/
|
|
55
|
-
addIncludes: (includes:
|
|
56
|
+
addIncludes: (includes: RuleCondition) => void;
|
|
56
57
|
/**
|
|
57
58
|
* use `source.exclude` instead
|
|
58
59
|
* @deprecated
|
|
59
60
|
*/
|
|
60
|
-
addExcludes: (excludes:
|
|
61
|
+
addExcludes: (excludes: RuleCondition) => void;
|
|
61
62
|
};
|
|
62
63
|
type PluginBabelOptions = {
|
|
64
|
+
include?: RuleCondition;
|
|
65
|
+
exclude?: RuleCondition;
|
|
63
66
|
babelLoaderOptions?: ChainedConfigWithUtils<TransformOptions, BabelConfigUtils>;
|
|
64
67
|
};
|
|
65
68
|
|
|
@@ -67,5 +70,10 @@ declare const pluginBabel: (options?: PluginBabelOptions) => RsbuildPlugin;
|
|
|
67
70
|
|
|
68
71
|
declare const getBabelUtils: (config: TransformOptions) => BabelConfigUtils;
|
|
69
72
|
declare const getUseBuiltIns: (config: NormalizedConfig) => false | "usage" | "entry";
|
|
73
|
+
declare const modifyBabelLoaderOptions: ({ chain, CHAIN_ID, modifier, }: {
|
|
74
|
+
chain: BundlerChain;
|
|
75
|
+
CHAIN_ID: ChainIdentifier;
|
|
76
|
+
modifier: (config: TransformOptions) => TransformOptions;
|
|
77
|
+
}) => void;
|
|
70
78
|
|
|
71
|
-
export { BabelConfigUtils, PluginBabelOptions, PresetEnvBuiltIns, PresetEnvOptions, PresetEnvTargets, getBabelUtils, getUseBuiltIns, pluginBabel };
|
|
79
|
+
export { BabelConfigUtils, PluginBabelOptions, PresetEnvBuiltIns, PresetEnvOptions, PresetEnvTargets, getBabelUtils, getUseBuiltIns, modifyBabelLoaderOptions, pluginBabel };
|
package/dist/index.js
CHANGED
|
@@ -32,6 +32,7 @@ var src_exports = {};
|
|
|
32
32
|
__export(src_exports, {
|
|
33
33
|
getBabelUtils: () => getBabelUtils,
|
|
34
34
|
getUseBuiltIns: () => getUseBuiltIns,
|
|
35
|
+
modifyBabelLoaderOptions: () => modifyBabelLoaderOptions,
|
|
35
36
|
pluginBabel: () => pluginBabel
|
|
36
37
|
});
|
|
37
38
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -44,6 +45,7 @@ var import_shared2 = require("@rsbuild/shared");
|
|
|
44
45
|
var import_path = require("path");
|
|
45
46
|
var import_shared = require("@rsbuild/shared");
|
|
46
47
|
var import_upath = __toESM(require("upath"));
|
|
48
|
+
var BABEL_JS_RULE = "babel-js";
|
|
47
49
|
var normalizeToPosixPath = (p) => import_upath.default.normalizeSafe((0, import_path.normalize)(p || "")).replace(/^([a-zA-Z]+):/, (_, m) => `/${m.toLowerCase()}`);
|
|
48
50
|
var formatPath = (originPath) => {
|
|
49
51
|
if ((0, import_path.isAbsolute)(originPath)) {
|
|
@@ -127,7 +129,7 @@ var getBabelUtils = (config) => {
|
|
|
127
129
|
// It can be overridden by `extraBabelUtils`.
|
|
128
130
|
addIncludes: noop,
|
|
129
131
|
addExcludes: noop,
|
|
130
|
-
// Compat `presetEnvOptions` and `presetReactOptions` in
|
|
132
|
+
// Compat `presetEnvOptions` and `presetReactOptions` in Modern.js
|
|
131
133
|
modifyPresetEnvOptions: (options) => modifyPresetOptions("@babel/preset-env", options, config.presets || []),
|
|
132
134
|
modifyPresetReactOptions: (options) => modifyPresetOptions("@babel/preset-react", options, config.presets || [])
|
|
133
135
|
};
|
|
@@ -153,6 +155,21 @@ var getUseBuiltIns = (config) => {
|
|
|
153
155
|
}
|
|
154
156
|
return polyfill;
|
|
155
157
|
};
|
|
158
|
+
var modifyBabelLoaderOptions = ({
|
|
159
|
+
chain,
|
|
160
|
+
CHAIN_ID,
|
|
161
|
+
modifier
|
|
162
|
+
}) => {
|
|
163
|
+
const ruleIds = [CHAIN_ID.RULE.JS, CHAIN_ID.RULE.JS_DATA_URI, BABEL_JS_RULE];
|
|
164
|
+
ruleIds.forEach((ruleId) => {
|
|
165
|
+
if (chain.module.rules.has(ruleId)) {
|
|
166
|
+
const rule = chain.module.rule(ruleId);
|
|
167
|
+
if (rule.uses.has(CHAIN_ID.USE.BABEL)) {
|
|
168
|
+
rule.use(CHAIN_ID.USE.BABEL).tap(modifier);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
};
|
|
156
173
|
|
|
157
174
|
// src/plugin.ts
|
|
158
175
|
var DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
|
|
@@ -169,24 +186,6 @@ var pluginBabel = (options = {}) => ({
|
|
|
169
186
|
setup(api) {
|
|
170
187
|
api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd }) => {
|
|
171
188
|
const getBabelOptions = () => {
|
|
172
|
-
const includes2 = [];
|
|
173
|
-
const excludes2 = [];
|
|
174
|
-
const babelUtils = {
|
|
175
|
-
addIncludes(items) {
|
|
176
|
-
if (Array.isArray(items)) {
|
|
177
|
-
includes2.push(...items);
|
|
178
|
-
} else {
|
|
179
|
-
includes2.push(items);
|
|
180
|
-
}
|
|
181
|
-
},
|
|
182
|
-
addExcludes(items) {
|
|
183
|
-
if (Array.isArray(items)) {
|
|
184
|
-
excludes2.push(...items);
|
|
185
|
-
} else {
|
|
186
|
-
excludes2.push(items);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
189
|
const baseConfig = {
|
|
191
190
|
plugins: [],
|
|
192
191
|
presets: [
|
|
@@ -199,30 +198,41 @@ var pluginBabel = (options = {}) => ({
|
|
|
199
198
|
};
|
|
200
199
|
const userBabelConfig = applyUserBabelConfig(
|
|
201
200
|
(0, import_shared2.cloneDeep)(baseConfig),
|
|
202
|
-
options.babelLoaderOptions
|
|
203
|
-
babelUtils
|
|
201
|
+
options.babelLoaderOptions
|
|
204
202
|
);
|
|
205
|
-
|
|
203
|
+
return {
|
|
206
204
|
babelrc: false,
|
|
207
205
|
configFile: false,
|
|
208
206
|
compact: isProd,
|
|
209
207
|
...userBabelConfig
|
|
210
208
|
};
|
|
211
|
-
return {
|
|
212
|
-
babelOptions: babelOptions2,
|
|
213
|
-
includes: includes2,
|
|
214
|
-
excludes: excludes2
|
|
215
|
-
};
|
|
216
209
|
};
|
|
217
|
-
const
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
210
|
+
const babelOptions = getBabelOptions();
|
|
211
|
+
const babelLoader = import_path2.default.resolve(
|
|
212
|
+
__dirname,
|
|
213
|
+
"../compiled/babel-loader/index.js"
|
|
214
|
+
);
|
|
215
|
+
const { include, exclude } = options;
|
|
216
|
+
if (include || exclude) {
|
|
217
|
+
const rule = chain.module.rule(BABEL_JS_RULE);
|
|
218
|
+
if (include) {
|
|
219
|
+
(0, import_shared2.castArray)(include).forEach((condition) => {
|
|
220
|
+
rule.include.add(condition);
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
if (exclude) {
|
|
224
|
+
(0, import_shared2.castArray)(exclude).forEach((condition) => {
|
|
225
|
+
rule.exclude.add(condition);
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
const swcRule = chain.module.rules.get(CHAIN_ID.RULE.JS).use(CHAIN_ID.USE.SWC);
|
|
229
|
+
const swcLoader = swcRule.get("loader");
|
|
230
|
+
const swcOptions = swcRule.get("options");
|
|
231
|
+
rule.test(import_shared2.SCRIPT_REGEX).use(CHAIN_ID.USE.SWC).loader(swcLoader).options(swcOptions).end().use(CHAIN_ID.USE.BABEL).loader(babelLoader).options(babelOptions);
|
|
232
|
+
} else {
|
|
233
|
+
const rule = chain.module.rule(CHAIN_ID.RULE.JS);
|
|
234
|
+
rule.test(import_shared2.SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(babelLoader).options(babelOptions);
|
|
235
|
+
}
|
|
226
236
|
});
|
|
227
237
|
}
|
|
228
238
|
});
|
|
@@ -230,5 +240,6 @@ var pluginBabel = (options = {}) => ({
|
|
|
230
240
|
0 && (module.exports = {
|
|
231
241
|
getBabelUtils,
|
|
232
242
|
getUseBuiltIns,
|
|
243
|
+
modifyBabelLoaderOptions,
|
|
233
244
|
pluginBabel
|
|
234
245
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -19,7 +19,7 @@ global.require = createRequire(import.meta.url);
|
|
|
19
19
|
|
|
20
20
|
// src/plugin.ts
|
|
21
21
|
import path2 from "path";
|
|
22
|
-
import { cloneDeep, SCRIPT_REGEX } from "@rsbuild/shared";
|
|
22
|
+
import { castArray as castArray2, cloneDeep, SCRIPT_REGEX } from "@rsbuild/shared";
|
|
23
23
|
|
|
24
24
|
// src/helper.ts
|
|
25
25
|
import { isAbsolute, normalize, sep } from "path";
|
|
@@ -28,6 +28,7 @@ import {
|
|
|
28
28
|
mergeChainedOptions
|
|
29
29
|
} from "@rsbuild/shared";
|
|
30
30
|
import upath from "upath";
|
|
31
|
+
var BABEL_JS_RULE = "babel-js";
|
|
31
32
|
var normalizeToPosixPath = (p) => upath.normalizeSafe(normalize(p || "")).replace(/^([a-zA-Z]+):/, (_, m) => `/${m.toLowerCase()}`);
|
|
32
33
|
var formatPath = (originPath) => {
|
|
33
34
|
if (isAbsolute(originPath)) {
|
|
@@ -111,7 +112,7 @@ var getBabelUtils = (config) => {
|
|
|
111
112
|
// It can be overridden by `extraBabelUtils`.
|
|
112
113
|
addIncludes: noop,
|
|
113
114
|
addExcludes: noop,
|
|
114
|
-
// Compat `presetEnvOptions` and `presetReactOptions` in
|
|
115
|
+
// Compat `presetEnvOptions` and `presetReactOptions` in Modern.js
|
|
115
116
|
modifyPresetEnvOptions: (options) => modifyPresetOptions("@babel/preset-env", options, config.presets || []),
|
|
116
117
|
modifyPresetReactOptions: (options) => modifyPresetOptions("@babel/preset-react", options, config.presets || [])
|
|
117
118
|
};
|
|
@@ -137,6 +138,21 @@ var getUseBuiltIns = (config) => {
|
|
|
137
138
|
}
|
|
138
139
|
return polyfill;
|
|
139
140
|
};
|
|
141
|
+
var modifyBabelLoaderOptions = ({
|
|
142
|
+
chain,
|
|
143
|
+
CHAIN_ID,
|
|
144
|
+
modifier
|
|
145
|
+
}) => {
|
|
146
|
+
const ruleIds = [CHAIN_ID.RULE.JS, CHAIN_ID.RULE.JS_DATA_URI, BABEL_JS_RULE];
|
|
147
|
+
ruleIds.forEach((ruleId) => {
|
|
148
|
+
if (chain.module.rules.has(ruleId)) {
|
|
149
|
+
const rule = chain.module.rule(ruleId);
|
|
150
|
+
if (rule.uses.has(CHAIN_ID.USE.BABEL)) {
|
|
151
|
+
rule.use(CHAIN_ID.USE.BABEL).tap(modifier);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
};
|
|
140
156
|
|
|
141
157
|
// src/plugin.ts
|
|
142
158
|
var DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
|
|
@@ -153,24 +169,6 @@ var pluginBabel = (options = {}) => ({
|
|
|
153
169
|
setup(api) {
|
|
154
170
|
api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd }) => {
|
|
155
171
|
const getBabelOptions = () => {
|
|
156
|
-
const includes2 = [];
|
|
157
|
-
const excludes2 = [];
|
|
158
|
-
const babelUtils = {
|
|
159
|
-
addIncludes(items) {
|
|
160
|
-
if (Array.isArray(items)) {
|
|
161
|
-
includes2.push(...items);
|
|
162
|
-
} else {
|
|
163
|
-
includes2.push(items);
|
|
164
|
-
}
|
|
165
|
-
},
|
|
166
|
-
addExcludes(items) {
|
|
167
|
-
if (Array.isArray(items)) {
|
|
168
|
-
excludes2.push(...items);
|
|
169
|
-
} else {
|
|
170
|
-
excludes2.push(items);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
172
|
const baseConfig = {
|
|
175
173
|
plugins: [],
|
|
176
174
|
presets: [
|
|
@@ -183,35 +181,47 @@ var pluginBabel = (options = {}) => ({
|
|
|
183
181
|
};
|
|
184
182
|
const userBabelConfig = applyUserBabelConfig(
|
|
185
183
|
cloneDeep(baseConfig),
|
|
186
|
-
options.babelLoaderOptions
|
|
187
|
-
babelUtils
|
|
184
|
+
options.babelLoaderOptions
|
|
188
185
|
);
|
|
189
|
-
|
|
186
|
+
return {
|
|
190
187
|
babelrc: false,
|
|
191
188
|
configFile: false,
|
|
192
189
|
compact: isProd,
|
|
193
190
|
...userBabelConfig
|
|
194
191
|
};
|
|
195
|
-
return {
|
|
196
|
-
babelOptions: babelOptions2,
|
|
197
|
-
includes: includes2,
|
|
198
|
-
excludes: excludes2
|
|
199
|
-
};
|
|
200
192
|
};
|
|
201
|
-
const
|
|
202
|
-
const
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
193
|
+
const babelOptions = getBabelOptions();
|
|
194
|
+
const babelLoader = path2.resolve(
|
|
195
|
+
__dirname,
|
|
196
|
+
"../compiled/babel-loader/index.js"
|
|
197
|
+
);
|
|
198
|
+
const { include, exclude } = options;
|
|
199
|
+
if (include || exclude) {
|
|
200
|
+
const rule = chain.module.rule(BABEL_JS_RULE);
|
|
201
|
+
if (include) {
|
|
202
|
+
castArray2(include).forEach((condition) => {
|
|
203
|
+
rule.include.add(condition);
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
if (exclude) {
|
|
207
|
+
castArray2(exclude).forEach((condition) => {
|
|
208
|
+
rule.exclude.add(condition);
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
const swcRule = chain.module.rules.get(CHAIN_ID.RULE.JS).use(CHAIN_ID.USE.SWC);
|
|
212
|
+
const swcLoader = swcRule.get("loader");
|
|
213
|
+
const swcOptions = swcRule.get("options");
|
|
214
|
+
rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.SWC).loader(swcLoader).options(swcOptions).end().use(CHAIN_ID.USE.BABEL).loader(babelLoader).options(babelOptions);
|
|
215
|
+
} else {
|
|
216
|
+
const rule = chain.module.rule(CHAIN_ID.RULE.JS);
|
|
217
|
+
rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(babelLoader).options(babelOptions);
|
|
218
|
+
}
|
|
210
219
|
});
|
|
211
220
|
}
|
|
212
221
|
});
|
|
213
222
|
export {
|
|
214
223
|
getBabelUtils,
|
|
215
224
|
getUseBuiltIns,
|
|
225
|
+
modifyBabelLoaderOptions,
|
|
216
226
|
pluginBabel
|
|
217
227
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsbuild/plugin-babel",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "Babel plugin for Rsbuild",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -27,13 +27,13 @@
|
|
|
27
27
|
"@babel/preset-typescript": "^7.23.2",
|
|
28
28
|
"@types/babel__core": "^7.20.3",
|
|
29
29
|
"upath": "2.0.1",
|
|
30
|
-
"@rsbuild/shared": "0.2.
|
|
30
|
+
"@rsbuild/shared": "0.2.2"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/node": "16.x",
|
|
34
34
|
"typescript": "^5.3.0",
|
|
35
|
-
"@rsbuild/
|
|
36
|
-
"@rsbuild/
|
|
35
|
+
"@rsbuild/test-helper": "0.2.2",
|
|
36
|
+
"@rsbuild/core": "0.2.2"
|
|
37
37
|
},
|
|
38
38
|
"publishConfig": {
|
|
39
39
|
"access": "public",
|