@rsbuild/plugin-babel 1.0.1-rc.5 → 1.0.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.cjs CHANGED
@@ -1,291 +1,336 @@
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-harmony 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_deepmerge_namespaceObject = require("deepmerge");
124
+ var external_deepmerge_default = /*#__PURE__*/ __webpack_require__.n(external_deepmerge_namespaceObject);
125
+ const external_reduce_configs_namespaceObject = require("reduce-configs");
126
+ const external_upath_namespaceObject = require("upath");
127
+ var external_upath_default = /*#__PURE__*/ __webpack_require__.n(external_upath_namespaceObject);
128
+ const BABEL_JS_RULE = 'babel-js';
129
+ const castArray = (arr)=>{
130
+ if (void 0 === arr) return [];
131
+ return Array.isArray(arr) ? arr : [
132
+ arr
133
+ ];
63
134
  };
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;
135
+ const normalizeToPosixPath = (p)=>external_upath_default().normalizeSafe((0, external_node_path_namespaceObject.normalize)(p || '')).replace(/^([a-zA-Z]+):/, (_, m)=>`/${m.toLowerCase()}`);
136
+ // compatible with windows path
137
+ const formatPath = (originPath)=>{
138
+ if ((0, external_node_path_namespaceObject.isAbsolute)(originPath)) return originPath.split(external_node_path_namespaceObject.sep).join('/');
139
+ return originPath;
72
140
  };
73
- var addPlugins = (plugins, config) => {
74
- if (config.plugins) {
75
- config.plugins.push(...plugins);
76
- } else {
77
- config.plugins = plugins;
78
- }
141
+ const getPluginItemName = (item)=>{
142
+ if ('string' == typeof item) return formatPath(item);
143
+ if (Array.isArray(item) && 'string' == typeof item[0]) return formatPath(item[0]);
144
+ return null;
79
145
  };
80
- var addPresets = (presets, config) => {
81
- if (config.presets) {
82
- config.presets.push(...presets);
83
- } else {
84
- config.presets = presets;
85
- }
146
+ const addPlugins = (plugins, config)=>{
147
+ if (config.plugins) config.plugins.push(...plugins);
148
+ else config.plugins = plugins;
86
149
  };
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
- });
150
+ const addPresets = (presets, config)=>{
151
+ if (config.presets) config.presets.push(...presets);
152
+ else config.presets = presets;
99
153
  };
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
- });
154
+ const removePlugins = (plugins, config)=>{
155
+ if (!config.plugins) return;
156
+ const removeList = castArray(plugins);
157
+ config.plugins = config.plugins.filter((item)=>{
158
+ const name = getPluginItemName(item);
159
+ if (name) return !removeList.find((removeItem)=>name.includes(removeItem));
160
+ return true;
161
+ });
112
162
  };
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
- });
163
+ const removePresets = (presets, config)=>{
164
+ if (!config.presets) return;
165
+ const removeList = castArray(presets);
166
+ config.presets = config.presets.filter((item)=>{
167
+ const name = getPluginItemName(item);
168
+ if (name) return !removeList.find((removeItem)=>name.includes(removeItem));
169
+ return true;
170
+ });
127
171
  };
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
- };
172
+ const modifyPresetOptions = function(presetName, options) {
173
+ let presets = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : [];
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 = (param)=>{
219
+ let { chain, CHAIN_ID, modifier } = param;
220
+ const ruleIds = [
221
+ CHAIN_ID.RULE.JS,
222
+ CHAIN_ID.RULE.JS_DATA_URI,
223
+ BABEL_JS_RULE
224
+ ];
225
+ for (const ruleId of ruleIds)if (chain.module.rules.has(ruleId)) {
226
+ const rule = chain.module.rule(ruleId);
227
+ if (rule.uses.has(CHAIN_ID.USE.BABEL)) rule.use(CHAIN_ID.USE.BABEL).tap(modifier);
171
228
  }
172
- }
173
229
  };
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
230
+ const plugin_require = (0, external_node_module_namespaceObject.createRequire)(/*#__PURE__*/ function() {
231
+ 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;
232
+ }());
233
+ const PLUGIN_BABEL_NAME = 'rsbuild:babel';
234
+ const SCRIPT_REGEX = /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/;
235
+ /**
236
+ * The `@babel/preset-typescript` default options.
237
+ */ const DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
238
+ allowNamespaces: true,
239
+ allExtensions: true,
240
+ allowDeclareFields: true,
241
+ // aligns Babel's behavior with TypeScript's default behavior.
242
+ // https://babeljs.io/docs/en/babel-preset-typescript#optimizeconstenums
243
+ optimizeConstEnums: true,
244
+ isTSX: true
186
245
  };
