@rsbuild/plugin-babel 0.3.10 → 0.4.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/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { RsbuildPlugin } from '@rsbuild/core';
2
- import { ChainedConfigWithUtils, NormalizedConfig, BundlerChain, ChainIdentifier } from '@rsbuild/shared';
2
+ import { ChainedConfigWithUtils, Decorators, NormalizedConfig, BundlerChain, ChainIdentifier } from '@rsbuild/shared';
3
3
  import { PluginItem, TransformOptions } from '@babel/core';
4
4
  export { TransformOptions as BabelTransformOptions } from '@babel/core';
5
5
 
@@ -76,11 +76,11 @@ type PluginBabelOptions = {
76
76
  babelLoaderOptions?: ChainedConfigWithUtils<TransformOptions, BabelConfigUtils>;
77
77
  };
78
78
 
79
- declare const getDefaultBabelOptions: () => {
79
+ declare const getDefaultBabelOptions: (decorators: Decorators) => {
80
80
  babelrc: boolean;
81
81
  configFile: boolean;
82
82
  compact: boolean;
83
- plugins: never[];
83
+ plugins: (string | Decorators)[][];
84
84
  presets: (string | {
85
85
  allowNamespaces: boolean;
86
86
  allExtensions: boolean;
@@ -89,7 +89,6 @@ declare const getDefaultBabelOptions: () => {
89
89
  isTSX: boolean;
90
90
  })[][];
91
91
  };
92
-
93
92
  declare const pluginBabel: (options?: PluginBabelOptions) => RsbuildPlugin;
94
93
 
95
94
  declare const getBabelUtils: (config: TransformOptions) => BabelConfigUtils;
package/dist/index.js CHANGED
@@ -40,7 +40,6 @@ module.exports = __toCommonJS(src_exports);
40
40
 
41
41
  // src/plugin.ts
42
42
  var import_node_path2 = __toESM(require("path"));
43
- var import_core = require("@rsbuild/core");
44
43
  var import_shared2 = require("@rsbuild/shared");
45
44
 
46
45
  // src/helper.ts
@@ -174,6 +173,7 @@ var modifyBabelLoaderOptions = ({
174
173
  };
175
174
 
176
175
  // src/plugin.ts
176
+ var PLUGIN_BABEL_NAME = "rsbuild:babel";
177
177
  var DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
178
178
  allowNamespaces: true,
179
179
  allExtensions: true,
@@ -183,12 +183,14 @@ var DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
183
183
  optimizeConstEnums: true,
184
184
  isTSX: true
185
185
  };
186
- var getDefaultBabelOptions = () => {
186
+ var getDefaultBabelOptions = (decorators) => {
187
187
  return {
188
188
  babelrc: false,
189
189
  configFile: false,
190
190
  compact: (0, import_shared2.isProd)(),
191
- plugins: [],
191
+ plugins: [
192
+ [require.resolve("@babel/plugin-proposal-decorators"), decorators]
193
+ ],
192
194
  presets: [
193
195
  // TODO: only apply preset-typescript for ts file (isTSX & allExtensions false)
194
196
  [
@@ -199,42 +201,46 @@ var getDefaultBabelOptions = () => {
199
201
  };
200
202
  };
201
203
  var pluginBabel = (options = {}) => ({
202
- name: import_core.PLUGIN_BABEL_NAME,
204
+ name: PLUGIN_BABEL_NAME,
203
205
  setup(api) {
204
- api.modifyBundlerChain(async (chain, { CHAIN_ID }) => {
205
- const getBabelOptions = () => {
206
- const baseConfig = getDefaultBabelOptions();
207
- const userBabelConfig = applyUserBabelConfig(
208
- (0, import_shared2.cloneDeep)(baseConfig),
209
- options.babelLoaderOptions
206
+ api.modifyBundlerChain({
207
+ order: "pre",
208
+ handler: async (chain, { CHAIN_ID }) => {
209
+ const config = api.getNormalizedConfig();
210
+ const getBabelOptions = () => {
211
+ const baseConfig = getDefaultBabelOptions(config.source.decorators);
212
+ const userBabelConfig = applyUserBabelConfig(
213
+ (0, import_shared2.cloneDeep)(baseConfig),
214
+ options.babelLoaderOptions
215
+ );
216
+ return userBabelConfig;
217
+ };
218
+ const babelOptions = getBabelOptions();
219
+ const babelLoader = import_node_path2.default.resolve(
220
+ __dirname,
221
+ "../compiled/babel-loader/index.js"
210
222
  );
211
- return userBabelConfig;
212
- };
213
- const babelOptions = getBabelOptions();
214
- const babelLoader = import_node_path2.default.resolve(
215
- __dirname,
216
- "../compiled/babel-loader/index.js"
217
- );
218
- const { include, exclude } = options;
219
- if (include || exclude) {
220
- const rule = chain.module.rule(BABEL_JS_RULE);
221
- if (include) {
222
- (0, import_shared2.castArray)(include).forEach((condition) => {
223
- rule.include.add(condition);
224
- });
225
- }
226
- if (exclude) {
227
- (0, import_shared2.castArray)(exclude).forEach((condition) => {
228
- rule.exclude.add(condition);
229
- });
223
+ const { include, exclude } = options;
224
+ if (include || exclude) {
225
+ const rule = chain.module.rule(BABEL_JS_RULE);
226
+ if (include) {
227
+ (0, import_shared2.castArray)(include).forEach((condition) => {
228
+ rule.include.add(condition);
229
+ });
230
+ }
231
+ if (exclude) {
232
+ (0, import_shared2.castArray)(exclude).forEach((condition) => {
233
+ rule.exclude.add(condition);
234
+ });
235
+ }
236
+ const swcRule = chain.module.rules.get(CHAIN_ID.RULE.JS).use(CHAIN_ID.USE.SWC);
237
+ const swcLoader = swcRule.get("loader");
238
+ const swcOptions = swcRule.get("options");
239
+ 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);
240
+ } else {
241
+ const rule = chain.module.rule(CHAIN_ID.RULE.JS);
242
+ rule.test(import_shared2.SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(babelLoader).options(babelOptions);
230
243
  }
231
- const swcRule = chain.module.rules.get(CHAIN_ID.RULE.JS).use(CHAIN_ID.USE.SWC);
232
- const swcLoader = swcRule.get("loader");
233
- const swcOptions = swcRule.get("options");
234
- 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);
235
- } else {
236
- const rule = chain.module.rule(CHAIN_ID.RULE.JS);
237
- rule.test(import_shared2.SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(babelLoader).options(babelOptions);
238
244
  }
239
245
  });
240
246
  }
package/dist/index.mjs CHANGED
@@ -18,8 +18,12 @@ var __dirname = /* @__PURE__ */ getDirname();
18
18
 
19
19
  // src/plugin.ts
20
20
  import path2 from "path";
21
- import { PLUGIN_BABEL_NAME } from "@rsbuild/core";
22
- import { castArray as castArray2, cloneDeep, SCRIPT_REGEX, isProd } from "@rsbuild/shared";
21
+ import {
22
+ isProd,
23
+ castArray as castArray2,
24
+ cloneDeep,
25
+ SCRIPT_REGEX
26
+ } from "@rsbuild/shared";
23
27
 
24
28
  // src/helper.ts
25
29
  import { isAbsolute, normalize, sep } from "path";
@@ -155,6 +159,7 @@ var modifyBabelLoaderOptions = ({
155
159
  };
156
160
 
157
161
  // src/plugin.ts
162
+ var PLUGIN_BABEL_NAME = "rsbuild:babel";
158
163
  var DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
159
164
  allowNamespaces: true,
160
165
  allExtensions: true,
@@ -164,12 +169,14 @@ var DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
164
169
  optimizeConstEnums: true,
165
170
  isTSX: true
166
171
  };
167
- var getDefaultBabelOptions = () => {
172
+ var getDefaultBabelOptions = (decorators) => {
168
173
  return {
169
174
  babelrc: false,
170
175
  configFile: false,
171
176
  compact: isProd(),
172
- plugins: [],
177
+ plugins: [
178
+ [__require.resolve("@babel/plugin-proposal-decorators"), decorators]
179
+ ],
173
180
  presets: [
174
181
  // TODO: only apply preset-typescript for ts file (isTSX & allExtensions false)
175
182
  [
@@ -182,40 +189,44 @@ var getDefaultBabelOptions = () => {
182
189
  var pluginBabel = (options = {}) => ({
183
190
  name: PLUGIN_BABEL_NAME,
184
191
  setup(api) {
185
- api.modifyBundlerChain(async (chain, { CHAIN_ID }) => {
186
- const getBabelOptions = () => {
187
- const baseConfig = getDefaultBabelOptions();
188
- const userBabelConfig = applyUserBabelConfig(
189
- cloneDeep(baseConfig),
190
- options.babelLoaderOptions
192
+ api.modifyBundlerChain({
193
+ order: "pre",
194
+ handler: async (chain, { CHAIN_ID }) => {
195
+ const config = api.getNormalizedConfig();
196
+ const getBabelOptions = () => {
197
+ const baseConfig = getDefaultBabelOptions(config.source.decorators);
198
+ const userBabelConfig = applyUserBabelConfig(
199
+ cloneDeep(baseConfig),
200
+ options.babelLoaderOptions
201
+ );
202
+ return userBabelConfig;
203
+ };
204
+ const babelOptions = getBabelOptions();
205
+ const babelLoader = path2.resolve(
206
+ __dirname,
207
+ "../compiled/babel-loader/index.js"
191
208
  );
192
- return userBabelConfig;
193
- };
194
- const babelOptions = getBabelOptions();
195
- const babelLoader = path2.resolve(
196
- __dirname,
197
- "../compiled/babel-loader/index.js"
198
- );
199
- const { include, exclude } = options;
200
- if (include || exclude) {
201
- const rule = chain.module.rule(BABEL_JS_RULE);
202
- if (include) {
203
- castArray2(include).forEach((condition) => {
204
- rule.include.add(condition);
205
- });
206
- }
207
- if (exclude) {
208
- castArray2(exclude).forEach((condition) => {
209
- rule.exclude.add(condition);
210
- });
209
+ const { include, exclude } = options;
210
+ if (include || exclude) {
211
+ const rule = chain.module.rule(BABEL_JS_RULE);
212
+ if (include) {
213
+ castArray2(include).forEach((condition) => {
214
+ rule.include.add(condition);
215
+ });
216
+ }
217
+ if (exclude) {
218
+ castArray2(exclude).forEach((condition) => {
219
+ rule.exclude.add(condition);
220
+ });
221
+ }
222
+ const swcRule = chain.module.rules.get(CHAIN_ID.RULE.JS).use(CHAIN_ID.USE.SWC);
223
+ const swcLoader = swcRule.get("loader");
224
+ const swcOptions = swcRule.get("options");
225
+ rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.SWC).loader(swcLoader).options(swcOptions).end().use(CHAIN_ID.USE.BABEL).loader(babelLoader).options(babelOptions);
226
+ } else {
227
+ const rule = chain.module.rule(CHAIN_ID.RULE.JS);
228
+ rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(babelLoader).options(babelOptions);
211
229
  }
212
- const swcRule = chain.module.rules.get(CHAIN_ID.RULE.JS).use(CHAIN_ID.USE.SWC);
213
- const swcLoader = swcRule.get("loader");
214
- const swcOptions = swcRule.get("options");
215
- rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.SWC).loader(swcLoader).options(swcOptions).end().use(CHAIN_ID.USE.BABEL).loader(babelLoader).options(babelOptions);
216
- } else {
217
- const rule = chain.module.rule(CHAIN_ID.RULE.JS);
218
- rule.test(SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(babelLoader).options(babelOptions);
219
230
  }
220
231
  });
221
232
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-babel",
3
- "version": "0.3.10",
3
+ "version": "0.4.0",
4
4
  "description": "Babel plugin for Rsbuild",
5
5
  "repository": {
6
6
  "type": "git",
@@ -25,18 +25,19 @@
25
25
  "dependencies": {
26
26
  "@babel/core": "^7.23.2",
27
27
  "@babel/preset-typescript": "^7.23.2",
28
+ "@babel/plugin-proposal-decorators": "^7.23.2",
28
29
  "@types/babel__core": "^7.20.3",
29
30
  "upath": "2.0.1",
30
- "@rsbuild/shared": "0.3.10"
31
+ "@rsbuild/shared": "0.4.0"
31
32
  },
32
33
  "devDependencies": {
33
34
  "@types/node": "16.x",
34
35
  "typescript": "^5.3.0",
35
- "@rsbuild/core": "0.3.10",
36
- "@scripts/test-helper": "0.3.10"
36
+ "@rsbuild/core": "0.4.0",
37
+ "@scripts/test-helper": "0.4.0"
37
38
  },
38
39
  "peerDependencies": {
39
- "@rsbuild/core": "^0.3.10"
40
+ "@rsbuild/core": "^0.4.0"
40
41
  },
41
42
  "publishConfig": {
42
43
  "access": "public",