Skip to content

fix(@angular/cli): ensure stylesheet relative tilde path conversion #9077

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

Merged
merged 1 commit into from
Jan 4, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"opn": "~5.1.0",
"portfinder": "~1.0.12",
"postcss-custom-properties": "^6.1.0",
"postcss-loader": "^2.0.8",
"postcss-loader": "^2.0.10",
"postcss-url": "^7.1.2",
"raw-loader": "^0.5.1",
"resolve": "^1.1.7",
Expand Down
21 changes: 15 additions & 6 deletions packages/@angular/cli/models/webpack-configs/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ const customProperties = require('postcss-custom-properties');
* require('sass-loader')
*/

interface PostcssUrlAsset {
url: string;
hash: string;
absolutePath: string;
}

export function getStylesConfig(wco: WebpackConfigOptions) {
const { projectRoot, buildOptions, appConfig } = wco;

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

const postcssPluginCreator = function() {
const postcssPluginCreator = function(loader: webpack.loader.LoaderContext) {
return [
postcssUrl({
filter: ({ url }: { url: string }) => url.startsWith('~'),
url: ({ url }: { url: string }) => path.join(projectRoot, 'node_modules', url.substr(1)),
filter: ({ url }: PostcssUrlAsset) => url.startsWith('~'),
url: ({ url }: PostcssUrlAsset) => {
const fullPath = path.join(projectRoot, 'node_modules', url.substr(1));
return path.relative(loader.context, fullPath).replace(/\\/g, '/');
}
}),
postcssUrl([
{
// Only convert root relative URLs, which CSS-Loader won't process into require().
filter: ({ url }: { url: string }) => url.startsWith('/') && !url.startsWith('//'),
url: ({ url }: { url: string }) => {
filter: ({ url }: PostcssUrlAsset) => url.startsWith('/') && !url.startsWith('//'),
url: ({ url }: PostcssUrlAsset) => {
if (deployUrl.match(/:\/\//) || deployUrl.startsWith('/')) {
// If deployUrl is absolute or root relative, ignore baseHref & use deployUrl as is.
return `${deployUrl.replace(/\/$/, '')}${url}`;
Expand All @@ -71,7 +80,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
},
{
// TODO: inline .cur if not supporting IE (use browserslist to check)
filter: (asset: any) => !asset.hash && !asset.absolutePath.endsWith('.cur'),
filter: (asset: PostcssUrlAsset) => !asset.hash && !asset.absolutePath.endsWith('.cur'),
url: 'inline',
// NOTE: maxSize is in KB
maxSize: 10
Expand Down
2 changes: 1 addition & 1 deletion packages/@angular/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"opn": "~5.1.0",
"portfinder": "~1.0.12",
"postcss-custom-properties": "^6.1.0",
"postcss-loader": "^2.0.8",
"postcss-loader": "^2.0.10",
"postcss-url": "^7.1.2",
"raw-loader": "^0.5.1",
"resolve": "^1.1.7",
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/tests/build/styles/inline-urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function () {
.then(() => silentNpm('install', '[email protected]'))
.then(() => writeMultipleFiles({
'src/styles.scss': `
$fa-font-path: "~font-awesome/font";
$fa-font-path: "~font-awesome/fonts";
@import "~font-awesome/scss/font-awesome";
h1 { background: url('./assets/large.png'); }
h2 { background: url('./assets/small.svg'); }
Expand Down