187
246
  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);
247
+ if (cacheDirectory) return (0, external_node_path_namespaceObject.isAbsolute)(cacheDirectory) ? cacheDirectory : (0, external_node_path_namespaceObject.join)(context.rootPath, cacheDirectory);
248
+ return (0, external_node_path_namespaceObject.join)(context.cachePath);
192
249
  }
193
250
  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;
251
+ let identifier = `${process.env.NODE_ENV}${JSON.stringify(options)}`;
252
+ const { version: coreVersion } = await Promise.resolve().then(__webpack_require__.t.bind(__webpack_require__, "@babel/core", 23));
253
+ const rawPkgJson = await external_node_fs_default().promises.readFile((0, external_node_path_namespaceObject.join)(__dirname, '../compiled/babel-loader/package.json'), 'utf-8');
254
+ const loaderVersion = JSON.parse(rawPkgJson).version ?? '';
255
+ identifier += `@babel/core@${coreVersion}`;
256
+ identifier += `babel-loader@${loaderVersion}`;
257
+ return identifier;
204
258
  }
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;
259
+ const getDefaultBabelOptions = (config, context)=>{
260
+ const isLegacyDecorators = 'legacy' === config.source.decorators.version;
261
+ const options = {
262
+ babelrc: false,
263
+ configFile: false,
264
+ compact: 'production' === config.mode,
265
+ plugins: [
266
+ [
267
+ plugin_require.resolve('@babel/plugin-proposal-decorators'),
268
+ config.source.decorators
269
+ ],
270
+ // 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:
271
+ // see https://babeljs.io/docs/babel-plugin-proposal-decorators#legacy
272
+ ...isLegacyDecorators ? [
273
+ plugin_require.resolve('@babel/plugin-transform-class-properties')
274
+ ] : []
275
+ ],
276
+ presets: [
277
+ // TODO: only apply preset-typescript for ts file (isTSX & allExtensions false)
278
+ [
279
+ plugin_require.resolve('@babel/preset-typescript'),
280
+ DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS
281
+ ]
282
+ ]
253
283
  };
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);
284
+ const { buildCache } = config.performance;
285
+ // Rspack does not yet support persistent cache
286
+ // so we use babel-loader's cache to improve rebuild performance
287
+ if (buildCache && 'rspack' === context.bundlerType) {
288
+ const cacheDirectory = getCacheDirectory(context, 'boolean' == typeof buildCache ? void 0 : buildCache.cacheDirectory);
289
+ // turn off compression to reduce overhead
290
+ options.cacheCompression = false;
291
+ options.cacheDirectory = (0, external_node_path_namespaceObject.join)(cacheDirectory, 'babel-loader');
292
+ }
293
+ return options;
294
+ };
295
+ const pluginBabel = function() {
296
+ let options = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
297
+ return {
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, param)=>{
311
+ let { CHAIN_ID, environment } = param;
312
+ const babelOptions = await getBabelOptions(environment);
313
+ const babelLoader = external_node_path_default().resolve(__dirname, '../compiled/babel-loader/index.js');
314
+ const { include, exclude } = options;
315
+ if (include || exclude) {
316
+ const rule = chain.module.rule(BABEL_JS_RULE) // run babel loader before the builtin SWC loader
317
+ // https://stackoverflow.com/questions/32234329/what-is-the-loader-order-for-webpack
318
+ .after(CHAIN_ID.RULE.JS);
319
+ if (include) for (const condition of castArray(include))rule.include.add(condition);
320
+ if (exclude) for (const condition of castArray(exclude))rule.exclude.add(condition);
321
+ rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).loader(babelLoader).options(babelOptions);
322
+ } else {
323
+ // already set source.include / exclude in plugin-swc
324
+ const rule = chain.module.rule(CHAIN_ID.RULE.JS);
325
+ rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(babelLoader).options(babelOptions);
326
+ }
327
+ }
328
+ });
279
329
  }
280
- }
281
- });
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
+ };
331
+ };
332
+ var __webpack_export_target__ = exports;
333
+ for(var i in __webpack_exports__)__webpack_export_target__[i] = __webpack_exports__[i];
334
+ if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
335
+ value: true
291
336
  });
@@ -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';