@rsbuild/plugin-babel 1.0.1 → 1.0.3

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.cjs CHANGED
@@ -1,291 +1,334 @@
1
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;
2
+ var __webpack_modules__ = {
3
+ "@babel/core": function(module1) {
4
+ module1.exports = require("@babel/core");
5
+ }
19
6
  };
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);
29
-
30
- // src/index.ts
31
- var src_exports = {};
32
- __export(src_exports, {
33
- PLUGIN_BABEL_NAME: () => PLUGIN_BABEL_NAME,
34
- getBabelUtils: () => getBabelUtils,
35
- getDefaultBabelOptions: () => getDefaultBabelOptions,
36
- modifyBabelLoaderOptions: () => modifyBabelLoaderOptions,
37
- pluginBabel: () => pluginBabel
7
+ /************************************************************************/ // The module cache
8
+ var __webpack_module_cache__ = {};
9
+ // The require function
10
+ function __webpack_require__(moduleId) {
11
+ // Check if module is in cache
12
+ var cachedModule = __webpack_module_cache__[moduleId];
13
+ if (void 0 !== cachedModule) return cachedModule.exports;
14
+ // Create a new module (and put it into the cache)
15
+ var module1 = __webpack_module_cache__[moduleId] = {
16
+ exports: {}
17
+ };
18
+ // Execute the module function
19
+ __webpack_modules__[moduleId](module1, module1.exports, __webpack_require__);
20
+ // Return the exports of the module
21
+ return module1.exports;
22
+ }
23
+ /************************************************************************/ // webpack/runtime/compat_get_default_export
24
+ (()=>{
25
+ // getDefaultExport function for compatibility with non-ESM modules
26
+ __webpack_require__.n = function(module1) {
27
+ var getter = module1 && module1.__esModule ? function() {
28
+ return module1['default'];
29
+ } : function() {
30
+ return module1;
31
+ };
32
+ __webpack_require__.d(getter, {
33
+ a: getter
34
+ });
35
+ return getter;
36
+ };
37
+ })();
38
+ // webpack/runtime/create_fake_namespace_object
39
+ (()=>{
40
+ var getProto = Object.getPrototypeOf ? function(obj) {
41
+ return Object.getPrototypeOf(obj);
42
+ } : function(obj) {
43
+ return obj.__proto__;
44
+ };
45
+ var leafPrototypes;
46
+ // create a fake namespace object
47
+ // mode & 1: value is a module id, require it
48
+ // mode & 2: merge all properties of value into the ns
49
+ // mode & 4: return value when already ns object
50
+ // mode & 16: return value when it's Promise-like
51
+ // mode & 8|1: behave like require
52
+ __webpack_require__.t = function(value, mode) {
53
+ if (1 & mode) value = this(value);
54
+ if (8 & mode) return value;
55
+ if ('object' == typeof value && value) {
56
+ if (4 & mode && value.__esModule) return value;
57
+ if (16 & mode && 'function' == typeof value.then) return value;
58
+ }
59
+ var ns = Object.create(null);
60
+ __webpack_require__.r(ns);
61
+ var def = {};
62
+ leafPrototypes = leafPrototypes || [
63
+ null,
64
+ getProto({}),
65
+ getProto([]),
66
+ getProto(getProto)
67
+ ];
68
+ for(var current = 2 & mode && value; 'object' == typeof current && !~leafPrototypes.indexOf(current); current = getProto(current))Object.getOwnPropertyNames(current).forEach(function(key) {
69
+ def[key] = function() {
70
+ return value[key];
71
+ };
72
+ });
73
+ def['default'] = function() {
74
+ return value;
75
+ };
76
+ __webpack_require__.d(ns, def);
77
+ return ns;
78
+ };
79
+ })();
80
+ // webpack/runtime/define_property_getters
81
+ (()=>{
82
+ __webpack_require__.d = function(exports1, definition) {
83
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
84
+ enumerable: true,
85
+ get: definition[key]
86
+ });
87
+ };
88
+ })();
89
+ // webpack/runtime/has_own_property
90
+ (()=>{
91
+ __webpack_require__.o = function(obj, prop) {
92
+ return Object.prototype.hasOwnProperty.call(obj, prop);
93
+ };
94
+ })();
95
+ // webpack/runtime/make_namespace_object
96
+ (()=>{
97
+ // define __esModule on exports
98
+ __webpack_require__.r = function(exports1) {
99
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
100
+ value: 'Module'
101
+ });
102
+ Object.defineProperty(exports1, '__esModule', {
103
+ value: true
104
+ });
105
+ };
106
+ })();
107
+ /************************************************************************/ var __webpack_exports__ = {};
108
+ // ESM COMPAT FLAG
109
+ __webpack_require__.r(__webpack_exports__);
110
+ // EXPORTS
111
+ __webpack_require__.d(__webpack_exports__, {
112
+ pluginBabel: ()=>/* reexport */ pluginBabel,
113
+ getBabelUtils: ()=>/* reexport */ getBabelUtils,
114
+ modifyBabelLoaderOptions: ()=>/* reexport */ modifyBabelLoaderOptions,
115
+ PLUGIN_BABEL_NAME: ()=>/* reexport */ PLUGIN_BABEL_NAME,
116
+ getDefaultBabelOptions: ()=>/* reexport */ getDefaultBabelOptions
38
117
  });
