@rsbuild/plugin-babel 0.0.17 → 0.0.19

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,3 +1,59 @@
1
- export { pluginBabel } from './plugin';
2
- export { getBabelUtils } from './helper';
3
- export type { PresetEnvOptions, PresetEnvTargets, PresetEnvBuiltIns, BabelConfigUtils, BabelTransformOptions } from './types';
1
+ import { ChainedConfigWithUtils, DefaultRsbuildPlugin } from '@rsbuild/shared';
2
+ import { PluginItem, TransformOptions } from '@babel/core';
3
+ export { TransformOptions as BabelTransformOptions } from '@babel/core';
4
+
5
+ type PresetEnvTargets = string | string[] | Record<string, string>;
6
+ type PresetEnvBuiltIns = 'usage' | 'entry' | false;
7
+ type PresetEnvOptions = {
8
+ targets?: PresetEnvTargets;
9
+ bugfixes?: boolean;
10
+ spec?: boolean;
11
+ loose?: boolean;
12
+ modules?: 'amd' | 'umd' | 'systemjs' | 'commonjs' | 'cjs' | 'auto' | false;
13
+ debug?: boolean;
14
+ include?: string[];
15
+ exclude?: string[];
16
+ useBuiltIns?: PresetEnvBuiltIns;
17
+ corejs?: string | {
18
+ version: string;
19
+ proposals: boolean;
20
+ };
21
+ forceAllTransforms?: boolean;
22
+ configPath?: string;
23
+ ignoreBrowserslistConfig?: boolean;
24
+ browserslistEnv?: string;
25
+ shippedProposals?: boolean;
26
+ };
27
+ interface SharedBabelPresetReactOptions {
28
+ development?: boolean;
29
+ throwIfNamespace?: boolean;
30
+ }
31
+ interface AutomaticRuntimePresetReactOptions extends SharedBabelPresetReactOptions {
32
+ runtime?: 'automatic';
33
+ importSource?: string;
34
+ }
35
+ interface ClassicRuntimePresetReactOptions extends SharedBabelPresetReactOptions {
36
+ runtime?: 'classic';
37
+ pragma?: string;
38
+ pragmaFrag?: string;
39
+ useBuiltIns?: boolean;
40
+ useSpread?: boolean;
41
+ }
42
+ type PresetReactOptions = AutomaticRuntimePresetReactOptions | ClassicRuntimePresetReactOptions;
43
+ type BabelConfigUtils = {
44
+ addPlugins: (plugins: PluginItem[]) => void;
45
+ addPresets: (presets: PluginItem[]) => void;
46
+ addIncludes: (includes: string | RegExp | (string | RegExp)[]) => void;
47
+ addExcludes: (excludes: string | RegExp | (string | RegExp)[]) => void;
48
+ removePlugins: (plugins: string | string[]) => void;
49
+ removePresets: (presets: string | string[]) => void;
50
+ modifyPresetEnvOptions: (options: PresetEnvOptions) => void;
51
+ modifyPresetReactOptions: (options: PresetReactOptions) => void;
52
+ };
53
+ type PluginBabelOptions = ChainedConfigWithUtils<TransformOptions, BabelConfigUtils>;
54
+
55
+ declare const pluginBabel: (options?: PluginBabelOptions) => DefaultRsbuildPlugin;
56
+
57
+ declare const getBabelUtils: (config: TransformOptions) => BabelConfigUtils;
58
+
59
+ export { BabelConfigUtils, PresetEnvBuiltIns, PresetEnvOptions, PresetEnvTargets, getBabelUtils, pluginBabel };
package/dist/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,15 +17,210 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
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
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
19
31
  var src_exports = {};
20
32
  __export(src_exports, {
21
- getBabelUtils: () => import_helper.getBabelUtils,
22
- pluginBabel: () => import_plugin.pluginBabel
33
+ getBabelUtils: () => getBabelUtils,
34
+ pluginBabel: () => pluginBabel
23
35
  });
24
36
  module.exports = __toCommonJS(src_exports);
25
- var import_plugin = require("./plugin");
26
- var import_helper = require("./helper");
37
+
38
+ // src/plugin.ts
39
+ var import_shared2 = require("@rsbuild/shared");
40
+ var import_lodash = require("lodash");
41
+
42
+ // src/helper.ts
43
+ var import_path = require("path");
44
+ var import_shared = require("@rsbuild/shared");
45
+ var import_upath = __toESM(require("upath"));
46
+ var normalizeToPosixPath = (p) => import_upath.default.normalizeSafe((0, import_path.normalize)(p || "")).replace(/^([a-zA-Z]+):/, (_, m) => `/${m.toLowerCase()}`);
47
+ var formatPath = (originPath) => {
48
+ if ((0, import_path.isAbsolute)(originPath)) {
49
+ return originPath.split(import_path.sep).join("/");
50
+ }
51
+ return originPath;
52
+ };
53
+ var getPluginItemName = (item) => {
54
+ if (typeof item === "string") {
55
+ return formatPath(item);
56
+ }
57
+ if (Array.isArray(item) && typeof item[0] === "string") {
58
+ return formatPath(item[0]);
59
+ }
60
+ return null;
61
+ };
62
+ var addPlugins = (plugins, config) => {
63
+ if (config.plugins) {
64
+ config.plugins.push(...plugins);
65
+ } else {
66
+ config.plugins = plugins;
67
+ }
68
+ };
69
+ var addPresets = (presets, config) => {
70
+ if (config.presets) {
71
+ config.presets.push(...presets);
72
+ } else {
73
+ config.presets = presets;
74
+ }
75
+ };
76
+ var removePlugins = (plugins, config) => {
77
+ if (!config.plugins) {
78
+ return;
79
+ }
80
+ const removeList = (0, import_shared.castArray)(plugins);
81
+ config.plugins = config.plugins.filter((item) => {
82
+ const name = getPluginItemName(item);
83
+ if (name) {
84
+ return !removeList.find((removeItem) => name.includes(removeItem));
85
+ }
86
+ return true;
87
+ });
88
+ };
89
+ var removePresets = (presets, config) => {
90
+ if (!config.presets) {
91
+ return;
92
+ }
93
+ const removeList = (0, import_shared.castArray)(presets);
94
+ config.presets = config.presets.filter((item) => {
95
+ const name = getPluginItemName(item);
96
+ if (name) {
97
+ return !removeList.find((removeItem) => name.includes(removeItem));
98
+ }
99
+ return true;
100
+ });
101
+ };
102
+ var modifyPresetOptions = (presetName, options, presets = []) => {
103
+ presets.forEach((preset, index) => {
104
+ if (Array.isArray(preset)) {
105
+ if (typeof preset[0] === "string" && normalizeToPosixPath(preset[0]).includes(presetName)) {
106
+ preset[1] = {
107
+ ...preset[1] || {},
108
+ ...options
109
+ // `options` is specific to different presets
110
+ };
111
+ }
112
+ } else if (typeof preset === "string" && normalizeToPosixPath(preset).includes(presetName)) {
113
+ presets[index] = [preset, options];
114
+ }
115
+ });
116
+ };
117
+ var getBabelUtils = (config) => {
118
+ const noop = () => {
119
+ };
120
+ return {
121
+ addPlugins: (plugins) => addPlugins(plugins, config),
122
+ addPresets: (presets) => addPresets(presets, config),
123
+ removePlugins: (plugins) => removePlugins(plugins, config),
124
+ removePresets: (presets) => removePresets(presets, config),
125
+ // `addIncludes` and `addExcludes` are noop functions by default,
126
+ // It can be overridden by `extraBabelUtils`.
127
+ addIncludes: noop,
128
+ addExcludes: noop,
129
+ // Compat `presetEnvOptions` and `presetReactOptions` in Eden.
130
+ modifyPresetEnvOptions: (options) => modifyPresetOptions("@babel/preset-env", options, config.presets || []),
131
+ modifyPresetReactOptions: (options) => modifyPresetOptions("@babel/preset-react", options, config.presets || [])
132
+ };
133
+ };
134
+ var applyUserBabelConfig = (defaultOptions, userBabelConfig, extraBabelUtils) => {
135
+ if (userBabelConfig) {
136
+ const babelUtils = {
137
+ ...getBabelUtils(defaultOptions),
138
+ ...extraBabelUtils
139
+ };
140
+ return (0, import_shared.mergeChainedOptions)({
141
+ defaults: defaultOptions,
142
+ options: userBabelConfig,
143
+ utils: babelUtils
144
+ });
145
+ }
146
+ return defaultOptions;
147
+ };
148
+
149
+ // src/plugin.ts
150
+ var DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
151
+ allowNamespaces: true,
152
+ allExtensions: true,
153
+ allowDeclareFields: true,
154
+ // aligns Babel's behavior with TypeScript's default behavior.
155
+ // https://babeljs.io/docs/en/babel-preset-typescript#optimizeconstenums
156
+ optimizeConstEnums: true,
157
+ isTSX: true
158
+ };
159
+ var pluginBabel = (options = {}) => ({
160
+ name: "plugin-babel",
161
+ setup(api) {
162
+ if (api.context.bundlerType === "webpack") {
163
+ return;
164
+ }
165
+ api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd }) => {
166
+ const getBabelOptions = () => {
167
+ const includes2 = [];
168
+ const excludes2 = [];
169
+ const babelUtils = {
170
+ addIncludes(items) {
171
+ if (Array.isArray(items)) {
172
+ includes2.push(...items);
173
+ } else {
174
+ includes2.push(items);
175
+ }
176
+ },
177
+ addExcludes(items) {
178
+ if (Array.isArray(items)) {
179
+ excludes2.push(...items);
180
+ } else {
181
+ excludes2.push(items);
182
+ }
183
+ }
184
+ };
185
+ const baseConfig = {
186
+ plugins: [],
187
+ presets: [
188
+ // TODO: only apply preset-typescript for ts file (isTSX & allExtensions false)
189
+ [
190
+ require.resolve("@babel/preset-typescript"),
191
+ DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS
192
+ ]
193
+ ]
194
+ };
195
+ const userBabelConfig = applyUserBabelConfig(
196
+ (0, import_lodash.cloneDeep)(baseConfig),
197
+ options,
198
+ babelUtils
199
+ );
200
+ const babelOptions2 = {
201
+ babelrc: false,
202
+ configFile: false,
203
+ compact: isProd,
204
+ ...userBabelConfig
205
+ };
206
+ return {
207
+ babelOptions: babelOptions2,
208
+ includes: includes2,
209
+ excludes: excludes2
210
+ };
211
+ };
212
+ const { babelOptions, includes = [], excludes = [] } = getBabelOptions();
213
+ const rule = chain.module.rule(CHAIN_ID.RULE.JS);
214
+ includes.forEach((condition) => {
215
+ rule.include.add(condition);
216
+ });
217
+ excludes.forEach((condition) => {
218
+ rule.exclude.add(condition);
219
+ });
220
+ rule.test((0, import_shared2.mergeRegex)(import_shared2.JS_REGEX, import_shared2.TS_REGEX)).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(require.resolve("babel-loader")).options(babelOptions);
221
+ });
222
+ }
223
+ });
27
224
  // Annotate the CommonJS export names for ESM import in node:
