@rsbuild/plugin-babel 0.0.10 → 0.0.12

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.
@@ -0,0 +1,4 @@
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 ADDED
@@ -0,0 +1,144 @@
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.ensureArray)(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.ensureArray)(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
+ defaultOptions,
134
+ userBabelConfig || {},
135
+ 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/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { DefaultRsbuildPlugin } from '@rsbuild/shared';
2
- import { PluginBabelOptions } from './type';
3
- export declare const pluginBabel: (options?: PluginBabelOptions) => DefaultRsbuildPlugin;
1
+ export { pluginBabel } from './plugin';
2
+ export { getBabelUtils } from './helper';
3
+ export type { PresetEnvOptions, PresetEnvTargets, PresetEnvBuiltIns, BabelConfigUtils, BabelTransformOptions } from './types';
package/dist/index.js CHANGED
@@ -18,19 +18,14 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var src_exports = {};
20
20
  __export(src_exports, {
21
- pluginBabel: () => pluginBabel
21
+ getBabelUtils: () => import_helper.getBabelUtils,
22
+ pluginBabel: () => import_plugin.pluginBabel
22
23
  });
23
24
  module.exports = __toCommonJS(src_exports);
24
- var import_rspack = require("./rspack");
25
- const pluginBabel = (options = {}) => ({
26
- name: "plugin-babel",
27
- setup(api) {
28
- if (api.context.bundlerType === "rspack") {
29
- (0, import_rspack.applyRspackBabelConfig)(api, options);
30
- }
31
- }
32
- });
25
+ var import_plugin = require("./plugin");
26
+ var import_helper = require("./helper");
33
27
  // Annotate the CommonJS export names for ESM import in node:
34
28
  0 && (module.exports = {
29
+ getBabelUtils,
35
30
  pluginBabel
36
31
  });
@@ -1,5 +1,5 @@
1
- import { SharedRsbuildPluginAPI } from '@rsbuild/shared';
2
- import { PluginBabelOptions } from './type';
1
+ import { DefaultRsbuildPlugin } from '@rsbuild/shared';
2
+ import type { PluginBabelOptions } from './types';
3
3
  /**
4
4
  * The `@babel/preset-typescript` default options.
5
5
  */
@@ -10,4 +10,4 @@ export declare const DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS: {
10
10
  optimizeConstEnums: boolean;
11
11
  isTSX: boolean;
12
12
  };
13
- export declare const applyRspackBabelConfig: (api: SharedRsbuildPluginAPI, userOptions: PluginBabelOptions) => void;
13
+ export declare const pluginBabel: (options?: PluginBabelOptions) => DefaultRsbuildPlugin;
@@ -26,14 +26,15 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
26
26
  mod
27
27
  ));
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var rspack_exports = {};
30
- __export(rspack_exports, {
29
+ var plugin_exports = {};
30
+ __export(plugin_exports, {
31
31
  DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS: () => DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS,
32
- applyRspackBabelConfig: () => applyRspackBabelConfig
32
+ pluginBabel: () => pluginBabel
33
33
  });
34
- module.exports = __toCommonJS(rspack_exports);
35
- var import_shared = require("@rsbuild/shared");
34
+ module.exports = __toCommonJS(plugin_exports);
35
+ var import_shared2 = require("@rsbuild/shared");
36
36
  var import_lodash = require("lodash");
37
+ var import_helper = require("./helper");
37
38
  const DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
38
39
  allowNamespaces: true,
39
40
  allExtensions: true,
@@ -43,11 +44,14 @@ const DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS = {
43
44
  optimizeConstEnums: true,
44
45
  isTSX: true
45
46
  };
46
- const applyRspackBabelConfig = (api, userOptions) => {
47
- api.modifyBundlerChain(
48
- async (chain, { CHAIN_ID, isProd, getCompiledPath }) => {
49
- const config = api.getNormalizedConfig();
50
- const getBabelOptions = (config2) => {
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 = () => {
51
55
  const includes2 = [];
52
56
  const excludes2 = [];
53
57
  const babelUtils = {
@@ -76,9 +80,9 @@ const applyRspackBabelConfig = (api, userOptions) => {
76
80
  ]
77
81
  ]
78
82
  };
79
- const userBabelConfig = (0, import_shared.applyUserBabelConfig)(
83
+ const userBabelConfig = (0, import_helper.applyUserBabelConfig)(
80
84
  (0, import_lodash.cloneDeep)(baseConfig),
81
- userOptions,
85
+ options,
82
86
  babelUtils
83
87
  );
84
88
  const babelOptions2 = {
@@ -93,11 +97,7 @@ const applyRspackBabelConfig = (api, userOptions) => {
93
97
  excludes: excludes2
94
98
  };
95
99
  };
96
- const {
97
- babelOptions,
98
- includes = [],
99
- excludes = []
100
- } = getBabelOptions(config);
100
+ const { babelOptions, includes = [], excludes = [] } = getBabelOptions();
101
101
  const rule = chain.module.rule(CHAIN_ID.RULE.JS);
102
102
  includes.forEach((condition) => {
103
103
  rule.include.add(condition);
@@ -105,12 +105,12 @@ const applyRspackBabelConfig = (api, userOptions) => {
105
105
  excludes.forEach((condition) => {
106
106
  rule.exclude.add(condition);
107
107
  });
108
- rule.test((0, import_shared.mergeRegex)(import_shared.JS_REGEX, import_shared.TS_REGEX)).use(CHAIN_ID.USE.BABEL).after(CHAIN_ID.USE.SWC).loader(getCompiledPath("babel-loader")).options(babelOptions);
109
- }
110
- );
111
- };
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
112
  // Annotate the CommonJS export names for ESM import in node:
113
113
  0 && (module.exports = {
114
114
  DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS,
115
- applyRspackBabelConfig
115
+ pluginBabel
116
116
  });
@@ -0,0 +1,52 @@
1
+ import type { ChainedConfig } 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 = ChainedConfig<BabelTransformOptions, BabelConfigUtils>;
@@ -12,5 +12,5 @@ var __copyProps = (to, from, except, desc) => {
12
12
  return to;
13
13
  };
14
14
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
- var type_exports = {};
16
- module.exports = __toCommonJS(type_exports);
15
+ var types_exports = {};
16
+ module.exports = __toCommonJS(types_exports);
package/package.json CHANGED
@@ -1,44 +1,47 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-babel",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "description": "Babel plugin for Rsbuild",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/web-infra-dev/rsbuild",
8
8
  "directory": "packages/plugin-babel"
9
9
  },
10
- "main": "./dist/index.js",
11
- "types": "./dist/index.d.ts",
10
+ "license": "MIT",
12
11
  "exports": {
13
12
  ".": {
14
13
  "types": "./dist/index.d.ts",
15
14
  "default": "./dist/index.js"
16
15
  }
17
16
  },
17
+ "main": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
18
19
  "files": [
19
20
  "dist"
20
21
  ],
21
- "license": "MIT",
22
22
  "dependencies": {
23
+ "@babel/core": "^7.23.2",
23
24
  "@babel/preset-typescript": "^7.23.2",
25
+ "@types/babel__core": "^7.20.3",
26
+ "babel-loader": "9.1.3",
24
27
  "lodash": "^4.17.21",
25
- "@rsbuild/shared": "0.0.10"
28
+ "upath": "2.0.1",
29
+ "@rsbuild/shared": "0.0.12"
26
30
  },
27
31
  "devDependencies": {
28
32
  "@types/lodash": "^4.14.200",
29
33
  "@types/node": "^16",
30
- "typescript": "^5",
31
- "@rsbuild/core": "0.0.10",
32
- "@rsbuild/test-helper": "0.0.10",
33
- "@rsbuild/webpack": "0.0.9"
34
+ "typescript": "^5.2.2",
35
+ "@rsbuild/core": "0.0.12",
36
+ "@rsbuild/test-helper": "0.0.12"
34
37
  },
35
38
  "publishConfig": {
36
- "registry": "https://registry.npmjs.org/",
37
39
  "access": "public",
38
- "provenance": true
40
+ "provenance": true,
41
+ "registry": "https://registry.npmjs.org/"
39
42
  },
40
43
  "scripts": {
41
- "dev": "modern build --watch",
42
- "build": "modern build"
44
+ "build": "modern build",
45
+ "dev": "modern build --watch"
43
46
  }
44
47
  }
package/dist/type.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import type { ChainedConfig, BabelTransformOptions, BabelConfigUtils } from '@rsbuild/shared';
2
- export type PluginBabelOptions = ChainedConfig<BabelTransformOptions, BabelConfigUtils>;