@rsbuild/plugin-babel 1.0.3 → 1.0.5

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.js CHANGED
@@ -1,203 +1,129 @@
1
- import * as __WEBPACK_EXTERNAL_MODULE_node_fs__ from "node:fs";
2
- import * as __WEBPACK_EXTERNAL_MODULE_node_module__ from "node:module";
3
- import * as __WEBPACK_EXTERNAL_MODULE_node_path__ from "node:path";
4
- import * as __WEBPACK_EXTERNAL_MODULE_node_url__ from "node:url";
1
+ import * as __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__ from "node:fs";
2
+ import * as __WEBPACK_EXTERNAL_MODULE_node_module_ab9f2194__ from "node:module";
3
+ import * as __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__ from "node:path";
4
+ import * as __WEBPACK_EXTERNAL_MODULE_node_url_e96de089__ from "node:url";
5
5
  import * as __WEBPACK_EXTERNAL_MODULE_deepmerge__ from "deepmerge";
6
- import * as __WEBPACK_EXTERNAL_MODULE_reduce_configs__ from "reduce-configs";
6
+ import * as __WEBPACK_EXTERNAL_MODULE_reduce_configs_02786df6__ from "reduce-configs";
7
7
  import * as __WEBPACK_EXTERNAL_MODULE_upath__ from "upath";
