Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@angular-devkit/build-angular): display actionable error when a style does not exist in Karma builder #24418

Merged
merged 1 commit into from Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -16,7 +16,7 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
'src/styles.css': 'p {display: none}',
'src/app/app.component.ts': `
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: '<p>Hello World</p>'
Expand All @@ -27,7 +27,7 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
'src/app/app.component.spec.ts': `
import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
Expand All @@ -38,7 +38,7 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
]
}).compileComponents();
});

it('should not contain text that is hidden via css', () => {
const fixture = TestBed.createComponent(AppComponent);
expect(fixture.nativeElement.innerText).not.toContain('Hello World');
Expand Down Expand Up @@ -129,5 +129,22 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
});

it('fails and shows an error if style does not exist', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
styles: ['src/test-style-a.css'],
});

const { result, logs } = await harness.executeOnce({ outputLogsOnFailure: false });

expect(result?.success).toBeFalse();
expect(logs).toContain(
jasmine.objectContaining({
level: 'error',
message: jasmine.stringMatching(`Can't resolve 'src/test-style-a.css'`),
}),
);
});
});
});
Expand Up @@ -7,7 +7,6 @@
*/

import assert from 'assert';
import { pluginName } from 'mini-css-extract-plugin';
import type { Compilation, Compiler } from 'webpack';
import { assertIsError } from '../../utils/error';
import { addError } from '../../utils/webpack-diagnostics';
Expand All @@ -30,10 +29,6 @@ export class StylesWebpackPlugin {

apply(compiler: Compiler): void {
const { entryPoints, preserveSymlinks, root } = this.options;
const webpackOptions = compiler.options;
const entry =
typeof webpackOptions.entry === 'function' ? webpackOptions.entry() : webpackOptions.entry;

const resolver = compiler.resolverFactory.get('global-styles', {
conditionNames: ['sass', 'less', 'style'],
mainFields: ['sass', 'less', 'style', 'main', '...'],
Expand All @@ -45,32 +40,38 @@ export class StylesWebpackPlugin {
fileSystem: compiler.inputFileSystem,
});

webpackOptions.entry = async () => {
const entrypoints = await entry;
const webpackOptions = compiler.options;
compiler.hooks.environment.tap(PLUGIN_NAME, () => {
const entry =
typeof webpackOptions.entry === 'function' ? webpackOptions.entry() : webpackOptions.entry;

for (const [bundleName, paths] of Object.entries(entryPoints)) {
entrypoints[bundleName] ??= {};
const entryImport = (entrypoints[bundleName].import ??= []);
webpackOptions.entry = async () => {
const entrypoints = await entry;

for (const path of paths) {
try {
const resolvedPath = resolver.resolveSync({}, root, path);
if (resolvedPath) {
entryImport.push(`${resolvedPath}?ngGlobalStyle`);
} else {
for (const [bundleName, paths] of Object.entries(entryPoints)) {
entrypoints[bundleName] ??= {};
const entryImport = (entrypoints[bundleName].import ??= []);

for (const path of paths) {
try {
const resolvedPath = resolver.resolveSync({}, root, path);
if (resolvedPath) {
entryImport.push(`${resolvedPath}?ngGlobalStyle`);
} else {
assert(this.compilation, 'Compilation cannot be undefined.');
addError(this.compilation, `Cannot resolve '${path}'.`);
}
} catch (error) {
assert(this.compilation, 'Compilation cannot be undefined.');
addError(this.compilation, `Cannot resolve '${path}'.`);
assertIsError(error);
addError(this.compilation, error.message);
}
} catch (error) {
assert(this.compilation, 'Compilation cannot be undefined.');
assertIsError(error);
addError(this.compilation, error.message);
}
}
}

return entrypoints;
};
return entrypoints;
};
});

compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
this.compilation = compilation;
Expand Down