Skip to content

Commit f1ffb10

Browse files
alan-agius4mgechev
authored andcommitted
fix(@angular-devkit/build-angular): emit error when a script is not found
While we currently invoke the `callback` with the error in https://github.com/angular/angular-cli/blob/d4f1ff82c5d994aca68c39ff31f5d8b22e694b87/packages/angular_devkit/build_angular/src/angular-cli-files/plugins/scripts-webpack-plugin.ts#L163 this is not bubbled up to the main webpack compilation due to the usage of `thisCompilation`. Closes #16659
1 parent e368533 commit f1ffb10

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/common.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from '@angular-devkit/build-optimizer';
1212
import { tags } from '@angular-devkit/core';
1313
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
14+
import { existsSync } from 'fs';
1415
import * as path from 'path';
1516
import { RollupOptions } from 'rollup';
1617
import { ScriptTarget } from 'typescript';
@@ -214,23 +215,28 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
214215
buildOptions.scripts,
215216
'scripts',
216217
).reduce((prev: { bundleName: string; paths: string[]; inject: boolean }[], curr) => {
217-
const bundleName = curr.bundleName;
218-
const resolvedPath = path.resolve(root, curr.input);
218+
const { bundleName, inject, input } = curr;
219+
const resolvedPath = path.resolve(root, input);
220+
221+
if (!existsSync(resolvedPath)) {
222+
throw new Error(`Script file ${input} does not exist.`);
223+
}
224+
219225
const existingEntry = prev.find(el => el.bundleName === bundleName);
220226
if (existingEntry) {
221-
if (existingEntry.inject && !curr.inject) {
227+
if (existingEntry.inject && !inject) {
222228
// All entries have to be lazy for the bundle to be lazy.
223229
throw new Error(
224-
`The ${curr.bundleName} bundle is mixing injected and non-injected scripts.`,
230+
`The ${bundleName} bundle is mixing injected and non-injected scripts.`,
225231
);
226232
}
227233

228234
existingEntry.paths.push(resolvedPath);
229235
} else {
230236
prev.push({
231237
bundleName,
238+
inject,
232239
paths: [resolvedPath],
233-
inject: curr.inject,
234240
});
235241
}
236242

packages/angular_devkit/build_angular/test/browser/scripts-array_spec_large.ts

+12
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,16 @@ describe('Browser Builder scripts array', () => {
175175
expect(logs.join('\n')).toMatch(/\(renamed-script\) 78 bytes.*\[entry].*\[rendered]/);
176176
expect(logs.join('\n')).toMatch(/\(renamed-lazy-script\) 88 bytes.*\[entry].*\[rendered]/);
177177
});
178+
179+
it(`should error when a script doesn't exist`, async () => {
180+
await expectAsync(browserBuild(
181+
architect,
182+
host,
183+
target,
184+
{
185+
scripts: ['./invalid.js'],
186+
},
187+
))
188+
.toBeRejectedWithError(`Script file ./invalid.js does not exist.`);
189+
});
178190
});

0 commit comments

Comments
 (0)