Skip to content

Commit

Permalink
fix(node): default webpack build to not perform default optimizations…
Browse files Browse the repository at this point in the history
… for Node (#16625)
  • Loading branch information
jaysoo authored Apr 27, 2023
1 parent 95c0fad commit b3d07a8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
19 changes: 17 additions & 2 deletions e2e/node/src/node-server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down Expand Up @@ -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');
Expand Down
22 changes: 13 additions & 9 deletions packages/webpack/src/utils/with-nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

1 comment on commit b3d07a8

@vercel
Copy link

@vercel vercel bot commented on b3d07a8 Apr 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-five.vercel.app

Please sign in to comment.