Skip to content

Commit 3f6769e

Browse files
committedFeb 16, 2023
fix(@angular-devkit/build-angular): allow empty scripts to be optimized
When using the internal JavaScript optimizer plugin for Webpack with an empty script file provided via the `scripts` option, the build would fail. This was because of a safety check that was checking whether the terser result was falsy. Since an empty string is considered falsy, the build considered the result to be an error. The safety check now will only trigger if the terser result is not a string value to avoid this case. (cherry picked from commit 2435b46)
1 parent 6498328 commit 3f6769e

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed
 

‎packages/angular_devkit/build_angular/src/builders/browser/tests/options/scripts_spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => {
2727
expect(result?.success).toBe(true);
2828
});
2929

30+
it('processes an empty script when optimizing', async () => {
31+
await harness.writeFile('src/test-script-a.js', '');
32+
33+
harness.useTarget('build', {
34+
...BASE_OPTIONS,
35+
optimization: {
36+
scripts: true,
37+
},
38+
scripts: ['src/test-script-a.js'],
39+
});
40+
41+
const { result } = await harness.executeOnce();
42+
43+
expect(result?.success).toBe(true);
44+
45+
harness.expectFile('dist/scripts.js').toExist();
46+
harness
47+
.expectFile('dist/index.html')
48+
.content.toContain('<script src="scripts.js" defer></script>');
49+
});
50+
3051
describe('shorthand syntax', () => {
3152
it('processes a single script into a single output', async () => {
3253
await harness.writeFile('src/test-script-a.js', 'console.log("a");');

‎packages/angular_devkit/build_angular/src/webpack/plugins/javascript-optimizer-worker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ async function optimizeWithTerser(
227227
},
228228
);
229229

230-
if (!result.code) {
230+
if (typeof result.code !== 'string') {
231231
throw new Error('Terser failed for unknown reason.');
232232
}
233233

0 commit comments

Comments
 (0)
Please sign in to comment.