"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { PLUGIN_SASS_NAME: () => PLUGIN_SASS_NAME, pluginSass: () => pluginSass }); module.exports = __toCommonJS(src_exports); var import_node_path2 = require("path"); var import_deepmerge = __toESM(require("deepmerge")); var import_reduce_configs = require("reduce-configs"); // src/helpers.ts var import_node_path = __toESM(require("path")); var import_node_url = require("url"); var GLOBAL_PATCHED_SYMBOL = Symbol("GLOBAL_PATCHED_SYMBOL"); function patchGlobalLocation() { if (!global.location) { const href = (0, import_node_url.pathToFileURL)(process.cwd()).href + import_node_path.default.sep; const location = Object.freeze({ [GLOBAL_PATCHED_SYMBOL]: true, href }); global.location = location; } } function unpatchGlobalLocation() { if (global.location?.[GLOBAL_PATCHED_SYMBOL]) { delete global.location; } } function patchCompilerGlobalLocation(compiler) { compiler.hooks.run.tap("PatchGlobalLocation", patchGlobalLocation); compiler.hooks.watchRun.tap("PatchGlobalLocation", patchGlobalLocation); compiler.hooks.watchClose.tap("PatchGlobalLocation", unpatchGlobalLocation); compiler.hooks.done.tap("PatchGlobalLocation", unpatchGlobalLocation); } var getResolveUrlJoinFn = async () => { const { createJoinFunction, asGenerator, createJoinImplementation, defaultJoinGenerator } = await import("../compiled/resolve-url-loader/index.js"); const rsbuildGenerator = asGenerator((item, ...rest) => { if (!item.uri.startsWith(".")) { return [null]; } return defaultJoinGenerator(item, ...rest); }); return createJoinFunction( "rsbuild-resolve-join-fn", createJoinImplementation(rsbuildGenerator) ); }; // src/index.ts var PLUGIN_SASS_NAME = "rsbuild:sass"; var getSassLoaderOptions = (userOptions, isUseCssSourceMap) => { const excludes = []; const addExcludes = (items) => { excludes.push(...Array.isArray(items) ? items : [items]); }; const mergeFn = (defaults, userOptions2) => { const getSassOptions = () => { if (defaults.sassOptions && userOptions2.sassOptions) { return (0, import_deepmerge.default)( defaults.sassOptions, userOptions2.sassOptions ); } return userOptions2.sassOptions || defaults.sassOptions; }; return { ...defaults, ...userOptions2, sassOptions: getSassOptions() }; }; const mergedOptions = (0, import_reduce_configs.reduceConfigsWithContext)({ initial: { sourceMap: isUseCssSourceMap, api: "modern-compiler", implementation: require.resolve("sass-embedded") }, config: userOptions, ctx: { addExcludes }, mergeFn }); return { options: mergedOptions, excludes }; }; var pluginSass = (pluginOptions = {}) => ({ name: PLUGIN_SASS_NAME, setup(api) { api.onAfterCreateCompiler(({ compiler }) => { patchCompilerGlobalLocation(compiler); }); api.modifyBundlerChain(async (chain, { CHAIN_ID }) => { const { excludes, options } = getSassLoaderOptions( pluginOptions.sassLoaderOptions, // source-maps required for loaders preceding resolve-url-loader // otherwise the resolve-url-loader will throw an error true ); const rule = chain.module.rule(CHAIN_ID.RULE.SASS).test(/\.s(?:a|c)ss$/).merge({ sideEffects: true }).resolve.preferRelative(true).end(); for (const item of excludes) { rule.exclude.add(item); } if (pluginOptions.exclude) { rule.exclude.add(pluginOptions.exclude); } const cssRule = chain.module.rules.get(CHAIN_ID.RULE.CSS); for (const id of Object.keys(cssRule.uses.entries())) { const loader = cssRule.uses.get(id); const options2 = loader.get("options") ?? {}; const clonedOptions = (0, import_deepmerge.default)({}, options2); if (id === CHAIN_ID.USE.CSS) { clonedOptions.importLoaders += 2; } rule.use(id).loader(loader.get("loader")).options(clonedOptions); } rule.use(CHAIN_ID.USE.RESOLVE_URL).loader((0, import_node_path2.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((0, import_node_path2.join)(__dirname, "../compiled/sass-loader/index.js")).options(options); }); } }); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { PLUGIN_SASS_NAME, pluginSass });