Skip to content

Commit 3ff3917

Browse files
clydinalan-agius4
authored andcommitted
fix(@angular-devkit/build-angular): account for package.json exports fields with CSS import statements
The `postcss-imports` package was previously used to support `@import` within CSS files. Unfortunately, the package does not account for `package.json` exports fields. This prevents imports defined within that field from working when used within a build. The `css-loader` package does provide this functionality and is now used to provide support for CSS `@import` instead of `postcss-imports`. This change does not affect preprocessors that provide their own import behavior. (cherry picked from commit 717bd03)
1 parent 6f2188d commit 3ff3917

File tree

7 files changed

+22
-29
lines changed

7 files changed

+22
-29
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@
185185
"piscina": "3.2.0",
186186
"popper.js": "^1.14.1",
187187
"postcss": "8.4.18",
188-
"postcss-import": "15.0.0",
189188
"postcss-loader": "7.0.1",
190189
"prettier": "^2.0.0",
191190
"protractor": "~7.0.0",

packages/angular_devkit/build_angular/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ ts_library(
162162
"@npm//parse5-html-rewriting-stream",
163163
"@npm//piscina",
164164
"@npm//postcss",
165-
"@npm//postcss-import",
166165
"@npm//postcss-loader",
167166
"@npm//regenerator-runtime",
168167
"@npm//resolve-url-loader",

packages/angular_devkit/build_angular/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
"parse5-html-rewriting-stream": "6.0.1",
4949
"piscina": "3.2.0",
5050
"postcss": "8.4.18",
51-
"postcss-import": "15.0.0",
5251
"postcss-loader": "7.0.1",
5352
"regenerator-runtime": "0.13.9",
5453
"resolve-url-loader": "5.0.0",

packages/angular_devkit/build_angular/src/builders/browser/specs/styles_spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ describe('Browser Builder styles', () => {
174174
};
175175

176176
const overrides = {
177+
aot: true,
177178
sourceMap: true,
178179
styles: [`src/styles.${ext}`],
179180
};
@@ -301,10 +302,13 @@ describe('Browser Builder styles', () => {
301302

302303
const overrides = { optimization: false };
303304
const { files } = await browserBuild(architect, host, target, overrides);
304-
expect(await files['styles.css']).toContain(tags.stripIndents`
305+
const content = await files['styles.css'];
306+
expect(content).toContain(tags.stripIndents`
305307
/* normal-comment */
306308
/*! important-comment */
307-
section { -webkit-mask-composite: source-over; mask-composite: add; }
309+
section { -webkit-mask-composite: source-over; mask-composite: add; }`);
310+
// Check separately because Webpack may add source file comments inbetween the rules
311+
expect(content).toContain(tags.stripIndents`
308312
/* normal-comment */
309313
/*! important-comment */
310314
div { -webkit-mask-composite: source-over; mask-composite: add; }`);

packages/angular_devkit/build_angular/src/builders/browser/specs/vendor-source-map_spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ describe('Identifying third-party code in source maps', () => {
108108
'./src/app/app.component.ts',
109109
'./src/app/app.module.ts',
110110
'./src/main.ts',
111+
'./src/app/app.component.css',
111112
]);
112113

113114
// Only some sources in the polyfills map are first-party.

packages/angular_devkit/build_angular/src/builders/server/tests/options/source-map_spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { BASE_OPTIONS, SERVER_BUILDER_INFO, describeBuilder } from '../setup';
1111

1212
describeBuilder(execute, SERVER_BUILDER_INFO, (harness) => {
1313
describe('Option: "sourceMap"', () => {
14-
const INLINE_SOURCEMAP_MARKER = '/*# sourceMappingURL=data:application/json;base64,';
14+
const INLINE_SOURCEMAP_MARKER =
15+
'/*# sourceMappingURL=data:application/json;charset=utf-8;base64,';
1516

1617
beforeEach(async () => {
1718
await harness.writeFiles({

packages/angular_devkit/build_angular/src/webpack/configs/styles.ts

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import * as path from 'path';
1212
import type { FileImporter } from 'sass';
1313
import { pathToFileURL } from 'url';
1414
import type { Configuration, LoaderContext, RuleSetUseItem } from 'webpack';
15-
import { StyleElement } from '../../builders/browser/schema';
1615
import { SassWorkerImplementation } from '../../sass/sass-service';
1716
import { SassLegacyWorkerImplementation } from '../../sass/sass-service-legacy';
1817
import { WebpackConfigOptions } from '../../utils/build-options';
@@ -28,13 +27,12 @@ import { StylesWebpackPlugin } from '../plugins/styles-webpack-plugin';
2827
import {
2928
assetNameTemplateFactory,
3029
getOutputHashFormat,
31-
normalizeExtraEntryPoints,
3230
normalizeGlobalStyles,
3331
} from '../utils/helpers';
3432

3533
// eslint-disable-next-line max-lines-per-function
3634
export function getStylesConfig(wco: WebpackConfigOptions): Configuration {
37-
const { root, projectRoot, buildOptions } = wco;
35+
const { root, buildOptions } = wco;
3836
const extraPlugins: Configuration['plugins'] = [];
3937

4038
extraPlugins.push(new AnyComponentStyleBudgetChecker(buildOptions.budgets));
@@ -103,35 +101,17 @@ export function getStylesConfig(wco: WebpackConfigOptions): Configuration {
103101
}
104102
}
105103

106-
const postcssImports = require('postcss-import');
107104
const autoprefixer: typeof import('autoprefixer') = require('autoprefixer');
108105

109106
const postcssOptionsCreator = (inlineSourcemaps: boolean, extracted: boolean) => {
110-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
111-
const optionGenerator = (loader: any) => ({
107+
const optionGenerator = (loader: LoaderContext<unknown>) => ({
112108
map: inlineSourcemaps
113109
? {
114110
inline: true,
115111
annotation: false,
116112
}
117113
: undefined,
118114
plugins: [
119-
postcssImports({
120-
load: (filename: string) => {
121-
return new Promise<string>((resolve, reject) => {
122-
loader.fs.readFile(filename, (err: Error, data: Buffer) => {
123-
if (err) {
124-
reject(err);
125-
126-
return;
127-
}
128-
129-
const content = data.toString();
130-
resolve(content);
131-
});
132-
});
133-
},
134-
}),
135115
PostcssCliResources({
136116
baseHref: buildOptions.baseHref,
137117
deployUrl: buildOptions.deployUrl,
@@ -178,6 +158,16 @@ export function getStylesConfig(wco: WebpackConfigOptions): Configuration {
178158
const postCssLoaderPath = require.resolve('postcss-loader');
179159

180160
const componentStyleLoaders: RuleSetUseItem[] = [
161+
{
162+
loader: require.resolve('css-loader'),
163+
options: {
164+
url: false,
165+
sourceMap: componentsSourceMap,
166+
importLoaders: 1,
167+
exportType: 'string',
168+
esModule: false,
169+
},
170+
},
181171
{
182172
loader: postCssLoaderPath,
183173
options: {
@@ -196,6 +186,7 @@ export function getStylesConfig(wco: WebpackConfigOptions): Configuration {
196186
options: {
197187
url: false,
198188
sourceMap: !!cssSourceMap,
189+
importLoaders: 1,
199190
},
200191
},
201192
{
@@ -294,7 +285,6 @@ export function getStylesConfig(wco: WebpackConfigOptions): Configuration {
294285
// Component styles are all styles except defined global styles
295286
{
296287
use: componentStyleLoaders,
297-
type: 'asset/source',
298288
resourceQuery: /\?ngResource/,
299289
},
300290
],

0 commit comments

Comments
 (0)