@rsbuild/plugin-babel 0.4.4 → 0.4.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.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { RsbuildPlugin } from '@rsbuild/core';
2
- import { ChainedConfigWithUtils, Decorators, NormalizedConfig, BundlerChain, ChainIdentifier } from '@rsbuild/shared';
1
+ import { NormalizedConfig, RsbuildContext, RsbuildPlugin } from '@rsbuild/core';
2
+ import { ChainedConfigWithUtils, NormalizedConfig as NormalizedConfig$1, BundlerChain, ChainIdentifier } from '@rsbuild/shared';
3
3
  import { PluginItem, TransformOptions } from '@babel/core';
4
4
  export { TransformOptions as BabelTransformOptions } from '@babel/core';
5
5
 
@@ -60,6 +60,28 @@ type BabelConfigUtils = {
60
60
  */
61
61
  addExcludes: (excludes: RuleCondition) => void;
62
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
+ };
63
85
  type PluginBabelOptions = {
64
86
  /**
65
87
  * Used to specify the files that need to be compiled by Babel.
@@ -73,26 +95,14 @@ type PluginBabelOptions = {
73
95
  * Options passed to `babel-loader`.
74
96
  * @see https://github.com/babel/babel-loader
75
97
  */
76
- babelLoaderOptions?: ChainedConfigWithUtils<TransformOptions, BabelConfigUtils>;
98
+ babelLoaderOptions?: ChainedConfigWithUtils<BabelLoaderOptions, BabelConfigUtils>;
77
99
  };
78
100
 
79
- declare const getDefaultBabelOptions: (decorators: Decorators) => {
80
- babelrc: boolean;
81
- configFile: boolean;
82
- compact: boolean;
83
- plugins: (string | Decorators)[][];
84
- presets: (string | {
85
- allowNamespaces: boolean;
86
- allExtensions: boolean;
87
- allowDeclareFields: boolean;
88
- optimizeConstEnums: boolean;
89
- isTSX: boolean;
90
- })[][];
91
- };
101
+ declare const getDefaultBabelOptions: (config: NormalizedConfig, context: RsbuildContext) => BabelLoaderOptions;
92
102
  declare const pluginBabel: (options?: PluginBabelOptions) => RsbuildPlugin;
93
103
 
94
104
  declare const getBabelUtils: (config: TransformOptions) => BabelConfigUtils;
