Skip to content

Commit 0b890e4

Browse files
committed
fix(@angular/cli): ensure stylesheet relative tilde path conversion
1 parent 1c78ff1 commit 0b890e4

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

package-lock.json

+21-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
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

+15-6
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

@@ -44,17 +50,20 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
4450
const baseHref = wco.buildOptions.baseHref || '';
4551
const deployUrl = wco.buildOptions.deployUrl || '';
4652

47-
const postcssPluginCreator = function() {
53+
const postcssPluginCreator = function(loader: webpack.loader.LoaderContext) {
4854
return [
4955
postcssUrl({
50-
filter: ({ url }: { url: string }) => url.startsWith('~'),
51-
url: ({ url }: { url: string }) => path.join(projectRoot, 'node_modules', url.substr(1)),
56+
filter: ({ url }: PostcssUrlAsset) => url.startsWith('~'),
57+
url: ({ url }: PostcssUrlAsset) => {
58+
const fullPath = path.join(projectRoot, 'node_modules', url.substr(1));
59+
return path.relative(loader.context, fullPath).replace(/\\/g, '/');
60+
}
5261
}),
5362
postcssUrl([
5463
{
5564
// Only convert root relative URLs, which CSS-Loader won't process into require().
56-
filter: ({ url }: { url: string }) => url.startsWith('/') && !url.startsWith('//'),
57-
url: ({ url }: { url: string }) => {
65+
filter: ({ url }: PostcssUrlAsset) => url.startsWith('/') && !url.startsWith('//'),
66+
url: ({ url }: PostcssUrlAsset) => {
5867
if (deployUrl.match(/:\/\//) || deployUrl.startsWith('/')) {
5968
// If deployUrl is absolute or root relative, ignore baseHref & use deployUrl as is.
6069
return `${deployUrl.replace(/\/$/, '')}${url}`;
@@ -71,7 +80,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
7180
},
7281
{
7382
// TODO: inline .cur if not supporting IE (use browserslist to check)
74-
filter: (asset: any) => !asset.hash && !asset.absolutePath.endsWith('.cur'),
83+
filter: (asset: PostcssUrlAsset) => !asset.hash && !asset.absolutePath.endsWith('.cur'),
7584
url: 'inline',
7685
// NOTE: maxSize is in KB
7786
maxSize: 10

packages/@angular/cli/package.json

+1-1
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

+1-1
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)