From 59709ef675ea0ca8700f0707c5c6252df03b5910 Mon Sep 17 00:00:00 2001 From: Junhea Hong <97426534+junhea@users.noreply.github.com> Date: Mon, 20 Jan 2025 11:56:43 +0900 Subject: [PATCH] feat(plugin-sass): add `useOriginalUrlResolver` option to disable `resolve-url-loader` (#4389) --- packages/plugin-sass/src/index.ts | 33 +++++++++++++------- packages/plugin-sass/src/types.ts | 5 +++ website/docs/en/plugins/list/plugin-sass.mdx | 8 +++++ 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/packages/plugin-sass/src/index.ts b/packages/plugin-sass/src/index.ts index 901b3a1044..d28ac98a19 100644 --- a/packages/plugin-sass/src/index.ts +++ b/packages/plugin-sass/src/index.ts @@ -136,24 +136,33 @@ export const pluginSass = ( const clonedOptions = deepmerge>({}, 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); diff --git a/packages/plugin-sass/src/types.ts b/packages/plugin-sass/src/types.ts index be737f1f8c..d9ec1dfbe4 100644 --- a/packages/plugin-sass/src/types.ts +++ b/packages/plugin-sass/src/types.ts @@ -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; }; diff --git a/website/docs/en/plugins/list/plugin-sass.mdx b/website/docs/en/plugins/list/plugin-sass.mdx index b29320339f..a277a0038f 100644 --- a/website/docs/en/plugins/list/plugin-sass.mdx +++ b/website/docs/en/plugins/list/plugin-sass.mdx @@ -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