Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): emit error when a script is not f…
Browse files Browse the repository at this point in the history
…ound

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
  • Loading branch information
alan-agius4 authored and mgechev committed Jan 15, 2020
1 parent e368533 commit f1ffb10
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@angular-devkit/build-optimizer';
import { tags } from '@angular-devkit/core';
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
import { existsSync } from 'fs';
import * as path from 'path';
import { RollupOptions } from 'rollup';
import { ScriptTarget } from 'typescript';
Expand Down Expand Up @@ -214,23 +215,28 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
buildOptions.scripts,
'scripts',
).reduce((prev: { bundleName: string; paths: string[]; inject: boolean }[], curr) => {
const bundleName = curr.bundleName;
const resolvedPath = path.resolve(root, curr.input);
const { bundleName, inject, input } = curr;
const resolvedPath = path.resolve(root, input);

if (!existsSync(resolvedPath)) {
throw new Error(`Script file ${input} does not exist.`);
}

const existingEntry = prev.find(el => el.bundleName === bundleName);
if (existingEntry) {
if (existingEntry.inject && !curr.inject) {
if (existingEntry.inject && !inject) {
// All entries have to be lazy for the bundle to be lazy.
throw new Error(
`The ${curr.bundleName} bundle is mixing injected and non-injected scripts.`,
`The ${bundleName} bundle is mixing injected and non-injected scripts.`,
);
}

existingEntry.paths.push(resolvedPath);
} else {
prev.push({
bundleName,
inject,
paths: [resolvedPath],
inject: curr.inject,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,16 @@ describe('Browser Builder scripts array', () => {
expect(logs.join('\n')).toMatch(/\(renamed-script\) 78 bytes.*\[entry].*\[rendered]/);
expect(logs.join('\n')).toMatch(/\(renamed-lazy-script\) 88 bytes.*\[entry].*\[rendered]/);
});

it(`should error when a script doesn't exist`, async () => {
await expectAsync(browserBuild(
architect,
host,
target,
{
scripts: ['./invalid.js'],
},
))
.toBeRejectedWithError(`Script file ./invalid.js does not exist.`);
});
});

0 comments on commit f1ffb10

Please sign in to comment.