@rsbuild/plugin-babel 0.6.14 → 0.7.0-beta.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/dist/helper.d.ts +11 -0
- package/dist/{index.mjs → index.cjs} +66 -53
- package/dist/index.d.ts +3 -112
- package/dist/index.js +53 -66
- package/dist/plugin.d.ts +15 -0
- package/dist/types.d.ts +97 -0
- package/package.json +9 -9
package/dist/helper.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type BundlerChain, type ChainIdentifier, type NormalizedConfig } from '@rsbuild/shared';
|
|
2
|
+
import type { BabelConfigUtils, BabelLoaderOptions, BabelTransformOptions, PluginBabelOptions } from './types';
|
|
3
|
+
export declare const BABEL_JS_RULE = "babel-js";
|
|
4
|
+
export declare const getBabelUtils: (config: BabelTransformOptions) => BabelConfigUtils;
|
|
5
|
+
export declare const applyUserBabelConfig: (defaultOptions: BabelLoaderOptions, userBabelConfig?: PluginBabelOptions['babelLoaderOptions'], extraBabelUtils?: Partial<BabelConfigUtils>) => BabelLoaderOptions;
|
|
6
|
+
export declare const getUseBuiltIns: (config: NormalizedConfig) => false | "usage" | "entry";
|
|
7
|
+
export declare const modifyBabelLoaderOptions: ({ chain, CHAIN_ID, modifier, }: {
|
|
8
|
+
chain: BundlerChain;
|
|
9
|
+
CHAIN_ID: ChainIdentifier;
|
|
10
|
+
modifier: (config: BabelTransformOptions) => BabelTransformOptions;
|
|
11
|
+
}) => void;
|
|
@@ -1,44 +1,56 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
3
29
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
getBabelUtils: () => getBabelUtils,
|
|
34
|
+
getDefaultBabelOptions: () => getDefaultBabelOptions,
|
|
35
|
+
getUseBuiltIns: () => getUseBuiltIns,
|
|
36
|
+
modifyBabelLoaderOptions: () => modifyBabelLoaderOptions,
|
|
37
|
+
pluginBabel: () => pluginBabel
|
|
10
38
|
});
|
|
11
|
-
|
|
12
|
-
// ../../node_modules/.pnpm/@modern-js+module-tools@2.49.2_eslint@8.57.0_typescript@5.4.5/node_modules/@modern-js/module-tools/shims/esm.js
|
|
13
|
-
import { fileURLToPath } from "url";
|
|
14
|
-
import path from "path";
|
|
15
|
-
var getFilename = () => fileURLToPath(import.meta.url);
|
|
16
|
-
var getDirname = () => path.dirname(getFilename());
|
|
17
|
-
var __dirname = /* @__PURE__ */ getDirname();
|
|
39
|
+
module.exports = __toCommonJS(src_exports);
|
|
18
40
|
|
|
19
41
|
// src/plugin.ts
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
SCRIPT_REGEX,
|
|
23
|
-
castArray as castArray2,
|
|
24
|
-
cloneDeep,
|
|
25
|
-
fse,
|
|
26
|
-
getNodeEnv,
|
|
27
|
-
isProd
|
|
28
|
-
} from "@rsbuild/shared";
|
|
42
|
+
var import_node_path2 = __toESM(require("path"));
|
|
43
|
+
var import_shared2 = require("@rsbuild/shared");
|
|
29
44
|
|
|
30
45
|
// src/helper.ts
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
mergeChainedOptions
|
|
35
|
-
} from "@rsbuild/shared";
|
|
36
|
-
import upath from "upath";
|
|
46
|
+
var import_node_path = require("path");
|
|
47
|
+
var import_shared = require("@rsbuild/shared");
|
|
48
|
+
var import_upath = __toESM(require("upath"));
|
|
37
49
|
var BABEL_JS_RULE = "babel-js";
|
|
38
|
-
var normalizeToPosixPath = (p) =>
|
|
50
|
+
var normalizeToPosixPath = (p) => import_upath.default.normalizeSafe((0, import_node_path.normalize)(p || "")).replace(/^([a-zA-Z]+):/, (_, m) => `/${m.toLowerCase()}`);
|
|
39
51
|
var formatPath = (originPath) => {
|
|
40
|
-
if (isAbsolute(originPath)) {
|
|
41
|
-
return originPath.split(sep).join("/");
|
|
52
|
+
if ((0, import_node_path.isAbsolute)(originPath)) {
|
|
53
|
+
return originPath.split(import_node_path.sep).join("/");
|
|
42
54
|
}
|
|
43
55
|
return originPath;
|
|
44
56
|
};
|
|
@@ -69,7 +81,7 @@ var removePlugins = (plugins, config) => {
|
|
|
69
81
|
if (!config.plugins) {
|
|
70
82
|
return;
|
|
71
83
|
}
|
|
72
|
-
const removeList = castArray(plugins);
|
|
84
|
+
const removeList = (0, import_shared.castArray)(plugins);
|
|
73
85
|
config.plugins = config.plugins.filter((item) => {
|
|
74
86
|
const name = getPluginItemName(item);
|
|
75
87
|
if (name) {
|
|
@@ -82,7 +94,7 @@ var removePresets = (presets, config) => {
|
|
|
82
94
|
if (!config.presets) {
|
|
83
95
|
return;
|
|
84
96
|
}
|
|
85
|
-
const removeList = castArray(presets);
|
|
97
|
+
const removeList = (0, import_shared.castArray)(presets);
|
|
86
98
|
config.presets = config.presets.filter((item) => {
|
|
87
99
|
const name = getPluginItemName(item);
|
|
88
100
|
if (name) {
|
|
@@ -129,7 +141,7 @@ var applyUserBabelConfig = (defaultOptions, userBabelConfig, extraBabelUtils) =>
|
|
|
129
141
|
...getBabelUtils(defaultOptions),
|
|
130
142
|
...extraBabelUtils
|
|
131
143
|
};
|
|
132
|
-
return mergeChainedOptions({
|
|
144
|
+
return (0, import_shared.mergeChainedOptions)({
|
|
133
145
|
defaults: defaultOptions,
|
|
134
146
|
options: userBabelConfig,
|
|
135
147
|
utils: babelUtils
|
|
@@ -173,14 +185,14 @@ var DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
|
|
|
173
185
|
};
|
|
174
186
|
function getCacheDirectory(context, cacheDirectory) {
|
|
175
187
|
if (cacheDirectory) {
|
|
176
|
-
return
|
|
188
|
+
return (0, import_node_path2.isAbsolute)(cacheDirectory) ? cacheDirectory : (0, import_node_path2.join)(context.rootPath, cacheDirectory);
|
|
177
189
|
}
|
|
178
|
-
return join(context.cachePath);
|
|
190
|
+
return (0, import_node_path2.join)(context.cachePath);
|
|
179
191
|
}
|
|
180
192
|
async function getCacheIdentifier(options) {
|
|
181
|
-
let identifier = `${getNodeEnv()}${JSON.stringify(options)}`;
|
|
193
|
+
let identifier = `${(0, import_shared2.getNodeEnv)()}${JSON.stringify(options)}`;
|
|
182
194
|
const { version: coreVersion } = await import("@babel/core");
|
|
183
|
-
const loaderVersion = (await fse.readJSON(join(__dirname, "../compiled/babel-loader/package.json"))).version;
|
|
195
|
+
const loaderVersion = (await import_shared2.fse.readJSON((0, import_node_path2.join)(__dirname, "../compiled/babel-loader/package.json"))).version;
|
|
184
196
|
identifier += `@babel/core@${coreVersion}`;
|
|
185
197
|
identifier += `babel-loader@${loaderVersion}`;
|
|
186
198
|
return identifier;
|
|
@@ -190,20 +202,20 @@ var getDefaultBabelOptions = (config, context) => {
|
|
|
190
202
|
const options = {
|
|
191
203
|
babelrc: false,
|
|
192
204
|
configFile: false,
|
|
193
|
-
compact: isProd(),
|
|
205
|
+
compact: (0, import_shared2.isProd)(),
|
|
194
206
|
plugins: [
|
|
195
207
|
[
|
|
196
|
-
|
|
208
|
+
require.resolve("@babel/plugin-proposal-decorators"),
|
|
197
209
|
config.source.decorators
|
|
198
210
|
],
|
|
199
211
|
// If you are using @babel/preset-env and legacy decorators, you must ensure the class elements transform is enabled regardless of your targets, because Babel only supports compiling legacy decorators when also compiling class properties:
|
|
200
212
|
// see https://babeljs.io/docs/babel-plugin-proposal-decorators#legacy
|
|
201
|
-
...isLegacyDecorators ? [
|
|
213
|
+
...isLegacyDecorators ? [require.resolve("@babel/plugin-transform-class-properties")] : []
|
|
202
214
|
],
|
|
203
215
|
presets: [
|
|
204
216
|
// TODO: only apply preset-typescript for ts file (isTSX & allExtensions false)
|
|
205
217
|
[
|
|
206
|
-
|
|
218
|
+
require.resolve("@babel/preset-typescript"),
|
|
207
219
|
DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS
|
|
208
220
|
]
|
|
209
221
|
]
|
|
@@ -215,7 +227,7 @@ var getDefaultBabelOptions = (config, context) => {
|
|
|
215
227
|
typeof buildCache === "boolean" ? void 0 : buildCache.cacheDirectory
|
|
216
228
|
);
|
|
217
229
|
options.cacheCompression = false;
|
|
218
|
-
options.cacheDirectory = join(cacheDirectory, "babel-loader");
|
|
230
|
+
options.cacheDirectory = (0, import_node_path2.join)(cacheDirectory, "babel-loader");
|
|
219
231
|
}
|
|
220
232
|
return options;
|
|
221
233
|
};
|
|
@@ -226,7 +238,7 @@ var pluginBabel = (options = {}) => ({
|
|
|
226
238
|
const config = api.getNormalizedConfig();
|
|
227
239
|
const baseOptions = getDefaultBabelOptions(config, api.context);
|
|
228
240
|
const mergedOptions = applyUserBabelConfig(
|
|
229
|
-
cloneDeep(baseOptions),
|
|
241
|
+
(0, import_shared2.cloneDeep)(baseOptions),
|
|
230
242
|
options.babelLoaderOptions
|
|
231
243
|
);
|
|
232
244
|
if (mergedOptions.cacheDirectory && !mergedOptions.cacheIdentifier) {
|
|
@@ -238,7 +250,7 @@ var pluginBabel = (options = {}) => ({
|
|
|
238
250
|
order: "pre",
|
|
239
251
|
handler: async (chain, { CHAIN_ID }) => {
|
|
240
252
|
const babelOptions = await getBabelOptions();
|
|
241
|
-
const babelLoader =
|
|
253
|
+
const babelLoader = import_node_path2.default.resolve(
|
|
242
254
|
__dirname,
|
|
243
255
|
"../compiled/babel-loader/index.js"
|
|
244
256
|
);
|
|
@@ -246,31 +258,32 @@ var pluginBabel = (options = {}) => ({
|
|
|
246
258
|
if (include || exclude) {
|
|
247
259
|
const rule = chain.module.rule(BABEL_JS_RULE);
|
|
248
260
|
if (include) {
|
|
249
|
-
for (const condition of
|
|
261
|
+
for (const condition of (0, import_shared2.castArray)(include)) {
|
|
250
262
|
rule.include.add(condition);
|
|
251
263
|
}
|
|
252
264
|
}
|
|
253
265
|
if (exclude) {
|
|
254
|
-
for (const condition of
|
|
266
|
+
for (const condition of (0, import_shared2.castArray)(exclude)) {
|
|
255
267
|
rule.exclude.add(condition);
|
|
256
268
|
}
|
|
257
269
|
}
|
|
258
270
|
const swcRule = chain.module.rules.get(CHAIN_ID.RULE.JS).use(CHAIN_ID.USE.SWC);
|
|
259
271
|
const swcLoader = swcRule.get("loader");
|
|
260
272
|
const swcOptions = swcRule.get("options");
|
|
261
|
-
rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.SWC).loader(swcLoader).options(swcOptions).end().use(CHAIN_ID.USE.BABEL).loader(babelLoader).options(babelOptions);
|
|
273
|
+
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);
|
|
262
274
|
} else {
|
|
263
275
|
const rule = chain.module.rule(CHAIN_ID.RULE.JS);
|
|
264
|
-
rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(babelLoader).options(babelOptions);
|
|
276
|
+
rule.test(import_shared2.SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(babelLoader).options(babelOptions);
|
|
265
277
|
}
|
|
266
278
|
}
|
|
267
279
|
});
|
|
268
280
|
}
|
|
269
281
|
});
|
|
270
|
-
export
|
|
282
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
283
|
+
0 && (module.exports = {
|
|
271
284
|
getBabelUtils,
|
|
272
285
|
getDefaultBabelOptions,
|
|
273
286
|
getUseBuiltIns,
|
|
274
287
|
modifyBabelLoaderOptions,
|
|
275
288
|
pluginBabel
|
|
276
|
-
};
|
|
289
|
+
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,112 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export {
|
|
4
|
-
import { ChainedConfigWithUtils, NormalizedConfig as NormalizedConfig$1, BundlerChain, ChainIdentifier } from '@rsbuild/shared';
|
|
5
|
-
|
|
6
|
-
type PresetEnvTargets = string | string[] | Record<string, string>;
|
|
7
|
-
type PresetEnvBuiltIns = 'usage' | 'entry' | false;
|
|
8
|
-
type PresetEnvOptions = {
|
|
9
|
-
targets?: PresetEnvTargets;
|
|
10
|
-
bugfixes?: boolean;
|
|
11
|
-
spec?: boolean;
|
|
12
|
-
loose?: boolean;
|
|
13
|
-
modules?: 'amd' | 'umd' | 'systemjs' | 'commonjs' | 'cjs' | 'auto' | false;
|
|
14
|
-
debug?: boolean;
|
|
15
|
-
include?: string[];
|
|
16
|
-
exclude?: string[];
|
|
17
|
-
useBuiltIns?: PresetEnvBuiltIns;
|
|
18
|
-
corejs?: string | {
|
|
19
|
-
version: string;
|
|
20
|
-
proposals: boolean;
|
|
21
|
-
};
|
|
22
|
-
forceAllTransforms?: boolean;
|
|
23
|
-
configPath?: string;
|
|
24
|
-
ignoreBrowserslistConfig?: boolean;
|
|
25
|
-
browserslistEnv?: string;
|
|
26
|
-
shippedProposals?: boolean;
|
|
27
|
-
};
|
|
28
|
-
interface SharedBabelPresetReactOptions {
|
|
29
|
-
development?: boolean;
|
|
30
|
-
throwIfNamespace?: boolean;
|
|
31
|
-
}
|
|
32
|
-
interface AutomaticRuntimePresetReactOptions extends SharedBabelPresetReactOptions {
|
|
33
|
-
runtime?: 'automatic';
|
|
34
|
-
importSource?: string;
|
|
35
|
-
}
|
|
36
|
-
interface ClassicRuntimePresetReactOptions extends SharedBabelPresetReactOptions {
|
|
37
|
-
runtime?: 'classic';
|
|
38
|
-
pragma?: string;
|
|
39
|
-
pragmaFrag?: string;
|
|
40
|
-
useBuiltIns?: boolean;
|
|
41
|
-
useSpread?: boolean;
|
|
42
|
-
}
|
|
43
|
-
type PresetReactOptions = AutomaticRuntimePresetReactOptions | ClassicRuntimePresetReactOptions;
|
|
44
|
-
type RuleCondition = string | RegExp | (string | RegExp)[];
|
|
45
|
-
type BabelConfigUtils = {
|
|
46
|
-
addPlugins: (plugins: PluginItem[]) => void;
|
|
47
|
-
addPresets: (presets: PluginItem[]) => void;
|
|
48
|
-
removePlugins: (plugins: string | string[]) => void;
|
|
49
|
-
removePresets: (presets: string | string[]) => void;
|
|
50
|
-
modifyPresetEnvOptions: (options: PresetEnvOptions) => void;
|
|
51
|
-
modifyPresetReactOptions: (options: PresetReactOptions) => void;
|
|
52
|
-
/**
|
|
53
|
-
* use `source.include` instead
|
|
54
|
-
* @deprecated
|
|
55
|
-
*/
|
|
56
|
-
addIncludes: (includes: RuleCondition) => void;
|
|
57
|
-
/**
|
|
58
|
-
* use `source.exclude` instead
|
|
59
|
-
* @deprecated
|
|
60
|
-
*/
|
|
61
|
-
addExcludes: (excludes: RuleCondition) => void;
|
|
62
|
-
};
|
|
63
|
-
type BabelLoaderOptions = TransformOptions & {
|
|
64
|
-
/**
|
|
65
|
-
* When set, the given directory will be used to cache the results of the loader.
|
|
66
|
-
*/
|
|
67
|
-
cacheDirectory?: string | boolean;
|
|
68
|
-
/**
|
|
69
|
-
* Can be set to a custom value to force cache busting if the identifier changes.
|
|
70
|
-
*/
|
|
71
|
-
cacheIdentifier?: string;
|
|
72
|
-
/**
|
|
73
|
-
* When set, each Babel transform output will be compressed with Gzip.
|
|
74
|
-
*/
|
|
75
|
-
cacheCompression?: boolean;
|
|
76
|
-
/**
|
|
77
|
-
* The path of a module that exports a custom callback.
|
|
78
|
-
*/
|
|
79
|
-
customize?: string | null;
|
|
80
|
-
/**
|
|
81
|
-
* Takes an array of context function names. E.g.
|
|
82
|
-
*/
|
|
83
|
-
metadataSubscribers?: string[];
|
|
84
|
-
};
|
|
85
|
-
type PluginBabelOptions = {
|
|
86
|
-
/**
|
|
87
|
-
* Used to specify the files that need to be compiled by Babel.
|
|
88
|
-
*/
|
|
89
|
-
include?: RuleCondition;
|
|
90
|
-
/**
|
|
91
|
-
* Used to specify the files that do not need to be compiled by Babel.
|
|
92
|
-
*/
|
|
93
|
-
exclude?: RuleCondition;
|
|
94
|
-
/**
|
|
95
|
-
* Options passed to `babel-loader`.
|
|
96
|
-
* @see https://github.com/babel/babel-loader
|
|
97
|
-
*/
|
|
98
|
-
babelLoaderOptions?: ChainedConfigWithUtils<BabelLoaderOptions, BabelConfigUtils>;
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
declare const getDefaultBabelOptions: (config: NormalizedConfig, context: RsbuildContext) => BabelLoaderOptions;
|
|
102
|
-
declare const pluginBabel: (options?: PluginBabelOptions) => RsbuildPlugin;
|
|
103
|
-
|
|
104
|
-
declare const getBabelUtils: (config: TransformOptions) => BabelConfigUtils;
|
|
105
|
-
declare const getUseBuiltIns: (config: NormalizedConfig$1) => false | "usage" | "entry";
|
|
106
|
-
declare const modifyBabelLoaderOptions: ({ chain, CHAIN_ID, modifier, }: {
|
|
107
|
-
chain: BundlerChain;
|
|
108
|
-
CHAIN_ID: ChainIdentifier;
|
|
109
|
-
modifier: (config: TransformOptions) => TransformOptions;
|
|
110
|
-
}) => void;
|
|
111
|
-
|
|
112
|
-
export { type BabelConfigUtils, type PluginBabelOptions, type PresetEnvBuiltIns, type PresetEnvOptions, type PresetEnvTargets, getBabelUtils, getDefaultBabelOptions, getUseBuiltIns, modifyBabelLoaderOptions, pluginBabel };
|
|
1
|
+
export { pluginBabel, getDefaultBabelOptions } from './plugin';
|
|
2
|
+
export { getBabelUtils, getUseBuiltIns, modifyBabelLoaderOptions, } from './helper';
|
|
3
|
+
export type { PresetEnvOptions, PresetEnvTargets, PresetEnvBuiltIns, BabelConfigUtils, BabelTransformOptions, PluginBabelOptions, } from './types';
|
package/dist/index.js
CHANGED
|
@@ -1,56 +1,44 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
1
|
+
import { createRequire } from 'module';
|
|
2
|
+
var require = createRequire(import.meta['url']);
|
|
29
3
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
modifyBabelLoaderOptions: () => modifyBabelLoaderOptions,
|
|
37
|
-
pluginBabel: () => pluginBabel
|
|
4
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
5
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
6
|
+
}) : x)(function(x) {
|
|
7
|
+
if (typeof require !== "undefined")
|
|
8
|
+
return require.apply(this, arguments);
|
|
9
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
38
10
|
});
|
|
39
|
-
|
|
11
|
+
|
|
12
|
+
// ../../node_modules/.pnpm/@modern-js+module-tools@2.49.3_eslint@8.57.0_typescript@5.4.5/node_modules/@modern-js/module-tools/shims/esm.js
|
|
13
|
+
import { fileURLToPath } from "url";
|
|
14
|
+
import path from "path";
|
|
15
|
+
var getFilename = () => fileURLToPath(import.meta.url);
|
|
16
|
+
var getDirname = () => path.dirname(getFilename());
|
|
17
|
+
var __dirname = /* @__PURE__ */ getDirname();
|
|
40
18
|
|
|
41
19
|
// src/plugin.ts
|
|
42
|
-
|
|
43
|
-
|
|
20
|
+
import path2, { isAbsolute as isAbsolute2, join } from "path";
|
|
21
|
+
import {
|
|
22
|
+
SCRIPT_REGEX,
|
|
23
|
+
castArray as castArray2,
|
|
24
|
+
cloneDeep,
|
|
25
|
+
fse,
|
|
26
|
+
getNodeEnv,
|
|
27
|
+
isProd
|
|
28
|
+
} from "@rsbuild/shared";
|
|
44
29
|
|
|
45
30
|
// src/helper.ts
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
31
|
+
import { isAbsolute, normalize, sep } from "path";
|
|
32
|
+
import {
|
|
33
|
+
castArray,
|
|
34
|
+
mergeChainedOptions
|
|
35
|
+
} from "@rsbuild/shared";
|
|
36
|
+
import upath from "upath";
|
|
49
37
|
var BABEL_JS_RULE = "babel-js";
|
|
50
|
-
var normalizeToPosixPath = (p) =>
|
|
38
|
+
var normalizeToPosixPath = (p) => upath.normalizeSafe(normalize(p || "")).replace(/^([a-zA-Z]+):/, (_, m) => `/${m.toLowerCase()}`);
|
|
51
39
|
var formatPath = (originPath) => {
|
|
52
|
-
if (
|
|
53
|
-
return originPath.split(
|
|
40
|
+
if (isAbsolute(originPath)) {
|
|
41
|
+
return originPath.split(sep).join("/");
|
|
54
42
|
}
|
|
55
43
|
return originPath;
|
|
56
44
|
};
|
|
@@ -81,7 +69,7 @@ var removePlugins = (plugins, config) => {
|
|
|
81
69
|
if (!config.plugins) {
|
|
82
70
|
return;
|
|
83
71
|
}
|
|
84
|
-
const removeList =
|
|
72
|
+
const removeList = castArray(plugins);
|
|
85
73
|
config.plugins = config.plugins.filter((item) => {
|
|
86
74
|
const name = getPluginItemName(item);
|
|
87
75
|
if (name) {
|
|
@@ -94,7 +82,7 @@ var removePresets = (presets, config) => {
|
|
|
94
82
|
if (!config.presets) {
|
|
95
83
|
return;
|
|
96
84
|
}
|
|
97
|
-
const removeList =
|
|
85
|
+
const removeList = castArray(presets);
|
|
98
86
|
config.presets = config.presets.filter((item) => {
|
|
99
87
|
const name = getPluginItemName(item);
|
|
100
88
|
if (name) {
|
|
@@ -141,7 +129,7 @@ var applyUserBabelConfig = (defaultOptions, userBabelConfig, extraBabelUtils) =>
|
|
|
141
129
|
...getBabelUtils(defaultOptions),
|
|
142
130
|
...extraBabelUtils
|
|
143
131
|
};
|
|
144
|
-
return
|
|
132
|
+
return mergeChainedOptions({
|
|
145
133
|
defaults: defaultOptions,
|
|
146
134
|
options: userBabelConfig,
|
|
147
135
|
utils: babelUtils
|
|
@@ -185,14 +173,14 @@ var DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
|
|
|
185
173
|
};
|
|
186
174
|
function getCacheDirectory(context, cacheDirectory) {
|
|
187
175
|
if (cacheDirectory) {
|
|
188
|
-
return (
|
|
176
|
+
return isAbsolute2(cacheDirectory) ? cacheDirectory : join(context.rootPath, cacheDirectory);
|
|
189
177
|
}
|
|
190
|
-
return
|
|
178
|
+
return join(context.cachePath);
|
|
191
179
|
}
|
|
192
180
|
async function getCacheIdentifier(options) {
|
|
193
|
-
let identifier = `${
|
|
181
|
+
let identifier = `${getNodeEnv()}${JSON.stringify(options)}`;
|
|
194
182
|
const { version: coreVersion } = await import("@babel/core");
|
|
195
|
-
const loaderVersion = (await
|
|
183
|
+
const loaderVersion = (await fse.readJSON(join(__dirname, "../compiled/babel-loader/package.json"))).version;
|
|
196
184
|
identifier += `@babel/core@${coreVersion}`;
|
|
197
185
|
identifier += `babel-loader@${loaderVersion}`;
|
|
198
186
|
return identifier;
|
|
@@ -202,20 +190,20 @@ var getDefaultBabelOptions = (config, context) => {
|
|
|
202
190
|
const options = {
|
|
203
191
|
babelrc: false,
|
|
204
192
|
configFile: false,
|
|
205
|
-
compact:
|
|
193
|
+
compact: isProd(),
|
|
206
194
|
plugins: [
|
|
207
195
|
[
|
|
208
|
-
|
|
196
|
+
__require.resolve("@babel/plugin-proposal-decorators"),
|
|
209
197
|
config.source.decorators
|
|
210
198
|
],
|
|
211
199
|
// If you are using @babel/preset-env and legacy decorators, you must ensure the class elements transform is enabled regardless of your targets, because Babel only supports compiling legacy decorators when also compiling class properties:
|
|
212
200
|
// see https://babeljs.io/docs/babel-plugin-proposal-decorators#legacy
|
|
213
|
-
...isLegacyDecorators ? [
|
|
201
|
+
...isLegacyDecorators ? [__require.resolve("@babel/plugin-transform-class-properties")] : []
|
|
214
202
|
],
|
|
215
203
|
presets: [
|
|
216
204
|
// TODO: only apply preset-typescript for ts file (isTSX & allExtensions false)
|
|
217
205
|
[
|
|
218
|
-
|
|
206
|
+
__require.resolve("@babel/preset-typescript"),
|
|
219
207
|
DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS
|
|
220
208
|
]
|
|
221
209
|
]
|
|
@@ -227,7 +215,7 @@ var getDefaultBabelOptions = (config, context) => {
|
|
|
227
215
|
typeof buildCache === "boolean" ? void 0 : buildCache.cacheDirectory
|
|
228
216
|
);
|
|
229
217
|
options.cacheCompression = false;
|
|
230
|
-
options.cacheDirectory =
|
|
218
|
+
options.cacheDirectory = join(cacheDirectory, "babel-loader");
|
|
231
219
|
}
|
|
232
220
|
return options;
|
|
233
221
|
};
|
|
@@ -238,7 +226,7 @@ var pluginBabel = (options = {}) => ({
|
|
|
238
226
|
const config = api.getNormalizedConfig();
|
|
239
227
|
const baseOptions = getDefaultBabelOptions(config, api.context);
|
|
240
228
|
const mergedOptions = applyUserBabelConfig(
|
|
241
|
-
|
|
229
|
+
cloneDeep(baseOptions),
|
|
242
230
|
options.babelLoaderOptions
|
|
243
231
|
);
|
|
244
232
|
if (mergedOptions.cacheDirectory && !mergedOptions.cacheIdentifier) {
|
|
@@ -250,7 +238,7 @@ var pluginBabel = (options = {}) => ({
|
|
|
250
238
|
order: "pre",
|
|
251
239
|
handler: async (chain, { CHAIN_ID }) => {
|
|
252
240
|
const babelOptions = await getBabelOptions();
|
|
253
|
-
const babelLoader =
|
|
241
|
+
const babelLoader = path2.resolve(
|
|
254
242
|
__dirname,
|
|
255
243
|
"../compiled/babel-loader/index.js"
|
|
256
244
|
);
|
|
@@ -258,32 +246,31 @@ var pluginBabel = (options = {}) => ({
|
|
|
258
246
|
if (include || exclude) {
|
|
259
247
|
const rule = chain.module.rule(BABEL_JS_RULE);
|
|
260
248
|
if (include) {
|
|
261
|
-
for (const condition of (
|
|
249
|
+
for (const condition of castArray2(include)) {
|
|
262
250
|
rule.include.add(condition);
|
|
263
251
|
}
|
|
264
252
|
}
|
|
265
253
|
if (exclude) {
|
|
266
|
-
for (const condition of (
|
|
254
|
+
for (const condition of castArray2(exclude)) {
|
|
267
255
|
rule.exclude.add(condition);
|
|
268
256
|
}
|
|
269
257
|
}
|
|
270
258
|
const swcRule = chain.module.rules.get(CHAIN_ID.RULE.JS).use(CHAIN_ID.USE.SWC);
|
|
271
259
|
const swcLoader = swcRule.get("loader");
|
|
272
260
|
const swcOptions = swcRule.get("options");
|
|
273
|
-
rule.test(
|
|
261
|
+
rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.SWC).loader(swcLoader).options(swcOptions).end().use(CHAIN_ID.USE.BABEL).loader(babelLoader).options(babelOptions);
|
|
274
262
|
} else {
|
|
275
263
|
const rule = chain.module.rule(CHAIN_ID.RULE.JS);
|
|
276
|
-
rule.test(
|
|
264
|
+
rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(babelLoader).options(babelOptions);
|
|
277
265
|
}
|
|
278
266
|
}
|
|
279
267
|
});
|
|
280
268
|
}
|
|
281
269
|
});
|
|
282
|
-
|
|
283
|
-
0 && (module.exports = {
|
|
270
|
+
export {
|
|
284
271
|
getBabelUtils,
|
|
285
272
|
getDefaultBabelOptions,
|
|
286
273
|
getUseBuiltIns,
|
|
287
274
|
modifyBabelLoaderOptions,
|
|
288
275
|
pluginBabel
|
|
289
|
-
}
|
|
276
|
+
};
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { NormalizedConfig, RsbuildContext, RsbuildPlugin } from '@rsbuild/core';
|
|
2
|
+
import type { BabelLoaderOptions, PluginBabelOptions } from './types';
|
|
3
|
+
export declare const PLUGIN_BABEL_NAME = "rsbuild:babel";
|
|
4
|
+
/**
|
|
5
|
+
* The `@babel/preset-typescript` default options.
|
|
6
|
+
*/
|
|
7
|
+
export declare const DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS: {
|
|
8
|
+
allowNamespaces: boolean;
|
|
9
|
+
allExtensions: boolean;
|
|
10
|
+
allowDeclareFields: boolean;
|
|
11
|
+
optimizeConstEnums: boolean;
|
|
12
|
+
isTSX: boolean;
|
|
13
|
+
};
|
|
14
|
+
export declare const getDefaultBabelOptions: (config: NormalizedConfig, context: RsbuildContext) => BabelLoaderOptions;
|
|
15
|
+
export declare const pluginBabel: (options?: PluginBabelOptions) => RsbuildPlugin;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { PluginItem as BabelPlugin, TransformOptions as BabelTransformOptions } from '@babel/core';
|
|
2
|
+
import type { ChainedConfigWithUtils } from '@rsbuild/shared';
|
|
3
|
+
export type { BabelPlugin, BabelTransformOptions };
|
|
4
|
+
export type PresetEnvTargets = string | string[] | Record<string, string>;
|
|
5
|
+
export type PresetEnvBuiltIns = 'usage' | 'entry' | false;
|
|
6
|
+
export type PresetEnvOptions = {
|
|
7
|
+
targets?: PresetEnvTargets;
|
|
8
|
+
bugfixes?: boolean;
|
|
9
|
+
spec?: boolean;
|
|
10
|
+
loose?: boolean;
|
|
11
|
+
modules?: 'amd' | 'umd' | 'systemjs' | 'commonjs' | 'cjs' | 'auto' | false;
|
|
12
|
+
debug?: boolean;
|
|
13
|
+
include?: string[];
|
|
14
|
+
exclude?: string[];
|
|
15
|
+
useBuiltIns?: PresetEnvBuiltIns;
|
|
16
|
+
corejs?: string | {
|
|
17
|
+
version: string;
|
|
18
|
+
proposals: boolean;
|
|
19
|
+
};
|
|
20
|
+
forceAllTransforms?: boolean;
|
|
21
|
+
configPath?: string;
|
|
22
|
+
ignoreBrowserslistConfig?: boolean;
|
|
23
|
+
browserslistEnv?: string;
|
|
24
|
+
shippedProposals?: boolean;
|
|
25
|
+
};
|
|
26
|
+
export interface SharedBabelPresetReactOptions {
|
|
27
|
+
development?: boolean;
|
|
28
|
+
throwIfNamespace?: boolean;
|
|
29
|
+
}
|
|
30
|
+
export interface AutomaticRuntimePresetReactOptions extends SharedBabelPresetReactOptions {
|
|
31
|
+
runtime?: 'automatic';
|
|
32
|
+
importSource?: string;
|
|
33
|
+
}
|
|
34
|
+
export interface ClassicRuntimePresetReactOptions extends SharedBabelPresetReactOptions {
|
|
35
|
+
runtime?: 'classic';
|
|
36
|
+
pragma?: string;
|
|
37
|
+
pragmaFrag?: string;
|
|
38
|
+
useBuiltIns?: boolean;
|
|
39
|
+
useSpread?: boolean;
|
|
40
|
+
}
|
|
41
|
+
export type PresetReactOptions = AutomaticRuntimePresetReactOptions | ClassicRuntimePresetReactOptions;
|
|
42
|
+
export type RuleCondition = string | RegExp | (string | RegExp)[];
|
|
43
|
+
export type BabelConfigUtils = {
|
|
44
|
+
addPlugins: (plugins: BabelPlugin[]) => void;
|
|
45
|
+
addPresets: (presets: BabelPlugin[]) => void;
|
|
46
|
+
removePlugins: (plugins: string | string[]) => void;
|
|
47
|
+
removePresets: (presets: string | string[]) => void;
|
|
48
|
+
modifyPresetEnvOptions: (options: PresetEnvOptions) => void;
|
|
49
|
+
modifyPresetReactOptions: (options: PresetReactOptions) => void;
|
|
50
|
+
/**
|
|
51
|
+
* use `source.include` instead
|
|
52
|
+
* @deprecated
|
|
53
|
+
*/
|
|
54
|
+
addIncludes: (includes: RuleCondition) => void;
|
|
55
|
+
/**
|
|
56
|
+
* use `source.exclude` instead
|
|
57
|
+
* @deprecated
|
|
58
|
+
*/
|
|
59
|
+
addExcludes: (excludes: RuleCondition) => void;
|
|
60
|
+
};
|
|
61
|
+
export type BabelLoaderOptions = BabelTransformOptions & {
|
|
62
|
+
/**
|
|
63
|
+
* When set, the given directory will be used to cache the results of the loader.
|
|
64
|
+
*/
|
|
65
|
+
cacheDirectory?: string | boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Can be set to a custom value to force cache busting if the identifier changes.
|
|
68
|
+
*/
|
|
69
|
+
cacheIdentifier?: string;
|
|
70
|
+
/**
|
|
71
|
+
* When set, each Babel transform output will be compressed with Gzip.
|
|
72
|
+
*/
|
|
73
|
+
cacheCompression?: boolean;
|
|
74
|
+
/**
|
|
75
|
+
* The path of a module that exports a custom callback.
|
|
76
|
+
*/
|
|
77
|
+
customize?: string | null;
|
|
78
|
+
/**
|
|
79
|
+
* Takes an array of context function names. E.g.
|
|
80
|
+
*/
|
|
81
|
+
metadataSubscribers?: string[];
|
|
82
|
+
};
|
|
83
|
+
export type PluginBabelOptions = {
|
|
84
|
+
/**
|
|
85
|
+
* Used to specify the files that need to be compiled by Babel.
|
|
86
|
+
*/
|
|
87
|
+
include?: RuleCondition;
|
|
88
|
+
/**
|
|
89
|
+
* Used to specify the files that do not need to be compiled by Babel.
|
|
90
|
+
*/
|
|
91
|
+
exclude?: RuleCondition;
|
|
92
|
+
/**
|
|
93
|
+
* Options passed to `babel-loader`.
|
|
94
|
+
* @see https://github.com/babel/babel-loader
|
|
95
|
+
*/
|
|
96
|
+
babelLoaderOptions?: ChainedConfigWithUtils<BabelLoaderOptions, BabelConfigUtils>;
|
|
97
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsbuild/plugin-babel",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0-beta.0",
|
|
4
4
|
"description": "Babel plugin for Rsbuild",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -8,15 +8,15 @@
|
|
|
8
8
|
"directory": "packages/plugin-babel"
|
|
9
9
|
},
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"type": "
|
|
11
|
+
"type": "module",
|
|
12
12
|
"exports": {
|
|
13
13
|
".": {
|
|
14
14
|
"types": "./dist/index.d.ts",
|
|
15
|
-
"import": "./dist/index.
|
|
16
|
-
"
|
|
15
|
+
"import": "./dist/index.js",
|
|
16
|
+
"require": "./dist/index.cjs"
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
|
-
"main": "./dist/index.
|
|
19
|
+
"main": "./dist/index.cjs",
|
|
20
20
|
"types": "./dist/index.d.ts",
|
|
21
21
|
"files": [
|
|
22
22
|
"dist",
|
|
@@ -29,18 +29,18 @@
|
|
|
29
29
|
"@babel/preset-typescript": "^7.24.1",
|
|
30
30
|
"@types/babel__core": "^7.20.5",
|
|
31
31
|
"upath": "2.0.1",
|
|
32
|
-
"@rsbuild/shared": "0.
|
|
32
|
+
"@rsbuild/shared": "0.7.0-beta.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@types/node": "18.x",
|
|
36
36
|
"babel-loader": "9.1.3",
|
|
37
37
|
"prebundle": "1.1.0",
|
|
38
38
|
"typescript": "^5.4.2",
|
|
39
|
-
"@rsbuild/core": "0.
|
|
40
|
-
"@scripts/test-helper": "0.
|
|
39
|
+
"@rsbuild/core": "0.7.0-beta.0",
|
|
40
|
+
"@scripts/test-helper": "0.7.0-beta.0"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"@rsbuild/core": "^0.
|
|
43
|
+
"@rsbuild/core": "^0.7.0-beta.0"
|
|
44
44
|
},
|
|
45
45
|
"publishConfig": {
|
|
46
46
|
"access": "public",
|