From 8c341add25c245832a6d504c5597a20fdf59466e Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Tue, 17 Jan 2023 11:08:52 -0500 Subject: [PATCH] fix(webpack): allow optimization to be turned off via CLI --- e2e/node/src/node-webpack.test.ts | 23 ++++++++++++++++++++--- packages/webpack/src/utils/with-nx.ts | 5 ++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/e2e/node/src/node-webpack.test.ts b/e2e/node/src/node-webpack.test.ts index 6a4dd84a70b1a..7e6e18eef2785 100644 --- a/e2e/node/src/node-webpack.test.ts +++ b/e2e/node/src/node-webpack.test.ts @@ -1,8 +1,8 @@ import { - checkFilesDoNotExist, checkFilesExist, cleanupProject, newProject, + readFile, runCLI, runCLIAsync, tmpProjPath, @@ -10,6 +10,7 @@ import { updateFile, } from '@nrwl/e2e/utils'; import { execSync } from 'child_process'; +import { read } from 'fs-extra'; describe('Node Applications + webpack', () => { beforeEach(() => newProject()); @@ -23,13 +24,29 @@ describe('Node Applications + webpack', () => { checkFilesExist(`apps/${app}/webpack.config.js`); - updateFile(`apps/${app}/src/main.ts`, `console.log('Hello World!');`); + updateFile( + `apps/${app}/src/main.ts`, + ` + function foo(x: string) { + return "foo " + x; + }; + console.log(foo("bar")); + ` + ); await runCLIAsync(`build ${app}`); checkFilesExist(`dist/apps/${app}/main.js`); + // no optimization by default + const content = readFile(`dist/apps/${app}/main.js`); + expect(content).toContain('console.log(foo("bar"))'); + const result = execSync(`node dist/apps/${app}/main.js`, { cwd: tmpProjPath(), }).toString(); - expect(result).toMatch(/Hello World!/); + expect(result).toMatch(/foo bar/); + + await runCLIAsync(`build ${app} --optimization`); + const optimizedContent = readFile(`dist/apps/${app}/main.js`); + expect(optimizedContent).toContain('console.log("foo "+"bar")'); }, 300_000); }); diff --git a/packages/webpack/src/utils/with-nx.ts b/packages/webpack/src/utils/with-nx.ts index f421a0209060f..6b4f29b6a67b9 100644 --- a/packages/webpack/src/utils/with-nx.ts +++ b/packages/webpack/src/utils/with-nx.ts @@ -185,7 +185,10 @@ export function withNx(opts?: { skipTypeChecking?: boolean }) { optimization: { ...config.optimization, sideEffects: true, - minimize: !!options.optimization, + minimize: + typeof options.optimization === 'object' + ? !!options.optimization.scripts + : !!options.optimization, minimizer: [ options.compiler !== 'swc' ? new TerserPlugin({