95
- declare const getUseBuiltIns: (config: NormalizedConfig) => false | "usage" | "entry";
105
+ declare const getUseBuiltIns: (config: NormalizedConfig$1) => false | "usage" | "entry";
96
106
  declare const modifyBabelLoaderOptions: ({ chain, CHAIN_ID, modifier, }: {
97
107
  chain: BundlerChain;
98
108
  CHAIN_ID: ChainIdentifier;
package/dist/index.js CHANGED
@@ -183,13 +183,30 @@ var DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
183
183
  optimizeConstEnums: true,
184
184
  isTSX: true
185
185
  };
186
- var getDefaultBabelOptions = (decorators) => {
187
- return {
186
+ function getCacheDirectory(context, cacheDirectory) {
187
+ if (cacheDirectory) {
188
+ return (0, import_node_path2.isAbsolute)(cacheDirectory) ? cacheDirectory : (0, import_node_path2.join)(context.rootPath, cacheDirectory);
189
+ }
190
+ return (0, import_node_path2.join)(context.cachePath);
191
+ }
192
+ async function getCacheIdentifier(options) {
193
+ let identifier = `${(0, import_shared2.getNodeEnv)()}${JSON.stringify(options)}`;
194
+ const { version: coreVersion } = await import("@babel/core");
195
+ const loaderVersion = (await import_shared2.fse.readJSON((0, import_node_path2.join)(__dirname, "../compiled/babel-loader/package.json"))).version;
196
+ identifier += `@babel/core@${coreVersion}`;
197
+ identifier += `babel-loader@${loaderVersion}`;
198
+ return identifier;
199
+ }
200
+ var getDefaultBabelOptions = (config, context) => {
201
+ const options = {
188
202
  babelrc: false,
189
203
  configFile: false,
190
204
  compact: (0, import_shared2.isProd)(),
191
205
  plugins: [
192
- [require.resolve("@babel/plugin-proposal-decorators"), decorators]
206
+ [
207
+ require.resolve("@babel/plugin-proposal-decorators"),
208
+ config.source.decorators
209
+ ]
193
210
  ],
194
211
  presets: [
195
212
  // TODO: only apply preset-typescript for ts file (isTSX & allExtensions false)
@@ -199,23 +216,36 @@ var getDefaultBabelOptions = (decorators) => {
199
216
  ]
200
217
  ]
201
218
  };
219
+ const { buildCache } = config.performance;
220
+ if (buildCache && context.bundlerType === "rspack") {
221
+ const cacheDirectory = getCacheDirectory(
222
+ context,
223
+ typeof buildCache === "boolean" ? void 0 : buildCache.cacheDirectory
224
+ );
225
+ options.cacheCompression = false;
226
+ options.cacheDirectory = (0, import_node_path2.join)(cacheDirectory, "babel-loader");
227
+ }
228
+ return options;
202
229
  };
203
230
  var pluginBabel = (options = {}) => ({
204
231
  name: PLUGIN_BABEL_NAME,
205
232
  setup(api) {
233
+ const getBabelOptions = async () => {
234
+ const config = api.getNormalizedConfig();
235
+ const baseOptions = getDefaultBabelOptions(config, api.context);
236
+ const mergedOptions = applyUserBabelConfig(
237
+ (0, import_shared2.cloneDeep)(baseOptions),
238
+ options.babelLoaderOptions
239
+ );
240
+ if (mergedOptions.cacheDirectory && !mergedOptions.cacheIdentifier) {
241
+ mergedOptions.cacheIdentifier = await getCacheIdentifier(mergedOptions);
242
+ }
243
+ return mergedOptions;
244
+ };
206
245
  api.modifyBundlerChain({
207
246
  order: "pre",
208
247
  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();
248
+ const babelOptions = await getBabelOptions();
219
249
  const babelLoader = import_node_path2.default.resolve(
220
250
  __dirname,
221
251
  "../compiled/babel-loader/index.js"
package/dist/index.mjs CHANGED
@@ -9,7 +9,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
9
9
  throw Error('Dynamic require of "' + x + '" is not supported');
10
10
  });
11
11
 
12
- // ../../node_modules/.pnpm/@modern-js+module-tools@2.46.1_typescript@5.3.2/node_modules/@modern-js/module-tools/shims/esm.js
12
+ // ../../node_modules/.pnpm/@modern-js+module-tools@2.47.0_typescript@5.3.2/node_modules/@modern-js/module-tools/shims/esm.js
13
13
  import { fileURLToPath } from "url";
14
14
  import path from "path";
15
15
  var getFilename = () => fileURLToPath(import.meta.url);
@@ -17,11 +17,13 @@ var getDirname = () => path.dirname(getFilename());
17
17
  var __dirname = /* @__PURE__ */ getDirname();
18
18
 
19
19
  // src/plugin.ts
20
- import path2 from "path";
20
+ import path2, { isAbsolute as isAbsolute2, join } from "path";
21
21
  import {
22
+ fse,
22
23
  isProd,
23
24
  castArray as castArray2,
24
25
  cloneDeep,
26
+ getNodeEnv,
25
27
  SCRIPT_REGEX
26
28
  } from "@rsbuild/shared";
27
29
 
@@ -169,13 +171,30 @@ var DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
169
171
  optimizeConstEnums: true,
170
172
  isTSX: true
171
173
  };
172
- var getDefaultBabelOptions = (decorators) => {
173
- return {
174
+ function getCacheDirectory(context, cacheDirectory) {
175
+ if (cacheDirectory) {
176
+ return isAbsolute2(cacheDirectory) ? cacheDirectory : join(context.rootPath, cacheDirectory);
177
+ }
178
+ return join(context.cachePath);
179
+ }
180
+ async function getCacheIdentifier(options) {
181
+ let identifier = `${getNodeEnv()}${JSON.stringify(options)}`;
182
+ const { version: coreVersion } = await import("@babel/core");
183
+ const loaderVersion = (await fse.readJSON(join(__dirname, "../compiled/babel-loader/package.json"))).version;
184
+ identifier += `@babel/core@${coreVersion}`;
185
+ identifier += `babel-loader@${loaderVersion}`;
186
+ return identifier;
187
+ }
188
+ var getDefaultBabelOptions = (config, context) => {
189
+ const options = {
174
190
  babelrc: false,
175
191
  configFile: false,
176
192
  compact: isProd(),
177
193
  plugins: [
178
- [__require.resolve("@babel/plugin-proposal-decorators"), decorators]
194
+ [
195
+ __require.resolve("@babel/plugin-proposal-decorators"),
196
+ config.source.decorators
197
+ ]
179
198
  ],
180
199
  presets: [
181
200
  // TODO: only apply preset-typescript for ts file (isTSX & allExtensions false)
@@ -185,23 +204,36 @@ var getDefaultBabelOptions = (decorators) => {
185
204
  ]
186
205
  ]
187
206
  };
207
+ const { buildCache } = config.performance;
208
+ if (buildCache && context.bundlerType === "rspack") {
209
+ const cacheDirectory = getCacheDirectory(
210
+ context,
211
+ typeof buildCache === "boolean" ? void 0 : buildCache.cacheDirectory
212
+ );
213
+ options.cacheCompression = false;
214
+ options.cacheDirectory = join(cacheDirectory, "babel-loader");
215
+ }
216
+ return options;
188
217
  };
189
218
  var pluginBabel = (options = {}) => ({
190
219
  name: PLUGIN_BABEL_NAME,
191
220
  setup(api) {
221
+ const getBabelOptions = async () => {
222
+ const config = api.getNormalizedConfig();
223
+ const baseOptions = getDefaultBabelOptions(config, api.context);
224
+ const mergedOptions = applyUserBabelConfig(
225
+ cloneDeep(baseOptions),
226
+ options.babelLoaderOptions
227
+ );
228
+ if (mergedOptions.cacheDirectory && !mergedOptions.cacheIdentifier) {
229
+ mergedOptions.cacheIdentifier = await getCacheIdentifier(mergedOptions);
230
+ }
231
+ return mergedOptions;
232
+ };
192
233
  api.modifyBundlerChain({
193
234
  order: "pre",
194
235
  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();
236
+ const babelOptions = await getBabelOptions();
205
237
  const babelLoader = path2.resolve(
206
238
  __dirname,
207
239
  "../compiled/babel-loader/index.js"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-babel",
3
- "version": "0.4.4",
3
+ "version": "0.4.5",
4
4
  "description": "Babel plugin for Rsbuild",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,16 +28,16 @@
28
28
  "@babel/plugin-proposal-decorators": "^7.23.2",
29
29
  "@types/babel__core": "^7.20.3",
30
30
  "upath": "2.0.1",
31
- "@rsbuild/shared": "0.4.4"
31
+ "@rsbuild/shared": "0.4.5"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/node": "16.x",
35
35
  "typescript": "^5.3.0",
36
- "@rsbuild/core": "0.4.4",
37
- "@scripts/test-helper": "0.4.4"
36
+ "@rsbuild/core": "0.4.5",
37
+ "@scripts/test-helper": "0.4.5"
38
38
  },
39
39
  "peerDependencies": {
40
- "@rsbuild/core": "^0.4.4"
40
+ "@rsbuild/core": "^0.4.5"
41
41
  },
42
42
  "publishConfig": {
43
43
  "access": "public",