28
225
  0 && (module.exports = {
29
226
  getBabelUtils,
package/dist/index.mjs ADDED
@@ -0,0 +1,206 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined")
5
+ return require.apply(this, arguments);
6
+ throw Error('Dynamic require of "' + x + '" is not supported');
7
+ });
8
+
9
+ // ../../node_modules/.pnpm/@modern-js+module-tools@2.40.0_typescript@5.2.2/node_modules/@modern-js/module-tools/shims/esm.js
10
+ import { fileURLToPath } from "url";
11
+ import path from "path";
12
+
13
+ // ../../scripts/require_shims.js
14
+ import { createRequire } from "module";
15
+ global.require = createRequire(import.meta.url);
16
+
17
+ // src/plugin.ts
18
+ import { JS_REGEX, TS_REGEX, mergeRegex } from "@rsbuild/shared";
19
+ import { cloneDeep } from "lodash";
20
+
21
+ // src/helper.ts
22
+ import { isAbsolute, normalize, sep } from "path";
23
+ import { castArray, mergeChainedOptions } from "@rsbuild/shared";
24
+ import upath from "upath";
25
+ var normalizeToPosixPath = (p) => upath.normalizeSafe(normalize(p || "")).replace(/^([a-zA-Z]+):/, (_, m) => `/${m.toLowerCase()}`);
26
+ var formatPath = (originPath) => {
27
+ if (isAbsolute(originPath)) {
28
+ return originPath.split(sep).join("/");
29
+ }
30
+ return originPath;
31
+ };
32
+ var getPluginItemName = (item) => {
33
+ if (typeof item === "string") {
34
+ return formatPath(item);
35
+ }
36
+ if (Array.isArray(item) && typeof item[0] === "string") {
37
+ return formatPath(item[0]);
38
+ }
39
+ return null;
40
+ };
41
+ var addPlugins = (plugins, config) => {
42
+ if (config.plugins) {
43
+ config.plugins.push(...plugins);
44
+ } else {
45
+ config.plugins = plugins;
46
+ }
47
+ };
48
+ var addPresets = (presets, config) => {
49
+ if (config.presets) {
50
+ config.presets.push(...presets);
51
+ } else {
52
+ config.presets = presets;
53
+ }
54
+ };
55
+ var removePlugins = (plugins, config) => {
56
+ if (!config.plugins) {
57
+ return;
58
+ }
59
+ const removeList = castArray(plugins);
60
+ config.plugins = config.plugins.filter((item) => {
61
+ const name = getPluginItemName(item);
62
+ if (name) {
63
+ return !removeList.find((removeItem) => name.includes(removeItem));
64
+ }
65
+ return true;
66
+ });
67
+ };
68
+ var removePresets = (presets, config) => {
69
+ if (!config.presets) {
70
+ return;
71
+ }
72
+ const removeList = castArray(presets);
73
+ config.presets = config.presets.filter((item) => {
74
+ const name = getPluginItemName(item);
75
+ if (name) {
76
+ return !removeList.find((removeItem) => name.includes(removeItem));
77
+ }
78
+ return true;
79
+ });
80
+ };
81
+ var modifyPresetOptions = (presetName, options, presets = []) => {
82
+ presets.forEach((preset, index) => {
83
+ if (Array.isArray(preset)) {
84
+ if (typeof preset[0] === "string" && normalizeToPosixPath(preset[0]).includes(presetName)) {
85
+ preset[1] = {
86
+ ...preset[1] || {},
87
+ ...options
88
+ // `options` is specific to different presets
89
+ };
90
+ }
91
+ } else if (typeof preset === "string" && normalizeToPosixPath(preset).includes(presetName)) {
92
+ presets[index] = [preset, options];
93
+ }
94
+ });
95
+ };
96
+ var getBabelUtils = (config) => {
97
+ const noop = () => {
98
+ };
99
+ return {
100
+ addPlugins: (plugins) => addPlugins(plugins, config),
101
+ addPresets: (presets) => addPresets(presets, config),
102
+ removePlugins: (plugins) => removePlugins(plugins, config),
103
+ removePresets: (presets) => removePresets(presets, config),
104
+ // `addIncludes` and `addExcludes` are noop functions by default,
105
+ // It can be overridden by `extraBabelUtils`.
106
+ addIncludes: noop,
107
+ addExcludes: noop,
108
+ // Compat `presetEnvOptions` and `presetReactOptions` in Eden.
109
+ modifyPresetEnvOptions: (options) => modifyPresetOptions("@babel/preset-env", options, config.presets || []),
110
+ modifyPresetReactOptions: (options) => modifyPresetOptions("@babel/preset-react", options, config.presets || [])
111
+ };
112
+ };
113
+ var applyUserBabelConfig = (defaultOptions, userBabelConfig, extraBabelUtils) => {
114
+ if (userBabelConfig) {
115
+ const babelUtils = {
116
+ ...getBabelUtils(defaultOptions),
117
+ ...extraBabelUtils
118
+ };
119
+ return mergeChainedOptions({
120
+ defaults: defaultOptions,
121
+ options: userBabelConfig,
122
+ utils: babelUtils
123
+ });
124
+ }
125
+ return defaultOptions;
126
+ };
127
+
128
+ // src/plugin.ts
129
+ var DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
130
+ allowNamespaces: true,
131
+ allExtensions: true,
132
+ allowDeclareFields: true,
133
+ // aligns Babel's behavior with TypeScript's default behavior.
134
+ // https://babeljs.io/docs/en/babel-preset-typescript#optimizeconstenums
135
+ optimizeConstEnums: true,
136
+ isTSX: true
137
+ };
138
+ var pluginBabel = (options = {}) => ({
139
+ name: "plugin-babel",
140
+ setup(api) {
141
+ if (api.context.bundlerType === "webpack") {
142
+ return;
143
+ }
144
+ api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd }) => {
145
+ const getBabelOptions = () => {
146
+ const includes2 = [];
147
+ const excludes2 = [];
148
+ const babelUtils = {
149
+ addIncludes(items) {
150
+ if (Array.isArray(items)) {
151
+ includes2.push(...items);
152
+ } else {
153
+ includes2.push(items);
154
+ }
155
+ },
156
+ addExcludes(items) {
157
+ if (Array.isArray(items)) {
158
+ excludes2.push(...items);
159
+ } else {
160
+ excludes2.push(items);
161
+ }
162
+ }
163
+ };
164
+ const baseConfig = {
165
+ plugins: [],
166
+ presets: [
167
+ // TODO: only apply preset-typescript for ts file (isTSX & allExtensions false)
168
+ [
169
+ __require.resolve("@babel/preset-typescript"),
170
+ DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS
171
+ ]
172
+ ]
173
+ };
174
+ const userBabelConfig = applyUserBabelConfig(
175
+ cloneDeep(baseConfig),
176
+ options,
177
+ babelUtils
178
+ );
179
+ const babelOptions2 = {
180
+ babelrc: false,
181
+ configFile: false,
182
+ compact: isProd,
183
+ ...userBabelConfig
184
+ };
185
+ return {
186
+ babelOptions: babelOptions2,
187
+ includes: includes2,
188
+ excludes: excludes2
189
+ };
190
+ };
191
+ const { babelOptions, includes = [], excludes = [] } = getBabelOptions();
192
+ const rule = chain.module.rule(CHAIN_ID.RULE.JS);
193
+ includes.forEach((condition) => {
194
+ rule.include.add(condition);
195
+ });
196
+ excludes.forEach((condition) => {
197
+ rule.exclude.add(condition);
198
+ });
199
+ rule.test(mergeRegex(JS_REGEX, TS_REGEX)).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(__require.resolve("babel-loader")).options(babelOptions);
200
+ });
201
+ }
202
+ });
203
+ export {
204
+ getBabelUtils,
205
+ pluginBabel
206
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-babel",
3
- "version": "0.0.17",
3
+ "version": "0.0.19",
4
4
  "description": "Babel plugin for Rsbuild",
5
5
  "repository": {
6
6
  "type": "git",
@@ -11,6 +11,7 @@
11
11
  "exports": {
12
12
  ".": {
13
13
  "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.mjs",
14
15
  "default": "./dist/index.js"
15
16
  }
16
17
  },
@@ -26,14 +27,14 @@
26
27
  "babel-loader": "9.1.3",
27
28
  "lodash": "^4.17.21",
28
29
  "upath": "2.0.1",
29
- "@rsbuild/shared": "0.0.17"
30
+ "@rsbuild/shared": "0.0.19"
30
31
  },
31
32
  "devDependencies": {
32
33
  "@types/lodash": "^4.14.200",
33
34
  "@types/node": "^16",
34
35
  "typescript": "^5.2.2",
35
- "@rsbuild/core": "0.0.17",
36
- "@rsbuild/test-helper": "0.0.17"
36
+ "@rsbuild/core": "0.0.19",
37
+ "@rsbuild/test-helper": "0.0.19"
37
38
  },
38
39
  "publishConfig": {
39
40
  "access": "public",
package/dist/helper.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import type { BabelConfigUtils, BabelTransformOptions } from './types';
2
- export declare const getBabelUtils: (config: BabelTransformOptions) => BabelConfigUtils;
3
- export type BabelConfig = BabelTransformOptions | ((config: BabelTransformOptions, utils: BabelConfigUtils) => BabelTransformOptions | void);
4
- export declare const applyUserBabelConfig: (defaultOptions: BabelTransformOptions, userBabelConfig?: BabelConfig | BabelConfig[], extraBabelUtils?: Partial<BabelConfigUtils>) => BabelTransformOptions;
package/dist/helper.js DELETED
@@ -1,144 +0,0 @@
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;
19
- };
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
- var helper_exports = {};
30
- __export(helper_exports, {
31
- applyUserBabelConfig: () => applyUserBabelConfig,
32
- getBabelUtils: () => getBabelUtils
33
- });
34
- module.exports = __toCommonJS(helper_exports);
35
- var import_path = require("path");
36
- var import_shared = require("@rsbuild/shared");
37
- var import_upath = __toESM(require("upath"));
38
- const normalizeToPosixPath = (p) => import_upath.default.normalizeSafe((0, import_path.normalize)(p || "")).replace(/^([a-zA-Z]+):/, (_, m) => `/${m.toLowerCase()}`);
39
- const formatPath = (originPath) => {
40
- if ((0, import_path.isAbsolute)(originPath)) {
41
- return originPath.split(import_path.sep).join("/");
42
- }
43
- return originPath;
44
- };
45
- const getPluginItemName = (item) => {
46
- if (typeof item === "string") {
47
- return formatPath(item);
48
- }
49
- if (Array.isArray(item) && typeof item[0] === "string") {
50
- return formatPath(item[0]);
51
- }
52
- return null;
53
- };
54
- const addPlugins = (plugins, config) => {
55
- if (config.plugins) {
56
- config.plugins.push(...plugins);
57
- } else {
58
- config.plugins = plugins;
59
- }
60
- };
61
- const addPresets = (presets, config) => {
62
- if (config.presets) {
63
- config.presets.push(...presets);
64
- } else {
65
- config.presets = presets;
66
- }
67
- };
68
- const removePlugins = (plugins, config) => {
69
- if (!config.plugins) {
70
- return;
71
- }
72
- const removeList = (0, import_shared.castArray)(plugins);
73
- config.plugins = config.plugins.filter((item) => {
74
- const name = getPluginItemName(item);
75
- if (name) {
76
- return !removeList.find((removeItem) => name.includes(removeItem));
77
- }
78
- return true;
79
- });
80
- };
81
- const removePresets = (presets, config) => {
82
- if (!config.presets) {
83
- return;
84
- }
85
- const removeList = (0, import_shared.castArray)(presets);
86
- config.presets = config.presets.filter((item) => {
87
- const name = getPluginItemName(item);
88
- if (name) {
89
- return !removeList.find((removeItem) => name.includes(removeItem));
90
- }
91
- return true;
92
- });
93
- };
94
- const modifyPresetOptions = (presetName, options, presets = []) => {
95
- presets.forEach((preset, index) => {
96
- if (Array.isArray(preset)) {
97
- if (typeof preset[0] === "string" && normalizeToPosixPath(preset[0]).includes(presetName)) {
98
- preset[1] = {
99
- ...preset[1] || {},
100
- ...options
101
- // `options` is specific to different presets
102
- };
103
- }
104
- } else if (typeof preset === "string" && normalizeToPosixPath(preset).includes(presetName)) {
105
- presets[index] = [preset, options];
106
- }
107
- });
108
- };
109
- const getBabelUtils = (config) => {
110
- const noop = () => {
111
- };
112
- return {
113
- addPlugins: (plugins) => addPlugins(plugins, config),
114
- addPresets: (presets) => addPresets(presets, config),
115
- removePlugins: (plugins) => removePlugins(plugins, config),
116
- removePresets: (presets) => removePresets(presets, config),
117
- // `addIncludes` and `addExcludes` are noop functions by default,
118
- // It can be overridden by `extraBabelUtils`.
119
- addIncludes: noop,
120
- addExcludes: noop,
121
- // Compat `presetEnvOptions` and `presetReactOptions` in Eden.
122
- modifyPresetEnvOptions: (options) => modifyPresetOptions("@babel/preset-env", options, config.presets || []),
123
- modifyPresetReactOptions: (options) => modifyPresetOptions("@babel/preset-react", options, config.presets || [])
124
- };
125
- };
126
- const applyUserBabelConfig = (defaultOptions, userBabelConfig, extraBabelUtils) => {
127
- if (userBabelConfig) {
128
- const babelUtils = {
129
- ...getBabelUtils(defaultOptions),
130
- ...extraBabelUtils
131
- };
132
- return (0, import_shared.mergeChainedOptions)({
133
- defaults: defaultOptions,
134
- options: userBabelConfig,
135
- utils: babelUtils
136
- });
137
- }
138
- return defaultOptions;
139
- };
140
- // Annotate the CommonJS export names for ESM import in node:
141
- 0 && (module.exports = {
142
- applyUserBabelConfig,
143
- getBabelUtils
144
- });
package/dist/plugin.d.ts DELETED
@@ -1,13 +0,0 @@
1
- import { DefaultRsbuildPlugin } from '@rsbuild/shared';
2
- import type { PluginBabelOptions } from './types';
3
- /**
4
- * The `@babel/preset-typescript` default options.
5
- */
6
- export declare const DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS: {
7
- allowNamespaces: boolean;
8
- allExtensions: boolean;
9
- allowDeclareFields: boolean;
10
- optimizeConstEnums: boolean;
11
- isTSX: boolean;
12
- };
13
- export declare const pluginBabel: (options?: PluginBabelOptions) => DefaultRsbuildPlugin;
package/dist/plugin.js DELETED
@@ -1,116 +0,0 @@
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;
19
- };
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
- var plugin_exports = {};
30
- __export(plugin_exports, {
31
- DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS: () => DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS,
32
- pluginBabel: () => pluginBabel
33
- });
34
- module.exports = __toCommonJS(plugin_exports);
35
- var import_shared2 = require("@rsbuild/shared");
36
- var import_lodash = require("lodash");
37
- var import_helper = require("./helper");
38
- const DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
39
- allowNamespaces: true,
40
- allExtensions: true,
41
- allowDeclareFields: true,
42
- // aligns Babel's behavior with TypeScript's default behavior.
43
- // https://babeljs.io/docs/en/babel-preset-typescript#optimizeconstenums
44
- optimizeConstEnums: true,
45
- isTSX: true
46
- };
47
- const pluginBabel = (options = {}) => ({
48
- name: "plugin-babel",
49
- setup(api) {
50
- if (api.context.bundlerType === "webpack") {
51
- return;
52
- }
53
- api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd }) => {
54
- const getBabelOptions = () => {
55
- const includes2 = [];
56
- const excludes2 = [];
57
- const babelUtils = {
58
- addIncludes(items) {
59
- if (Array.isArray(items)) {
60
- includes2.push(...items);
61
- } else {
62
- includes2.push(items);
63
- }
64
- },
65
- addExcludes(items) {
66
- if (Array.isArray(items)) {
67
- excludes2.push(...items);
68
- } else {
69
- excludes2.push(items);
70
- }
71
- }
72
- };
73
- const baseConfig = {
74
- plugins: [],
75
- presets: [
76
- // TODO: only apply preset-typescript for ts file (isTSX & allExtensions false)
77
- [
78
- require.resolve("@babel/preset-typescript"),
79
- DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS
80
- ]
81
- ]
82
- };
83
- const userBabelConfig = (0, import_helper.applyUserBabelConfig)(
84
- (0, import_lodash.cloneDeep)(baseConfig),
85
- options,
86
- babelUtils
87
- );
88
- const babelOptions2 = {
89
- babelrc: false,
90
- configFile: false,
91
- compact: isProd,
92
- ...userBabelConfig
93
- };
94
- return {
95
- babelOptions: babelOptions2,
96
- includes: includes2,
97
- excludes: excludes2
98
- };
99
- };
100
- const { babelOptions, includes = [], excludes = [] } = getBabelOptions();
101
- const rule = chain.module.rule(CHAIN_ID.RULE.JS);
102
- includes.forEach((condition) => {
103
- rule.include.add(condition);
104
- });
105
- excludes.forEach((condition) => {
106
- rule.exclude.add(condition);
107
- });
108
- rule.test((0, import_shared2.mergeRegex)(import_shared2.JS_REGEX, import_shared2.TS_REGEX)).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(require.resolve("babel-loader")).options(babelOptions);
109
- });
110
- }
111
- });
112
- // Annotate the CommonJS export names for ESM import in node:
113
- 0 && (module.exports = {
114
- DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS,
115
- pluginBabel
116
- });
package/dist/types.d.ts DELETED
@@ -1,52 +0,0 @@
1
- import type { ChainedConfigWithUtils } from '@rsbuild/shared';
2
- import type { PluginItem as BabelPlugin, PluginOptions as BabelPluginOptions, TransformOptions as BabelTransformOptions } from '@babel/core';
3
- export { BabelPlugin, BabelPluginOptions, BabelTransformOptions };
4
- export type PresetEnvTargets = string | string[] | Record<string, string>;
5
- export type PresetEnvBuiltIns = 'usage' | 'entry' | false;
6
- export type PresetEnvOptions = {
7
- targets?: PresetEnvTargets;
8
- bugfixes?: boolean;
9
- spec?: boolean;
10
- loose?: boolean;
11
- modules?: 'amd' | 'umd' | 'systemjs' | 'commonjs' | 'cjs' | 'auto' | false;
12
- debug?: boolean;
13
- include?: string[];
14
- exclude?: string[];
15
- useBuiltIns?: PresetEnvBuiltIns;
16
- corejs?: string | {
17
- version: string;
18
- proposals: boolean;
19
- };
20
- forceAllTransforms?: boolean;
21
- configPath?: string;
22
- ignoreBrowserslistConfig?: boolean;
23
- browserslistEnv?: string;
24
- shippedProposals?: boolean;
25
- };
26
- export interface SharedBabelPresetReactOptions {
27
- development?: boolean;
28
- throwIfNamespace?: boolean;
29
- }
30
- export interface AutomaticRuntimePresetReactOptions extends SharedBabelPresetReactOptions {
31
- runtime?: 'automatic';
32
- importSource?: string;
33
- }
34
- export interface ClassicRuntimePresetReactOptions extends SharedBabelPresetReactOptions {
35
- runtime?: 'classic';
36
- pragma?: string;
37
- pragmaFrag?: string;
38
- useBuiltIns?: boolean;
39
- useSpread?: boolean;
40
- }
41
- export type PresetReactOptions = AutomaticRuntimePresetReactOptions | ClassicRuntimePresetReactOptions;
42
- export type BabelConfigUtils = {
43
- addPlugins: (plugins: BabelPlugin[]) => void;
44
- addPresets: (presets: BabelPlugin[]) => void;
45
- addIncludes: (includes: string | RegExp | (string | RegExp)[]) => void;
46
- addExcludes: (excludes: string | RegExp | (string | RegExp)[]) => void;
47
- removePlugins: (plugins: string | string[]) => void;
48
- removePresets: (presets: string | string[]) => void;
49
- modifyPresetEnvOptions: (options: PresetEnvOptions) => void;
50
- modifyPresetReactOptions: (options: PresetReactOptions) => void;
51
- };
52
- export type PluginBabelOptions = ChainedConfigWithUtils<BabelTransformOptions, BabelConfigUtils>;
package/dist/types.js DELETED
@@ -1,16 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
13
- };
14
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
- var types_exports = {};
16
- module.exports = __toCommonJS(types_exports);