Skip to content

Commit

Permalink
test: more
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Apr 11, 2024
1 parent 3d854e6 commit e633761
Show file tree
Hide file tree
Showing 12 changed files with 15,629 additions and 1,234 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -664,9 +664,9 @@ Default: `"legacy"`

Allows you to switch between `legacy` and `modern` API. You can find more information [here](https://sass-lang.com/documentation/js-api). The `modern-compiler` option enables the modern API with support for [Shared Resources](https://github.com/sass/sass/blob/main/accepted/shared-resources.d.ts.md).

> **Warning**
> **Note**
>
> "modern" API is experimental, so some features may not work (known: built-in `importer` is not working and files with errors is not watching on initial run), you can follow this [here](https://github.com/webpack-contrib/sass-loader/issues/774).
> Using `modern-compiler` and `sass-embedded` together significantly improve performance and decrease built time. We strongly recommend their use. We will enable them by default in a future major release.
> **Warning**
>
Expand Down
6 changes: 2 additions & 4 deletions src/index.js
Expand Up @@ -57,11 +57,9 @@ async function loader(content) {
getWebpackImporter(this, implementation, includePaths),
);
} else {
// TODO loadsPaths
const { loadPaths } = sassOptions;

sassOptions.importers.push(
getModernWebpackImporter(this, implementation, loadPaths),
// No need to pass `loadPaths`, because modern API handle them itself
getModernWebpackImporter(this, implementation, []),
);
}
}
Expand Down
30 changes: 24 additions & 6 deletions src/utils.js
Expand Up @@ -203,6 +203,24 @@ async function getSassOptions(
}
}

sassOptions.loadPaths = []
.concat(
// We use `loadPaths` in context for resolver, so it should be always absolute
(sassOptions.loadPaths ? sassOptions.loadPaths.slice() : []).map(
(includePath) =>
path.isAbsolute(includePath)
? includePath
: path.join(process.cwd(), includePath),
),
)
.concat(
process.env.SASS_PATH
? process.env.SASS_PATH.split(
process.platform === "win32" ? ";" : ":",
)
: [],
);

sassOptions.importers = sassOptions.importers
? Array.isArray(sassOptions.importers)
? sassOptions.importers.slice()
Expand Down Expand Up @@ -458,8 +476,10 @@ function getWebpackResolver(
implementation,
includePaths = [],
) {
const isDartSass =
implementation && implementation.info.includes("dart-sass");
const isModernSass =
implementation &&
(implementation.info.includes("dart-sass") ||
implementation.info.includes("sass-embedded"));
// We only have one difference with the built-in sass resolution logic and out resolution logic:
// First, we look at the files starting with `_`, then without `_` (i.e. `_name.sass`, `_name.scss`, `_name.css`, `name.sass`, `name.scss`, `name.css`),
// although `sass` look together by extensions (i.e. `_name.sass`/`name.sass`/`_name.scss`/`name.scss`/`_name.css`/`name.css`).
Expand Down Expand Up @@ -525,7 +545,7 @@ function getWebpackResolver(
// See https://github.com/webpack/webpack/issues/12340
// Because `node-sass` calls our importer before `1. Filesystem imports relative to the base file.`
// custom importer may not return `{ file: '/path/to/name.ext' }` and therefore our `context` will be relative
if (!isDartSass && !path.isAbsolute(context)) {
if (!isModernSass && !path.isAbsolute(context)) {
return Promise.reject();
}

Expand Down Expand Up @@ -574,7 +594,7 @@ function getWebpackResolver(
);

// `node-sass` calls our importer before `1. Filesystem imports relative to the base file.`, so we need emulate this too
if (!isDartSass) {
if (!isModernSass) {
resolutionMap = resolutionMap.concat({
resolve: fromImport ? sassImportResolve : sassModuleResolve,
context: path.dirname(context),
Expand Down Expand Up @@ -622,7 +642,6 @@ function getModernWebpackImporter(loaderContext, implementation, loadPaths) {
return {
async canonicalize(originalUrl, context) {
const { fromImport } = context;
// TODO memorize?
const prev = context.containingUrl
? url.fileURLToPath(context.containingUrl.toString())
: loaderContext.resourcePath;
Expand Down Expand Up @@ -660,7 +679,6 @@ function getModernWebpackImporter(loaderContext, implementation, loadPaths) {
const contents = await new Promise((resolve, reject) => {
// Old version of `enhanced-resolve` supports only path as a string
// TODO simplify in the next major release and pass URL
// TODO memorize?
const canonicalPath = url.fileURLToPath(canonicalUrl);

loaderContext.fs.readFile(canonicalPath, "utf8", (err, content) => {
Expand Down

0 comments on commit e633761

Please sign in to comment.