8
- const BABEL_JS_RULE = 'babel-js';
9
- const castArray = (arr)=>{
10
- if (void 0 === arr) return [];
11
- return Array.isArray(arr) ? arr : [
8
+ let BABEL_JS_RULE = 'babel-js', castArray = (arr)=>void 0 === arr ? [] : Array.isArray(arr) ? arr : [
12
9
  arr
13
- ];
14
- };
15
- const normalizeToPosixPath = (p)=>__WEBPACK_EXTERNAL_MODULE_upath__["default"].normalizeSafe((0, __WEBPACK_EXTERNAL_MODULE_node_path__.normalize)(p || '')).replace(/^([a-zA-Z]+):/, (_, m)=>`/${m.toLowerCase()}`);
16
- // compatible with Windows path
17
- const formatPath = (originPath)=>{
18
- if ((0, __WEBPACK_EXTERNAL_MODULE_node_path__.isAbsolute)(originPath)) return originPath.split(__WEBPACK_EXTERNAL_MODULE_node_path__.sep).join('/');
19
- return originPath;
20
- };
21
- const getPluginItemName = (item)=>{
22
- if ('string' == typeof item) return formatPath(item);
23
- if (Array.isArray(item) && 'string' == typeof item[0]) return formatPath(item[0]);
24
- return null;
25
- };
26
- const addPlugins = (plugins, config)=>{
27
- if (config.plugins) config.plugins.push(...plugins);
28
- else config.plugins = plugins;
29
- };
30
- const addPresets = (presets, config)=>{
31
- if (config.presets) config.presets.push(...presets);
32
- else config.presets = presets;
33
- };
34
- const removePlugins = (plugins, config)=>{
10
+ ], normalizeToPosixPath = (p)=>__WEBPACK_EXTERNAL_MODULE_upath__.default.normalizeSafe((0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.normalize)(p || '')).replace(/^([a-zA-Z]+):/, (_, m)=>`/${m.toLowerCase()}`), formatPath = (originPath)=>(0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.isAbsolute)(originPath) ? originPath.split(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.sep).join('/') : originPath, getPluginItemName = (item)=>'string' == typeof item ? formatPath(item) : Array.isArray(item) && 'string' == typeof item[0] ? formatPath(item[0]) : null, addPlugins = (plugins, config)=>{
11
+ config.plugins ? config.plugins.push(...plugins) : config.plugins = plugins;
12
+ }, addPresets = (presets, config)=>{
13
+ config.presets ? config.presets.push(...presets) : config.presets = presets;
14
+ }, removePlugins = (plugins, config)=>{
35
15
  if (!config.plugins) return;
36
- const removeList = castArray(plugins);
16
+ let removeList = castArray(plugins);
37
17
  config.plugins = config.plugins.filter((item)=>{
38
- const name = getPluginItemName(item);
39
- if (name) return !removeList.find((removeItem)=>name.includes(removeItem));
40
- return true;
18
+ let name = getPluginItemName(item);
19
+ return !name || !removeList.find((removeItem)=>name.includes(removeItem));
41
20
  });
42
- };
43
- const removePresets = (presets, config)=>{
21
+ }, removePresets = (presets, config)=>{
44
22
  if (!config.presets) return;
45
- const removeList = castArray(presets);
23
+ let removeList = castArray(presets);
46
24
  config.presets = config.presets.filter((item)=>{
47
- const name = getPluginItemName(item);
48
- if (name) return !removeList.find((removeItem)=>name.includes(removeItem));
49
- return true;
25
+ let name = getPluginItemName(item);
26
+ return !name || !removeList.find((removeItem)=>name.includes(removeItem));
50
27
  });
51
- };
52
- const modifyPresetOptions = (presetName, options, presets = [])=>{
28
+ }, modifyPresetOptions = (presetName, options, presets = [])=>{
53
29
  presets.forEach((preset, index)=>{
54
- // 1. ['@babel/preset-env', ...]
55
- if (Array.isArray(preset)) {
56
- if ('string' == typeof preset[0] && normalizeToPosixPath(preset[0]).includes(presetName)) preset[1] = {
57
- ...preset[1] || {},
58
- ...options
59
- };
60
- } else if ('string' == typeof preset && normalizeToPosixPath(preset).includes(presetName)) // 2. '@babel/preset-env'
61
- presets[index] = [
30
+ Array.isArray(preset) ? 'string' == typeof preset[0] && normalizeToPosixPath(preset[0]).includes(presetName) && (preset[1] = {
31
+ ...preset[1] || {},
32
+ ...options
33
+ }) : 'string' == typeof preset && normalizeToPosixPath(preset).includes(presetName) && (presets[index] = [
62
34
  preset,
63
35
  options
64
- ];
36
+ ]);
65
37
  });
66
- };
67
- const getBabelUtils = (config)=>{
68
- const noop = ()=>{};
38
+ }, getBabelUtils = (config)=>{
39
+ let noop = ()=>{};
69
40
  return {
70
41
  addPlugins: (plugins)=>addPlugins(plugins, config),
71
42
  addPresets: (presets)=>addPresets(presets, config),
72
43
  removePlugins: (plugins)=>removePlugins(plugins, config),
73
44
  removePresets: (presets)=>removePresets(presets, config),
74
- // `addIncludes` and `addExcludes` are noop functions by default,
75
- // It can be overridden by `extraBabelUtils`.
76
45
  addIncludes: noop,
77
46
  addExcludes: noop,
78
- // Compat `presetEnvOptions` and `presetReactOptions` in Modern.js
79
47
  modifyPresetEnvOptions: (options)=>modifyPresetOptions('@babel/preset-env', options, config.presets || []),
80
48
  modifyPresetReactOptions: (options)=>modifyPresetOptions('@babel/preset-react', options, config.presets || [])
81
49
  };
82
- };
83
- const applyUserBabelConfig = (defaultOptions, userBabelConfig, extraBabelUtils)=>{
50
+ }, applyUserBabelConfig = (defaultOptions, userBabelConfig, extraBabelUtils)=>{
84
51
  if (userBabelConfig) {
85
- const babelUtils = {
52
+ let babelUtils = {
86
53
  ...getBabelUtils(defaultOptions),
87
54
  ...extraBabelUtils
88
55
  };
89
- return (0, __WEBPACK_EXTERNAL_MODULE_reduce_configs__.reduceConfigsWithContext)({
56
+ return (0, __WEBPACK_EXTERNAL_MODULE_reduce_configs_02786df6__.reduceConfigsWithContext)({
90
57
  initial: defaultOptions,
91
58
  config: userBabelConfig,
92
59
  ctx: babelUtils
93
60
  });
94
61
  }
95
62
  return defaultOptions;
96
- };
97
- const modifyBabelLoaderOptions = ({ chain, CHAIN_ID, modifier })=>{
98
- const ruleIds = [
63
+ }, modifyBabelLoaderOptions = ({ chain, CHAIN_ID, modifier })=>{
64
+ for (let ruleId of [
99
65
  CHAIN_ID.RULE.JS,
100
66
  CHAIN_ID.RULE.JS_DATA_URI,
101
67
  BABEL_JS_RULE
102
- ];
103
- for (const ruleId of ruleIds)if (chain.module.rules.has(ruleId)) {
104
- const rule = chain.module.rule(ruleId);
105
- if (rule.uses.has(CHAIN_ID.USE.BABEL)) rule.use(CHAIN_ID.USE.BABEL).tap(modifier);
68
+ ])if (chain.module.rules.has(ruleId)) {
69
+ let rule = chain.module.rule(ruleId);
70
+ rule.uses.has(CHAIN_ID.USE.BABEL) && rule.use(CHAIN_ID.USE.BABEL).tap(modifier);
106
71
  }
72
+ }, plugin_dirname = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.default.dirname((0, __WEBPACK_EXTERNAL_MODULE_node_url_e96de089__.fileURLToPath)(import.meta.url)), plugin_require = (0, __WEBPACK_EXTERNAL_MODULE_node_module_ab9f2194__.createRequire)(import.meta.url), PLUGIN_BABEL_NAME = 'rsbuild:babel', SCRIPT_REGEX = /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/, DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
73
+ allowNamespaces: !0,
74
+ allExtensions: !0,
75
+ allowDeclareFields: !0,
76
+ optimizeConstEnums: !0,
77
+ isTSX: !0
107
78
  };
108
- const plugin_dirname = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname((0, __WEBPACK_EXTERNAL_MODULE_node_url__.fileURLToPath)(import.meta.url));
109
- const plugin_require = (0, __WEBPACK_EXTERNAL_MODULE_node_module__.createRequire)(import.meta.url);
110
- const PLUGIN_BABEL_NAME = 'rsbuild:babel';
111
- const SCRIPT_REGEX = /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/;
112
- /**
113
- * The `@babel/preset-typescript` default options.
114
- */ const DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
115
- allowNamespaces: true,
116
- allExtensions: true,
117
- allowDeclareFields: true,
118
- // aligns Babel's behavior with TypeScript's default behavior.
119
- // https://babeljs.io/docs/en/babel-preset-typescript#optimizeconstenums
120
- optimizeConstEnums: true,
121
- isTSX: true
122
- };
123
- function getCacheDirectory(context, cacheDirectory) {
124
- if (cacheDirectory) return (0, __WEBPACK_EXTERNAL_MODULE_node_path__.isAbsolute)(cacheDirectory) ? cacheDirectory : (0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(context.rootPath, cacheDirectory);
125
- return (0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(context.cachePath);
126
- }
127
79
  async function getCacheIdentifier(options) {
128
- let identifier = `${process.env.NODE_ENV}${JSON.stringify(options)}`;
129
- const { version: coreVersion } = await import("@babel/core");
130
- const rawPkgJson = await __WEBPACK_EXTERNAL_MODULE_node_fs__["default"].promises.readFile((0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(plugin_dirname, '../compiled/babel-loader/package.json'), 'utf-8');
131
- const loaderVersion = JSON.parse(rawPkgJson).version ?? '';
132
- identifier += `@babel/core@${coreVersion}`;
133
- identifier += `babel-loader@${loaderVersion}`;
134
- return identifier;
80
+ let identifier = `${process.env.NODE_ENV}${JSON.stringify(options)}`, { version: coreVersion } = await import("@babel/core"), loaderVersion = JSON.parse(await __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__.default.promises.readFile((0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(plugin_dirname, '../compiled/babel-loader/package.json'), 'utf-8')).version ?? '';
81
+ return identifier + `@babel/core@${coreVersion}babel-loader@${loaderVersion}`;
135
82
  }
136
- const getDefaultBabelOptions = (config, context)=>{
137
- const isLegacyDecorators = 'legacy' === config.source.decorators.version;
138
- const options = {
139
- babelrc: false,
140
- configFile: false,
83
+ let getDefaultBabelOptions = (config, context)=>{
84
+ let isLegacyDecorators = 'legacy' === config.source.decorators.version, options = {
85
+ babelrc: !1,
86
+ configFile: !1,
141
87
  compact: 'production' === config.mode,
142
88
  plugins: [
143
89
  [
144
90
  plugin_require.resolve('@babel/plugin-proposal-decorators'),
145
91
  config.source.decorators
146
92
  ],
147
- // 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:
148
- // see https://babeljs.io/docs/babel-plugin-proposal-decorators#legacy
149
93
  ...isLegacyDecorators ? [
150
94
  plugin_require.resolve('@babel/plugin-transform-class-properties')
151
95
  ] : []
152
96
  ],
153
97
  presets: [
154
- // TODO: only apply preset-typescript for ts file (isTSX & allExtensions false)
155
98
  [
156
- plugin_require.resolve('@babel/preset-typescript'),
99
+ plugin_require.resolve("@babel/preset-typescript"),
157
100
  DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS
158
101
  ]
159
102
  ]
160
- };
161
- const { buildCache } = config.performance;
162
- // Rspack does not yet support persistent cache
163
- // so we use babel-loader's cache to improve rebuild performance
103
+ }, { buildCache = !0 } = config.performance;
164
104
  if (buildCache && 'rspack' === context.bundlerType) {
165
- const cacheDirectory = getCacheDirectory(context, 'boolean' == typeof buildCache ? void 0 : buildCache.cacheDirectory);
166
- // turn off compression to reduce overhead
167
- options.cacheCompression = false;
168
- options.cacheDirectory = (0, __WEBPACK_EXTERNAL_MODULE_node_path__.join)(cacheDirectory, 'babel-loader');
105
+ var cacheDirectory;
106
+ let cacheDirectory1 = (cacheDirectory = 'boolean' == typeof buildCache ? void 0 : buildCache.cacheDirectory) ? (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.isAbsolute)(cacheDirectory) ? cacheDirectory : (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(context.rootPath, cacheDirectory) : (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(context.cachePath);
107
+ options.cacheCompression = !1, options.cacheDirectory = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(cacheDirectory1, 'babel-loader');
169
108
  }
170
109
  return options;
171
- };
172
- const pluginBabel = (options = {})=>({
110
+ }, pluginBabel = (options = {})=>({
173
111
  name: PLUGIN_BABEL_NAME,
174
112
  setup (api) {
175
- const getBabelOptions = async (environment)=>{
176
- const { config } = environment;
177
- const baseOptions = getDefaultBabelOptions(config, api.context);
178
- const mergedOptions = applyUserBabelConfig((0, __WEBPACK_EXTERNAL_MODULE_deepmerge__["default"])({}, baseOptions), options.babelLoaderOptions);
179
- // calculate cacheIdentifier with the merged options
180
- if (mergedOptions.cacheDirectory && !mergedOptions.cacheIdentifier) mergedOptions.cacheIdentifier = await getCacheIdentifier(mergedOptions);
181
- return mergedOptions;
113
+ let getBabelOptions = async (environment)=>{
114
+ let { config } = environment, baseOptions = getDefaultBabelOptions(config, api.context), mergedOptions = applyUserBabelConfig((0, __WEBPACK_EXTERNAL_MODULE_deepmerge__.default)({}, baseOptions), options.babelLoaderOptions);
115
+ return mergedOptions.cacheDirectory && !mergedOptions.cacheIdentifier && (mergedOptions.cacheIdentifier = await getCacheIdentifier(mergedOptions)), mergedOptions;
182
116
  };
183
117
  api.modifyBundlerChain({
184
118
  order: 'pre',
185
119
  handler: async (chain, { CHAIN_ID, environment })=>{
186
- const babelOptions = await getBabelOptions(environment);
187
- const babelLoader = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].resolve(plugin_dirname, '../compiled/babel-loader/index.js');
188
- const { include, exclude } = options;
120
+ let babelOptions = await getBabelOptions(environment), babelLoader = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.default.resolve(plugin_dirname, '../compiled/babel-loader/index.js'), { include, exclude } = options;
189
121
  if (include || exclude) {
190
- const rule = chain.module.rule(BABEL_JS_RULE) // run babel loader before the builtin SWC loader
191
- // https://stackoverflow.com/questions/32234329/what-is-the-loader-order-for-webpack
192
- .after(CHAIN_ID.RULE.JS);
193
- if (include) for (const condition of castArray(include))rule.include.add(condition);
194
- if (exclude) for (const condition of castArray(exclude))rule.exclude.add(condition);
122
+ let rule = chain.module.rule(BABEL_JS_RULE).after(CHAIN_ID.RULE.JS);
123
+ if (include) for (let condition of castArray(include))rule.include.add(condition);
124
+ if (exclude) for (let condition of castArray(exclude))rule.exclude.add(condition);
195
125
  rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).loader(babelLoader).options(babelOptions);
196
- } else {
197
- // already set source.include / exclude in plugin-swc
198
- const rule = chain.module.rule(CHAIN_ID.RULE.JS);
199
- rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(babelLoader).options(babelOptions);
200
- }
126
+ } else chain.module.rule(CHAIN_ID.RULE.JS).test(SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(babelLoader).options(babelOptions);
201
127
  }
202
128
  });
203
129
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-babel",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Babel plugin for Rsbuild",
5
5
  "repository": {
6
6
  "type": "git",
@@ -23,21 +23,22 @@
23
23
  "compiled"
24
24
  ],
25
25
  "dependencies": {
26
- "@babel/core": "^7.26.0",
26
+ "@babel/core": "^7.26.10",
27
27
  "@babel/plugin-proposal-decorators": "^7.25.9",
28
28
  "@babel/plugin-transform-class-properties": "^7.25.9",
29
- "@babel/preset-typescript": "^7.26.0",
29
+ "@babel/preset-typescript": "^7.27.0",
30
30
  "@types/babel__core": "^7.20.5",
31
31
  "deepmerge": "^4.3.1",
32
- "reduce-configs": "^1.0.0",
32
+ "reduce-configs": "^1.1.0",
33
33
  "upath": "2.0.1"
34
34
  },
35
35
  "devDependencies": {
36
- "@types/node": "18.x",
37
- "babel-loader": "9.2.1",
38
- "prebundle": "1.2.2",
39
- "typescript": "^5.6.3",
40
- "@rsbuild/core": "1.0.19",
36
+ "@rslib/core": "0.6.1",
37
+ "@types/node": "^22.14.0",
38
+ "babel-loader": "10.0.0",
39
+ "prebundle": "1.3.3",
40
+ "typescript": "^5.8.3",
41
+ "@rsbuild/core": "1.3.5",
41
42
  "@scripts/test-helper": "1.0.1"
42
43
  },
43
44
  "peerDependencies": {