diff --git a/docs/generated/packages/webpack.json b/docs/generated/packages/webpack.json index 1981daf246..f086adfa65 100644 --- a/docs/generated/packages/webpack.json +++ b/docs/generated/packages/webpack.json @@ -146,7 +146,8 @@ }, "target": { "type": "string", - "description": "Target platform for the build, same as the Webpack config option.", + "alias": "platform", + "description": "Target platform for the build, same as the Webpack target option.", "enum": ["node", "web"], "default": "web" }, diff --git a/e2e/webpack/src/webpack.test.ts b/e2e/webpack/src/webpack.test.ts index d3d0154b4e..844dc27e6a 100644 --- a/e2e/webpack/src/webpack.test.ts +++ b/e2e/webpack/src/webpack.test.ts @@ -1,6 +1,7 @@ import { cleanupProject, newProject, + readFile, rmDist, runCLI, runCommand, @@ -55,4 +56,27 @@ describe('Webpack Plugin', () => { output = runCommand(`node dist/libs/${myPkg}/main.js`); expect(output).toMatch(/Hello/); }, 500000); + + it('should define process.env variables only for --platform=web', async () => { + const myPkg = uniq('my-pkg'); + runCLI(`generate @nrwl/js:lib ${myPkg} --bundler=webpack`); + updateFile( + `libs/${myPkg}/src/index.ts`, + `console.log(process.env['NX_TEST_VAR']);\n` + ); + + process.env.NX_TEST_VAR = 'Hello build time'; + runCLI(`build ${myPkg} --platform=node`); + + process.env.NX_TEST_VAR = 'Hello run time'; + expect(runCommand(`node dist/libs/${myPkg}/main.js`)).toMatch( + /Hello run time/ + ); + + process.env.NX_TEST_VAR = 'Hello build time'; + runCLI(`build ${myPkg} --platform=web`); + + expect(readFile(`dist/libs/${myPkg}/main.js`)).toMatch(/Hello build time/); + delete process.env.NX_TEST_VAR; + }, 300_000); }); diff --git a/packages/js/src/generators/library/files/lib/tsconfig.lib.json__tmpl__ b/packages/js/src/generators/library/files/lib/tsconfig.lib.json__tmpl__ index 15a16deae4..9143d881df 100644 --- a/packages/js/src/generators/library/files/lib/tsconfig.lib.json__tmpl__ +++ b/packages/js/src/generators/library/files/lib/tsconfig.lib.json__tmpl__ @@ -3,7 +3,7 @@ "compilerOptions": { "outDir": "<%= offsetFromRoot %>dist/out-tsc", "declaration": true, - "types": [] + "types": ["node"] }, "include": ["**/*.ts"<% if (js) { %>, "**/*.js"<% } %>], "exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"<% if (js) { %>, "**/*.spec.js", "**/*.test.js"<% } %>] diff --git a/packages/js/src/generators/library/library.ts b/packages/js/src/generators/library/library.ts index 454934888d..9305403b21 100644 --- a/packages/js/src/generators/library/library.ts +++ b/packages/js/src/generators/library/library.ts @@ -29,7 +29,7 @@ import { addMinimalPublishScript } from '../../utils/minimal-publish-script'; import { LibraryGeneratorSchema } from '../../utils/schema'; import { addSwcConfig } from '../../utils/swc/add-swc-config'; import { addSwcDependencies } from '../../utils/swc/add-swc-dependencies'; -import { nxVersion } from '../../utils/versions'; +import { nxVersion, typesNodeVersion } from '../../utils/versions'; export async function libraryGenerator( tree: Tree, @@ -58,6 +58,10 @@ export async function projectGenerator( updateRootTsConfig(tree, options); } + if (schema.bundler === 'webpack' || schema.bundler === 'rollup') { + ensureBabelRootConfigExists(tree); + } + if (options.linter !== 'none') { const lintCallback = await addLint(tree, options); tasks.push(lintCallback); @@ -108,7 +112,11 @@ function addProject( outputPath, main: `${options.projectRoot}/src/index` + (options.js ? '.js' : '.ts'), tsConfig: `${options.projectRoot}/tsconfig.lib.json`, - assets: [`${options.projectRoot}/*.md`], + // TODO(jack): assets for webpack and rollup have validation that we need to fix (assets must be under /src) + assets: + options.bundler === 'webpack' || options.bundler === 'rollup' + ? [] + : [`${options.projectRoot}/*.md`], }, }; @@ -419,7 +427,7 @@ function addProjectDependencies( return addDependenciesToPackageJson( tree, {}, - { '@nrwl/esbuild': nxVersion } + { '@nrwl/esbuild': nxVersion, '@types/node': typesNodeVersion } ); } @@ -427,7 +435,7 @@ function addProjectDependencies( return addDependenciesToPackageJson( tree, {}, - { '@nrwl/rollup': nxVersion } + { '@nrwl/rollup': nxVersion, '@types/node': typesNodeVersion } ); } @@ -435,7 +443,7 @@ function addProjectDependencies( return addDependenciesToPackageJson( tree, {}, - { '@nrwl/webpack': nxVersion } + { '@nrwl/webpack': nxVersion, '@types/node': typesNodeVersion } ); } @@ -456,5 +464,13 @@ function getBuildExecutor(options: NormalizedSchema) { } } +function ensureBabelRootConfigExists(tree: Tree) { + if (tree.exists('babel.config.json')) return; + + writeJson(tree, 'babel.config.json', { + babelrcRoots: ['*'], + }); +} + export default libraryGenerator; export const librarySchematic = convertNxGenerator(libraryGenerator); diff --git a/packages/js/src/utils/versions.ts b/packages/js/src/utils/versions.ts index d0b49fd710..fdd177db2b 100644 --- a/packages/js/src/utils/versions.ts +++ b/packages/js/src/utils/versions.ts @@ -4,3 +4,4 @@ export const nxVersion = require('../../package.json').version; export const swcCliVersion = '~0.1.55'; export const swcHelpersVersion = '~0.3.3'; +export const typesNodeVersion = '18.7.1'; diff --git a/packages/webpack/src/executors/webpack/schema.json b/packages/webpack/src/executors/webpack/schema.json index e210c466f6..03fa463654 100644 --- a/packages/webpack/src/executors/webpack/schema.json +++ b/packages/webpack/src/executors/webpack/schema.json @@ -33,7 +33,8 @@ }, "target": { "type": "string", - "description": "Target platform for the build, same as the Webpack config option.", + "alias": "platform", + "description": "Target platform for the build, same as the Webpack target option.", "enum": ["node", "web"], "default": "web" },