Skip to content

Commit

Permalink
fix: handle pkg: scheme (#1191)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Feb 19, 2024
1 parent 1fa54da commit c34c8e3
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ const MODULE_REQUEST_REGEX = /^[^?]*~/;
const IS_MODULE_IMPORT =
/^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^/]+\/)$/;

const IS_PKG_SCHEME = /^pkg:/i;

/**
* When `sass`/`node-sass` tries to resolve an import, it uses a special algorithm.
* Since the `sass-loader` uses webpack to resolve the modules, we need to simulate that algorithm.
Expand Down Expand Up @@ -331,7 +333,13 @@ function getPossibleRequests(
request = request.replace(MODULE_REQUEST_REGEX, "");
}

if (IS_MODULE_IMPORT.test(url)) {
if (IS_PKG_SCHEME.test(url)) {
request = `${request.slice(4)}`;

return [...new Set([request, url])];
}

if (IS_MODULE_IMPORT.test(url) || IS_PKG_SCHEME.test(url)) {
request = request[request.length - 1] === "/" ? request : `${request}/`;

return [...new Set([request, url])];
Expand Down Expand Up @@ -538,6 +546,8 @@ function getWebpackResolver(
const needEmulateSassResolver =
// `sass` doesn't support module import
!IS_SPECIAL_MODULE_IMPORT.test(request) &&
// don't handle `pkg:` scheme
!IS_PKG_SCHEME.test(request) &&
// We need improve absolute paths handling.
// Absolute paths should be resolved:
// - Server-relative URLs - `<context>/path/to/file.ext` (where `<context>` is root context)
Expand Down
40 changes: 40 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -359192,6 +359192,46 @@ exports[`loader should work with "bootstrap" package v5, import as a package ('s

exports[`loader should work with "bootstrap" package v5, import as a package ('sass-embedded', 'legacy' API, 'scss' syntax): warnings 1`] = `[]`;

exports[`loader should work with "pkg" prefix in "@use" ('dart-sass', 'legacy' API, 'sass' syntax) with "@charset "UTF-8";": css 1`] = `
".foo {
color: red;
}"
`;

exports[`loader should work with "pkg" prefix in "@use" ('dart-sass', 'legacy' API, 'sass' syntax) with "@charset "UTF-8";": errors 1`] = `[]`;

exports[`loader should work with "pkg" prefix in "@use" ('dart-sass', 'legacy' API, 'sass' syntax) with "@charset "UTF-8";": warnings 1`] = `[]`;

exports[`loader should work with "pkg" prefix in "@use" ('dart-sass', 'legacy' API, 'scss' syntax) with "@charset "UTF-8";": css 1`] = `
".foo {
color: red;
}"
`;

exports[`loader should work with "pkg" prefix in "@use" ('dart-sass', 'legacy' API, 'scss' syntax) with "@charset "UTF-8";": errors 1`] = `[]`;

exports[`loader should work with "pkg" prefix in "@use" ('dart-sass', 'legacy' API, 'scss' syntax) with "@charset "UTF-8";": warnings 1`] = `[]`;

exports[`loader should work with "pkg" prefix in "@use" ('sass-embedded', 'legacy' API, 'sass' syntax) with "@charset "UTF-8";": css 1`] = `
".foo {
color: red;
}"
`;

exports[`loader should work with "pkg" prefix in "@use" ('sass-embedded', 'legacy' API, 'sass' syntax) with "@charset "UTF-8";": errors 1`] = `[]`;

exports[`loader should work with "pkg" prefix in "@use" ('sass-embedded', 'legacy' API, 'sass' syntax) with "@charset "UTF-8";": warnings 1`] = `[]`;

exports[`loader should work with "pkg" prefix in "@use" ('sass-embedded', 'legacy' API, 'scss' syntax) with "@charset "UTF-8";": css 1`] = `
".foo {
color: red;
}"
`;

exports[`loader should work with "pkg" prefix in "@use" ('sass-embedded', 'legacy' API, 'scss' syntax) with "@charset "UTF-8";": errors 1`] = `[]`;

exports[`loader should work with "pkg" prefix in "@use" ('sass-embedded', 'legacy' API, 'scss' syntax) with "@charset "UTF-8";": warnings 1`] = `[]`;

exports[`loader should work with a package with "sass" and "exports" fields ('dart-sass', 'legacy' API, 'sass' syntax): css 1`] = `
".load-me {
color: red;
Expand Down
17 changes: 17 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2142,6 +2142,23 @@ describe("loader", () => {
expect(getErrors(stats)).toMatchSnapshot("errors");
});
}

if (!isModernAPI) {
it(`should work with "pkg" prefix in "@use" ('${implementationName}', '${api}' API, '${syntax}' syntax) with "@charset "UTF-8";"`, async () => {
const testId = getTestId("use-pkg", syntax);
const options = {
implementation,
api,
};
const compiler = getCompiler(testId, { loader: { options } });
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);

expect(codeFromBundle.css).toMatchSnapshot("css");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});
}
}
});
});
Expand Down
7 changes: 7 additions & 0 deletions test/node_modules/pkg/package.json

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

3 changes: 3 additions & 0 deletions test/node_modules/pkg/styles/index.scss

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

1 change: 1 addition & 0 deletions test/sass/use-pkg.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@use 'pkg:pkg'
1 change: 1 addition & 0 deletions test/scss/use-pkg.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@use 'pkg:pkg';

0 comments on commit c34c8e3

Please sign in to comment.