fix(webpack): allow optimization to be turned off via CLI (#14431)

This commit is contained in:
Jack Hsu 2023-01-17 11:55:14 -05:00 committed by GitHub
parent 2773f40202
commit da8b855c6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View File

@ -1,8 +1,8 @@
import { import {
checkFilesDoNotExist,
checkFilesExist, checkFilesExist,
cleanupProject, cleanupProject,
newProject, newProject,
readFile,
runCLI, runCLI,
runCLIAsync, runCLIAsync,
tmpProjPath, tmpProjPath,
@ -10,6 +10,7 @@ import {
updateFile, updateFile,
} from '@nrwl/e2e/utils'; } from '@nrwl/e2e/utils';
import { execSync } from 'child_process'; import { execSync } from 'child_process';
import { read } from 'fs-extra';
describe('Node Applications + webpack', () => { describe('Node Applications + webpack', () => {
beforeEach(() => newProject()); beforeEach(() => newProject());
@ -23,13 +24,29 @@ describe('Node Applications + webpack', () => {
checkFilesExist(`apps/${app}/webpack.config.js`); 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}`); await runCLIAsync(`build ${app}`);
checkFilesExist(`dist/apps/${app}/main.js`); 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`, { const result = execSync(`node dist/apps/${app}/main.js`, {
cwd: tmpProjPath(), cwd: tmpProjPath(),
}).toString(); }).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); }, 300_000);
}); });

View File

@ -185,7 +185,10 @@ export function withNx(opts?: { skipTypeChecking?: boolean }) {
optimization: { optimization: {
...config.optimization, ...config.optimization,
sideEffects: true, sideEffects: true,
minimize: !!options.optimization, minimize:
typeof options.optimization === 'object'
? !!options.optimization.scripts
: !!options.optimization,
minimizer: [ minimizer: [
options.compiler !== 'swc' options.compiler !== 'swc'
? new TerserPlugin({ ? new TerserPlugin({