Skip to content

Commit c034477

Browse files
clydinmgechev
authored andcommittedApr 27, 2020
feat(@angular-devkit/build-angular): rebase relative stylesheet assets when using preprocessors
Previously, when using a preprocessor, resources (e.g., `url(./my-image.jpg)`) referenced in a stylesheet that was imported into another stylesheet would retain the exact URL. This would be problematic as the resource would not be at the relative location within the new combined stylesheet. With this change the resource URLs will now be adjusted to reference the origin location of the resource. This allows the resources to be found without any additional changes to the application or build process. CSS and Less already functioned in this manner. This change brings Sass and Stylus to parity. Fixes: #12797
1 parent 06e9955 commit c034477

File tree

4 files changed

+233
-13
lines changed

4 files changed

+233
-13
lines changed
 

‎packages/angular_devkit/build_angular/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"postcss-loader": "3.0.0",
4848
"raw-loader": "4.0.1",
4949
"regenerator-runtime": "0.13.5",
50+
"resolve-url-loader": "3.1.1",
5051
"rimraf": "3.0.2",
5152
"rollup": "2.7.2",
5253
"rxjs": "6.5.5",

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,17 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
126126
{
127127
test: /\.scss$|\.sass$/,
128128
use: [
129+
{
130+
loader: require.resolve('resolve-url-loader'),
131+
options: {
132+
sourceMap: cssSourceMap,
133+
},
134+
},
129135
{
130136
loader: require.resolve('sass-loader'),
131137
options: {
132138
implementation: sassImplementation,
133-
sourceMap: cssSourceMap,
139+
sourceMap: true,
134140
sassOptions: {
135141
// bootstrap-sass requires a minimum precision of 8
136142
precision: 8,
@@ -162,9 +168,15 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
162168
test: /\.styl$/,
163169
use: [
164170
{
165-
loader: require.resolve('stylus-loader'),
171+
loader: require.resolve('resolve-url-loader'),
166172
options: {
167173
sourceMap: cssSourceMap,
174+
},
175+
},
176+
{
177+
loader: require.resolve('stylus-loader'),
178+
options: {
179+
sourceMap: { comment: false },
168180
paths: includePaths,
169181
},
170182
},

‎packages/angular_devkit/build_angular/test/browser/styles_spec_large.ts

+49
Original file line numberDiff line numberDiff line change
@@ -647,4 +647,53 @@ describe('Browser Builder styles', () => {
647647
expect(await files['styles.css']).toMatch(/\.one(.|\n|\r)*\.two(.|\n|\r)*\.three/);
648648
});
649649
});
650+
651+
extensionsWithImportSupport.forEach(ext => {
652+
it(`adjusts relative resource URLs when using @import in ${ext} (global)`, async () => {
653+
host.copyFile('src/spectrum.png', './src/more-styles/images/global-img-relative.png');
654+
host.writeMultipleFiles({
655+
[`src/styles-one.${ext}`]: tags.stripIndents`
656+
@import "more-styles/styles-two.${ext}";
657+
`,
658+
[`src/more-styles/styles-two.${ext}`]: tags.stripIndents`
659+
.two {
660+
background-image: url(images/global-img-relative.png);
661+
}
662+
`,
663+
});
664+
665+
const overrides = {
666+
sourceMap: false,
667+
extractCss: true,
668+
styles: [
669+
`src/styles-one.${ext}`,
670+
],
671+
};
672+
const { files } = await browserBuild(architect, host, target, overrides);
673+
expect(await files['styles.css']).toContain('\'global-img-relative.png\'');
674+
});
675+
676+
it(`adjusts relative resource URLs when using @import in ${ext} (component)`, async () => {
677+
host.copyFile('src/spectrum.png', './src/app/images/component-img-relative.png');
678+
host.writeMultipleFiles({
679+
[`src/app/styles/component-styles.${ext}`]: `
680+
div { background-image: url(../images/component-img-relative.png); }
681+
`,
682+
[`src/app/app.component.${ext}`]: `
683+
@import "styles/component-styles.${ext}";
684+
`,
685+
});
686+
687+
host.replaceInFile(
688+
'src/app/app.component.ts',
689+
'./app.component.css',
690+
`./app.component.${ext}`,
691+
);
692+
693+
const overrides = {
694+
sourceMap: false,
695+
};
696+
await browserBuild(architect, host, target, overrides);
697+
});
698+
});
650699
});

0 commit comments

Comments
 (0)
Please sign in to comment.