Skip to content

Commit 23604bf

Browse files
committed
fix(@angular/cli): ensure stylesheet relative tilde path conversion
1 parent e4e46c9 commit 23604bf

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

package-lock.json

Lines changed: 21 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"opn": "~5.1.0",
7777
"portfinder": "~1.0.12",
7878
"postcss-custom-properties": "^6.1.0",
79-
"postcss-loader": "^2.0.8",
79+
"postcss-loader": "^2.0.10",
8080
"postcss-url": "^7.1.2",
8181
"raw-loader": "^0.5.1",
8282
"resolve": "^1.1.7",

packages/@angular/cli/models/webpack-configs/styles.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ const customProperties = require('postcss-custom-properties');
2929
* require('sass-loader')
3030
*/
3131

32+
interface PostcssUrlAsset {
33+
url: string;
34+
hash: string;
35+
absolutePath: string;
36+
}
37+
3238
export function getStylesConfig(wco: WebpackConfigOptions) {
3339
const { projectRoot, buildOptions, appConfig } = wco;
3440

@@ -47,7 +53,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
4753
const baseHref = wco.buildOptions.baseHref || '';
4854
const deployUrl = wco.buildOptions.deployUrl || '';
4955

50-
const postcssPluginCreator = function() {
56+
const postcssPluginCreator = function(loader: webpack.loader.LoaderContext) {
5157
// safe settings based on: https://github.com/ben-eb/cssnano/issues/358#issuecomment-283696193
5258
const importantCommentRe = /@preserve|@licen[cs]e|[@#]\s*source(?:Mapping)?URL|^!/i;
5359
const minimizeOptions = {
@@ -59,14 +65,17 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
5965

6066
return [
6167
postcssUrl({
62-
filter: ({ url }: { url: string }) => url.startsWith('~'),
63-
url: ({ url }: { url: string }) => path.join(projectRoot, 'node_modules', url.substr(1)),
68+
filter: ({ url }: PostcssUrlAsset) => url.startsWith('~'),
69+
url: ({ url }: PostcssUrlAsset) => {
70+
const fullPath = path.join(projectRoot, 'node_modules', url.substr(1));
71+
return path.relative(loader.context, fullPath).replace(/\\/g, '/');
72+
}
6473
}),
6574
postcssUrl([
6675
{
6776
// Only convert root relative URLs, which CSS-Loader won't process into require().
68-
filter: ({ url }: { url: string }) => url.startsWith('/') && !url.startsWith('//'),
69-
url: ({ url }: { url: string }) => {
77+
filter: ({ url }: PostcssUrlAsset) => url.startsWith('/') && !url.startsWith('//'),
78+
url: ({ url }: PostcssUrlAsset) => {
7079
if (deployUrl.match(/:\/\//) || deployUrl.startsWith('/')) {
7180
// If deployUrl is absolute or root relative, ignore baseHref & use deployUrl as is.
7281
return `${deployUrl.replace(/\/$/, '')}${url}`;
@@ -83,7 +92,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
8392
},
8493
{
8594
// TODO: inline .cur if not supporting IE (use browserslist to check)
86-
filter: (asset: any) => !asset.hash && !asset.absolutePath.endsWith('.cur'),
95+
filter: (asset: PostcssUrlAsset) => !asset.hash && !asset.absolutePath.endsWith('.cur'),
8796
url: 'inline',
8897
// NOTE: maxSize is in KB
8998
maxSize: 10

packages/@angular/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"opn": "~5.1.0",
6262
"portfinder": "~1.0.12",
6363
"postcss-custom-properties": "^6.1.0",
64-
"postcss-loader": "^2.0.8",
64+
"postcss-loader": "^2.0.10",
6565
"postcss-url": "^7.1.2",
6666
"raw-loader": "^0.5.1",
6767
"resolve": "^1.1.7",

tests/e2e/tests/build/styles/inline-urls.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default function () {
2020
.then(() => silentNpm('install', '[email protected]'))
2121
.then(() => writeMultipleFiles({
2222
'src/styles.scss': `
23-
$fa-font-path: "~font-awesome/font";
23+
$fa-font-path: "~font-awesome/fonts";
2424
@import "~font-awesome/scss/font-awesome";
2525
h1 { background: url('./assets/large.png'); }
2626
h2 { background: url('./assets/small.svg'); }

0 commit comments

Comments
 (0)