Skip to content

Commit dedaa6a

Browse files
author
Angular Builds
committed
f9e982c fix(@angular-devkit/build-angular): check namespaced Sass variables when rebasing URLs
1 parent e81f839 commit dedaa6a

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"name": "@angular-devkit/build-angular",
3-
"version": "17.0.5+sha-7df4b06",
3+
"version": "17.0.5+sha-f9e982c",
44
"description": "Angular Webpack Build Facade",
55
"main": "src/index.js",
66
"typings": "src/index.d.ts",
77
"builders": "builders.json",
88
"dependencies": {
99
"@ampproject/remapping": "2.2.1",
10-
"@angular-devkit/architect": "github:angular/angular-devkit-architect-builds#7df4b06",
11-
"@angular-devkit/build-webpack": "github:angular/angular-devkit-build-webpack-builds#7df4b06",
12-
"@angular-devkit/core": "github:angular/angular-devkit-core-builds#7df4b06",
10+
"@angular-devkit/architect": "github:angular/angular-devkit-architect-builds#f9e982c",
11+
"@angular-devkit/build-webpack": "github:angular/angular-devkit-build-webpack-builds#f9e982c",
12+
"@angular-devkit/core": "github:angular/angular-devkit-core-builds#f9e982c",
1313
"@babel/core": "7.23.2",
1414
"@babel/generator": "7.23.0",
1515
"@babel/helper-annotate-as-pure": "7.22.5",
@@ -20,7 +20,7 @@
2020
"@babel/preset-env": "7.23.2",
2121
"@babel/runtime": "7.23.2",
2222
"@discoveryjs/json-ext": "0.5.7",
23-
"@ngtools/webpack": "github:angular/ngtools-webpack-builds#7df4b06",
23+
"@ngtools/webpack": "github:angular/ngtools-webpack-builds#f9e982c",
2424
"@vitejs/plugin-basic-ssl": "1.0.1",
2525
"ansi-colors": "4.1.3",
2626
"autoprefixer": "10.4.16",

src/tools/sass/rebasing-importer.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@ const node_fs_1 = require("node:fs");
1616
const node_path_1 = require("node:path");
1717
const node_url_1 = require("node:url");
1818
const lexer_1 = require("./lexer");
19+
/**
20+
* Ensures that a bare specifier URL path that is intended to be treated as
21+
* a relative path has a leading `./` or `../` prefix.
22+
*
23+
* @param url A bare specifier URL path that should be considered relative.
24+
* @returns
25+
*/
26+
function ensureRelative(url) {
27+
// Empty
28+
if (!url) {
29+
return url;
30+
}
31+
// Already relative
32+
if (url[0] === '.' && (url[1] === '/' || (url[1] === '.' && url[2] === '/'))) {
33+
return url;
34+
}
35+
// Needs prefix
36+
return './' + url;
37+
}
1938
/**
2039
* A Sass Importer base class that provides the load logic to rebase all `url()` functions
2140
* within a stylesheet. The rebasing will ensure that the URLs in the output of the Sass compiler
@@ -46,8 +65,13 @@ class UrlRebasingImporter {
4665
// Rebase any URLs that are found
4766
let updatedContents;
4867
for (const { start, end, value } of (0, lexer_1.findUrls)(contents)) {
49-
// Skip if value is empty, a Sass variable, or Webpack-specific prefix
50-
if (value.length === 0 || value[0] === '$' || value[0] === '~' || value[0] === '^') {
68+
// Skip if value is empty or Webpack-specific prefix
69+
if (value.length === 0 || value[0] === '~' || value[0] === '^') {
70+
continue;
71+
}
72+
// Skip if value is a Sass variable.
73+
// Sass variable usage either starts with a `$` or contains a namespace and a `.$`
74+
if (value[0] === '$' || /^\w+\.\$/.test(value)) {
5175
continue;
5276
}
5377
// Skip if root-relative, absolute or protocol relative url
@@ -57,9 +81,10 @@ class UrlRebasingImporter {
5781
const rebasedPath = (0, node_path_1.relative)(this.entryDirectory, (0, node_path_1.join)(stylesheetDirectory, value));
5882
// Normalize path separators and escape characters
5983
// https://developer.mozilla.org/en-US/docs/Web/CSS/url#syntax
60-
const rebasedUrl = './' + rebasedPath.replace(/\\/g, '/').replace(/[()\s'"]/g, '\\$&');
84+
const rebasedUrl = ensureRelative(rebasedPath.replace(/\\/g, '/').replace(/[()\s'"]/g, '\\$&'));
6185
updatedContents ??= new magic_string_1.default(contents);
62-
updatedContents.update(start, end, rebasedUrl);
86+
// Always quote the URL to avoid potential downstream parsing problems
87+
updatedContents.update(start, end, `"${rebasedUrl}"`);
6388
}
6489
if (updatedContents) {
6590
contents = updatedContents.toString();

uniqueId

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Mon Dec 04 2023 16:44:44 GMT+0000 (Coordinated Universal Time)
1+
Tue Dec 05 2023 15:10:46 GMT+0000 (Coordinated Universal Time)

0 commit comments

Comments
 (0)