fix(node): default webpack build to not perform default optimizations for Node (#16625)

This commit is contained in:
Jack Hsu 2023-04-27 18:59:22 -04:00 committed by GitHub
parent 95c0fad6bf
commit b3d07a8293
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 11 deletions

View File

@ -64,7 +64,7 @@ describe('Node Applications + webpack', () => {
const expressApp = uniq('expressapp'); const expressApp = uniq('expressapp');
const fastifyApp = uniq('fastifyapp'); const fastifyApp = uniq('fastifyapp');
const koaApp = uniq('koaapp'); const koaApp = uniq('koaapp');
const nestApp = uniq('koaapp'); const nestApp = uniq('nest');
runCLI(`generate @nx/node:lib ${testLib1}`); runCLI(`generate @nx/node:lib ${testLib1}`);
runCLI(`generate @nx/node:lib ${testLib2} --importPath=@acme/test2`); runCLI(`generate @nx/node:lib ${testLib2} --importPath=@acme/test2`);
@ -98,7 +98,22 @@ describe('Node Applications + webpack', () => {
expect(() => runCLI(`lint ${nestApp}-e2e`)).not.toThrow(); expect(() => runCLI(`lint ${nestApp}-e2e`)).not.toThrow();
// Only Fastify generates with unit tests since it supports them without additional libraries. // Only Fastify generates with unit tests since it supports them without additional libraries.
expect(() => runCLI(`lint ${fastifyApp}`)).not.toThrow(); expect(() => runCLI(`test ${fastifyApp}`)).not.toThrow();
// https://github.com/nrwl/nx/issues/16601
const nestMainContent = readFile(`apps/${nestApp}/src/main.ts`);
updateFile(
`apps/${nestApp}/src/main.ts`,
`
${nestMainContent}
// Make sure this is not replaced during build time
console.log('env: ' + process.env['NODE_ENV']);
`
);
runCLI(`build ${nestApp}`);
expect(readFile(`dist/apps/${nestApp}/main.js`)).toContain(
`'env: ' + process.env['NODE_ENV']`
);
addLibImport(expressApp, testLib1); addLibImport(expressApp, testLib1);
addLibImport(expressApp, testLib2, '@acme/test2'); addLibImport(expressApp, testLib2, '@acme/test2');

View File

@ -139,6 +139,11 @@ export function withNx(pluginOptions?: WithNxOptions): NxWebpackPlugin {
: undefined, : undefined,
target: options.target, target: options.target,
node: false as const, node: false as const,
mode:
// When the target is Node avoid any optimizations, such as replacing `process.env.NODE_ENV` with build time value.
options.target === ('node' as const)
? 'none'
: // Otherwise, make sure it matches `process.env.NODE_ENV`.
// When mode is development or production, webpack will automatically // When mode is development or production, webpack will automatically
// configure DefinePlugin to replace `process.env.NODE_ENV` with the // configure DefinePlugin to replace `process.env.NODE_ENV` with the
// build-time value. Thus, we need to make sure it's the same value to // build-time value. Thus, we need to make sure it's the same value to
@ -146,7 +151,6 @@ export function withNx(pluginOptions?: WithNxOptions): NxWebpackPlugin {
// //
// When the NODE_ENV is something else (e.g. test), then set it to none // When the NODE_ENV is something else (e.g. test), then set it to none
// to prevent extra behavior from webpack. // to prevent extra behavior from webpack.
mode:
process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'development' ||
process.env.NODE_ENV === 'production' process.env.NODE_ENV === 'production'
? (process.env.NODE_ENV as 'development' | 'production') ? (process.env.NODE_ENV as 'development' | 'production')