Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): encode Sass package resolve direc…
Browse files Browse the repository at this point in the history
…tories in importer URLs

When using the new developer preview application build system, Sass import/use usage that specifies
a package is adjusted to contain the resolve directory to workaround Sass import plugin limitations.
This resolve directory is now encoded to prevent the new specifier from looking like a URL with a
scheme to the Sass compiler. This can occur on Windows when a drive letter is present (`C:\`).

(cherry picked from commit 8b74a50)
  • Loading branch information
clydin authored and dgp1130 committed Aug 11, 2023
1 parent 221ab24 commit fe752ad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,20 @@ export interface DirectoryEntry {
const MODULE_RESOLUTION_PREFIX = '__NG_PACKAGE__';

function packModuleSpecifier(specifier: string, resolveDir: string): string {
const packed = MODULE_RESOLUTION_PREFIX + ';' + resolveDir + ';' + specifier;

// Normalize path separators and escape characters
// https://developer.mozilla.org/en-US/docs/Web/CSS/url#syntax
const normalizedPacked = packed.replace(/\\/g, '/').replace(/[()\s'"]/g, '\\$&');

return normalizedPacked;
const packed =
MODULE_RESOLUTION_PREFIX +
';' +
// Encode the resolve directory to prevent unsupported characters from being present when
// Sass processes the URL. This is important on Windows which can contain drive letters
// and colons which would otherwise be interpreted as a URL scheme.
encodeURIComponent(resolveDir) +
';' +
// Escape characters instead of encoding to provide more friendly not found error messages.
// Unescaping is automatically handled by Sass.
// https://developer.mozilla.org/en-US/docs/Web/CSS/url#syntax
specifier.replace(/[()\s'"]/g, '\\$&');

return packed;
}

function unpackModuleSpecifier(specifier: string): { specifier: string; resolveDir?: string } {
Expand All @@ -51,7 +58,7 @@ function unpackModuleSpecifier(specifier: string): { specifier: string; resolveD

return {
specifier: values[2],
resolveDir: values[1],
resolveDir: decodeURIComponent(values[1]),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ export default async function () {

await writeMultipleFiles({
'src/styles.scss': `
@use '@material/button/button' as mat;
@use '@material/button/button';
@include button.core-styles;
`,
'src/app/app.component.scss': `
@use '@material/button/button' as mat;
@use '@material/button/button';
@include button.core-styles;
`,
});

Expand Down

0 comments on commit fe752ad

Please sign in to comment.