-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js): move babel preset to
@nrwl/js/babel
so @nrwl/web
is not…
… required for JS projects (e.g. React, Node)
- Loading branch information
Showing
17 changed files
with
266 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
], | ||
], | ||
}, | ||
], | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/web/src/generators/application/files/app-vite/.babelrc__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"presets": [ | ||
"@nrwl/web/babel" | ||
"@nrwl/js/babel" | ||
] | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/web/src/generators/application/files/app-webpack/.babelrc__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"presets": [ | ||
"@nrwl/web/babel" | ||
"@nrwl/js/babel" | ||
] | ||
} |
Oops, something went wrong.