Skip to content

Commit

Permalink
feat(babel-preset): support Jest to use node preset (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Oct 12, 2023
1 parent a1a60b8 commit fd18cc3
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 77 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-cougars-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rsbuild/babel-preset': patch
---

feat(babel-preset): support Jest to use node preset
21 changes: 5 additions & 16 deletions packages/babel-preset/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ export const generateBaseConfig = (
presetEnv = {},
presetTypeScript = {},
pluginDecorators = false,
pluginTransformRuntime = {},
} = options;

if (presetEnv) {
const isTest = process.env.NODE_ENV === 'test';

config.presets?.push([
require.resolve('@babel/preset-env'),
{
modules: false,
// Jest only supports commonjs
modules: isTest ? 'commonjs' : false,
exclude: ['transform-typeof-symbol'],
...presetEnv,
},
Expand All @@ -43,34 +45,21 @@ export const generateBaseConfig = (
]);
}

if (pluginTransformRuntime) {
config.plugins?.push([
require.resolve('@babel/plugin-transform-runtime'),
{
version: require('@babel/runtime/package.json').version,
// this option has been deprecated
// but enabling it can help to reduce bundle size
useESModules: true,
},
]);
}

if (pluginDecorators) {
// link: https://github.com/tc39/proposal-decorators
config.plugins?.push([
require.resolve('@babel/plugin-proposal-decorators'),
{
version: pluginDecorators.version,
// decoratorsBeforeExport property is not allowed when version is legacy
...(pluginDecorators.version === '2018-09'
? { decoratorsBeforeExport: true }
: {}),
...pluginDecorators,
},
]);
}

config.plugins?.push(
join(__dirname, './pluginLockCorejsVersion'),
// Stage 1
// link: https://github.com/tc39/proposal-export-default-from
require.resolve('@babel/plugin-proposal-export-default-from'),
Expand Down
3 changes: 0 additions & 3 deletions packages/babel-preset/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ export const getBabelConfigForNode = (options: NodePresetOptions = {}) => {
presetEnv: {
targets: ['node >= 14'],
},
pluginTransformRuntime: {
regenerator: true,
},
},
options,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-preset/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ export type BasePresetOptions = {
presetEnv?: PresetEnvOptions | false;
presetTypeScript?: Record<string, unknown> | false;
pluginDecorators?: PluginDecoratorsOptions | false;
pluginTransformRuntime?: Record<string, unknown> | false;
};

export type WebPresetOptions = BasePresetOptions & {
presetEnv: PresetEnvOptions & {
targets: PresetEnvTargets;
useBuiltIns: PresetEnvBuiltIns;
};
pluginTransformRuntime?: Record<string, unknown> | false;
};

export type NodePresetOptions = BasePresetOptions & {
Expand Down
21 changes: 18 additions & 3 deletions packages/babel-preset/src/web.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { join } from 'path';
import { getCoreJsVersion } from '@rsbuild/shared';
import { deepmerge } from '@rsbuild/shared/deepmerge';
import { generateBaseConfig } from './base';
Expand All @@ -6,9 +7,6 @@ import type { BabelConfig, WebPresetOptions } from './types';
export const getBabelConfigForWeb = (options: WebPresetOptions) => {
const mergedOptions = deepmerge(
{
pluginTransformRuntime: {
useESModules: true,
},
presetEnv: {
bugfixes: true,
corejs: options.presetEnv.useBuiltIns
Expand All @@ -25,6 +23,23 @@ export const getBabelConfigForWeb = (options: WebPresetOptions) => {
);

const config = generateBaseConfig(mergedOptions);
const { pluginTransformRuntime = {} } = mergedOptions;

// Skip plugin-transform-runtime when testing
if (pluginTransformRuntime) {
config.plugins?.push([
require.resolve('@babel/plugin-transform-runtime'),
{
version: require('@babel/runtime/package.json').version,
// this option has been deprecated
// but enabling it can help to reduce bundle size
useESModules: true,
...pluginTransformRuntime,
},
]);
}

config.plugins?.push(join(__dirname, './pluginLockCorejsVersion'));

return config;
};
Expand Down
10 changes: 1 addition & 9 deletions packages/babel-preset/tests/__snapshots__/node.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
exports[`should provide node preset as expected 1`] = `
{
"plugins": [
[
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-transform-runtime/lib/index.js",
{
"useESModules": true,
"version": "7.23.1",
},
],
"<ROOT>/packages/babel-preset/src/pluginLockCorejsVersion",
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-export-default-from/lib/index.js",
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-partial-application/lib/index.js",
[
Expand All @@ -28,7 +20,7 @@ exports[`should provide node preset as expected 1`] = `
"exclude": [
"transform-typeof-symbol",
],
"modules": false,
"modules": "commonjs",
"targets": [
"node >= 14",
],
Expand Down
90 changes: 45 additions & 45 deletions packages/babel-preset/tests/__snapshots__/web.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,12 @@
exports[`should allow to enable legacy decorator 1`] = `
{
"plugins": [
[
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-transform-runtime/lib/index.js",
{
"useESModules": true,
"version": "7.23.1",
},
],
[
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-decorators/lib/index.js",
{
"version": "legacy",
},
],
"<ROOT>/packages/babel-preset/src/pluginLockCorejsVersion",
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-export-default-from/lib/index.js",
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-partial-application/lib/index.js",
[
Expand All @@ -25,6 +17,14 @@ exports[`should allow to enable legacy decorator 1`] = `
"proposal": "minimal",
},
],
[
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-transform-runtime/lib/index.js",
{
"useESModules": true,
"version": "7.23.1",
},
],
"<ROOT>/packages/babel-preset/src/pluginLockCorejsVersion",
],
"presets": [
[
Expand All @@ -35,7 +35,7 @@ exports[`should allow to enable legacy decorator 1`] = `
"exclude": [
"transform-typeof-symbol",
],
"modules": false,
"modules": "commonjs",
"targets": [
"Chrome >= 53",
],
Expand All @@ -59,21 +59,13 @@ exports[`should allow to enable legacy decorator 1`] = `
exports[`should allow to enable specific version decorator 1`] = `
{
"plugins": [
[
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-transform-runtime/lib/index.js",
{
"useESModules": true,
"version": "7.23.1",
},
],
[
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-decorators/lib/index.js",
{
"decoratorsBeforeExport": true,
"version": "2018-09",
},
],
"<ROOT>/packages/babel-preset/src/pluginLockCorejsVersion",
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-export-default-from/lib/index.js",
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-partial-application/lib/index.js",
[
Expand All @@ -82,6 +74,14 @@ exports[`should allow to enable specific version decorator 1`] = `
"proposal": "minimal",
},
],
[
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-transform-runtime/lib/index.js",
{
"useESModules": true,
"version": "7.23.1",
},
],
"<ROOT>/packages/babel-preset/src/pluginLockCorejsVersion",
],
"presets": [
[
Expand All @@ -92,7 +92,7 @@ exports[`should allow to enable specific version decorator 1`] = `
"exclude": [
"transform-typeof-symbol",
],
"modules": false,
"modules": "commonjs",
"targets": [
"Chrome >= 53",
],
Expand All @@ -116,14 +116,6 @@ exports[`should allow to enable specific version decorator 1`] = `
exports[`should provide web preset as expected 1`] = `
{
"plugins": [
[
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-transform-runtime/lib/index.js",
{
"useESModules": true,
"version": "7.23.1",
},
],
"<ROOT>/packages/babel-preset/src/pluginLockCorejsVersion",
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-export-default-from/lib/index.js",
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-partial-application/lib/index.js",
[
Expand All @@ -132,6 +124,14 @@ exports[`should provide web preset as expected 1`] = `
"proposal": "minimal",
},
],
[
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-transform-runtime/lib/index.js",
{
"useESModules": true,
"version": "7.23.1",
},
],
"<ROOT>/packages/babel-preset/src/pluginLockCorejsVersion",
],
"presets": [
[
Expand All @@ -142,7 +142,7 @@ exports[`should provide web preset as expected 1`] = `
"exclude": [
"transform-typeof-symbol",
],
"modules": false,
"modules": "commonjs",
"targets": [
"Chrome >= 53",
],
Expand All @@ -166,14 +166,6 @@ exports[`should provide web preset as expected 1`] = `
exports[`should support inject core-js polyfills by entry 1`] = `
{
"plugins": [
[
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-transform-runtime/lib/index.js",
{
"useESModules": true,
"version": "7.23.1",
},
],
"<ROOT>/packages/babel-preset/src/pluginLockCorejsVersion",
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-export-default-from/lib/index.js",
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-partial-application/lib/index.js",
[
Expand All @@ -182,6 +174,14 @@ exports[`should support inject core-js polyfills by entry 1`] = `
"proposal": "minimal",
},
],
[
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-transform-runtime/lib/index.js",
{
"useESModules": true,
"version": "7.23.1",
},
],
"<ROOT>/packages/babel-preset/src/pluginLockCorejsVersion",
],
"presets": [
[
Expand All @@ -195,7 +195,7 @@ exports[`should support inject core-js polyfills by entry 1`] = `
"exclude": [
"transform-typeof-symbol",
],
"modules": false,
"modules": "commonjs",
"targets": [
"Chrome >= 53",
],
Expand All @@ -219,14 +219,6 @@ exports[`should support inject core-js polyfills by entry 1`] = `
exports[`should support inject core-js polyfills by usage 1`] = `
{
"plugins": [
[
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-transform-runtime/lib/index.js",
{
"useESModules": true,
"version": "7.23.1",
},
],
"<ROOT>/packages/babel-preset/src/pluginLockCorejsVersion",
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-export-default-from/lib/index.js",
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-proposal-partial-application/lib/index.js",
[
Expand All @@ -235,6 +227,14 @@ exports[`should support inject core-js polyfills by usage 1`] = `
"proposal": "minimal",
},
],
[
"<ROOT>/node_modules/<PNPM_INNER>/@babel/plugin-transform-runtime/lib/index.js",
{
"useESModules": true,
"version": "7.23.1",
},
],
"<ROOT>/packages/babel-preset/src/pluginLockCorejsVersion",
],
"presets": [
[
Expand All @@ -248,7 +248,7 @@ exports[`should support inject core-js polyfills by usage 1`] = `
"exclude": [
"transform-typeof-symbol",
],
"modules": false,
"modules": "commonjs",
"targets": [
"Chrome >= 53",
],
Expand Down

0 comments on commit fd18cc3

Please sign in to comment.