39
- module.exports = __toCommonJS(src_exports);
40
-
41
- // src/plugin.ts
42
- var import_node_fs = __toESM(require("fs"));
43
- var import_node_path2 = __toESM(require("path"));
44
- var import_deepmerge = __toESM(require("deepmerge"));
45
-
46
- // src/helper.ts
47
- var import_node_path = require("path");
48
- var import_reduce_configs = require("reduce-configs");
49
- var import_upath = __toESM(require("upath"));
50
- var BABEL_JS_RULE = "babel-js";
51
- var castArray = (arr) => {
52
- if (arr === void 0) {
53
- return [];
54
- }
55
- return Array.isArray(arr) ? arr : [arr];
56
- };
57
- var normalizeToPosixPath = (p) => import_upath.default.normalizeSafe((0, import_node_path.normalize)(p || "")).replace(/^([a-zA-Z]+):/, (_, m) => `/${m.toLowerCase()}`);
58
- var formatPath = (originPath) => {
59
- if ((0, import_node_path.isAbsolute)(originPath)) {
60
- return originPath.split(import_node_path.sep).join("/");
61
- }
62
- return originPath;
118
+ const external_node_fs_namespaceObject = require("node:fs");
119
+ var external_node_fs_default = /*#__PURE__*/ __webpack_require__.n(external_node_fs_namespaceObject);
120
+ const external_node_module_namespaceObject = require("node:module");
121
+ const external_node_path_namespaceObject = require("node:path");
122
+ var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
123
+ const external_node_url_namespaceObject = require("node:url");
124
+ const external_deepmerge_namespaceObject = require("deepmerge");
125
+ var external_deepmerge_default = /*#__PURE__*/ __webpack_require__.n(external_deepmerge_namespaceObject);
126
+ const external_reduce_configs_namespaceObject = require("reduce-configs");
127
+ const external_upath_namespaceObject = require("upath");
128
+ var external_upath_default = /*#__PURE__*/ __webpack_require__.n(external_upath_namespaceObject);
129
+ const BABEL_JS_RULE = 'babel-js';
130
+ const castArray = (arr)=>{
131
+ if (void 0 === arr) return [];
132
+ return Array.isArray(arr) ? arr : [
133
+ arr
134
+ ];
63
135
  };
64
- var getPluginItemName = (item) => {
65
- if (typeof item === "string") {
66
- return formatPath(item);
67
- }
68
- if (Array.isArray(item) && typeof item[0] === "string") {
69
- return formatPath(item[0]);
70
- }
71
- return null;
136
+ const normalizeToPosixPath = (p)=>external_upath_default().normalizeSafe((0, external_node_path_namespaceObject.normalize)(p || '')).replace(/^([a-zA-Z]+):/, (_, m)=>`/${m.toLowerCase()}`);
137
+ // compatible with Windows path
138
+ const formatPath = (originPath)=>{
139
+ if ((0, external_node_path_namespaceObject.isAbsolute)(originPath)) return originPath.split(external_node_path_namespaceObject.sep).join('/');
140
+ return originPath;
72
141
  };
73
- var addPlugins = (plugins, config) => {
74
- if (config.plugins) {
75
- config.plugins.push(...plugins);
76
- } else {
77
- config.plugins = plugins;
78
- }
142
+ const getPluginItemName = (item)=>{
143
+ if ('string' == typeof item) return formatPath(item);
144
+ if (Array.isArray(item) && 'string' == typeof item[0]) return formatPath(item[0]);
145
+ return null;
79
146
  };
80
- var addPresets = (presets, config) => {
81
- if (config.presets) {
82
- config.presets.push(...presets);
83
- } else {
84
- config.presets = presets;
85
- }
147
+ const addPlugins = (plugins, config)=>{
148
+ if (config.plugins) config.plugins.push(...plugins);
149
+ else config.plugins = plugins;
86
150
  };
87
- var removePlugins = (plugins, config) => {
88
- if (!config.plugins) {
89
- return;
90
- }
91
- const removeList = castArray(plugins);
92
- config.plugins = config.plugins.filter((item) => {
93
- const name = getPluginItemName(item);
94
- if (name) {
95
- return !removeList.find((removeItem) => name.includes(removeItem));
96
- }
97
- return true;
98
- });
151
+ const addPresets = (presets, config)=>{
152
+ if (config.presets) config.presets.push(...presets);
153
+ else config.presets = presets;
99
154
  };
100
- var removePresets = (presets, config) => {
101
- if (!config.presets) {
102
- return;
103
- }
104
- const removeList = castArray(presets);
105
- config.presets = config.presets.filter((item) => {
106
- const name = getPluginItemName(item);
107
- if (name) {
108
- return !removeList.find((removeItem) => name.includes(removeItem));
109
- }
110
- return true;
111
- });
155
+ const removePlugins = (plugins, config)=>{
156
+ if (!config.plugins) return;
157
+ const removeList = castArray(plugins);
158
+ config.plugins = config.plugins.filter((item)=>{
159
+ const name = getPluginItemName(item);
160
+ if (name) return !removeList.find((removeItem)=>name.includes(removeItem));
161
+ return true;
162
+ });
112
163
  };
113
- var modifyPresetOptions = (presetName, options, presets = []) => {
114
- presets.forEach((preset, index) => {
115
- if (Array.isArray(preset)) {
116
- if (typeof preset[0] === "string" && normalizeToPosixPath(preset[0]).includes(presetName)) {
117
- preset[1] = {
118
- ...preset[1] || {},
119
- ...options
120
- // `options` is specific to different presets
121
- };
122
- }
123
- } else if (typeof preset === "string" && normalizeToPosixPath(preset).includes(presetName)) {
124
- presets[index] = [preset, options];
125
- }
126
- });
164
+ const removePresets = (presets, config)=>{
165
+ if (!config.presets) return;
166
+ const removeList = castArray(presets);
167
+ config.presets = config.presets.filter((item)=>{
168
+ const name = getPluginItemName(item);
169
+ if (name) return !removeList.find((removeItem)=>name.includes(removeItem));
170
+ return true;
171
+ });
127
172
  };
128
- var getBabelUtils = (config) => {
129
- const noop = () => {
130
- };
131
- return {
132
- addPlugins: (plugins) => addPlugins(plugins, config),
133
- addPresets: (presets) => addPresets(presets, config),
134
- removePlugins: (plugins) => removePlugins(plugins, config),
135
- removePresets: (presets) => removePresets(presets, config),
136
- // `addIncludes` and `addExcludes` are noop functions by default,
137
- // It can be overridden by `extraBabelUtils`.
138
- addIncludes: noop,
139
- addExcludes: noop,
140
- // Compat `presetEnvOptions` and `presetReactOptions` in Modern.js
141
- modifyPresetEnvOptions: (options) => modifyPresetOptions("@babel/preset-env", options, config.presets || []),
142
- modifyPresetReactOptions: (options) => modifyPresetOptions("@babel/preset-react", options, config.presets || [])
143
- };
173
+ const modifyPresetOptions = (presetName, options, presets = [])=>{
174
+ presets.forEach((preset, index)=>{
175
+ // 1. ['@babel/preset-env', ...]
176
+ if (Array.isArray(preset)) {
177
+ if ('string' == typeof preset[0] && normalizeToPosixPath(preset[0]).includes(presetName)) preset[1] = {
178
+ ...preset[1] || {},
179
+ ...options
180
+ };
181
+ } else if ('string' == typeof preset && normalizeToPosixPath(preset).includes(presetName)) // 2. '@babel/preset-env'
182
+ presets[index] = [
183
+ preset,
184
+ options
185
+ ];
186
+ });
144
187
  };
145
- var applyUserBabelConfig = (defaultOptions, userBabelConfig, extraBabelUtils) => {
146
- if (userBabelConfig) {
147
- const babelUtils = {
148
- ...getBabelUtils(defaultOptions),
149
- ...extraBabelUtils
188
+ const getBabelUtils = (config)=>{
189
+ const noop = ()=>{};
190
+ return {
191
+ addPlugins: (plugins)=>addPlugins(plugins, config),
192
+ addPresets: (presets)=>addPresets(presets, config),
193
+ removePlugins: (plugins)=>removePlugins(plugins, config),
194
+ removePresets: (presets)=>removePresets(presets, config),
195
+ // `addIncludes` and `addExcludes` are noop functions by default,
196
+ // It can be overridden by `extraBabelUtils`.
197
+ addIncludes: noop,
198
+ addExcludes: noop,
199
+ // Compat `presetEnvOptions` and `presetReactOptions` in Modern.js
200
+ modifyPresetEnvOptions: (options)=>modifyPresetOptions('@babel/preset-env', options, config.presets || []),
201
+ modifyPresetReactOptions: (options)=>modifyPresetOptions('@babel/preset-react', options, config.presets || [])
150
202
  };
151
- return (0, import_reduce_configs.reduceConfigsWithContext)({
152
- initial: defaultOptions,
153
- config: userBabelConfig,
154
- ctx: babelUtils
155
- });
156
- }
157
- return defaultOptions;
158
203
  };
159
- var modifyBabelLoaderOptions = ({
160
- chain,
161
- CHAIN_ID,
162
- modifier
163
- }) => {
164
- const ruleIds = [CHAIN_ID.RULE.JS, CHAIN_ID.RULE.JS_DATA_URI, BABEL_JS_RULE];
165
- for (const ruleId of ruleIds) {
166
- if (chain.module.rules.has(ruleId)) {
167
- const rule = chain.module.rule(ruleId);
168
- if (rule.uses.has(CHAIN_ID.USE.BABEL)) {
169
- rule.use(CHAIN_ID.USE.BABEL).tap(modifier);
170
- }
204
+ const applyUserBabelConfig = (defaultOptions, userBabelConfig, extraBabelUtils)=>{
205
+ if (userBabelConfig) {
206
+ const babelUtils = {
207
+ ...getBabelUtils(defaultOptions),
208
+ ...extraBabelUtils
209
+ };
210
+ return (0, external_reduce_configs_namespaceObject.reduceConfigsWithContext)({
211
+ initial: defaultOptions,
212
+ config: userBabelConfig,
213
+ ctx: babelUtils
214
+ });
215
+ }
216
+ return defaultOptions;
217
+ };
218
+ const modifyBabelLoaderOptions = ({ chain, CHAIN_ID, modifier })=>{
219
+ const ruleIds = [
220
+ CHAIN_ID.RULE.JS,
221
+ CHAIN_ID.RULE.JS_DATA_URI,
222
+ BABEL_JS_RULE
223
+ ];
224
+ for (const ruleId of ruleIds)if (chain.module.rules.has(ruleId)) {
225
+ const rule = chain.module.rule(ruleId);
226
+ if (rule.uses.has(CHAIN_ID.USE.BABEL)) rule.use(CHAIN_ID.USE.BABEL).tap(modifier);
171
227
  }
172
- }
173
228
  };
174
-
175
- // src/plugin.ts
176
- var PLUGIN_BABEL_NAME = "rsbuild:babel";
177
- var SCRIPT_REGEX = /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/;
178
- var DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
179
- allowNamespaces: true,
180
- allExtensions: true,
181
- allowDeclareFields: true,
182
- // aligns Babel's behavior with TypeScript's default behavior.
183
- // https://babeljs.io/docs/en/babel-preset-typescript#optimizeconstenums
184
- optimizeConstEnums: true,
185
- isTSX: true
229
+ const plugin_dirname = external_node_path_default().dirname((0, external_node_url_namespaceObject.fileURLToPath)(/*#__PURE__*/ function() {
230
+ return 'undefined' == typeof document ? new (module.require('url'.replace('', ''))).URL('file:' + __filename).href : document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href;
231
+ }()));
232
+ const plugin_require = (0, external_node_module_namespaceObject.createRequire)(/*#__PURE__*/ function() {
233
+ return 'undefined' == typeof document ? new (module.require('url'.replace('', ''))).URL('file:' + __filename).href : document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href;
234
+ }());
235
+ const PLUGIN_BABEL_NAME = 'rsbuild:babel';
236
+ const SCRIPT_REGEX = /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/;
237
+ /**
238
+ * The `@babel/preset-typescript` default options.
239
+ */ const DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
240
+ allowNamespaces: true,
241
+ allExtensions: true,
242
+ allowDeclareFields: true,
243
+ // aligns Babel's behavior with TypeScript's default behavior.
244
+ // https://babeljs.io/docs/en/babel-preset-typescript#optimizeconstenums
245
+ optimizeConstEnums: true,
246
+ isTSX: true
186
247
  };
187
248
  function getCacheDirectory(context, cacheDirectory) {
188
- if (cacheDirectory) {
189
- return (0, import_node_path2.isAbsolute)(cacheDirectory) ? cacheDirectory : (0, import_node_path2.join)(context.rootPath, cacheDirectory);
190
- }
191
- return (0, import_node_path2.join)(context.cachePath);
249
+ if (cacheDirectory) return (0, external_node_path_namespaceObject.isAbsolute)(cacheDirectory) ? cacheDirectory : (0, external_node_path_namespaceObject.join)(context.rootPath, cacheDirectory);
250
+ return (0, external_node_path_namespaceObject.join)(context.cachePath);
192
251
  }
193
252
  async function getCacheIdentifier(options) {
194
- let identifier = `${process.env.NODE_ENV}${JSON.stringify(options)}`;
195
- const { version: coreVersion } = await import("@babel/core");
196
- const rawPkgJson = await import_node_fs.default.promises.readFile(
197
- (0, import_node_path2.join)(__dirname, "../compiled/babel-loader/package.json"),
198
- "utf-8"
199
- );
200
- const loaderVersion = JSON.parse(rawPkgJson).version ?? "";
201
- identifier += `@babel/core@${coreVersion}`;
202
- identifier += `babel-loader@${loaderVersion}`;
203
- return identifier;
253
+ let identifier = `${process.env.NODE_ENV}${JSON.stringify(options)}`;
254
+ const { version: coreVersion } = await Promise.resolve().then(__webpack_require__.t.bind(__webpack_require__, "@babel/core", 23));
255
+ const rawPkgJson = await external_node_fs_default().promises.readFile((0, external_node_path_namespaceObject.join)(plugin_dirname, '../compiled/babel-loader/package.json'), 'utf-8');
256
+ const loaderVersion = JSON.parse(rawPkgJson).version ?? '';
257
+ identifier += `@babel/core@${coreVersion}`;
258
+ identifier += `babel-loader@${loaderVersion}`;
259
+ return identifier;
204
260
  }
205
- var getDefaultBabelOptions = (config, context) => {
206
- const isLegacyDecorators = config.source.decorators.version === "legacy";
207
- const options = {
208
- babelrc: false,
209
- configFile: false,
210
- compact: config.mode === "production",
211
- plugins: [
212
- [
213
- require.resolve("@babel/plugin-proposal-decorators"),
214
- config.source.decorators
215
- ],
216
- // 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:
217
- // see https://babeljs.io/docs/babel-plugin-proposal-decorators#legacy
218
- ...isLegacyDecorators ? [require.resolve("@babel/plugin-transform-class-properties")] : []
219
- ],
220
- presets: [
221
- // TODO: only apply preset-typescript for ts file (isTSX & allExtensions false)
222
- [
223
- require.resolve("@babel/preset-typescript"),
224
- DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS
225
- ]
226
- ]
227
- };
228
- const { buildCache } = config.performance;
229
- if (buildCache && context.bundlerType === "rspack") {
230
- const cacheDirectory = getCacheDirectory(
231
- context,
232
- typeof buildCache === "boolean" ? void 0 : buildCache.cacheDirectory
233
- );
234
- options.cacheCompression = false;
235
- options.cacheDirectory = (0, import_node_path2.join)(cacheDirectory, "babel-loader");
236
- }
237
- return options;
238
- };
239
- var pluginBabel = (options = {}) => ({
240
- name: PLUGIN_BABEL_NAME,
241
- setup(api) {
242
- const getBabelOptions = async (environment) => {
243
- const { config } = environment;
244
- const baseOptions = getDefaultBabelOptions(config, api.context);
245
- const mergedOptions = applyUserBabelConfig(
246
- (0, import_deepmerge.default)({}, baseOptions),
247
- options.babelLoaderOptions
248
- );
249
- if (mergedOptions.cacheDirectory && !mergedOptions.cacheIdentifier) {
250
- mergedOptions.cacheIdentifier = await getCacheIdentifier(mergedOptions);
251
- }
252
- return mergedOptions;
261
+ const getDefaultBabelOptions = (config, context)=>{
262
+ const isLegacyDecorators = 'legacy' === config.source.decorators.version;
263
+ const options = {
264
+ babelrc: false,
265
+ configFile: false,
266
+ compact: 'production' === config.mode,
267
+ plugins: [
268
+ [
269
+ plugin_require.resolve('@babel/plugin-proposal-decorators'),
270
+ config.source.decorators
271
+ ],
272
+ // 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:
273
+ // see https://babeljs.io/docs/babel-plugin-proposal-decorators#legacy
274
+ ...isLegacyDecorators ? [
275
+ plugin_require.resolve('@babel/plugin-transform-class-properties')
276
+ ] : []
277
+ ],
278
+ presets: [
279
+ // TODO: only apply preset-typescript for ts file (isTSX & allExtensions false)
280
+ [
281
+ plugin_require.resolve('@babel/preset-typescript'),
282
+ DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS
283
+ ]
284
+ ]
253
285
  };
254
- api.modifyBundlerChain({
255
- order: "pre",
256
- handler: async (chain, { CHAIN_ID, environment }) => {
257
- const babelOptions = await getBabelOptions(environment);
258
- const babelLoader = import_node_path2.default.resolve(
259
- __dirname,
260
- "../compiled/babel-loader/index.js"
261
- );
262
- const { include, exclude } = options;
263
- if (include || exclude) {
264
- const rule = chain.module.rule(BABEL_JS_RULE).after(CHAIN_ID.RULE.JS);
265
- if (include) {
266
- for (const condition of castArray(include)) {
267
- rule.include.add(condition);
268
- }
269
- }
270
- if (exclude) {
271
- for (const condition of castArray(exclude)) {
272
- rule.exclude.add(condition);
273
- }
274
- }
275
- rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).loader(babelLoader).options(babelOptions);
276
- } else {
277
- const rule = chain.module.rule(CHAIN_ID.RULE.JS);
278
- rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(babelLoader).options(babelOptions);
286
+ const { buildCache } = config.performance;
287
+ // Rspack does not yet support persistent cache
288
+ // so we use babel-loader's cache to improve rebuild performance
289
+ if (buildCache && 'rspack' === context.bundlerType) {
290
+ const cacheDirectory = getCacheDirectory(context, 'boolean' == typeof buildCache ? void 0 : buildCache.cacheDirectory);
291
+ // turn off compression to reduce overhead
292
+ options.cacheCompression = false;
293
+ options.cacheDirectory = (0, external_node_path_namespaceObject.join)(cacheDirectory, 'babel-loader');
294
+ }
295
+ return options;
296
+ };
297
+ const pluginBabel = (options = {})=>({
298
+ name: PLUGIN_BABEL_NAME,
299
+ setup (api) {
300
+ const getBabelOptions = async (environment)=>{
301
+ const { config } = environment;
302
+ const baseOptions = getDefaultBabelOptions(config, api.context);
303
+ const mergedOptions = applyUserBabelConfig(external_deepmerge_default()({}, baseOptions), options.babelLoaderOptions);
304
+ // calculate cacheIdentifier with the merged options
305
+ if (mergedOptions.cacheDirectory && !mergedOptions.cacheIdentifier) mergedOptions.cacheIdentifier = await getCacheIdentifier(mergedOptions);
306
+ return mergedOptions;
307
+ };
308
+ api.modifyBundlerChain({
309
+ order: 'pre',
310
+ handler: async (chain, { CHAIN_ID, environment })=>{
311
+ const babelOptions = await getBabelOptions(environment);
312
+ const babelLoader = external_node_path_default().resolve(plugin_dirname, '../compiled/babel-loader/index.js');
313
+ const { include, exclude } = options;
314
+ if (include || exclude) {
315
+ const rule = chain.module.rule(BABEL_JS_RULE) // run babel loader before the builtin SWC loader
316
+ // https://stackoverflow.com/questions/32234329/what-is-the-loader-order-for-webpack
317
+ .after(CHAIN_ID.RULE.JS);
318
+ if (include) for (const condition of castArray(include))rule.include.add(condition);
319
+ if (exclude) for (const condition of castArray(exclude))rule.exclude.add(condition);
320
+ rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).loader(babelLoader).options(babelOptions);
321
+ } else {
322
+ // already set source.include / exclude in plugin-swc
323
+ const rule = chain.module.rule(CHAIN_ID.RULE.JS);
324
+ rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(babelLoader).options(babelOptions);
325
+ }
326
+ }
327
+ });
279
328
  }
280
- }
281
329
  });
282
- }
283
- });
284
- // Annotate the CommonJS export names for ESM import in node:
285
- 0 && (module.exports = {
286
- PLUGIN_BABEL_NAME,
287
- getBabelUtils,
288
- getDefaultBabelOptions,
289
- modifyBabelLoaderOptions,
290
- pluginBabel
330
+ var __webpack_export_target__ = exports;
331
+ for(var i in __webpack_exports__)__webpack_export_target__[i] = __webpack_exports__[i];
332
+ if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
333
+ value: true
291
334
  });
@@ -1,3 +1,3 @@
1
- export { pluginBabel, getDefaultBabelOptions, PLUGIN_BABEL_NAME, } from './plugin';
2
- export { getBabelUtils, modifyBabelLoaderOptions } from './helper';
3
- export type { PresetEnvOptions, PresetEnvTargets, PresetEnvBuiltIns, BabelConfigUtils, BabelTransformOptions, PluginBabelOptions, } from './types';
1
+ export { pluginBabel, getDefaultBabelOptions, PLUGIN_BABEL_NAME, } from './plugin.js';
2
+ export { getBabelUtils, modifyBabelLoaderOptions } from './helper.js';
3
+ export type { PresetEnvOptions, PresetEnvTargets, PresetEnvBuiltIns, BabelConfigUtils, BabelTransformOptions, PluginBabelOptions, } from './types.js';