Skip to content

Commit

Permalink
feat(plugin-sass): add useOriginalUrlResolver option to disable `re…
Browse files Browse the repository at this point in the history
…solve-url-loader` (#4389)
  • Loading branch information
junhea authored Jan 20, 2025
1 parent 4596d65 commit 59709ef
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
33 changes: 21 additions & 12 deletions packages/plugin-sass/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,24 +136,33 @@ export const pluginSass = (
const clonedOptions = deepmerge<Record<string, any>>({}, options);

if (id === CHAIN_ID.USE.CSS) {
// add resolve-url-loader and sass-loader
clonedOptions.importLoaders += 2;
// add resolve-url-loader
if (!pluginOptions.useOriginalUrlResolver)
clonedOptions.importLoaders += 1;
// add sass-loader
clonedOptions.importLoaders += 1;
}

rule.use(id).loader(loader.get('loader')).options(clonedOptions);
}

// use resolve-url-loader if useOriginalResolver is not set
if (!pluginOptions.useOriginalUrlResolver)
rule
.use(CHAIN_ID.USE.RESOLVE_URL)
.loader(
path.join(__dirname, '../compiled/resolve-url-loader/index.js'),
)
.options({
join: await getResolveUrlJoinFn(),
// 'resolve-url-loader' relies on 'adjust-sourcemap-loader',
// it has performance regression issues in some scenarios,
// so we need to disable the sourceMap option.
sourceMap: false,
})
.end();

rule
.use(CHAIN_ID.USE.RESOLVE_URL)
.loader(path.join(__dirname, '../compiled/resolve-url-loader/index.js'))
.options({
join: await getResolveUrlJoinFn(),
// 'resolve-url-loader' relies on 'adjust-sourcemap-loader',
// it has performance regression issues in some scenarios,
// so we need to disable the sourceMap option.
sourceMap: false,
})
.end()
.use(CHAIN_ID.USE.SASS)
.loader(path.join(__dirname, '../compiled/sass-loader/index.js'))
.options(options);
Expand Down
5 changes: 5 additions & 0 deletions packages/plugin-sass/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ export type PluginSassOptions = {
* Exclude some `.scss` or `.sass` files, they will not be transformed by sass-loader.
*/
exclude?: Rspack.RuleSetCondition;

/**
* Use original url resolver instead of resolve-url-loader.
*/
useOriginalUrlResolver?: boolean;
};
8 changes: 8 additions & 0 deletions website/docs/en/plugins/list/plugin-sass.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ pluginSass({
});
```

### useOriginalUrlResolver

- **Type:** `boolean`
- **Default:** `undefined`
- **Version:** `>= 1.2.0`

Use the original resolver instead of `resolve-url-loader` when resolving urls

## Practices

### Modify Sass implementation
Expand Down

1 comment on commit 59709ef

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
plugins ✅ success
rspress ✅ success
rslib ✅ success
examples ✅ success

Please sign in to comment.