diff --git a/e2e/node/src/node-server.test.ts b/e2e/node/src/node-server.test.ts index 94e02db3da0cc..25c18b45bc91e 100644 --- a/e2e/node/src/node-server.test.ts +++ b/e2e/node/src/node-server.test.ts @@ -64,7 +64,7 @@ describe('Node Applications + webpack', () => { const expressApp = uniq('expressapp'); const fastifyApp = uniq('fastifyapp'); const koaApp = uniq('koaapp'); - const nestApp = uniq('koaapp'); + const nestApp = uniq('nest'); runCLI(`generate @nx/node:lib ${testLib1}`); runCLI(`generate @nx/node:lib ${testLib2} --importPath=@acme/test2`); @@ -98,7 +98,22 @@ describe('Node Applications + webpack', () => { expect(() => runCLI(`lint ${nestApp}-e2e`)).not.toThrow(); // 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, testLib2, '@acme/test2'); diff --git a/packages/webpack/src/utils/with-nx.ts b/packages/webpack/src/utils/with-nx.ts index b8e0bfb9bfa18..870b6026e0c1b 100644 --- a/packages/webpack/src/utils/with-nx.ts +++ b/packages/webpack/src/utils/with-nx.ts @@ -139,16 +139,20 @@ export function withNx(pluginOptions?: WithNxOptions): NxWebpackPlugin { : undefined, target: options.target, node: false as const, - // When mode is development or production, webpack will automatically - // 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 - // avoid conflicts. - // - // When the NODE_ENV is something else (e.g. test), then set it to none - // to prevent extra behavior from webpack. mode: - process.env.NODE_ENV === 'development' || - process.env.NODE_ENV === 'production' + // 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 + // 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 + // avoid conflicts. + // + // When the NODE_ENV is something else (e.g. test), then set it to none + // to prevent extra behavior from webpack. + process.env.NODE_ENV === 'development' || + process.env.NODE_ENV === 'production' ? (process.env.NODE_ENV as 'development' | 'production') : ('none' as const), devtool: