@rsbuild/plugin-babel 0.0.0-next-20231103091827

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-present Bytedance, Inc. and its affiliates.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,19 @@
1
+ <p align="center">
2
+ <a href="https://rsbuild.dev" target="blank"><img src="https://github.com/web-infra-dev/rsbuild/assets/7237365/84abc13e-b620-468f-a90b-dbf28e7e9427" alt="Rsbuild Logo" /></a>
3
+ </p>
4
+
5
+ # Rsbuild
6
+
7
+ Unleash the power of Rspack with the out-of-the-box build tool.
8
+
9
+ ## Documentation
10
+
11
+ https://rsbuild.dev/
12
+
13
+ ## Contributing
14
+
15
+ Please read the [Contributing Guide](https://github.com/web-infra-dev/rsbuild/blob/main/CONTRIBUTING.md).
16
+
17
+ ## License
18
+
19
+ Rsbuild is [MIT licensed](https://github.com/web-infra-dev/rsbuild/blob/main/LICENSE).
@@ -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.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
+ 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
+ });
@@ -0,0 +1,3 @@
1
+ export { pluginBabel } from './plugin';
2
+ export { getBabelUtils } from './helper';
3
+ export type { PresetEnvOptions, PresetEnvTargets, PresetEnvBuiltIns, BabelConfigUtils, BabelTransformOptions } from './types';
package/dist/index.js ADDED
@@ -0,0 +1,31 @@
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 __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var src_exports = {};
20
+ __export(src_exports, {
21
+ getBabelUtils: () => import_helper.getBabelUtils,
22
+ pluginBabel: () => import_plugin.pluginBabel
23
+ });
24
+ module.exports = __toCommonJS(src_exports);
25
+ var import_plugin = require("./plugin");
26
+ var import_helper = require("./helper");
27
+ // Annotate the CommonJS export names for ESM import in node:
28
+ 0 && (module.exports = {
29
+ getBabelUtils,
30
+ pluginBabel
31
+ });
@@ -0,0 +1,13 @@
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 ADDED
@@ -0,0 +1,116 @@
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
+ });
@@ -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>;
package/dist/types.js ADDED
@@ -0,0 +1,16 @@
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);
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@rsbuild/plugin-babel",
3
+ "version": "0.0.0-next-20231103091827",
4
+ "description": "Babel plugin for Rsbuild",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/web-infra-dev/rsbuild",
8
+ "directory": "packages/plugin-babel"
9
+ },
10
+ "license": "MIT",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "default": "./dist/index.js"
15
+ }
16
+ },
17
+ "main": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "dependencies": {
23
+ "@babel/core": "^7.23.2",
24
+ "@babel/preset-typescript": "^7.23.2",
25
+ "@types/babel__core": "^7.20.3",
26
+ "babel-loader": "9.1.3",
27
+ "lodash": "^4.17.21",
28
+ "upath": "2.0.1",
29
+ "@rsbuild/shared": "0.0.0-next-20231103091827"
30
+ },
31
+ "devDependencies": {
32
+ "@types/lodash": "^4.14.200",
33
+ "@types/node": "^16",
34
+ "typescript": "^5.2.2",
35
+ "@rsbuild/core": "0.0.0-next-20231103091827",
36
+ "@rsbuild/test-helper": "0.0.0-next-20231103091827"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public",
40
+ "provenance": true,
41
+ "registry": "https://registry.npmjs.org/"
42
+ },
43
+ "scripts": {
44
+ "build": "modern build",
45
+ "dev": "modern build --watch"
46
+ }
47
+ }