Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(js): move babel preset to @nrwl/js/babel so @nrwl/web is not… #14478

Merged
merged 1 commit into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e/js/src/js-tsc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('js e2e', () => {
const babelRc = readJson(`libs/${lib}/.babelrc`);
expect(babelRc.plugins).toBeUndefined();
expect(babelRc.presets).toStrictEqual([
['@nrwl/web/babel', { useBuiltIns: 'usage' }],
['@nrwl/js/babel', { useBuiltIns: 'usage' }],
]);

expect(runCLI(`build ${lib}`)).toContain('Done compiling TypeScript files');
Expand Down
4 changes: 2 additions & 2 deletions packages/jest/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ function updateDependencies(tree: Tree, options: NormalizedSchema) {

if (options.compiler === 'babel' || options.babelJest) {
devDeps['babel-jest'] = babelJestVersion;
// in some cases @nrwl/web will not already be present i.e. node only projects
devDeps['@nrwl/web'] = nxVersion;
// in some cases @nrwl/js will not already be present i.e. node only projects
devDeps['@nrwl/js'] = nxVersion;
} else if (options.compiler === 'swc') {
devDeps['@swc/jest'] = swcJestVersion;
}
Expand Down
110 changes: 110 additions & 0 deletions packages/js/babel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { dirname } from 'path';

/*
* Babel preset to provide TypeScript support and module/nomodule for Nx.
*/

export interface NxWebBabelPresetOptions {
useBuiltIns?: boolean | string;
decorators?: {
decoratorsBeforeExport?: boolean;
legacy?: boolean;
};
classProperties?: {
loose?: boolean;
};
}

module.exports = function (api: any, options: NxWebBabelPresetOptions = {}) {
api.assertVersion(7);

const isModern = api.caller((caller) => caller?.isModern);

// This is set by `@nrwl/web:rollup` executor
const isNxPackage = api.caller((caller) => caller?.isNxPackage);

const emitDecoratorMetadata = api.caller(
(caller) => caller?.emitDecoratorMetadata ?? true
);

// Determine settings for `@babel/plugin-proposal-class-properties`,
// so that we can sync the `loose` option with `@babel/preset-env`.
const classProperties = options.classProperties ?? { loose: true };

return {
presets: [
// Support module/nomodule pattern.
[
require.resolve('@babel/preset-env'),
// For Jest tests, NODE_ENV is set as 'test' and we only want to set target as Node.
// All other options will fail in Jest since Node does not support some ES features
// such as import syntax.
process.env.NODE_ENV === 'test'
? { targets: { node: 'current' }, loose: true }
: {
// Allow importing core-js in entrypoint and use browserlist to select polyfills.
useBuiltIns: options.useBuiltIns ?? 'entry',
corejs: 3,
// Do not transform modules to CJS
modules: false,
targets: isModern ? { esmodules: 'intersect' } : undefined,
bugfixes: true,
// Exclude transforms that make all code slower
exclude: ['transform-typeof-symbol'],
// This must match the setting for `@babel/plugin-proposal-class-properties`
loose: classProperties.loose,
},
],
[
require.resolve('@babel/preset-typescript'),
{
allowDeclareFields: true,
},
],
],
plugins: [
!isNxPackage
? [
require.resolve('@babel/plugin-transform-runtime'),
{
corejs: false,
helpers: true,
regenerator: true,
useESModules: isModern,
absoluteRuntime: dirname(
require.resolve('@babel/runtime/package.json')
),
},
]
: null,
require.resolve('babel-plugin-macros'),
emitDecoratorMetadata
? require.resolve('babel-plugin-transform-typescript-metadata')
: undefined,
// Must use legacy decorators to remain compatible with TypeScript.
[
require.resolve('@babel/plugin-proposal-decorators'),
options.decorators ?? { legacy: true },
],
[
require.resolve('@babel/plugin-proposal-class-properties'),
classProperties,
],
].filter(Boolean),
overrides: [
// Convert `const enum` to `enum`. The former cannot be supported by babel
// but at least we can get it to not error out.
{
test: /\.tsx?$/,
plugins: [
[
require.resolve('babel-plugin-const-enum'),
{
transform: 'removeConst',
},
],
],
},
],
};
};
10 changes: 10 additions & 0 deletions packages/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,19 @@
"executors": "./executors.json",
"builders": "./executors.json",
"dependencies": {
"@babel/core": "^7.15.0",
"@babel/plugin-proposal-class-properties": "^7.14.5",
"@babel/plugin-proposal-decorators": "^7.14.5",
"@babel/plugin-transform-runtime": "^7.15.0",
"@babel/preset-env": "^7.15.0",
"@babel/preset-typescript": "^7.15.0",
"@babel/runtime": "^7.14.8",
"@nrwl/devkit": "file:../devkit",
"@nrwl/linter": "file:../linter",
"@nrwl/workspace": "file:../workspace",
"babel-plugin-const-enum": "^1.0.1",
"babel-plugin-macros": "^2.8.0",
"babel-plugin-transform-typescript-metadata": "^0.3.1",
"chalk": "^4.1.0",
"fast-glob": "3.2.7",
"fs-extra": "^11.1.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/js/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ describe('lib', () => {
Object {
"presets": Array [
Array [
"@nrwl/web/babel",
"@nrwl/js/babel",
Object {
"useBuiltIns": "usage",
},
Expand Down Expand Up @@ -950,7 +950,7 @@ describe('lib', () => {
Object {
"presets": Array [
Array [
"@nrwl/web/babel",
"@nrwl/js/babel",
Object {
"useBuiltIns": "usage",
},
Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ function addBabelRc(tree: Tree, options: NormalizedSchema) {
const filename = '.babelrc';

const babelrc = {
presets: [['@nrwl/web/babel', { useBuiltIns: 'usage' }]],
presets: [['@nrwl/js/babel', { useBuiltIns: 'usage' }]],
};

writeJson(tree, join(options.projectRoot, filename), babelrc);
Expand Down
14 changes: 10 additions & 4 deletions packages/react/babel.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import type { NxWebBabelPresetOptions } from '@nrwl/web';

/*
* Babel preset to provide React support for Nx.
*/

interface NxReactBabelOptions extends NxWebBabelPresetOptions {
interface NxReactBabelOptions {
runtime?: string;
importSource?: string;
useBuiltIns?: boolean | string;
decorators?: {
decoratorsBeforeExport?: boolean;
legacy?: boolean;
};
classProperties?: {
loose?: boolean;
};
}

module.exports = function (api: any, options: NxReactBabelOptions) {
Expand All @@ -17,7 +23,7 @@ module.exports = function (api: any, options: NxReactBabelOptions) {
*/
const isNextJs = api.caller((caller) => caller?.pagesDir);

const presets: any[] = [['@nrwl/web/babel', options]];
const presets: any[] = [['@nrwl/js/babel', options]];

/**
* Next.js already includes the preset-react, and including it
Expand Down
2 changes: 1 addition & 1 deletion packages/rollup/src/executors/rollup/rollup.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export function createRollupOptions(
useSwc && swc(),
useBabel &&
getBabelInputPlugin({
// Let's `@nrwl/web/babel` preset know that we are packaging.
// Lets `@nrwl/js/babel` preset know that we are packaging.
caller: {
// @ts-ignore
// Ignoring type checks for caller since we have custom attributes
Expand Down
113 changes: 6 additions & 107 deletions packages/web/babel.ts
Original file line number Diff line number Diff line change
@@ -1,110 +1,9 @@
import { dirname } from 'path';
const nxJsBabelPreset = require('@nrwl/js/babel');

/*
* Babel preset to provide TypeScript support and module/nomodule for Nx.
*/

export interface NxWebBabelPresetOptions {
useBuiltIns?: boolean | string;
decorators?: {
decoratorsBeforeExport?: boolean;
legacy?: boolean;
};
classProperties?: {
loose?: boolean;
};
}

module.exports = function (api: any, options: NxWebBabelPresetOptions = {}) {
api.assertVersion(7);

const isModern = api.caller((caller) => caller?.isModern);

// This is set by `@nrwl/web:rollup` executor
const isNxPackage = api.caller((caller) => caller?.isNxPackage);

const emitDecoratorMetadata = api.caller(
(caller) => caller?.emitDecoratorMetadata ?? true
/** @deprecated Use `@nrwl/js/babel`. */
module.exports = function (api: any, options: any = {}) {
console.warn(
'`@nrwl/web/babel` has been deprecated. Use `@nrwl/js/babel` instead in your .babelrc files.'
);

// Determine settings for `@babel/plugin-proposal-class-properties`,
// so that we can sync the `loose` option with `@babel/preset-env`.
const classProperties = options.classProperties ?? { loose: true };

return {
presets: [
// Support module/nomodule pattern.
[
require.resolve('@babel/preset-env'),
// For Jest tests, NODE_ENV is set as 'test' and we only want to set target as Node.
// All other options will fail in Jest since Node does not support some ES features
// such as import syntax.
process.env.NODE_ENV === 'test'
? { targets: { node: 'current' }, loose: true }
: {
// Allow importing core-js in entrypoint and use browserlist to select polyfills.
useBuiltIns: options.useBuiltIns ?? 'entry',
corejs: 3,
// Do not transform modules to CJS
modules: false,
targets: isModern ? { esmodules: 'intersect' } : undefined,
bugfixes: true,
// Exclude transforms that make all code slower
exclude: ['transform-typeof-symbol'],
// This must match the setting for `@babel/plugin-proposal-class-properties`
loose: classProperties.loose,
},
],
[
require.resolve('@babel/preset-typescript'),
{
allowDeclareFields: true,
},
],
],
plugins: [
!isNxPackage
? [
require.resolve('@babel/plugin-transform-runtime'),
{
corejs: false,
helpers: true,
regenerator: true,
useESModules: isModern,
absoluteRuntime: dirname(
require.resolve('@babel/runtime/package.json')
),
},
]
: null,
require.resolve('babel-plugin-macros'),
emitDecoratorMetadata
? require.resolve('babel-plugin-transform-typescript-metadata')
: undefined,
// Must use legacy decorators to remain compatible with TypeScript.
[
require.resolve('@babel/plugin-proposal-decorators'),
options.decorators ?? { legacy: true },
],
[
require.resolve('@babel/plugin-proposal-class-properties'),
classProperties,
],
].filter(Boolean),
overrides: [
// Convert `const enum` to `enum`. The former cannot be supported by babel
// but at least we can get it to not error out.
{
test: /\.tsx?$/,
plugins: [
[
require.resolve('babel-plugin-const-enum'),
{
transform: 'removeConst',
},
],
],
},
],
};
return nxJsBabelPreset(api, options);
};
1 change: 0 additions & 1 deletion packages/web/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { webInitGenerator } from './src/generators/init/init';
export { applicationGenerator } from './src/generators/application/application';
export type { NxWebBabelPresetOptions } from './babel';
6 changes: 6 additions & 0 deletions packages/web/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
"version": "15.0.0-beta.1",
"description": "Update usages of rollup executors to @nrwl/rollup",
"factory": "./src/migrations/update-15-0-0/update-rollup-executor"
},
"update-babel-preset": {
"cli": "nx",
"version": "15.5.4-beta.0",
"description": "Update `@nrwl/web/babel` preset to `@nrwl/js/babel` for projects that have a .babelrc file.",
"factory": "./src/migrations/update-15-5-4/update-babel-preset"
}
},
"packageJsonUpdates": {
Expand Down
10 changes: 0 additions & 10 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,13 @@
"migrations": "./migrations.json"
},
"dependencies": {
"@babel/core": "^7.15.0",
"@babel/plugin-proposal-class-properties": "^7.14.5",
"@babel/plugin-proposal-decorators": "^7.14.5",
"@babel/plugin-transform-runtime": "^7.15.0",
"@babel/preset-env": "^7.15.0",
"@babel/preset-typescript": "^7.15.0",
"@babel/runtime": "^7.14.8",
"@nrwl/cypress": "file:../cypress",
"@nrwl/devkit": "file:../devkit",
"@nrwl/jest": "file:../jest",
"@nrwl/js": "file:../js",
"@nrwl/linter": "file:../linter",
"@nrwl/rollup": "file:../rollup",
"@nrwl/workspace": "file:../workspace",
"babel-plugin-const-enum": "^1.0.1",
"babel-plugin-macros": "^2.8.0",
"babel-plugin-transform-typescript-metadata": "^0.3.1",
"chalk": "^4.1.0",
"chokidar": "^3.5.1",
"http-server": "^14.1.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"presets": [
"@nrwl/web/babel"
"@nrwl/js/babel"
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"presets": [
"@nrwl/web/babel"
"@nrwl/js/babel"
]
}
Loading