diff --git a/src/index.ts b/src/index.ts index da5bccc9..efcb2821 100644 --- a/src/index.ts +++ b/src/index.ts @@ -67,7 +67,7 @@ export default (options: Options = {}): Plugin => { name: "styles", buildStart(opts) { - preserveModules = Boolean(opts.preserveModules); + preserveModules = opts.preserveModules; }, async transform(code, id) { diff --git a/src/loaders/postcss/icss/index.ts b/src/loaders/postcss/icss/index.ts index 59e83ec0..8d0c0c2e 100644 --- a/src/loaders/postcss/icss/index.ts +++ b/src/loaders/postcss/icss/index.ts @@ -1,6 +1,5 @@ import postcss from "postcss"; import { extractICSS, replaceSymbols, replaceValueSymbols } from "icss-utils"; -import { ModulesOptions } from "../modules"; import loadDefault, { Load } from "./load"; import resolve from "./resolve"; @@ -9,7 +8,6 @@ const extensionsDefault = [".css", ".pcss", ".postcss", ".sss"]; export interface InteroperableCSSOptions { load?: Load; - getReplacements?: ModulesOptions["getReplacements"]; extensions?: string[]; } @@ -38,15 +36,13 @@ const plugin: postcss.Plugin = postcss.plugin( replaceSymbols(css, imports); - const exports: Record = {}; for (const [k, v] of Object.entries(icssExports)) { - exports[k] = replaceValueSymbols(v, imports); + res.messages.push({ + plugin: name, + type: "icss", + export: { [k]: replaceValueSymbols(v, imports) }, + }); } - - res.messages.push({ plugin: name, type: "icss", exports }); - - if (typeof options.getReplacements === "function") - options.getReplacements(css.source.input.file, exports, opts.to); }, ); diff --git a/src/loaders/postcss/icss/load.ts b/src/loaders/postcss/icss/load.ts index 233d3510..70134475 100644 --- a/src/loaders/postcss/icss/load.ts +++ b/src/loaders/postcss/icss/load.ts @@ -26,7 +26,7 @@ const load: Load = async (url, file, extensions, processor, opts) => { const exports: Record = {}; for (const msg of messages) { if (msg.type !== "icss") continue; - Object.assign(exports, msg.exports as Record); + Object.assign(exports, msg.export as Record); } return exports; diff --git a/src/loaders/postcss/index.ts b/src/loaders/postcss/index.ts index 26857499..2a163b03 100644 --- a/src/loaders/postcss/index.ts +++ b/src/loaders/postcss/index.ts @@ -65,7 +65,7 @@ const loader: Loader = { const plugins: (postcss.Transformer | postcss.Processor)[] = []; const autoModules = ensureAutoModules(options.autoModules, this.id); const supportModules = Boolean(options.modules || autoModules); - const modulesExports: Record> = {}; + const modulesExports: Record = {}; const postcssOpts: PostCSSOptions = { ...config.options, @@ -99,18 +99,7 @@ const loader: Loader = { failOnWrongOrder: true, ...modulesOptions, }), - postcssICSS({ - extensions: options.extensions, - getReplacements(file, replacements, out) { - modulesExports[file] = replacements; - if ( - typeof options.modules === "object" && - typeof options.modules.getReplacements === "function" - ) { - return options.modules.getReplacements(file, replacements, out); - } - }, - }), + postcssICSS({ extensions: options.extensions }), ); } @@ -122,14 +111,21 @@ const loader: Loader = { const res = await postcss(plugins).process(code, postcssOpts); - for (const warning of res.warnings()) - this.warn({ name: warning.plugin, message: warning.text }); - - const deps = res.messages.filter(msg => msg.type === "dependency"); - for (const dep of deps) this.deps.add(normalizePath(dep.file)); - - const assets = res.messages.filter(msg => msg.type === "asset"); - for (const asset of assets) this.assets.set(asset.to, asset.source); + for (const msg of res.messages) + switch (msg.type) { + case "warning": + this.warn({ name: msg.plugin, message: msg.text as string }); + break; + case "icss": + Object.assign(modulesExports, msg.export as Record); + break; + case "dependency": + this.deps.add(normalizePath(msg.file)); + break; + case "asset": + this.assets.set(msg.to, msg.source); + break; + } map = mm((res.map?.toJSON() as unknown) as RawSourceMap) .resolve(path.dirname(postcssOpts.to)) @@ -149,26 +145,24 @@ const loader: Loader = { const output = [ `const ${cssVarName} = ${JSON.stringify(res.css)}`, - `const ${modulesVarName} = ${JSON.stringify(modulesExports[this.id] ?? {})}`, + `const ${modulesVarName} = ${JSON.stringify(modulesExports)}`, `export const css = ${cssVarName}`, `export default ${supportModules ? modulesVarName : cssVarName}`, ]; if (options.namedExports) { - const json = modulesExports[this.id]; - const getClassName = typeof options.namedExports === "function" ? options.namedExports : getClassNameDefault; - for (const name in json) { + for (const name in modulesExports) { const newName = getClassName(name); if (name !== newName) this.warn(`Exported \`${name}\` as \`${newName}\` in ${humanlizePath(this.id)}`); - if (!json[newName]) json[newName] = json[name]; + if (!modulesExports[newName]) modulesExports[newName] = modulesExports[name]; - output.push(`export const ${newName} = ${JSON.stringify(json[name])}`); + output.push(`export const ${newName} = ${JSON.stringify(modulesExports[name])}`); } } diff --git a/src/loaders/postcss/modules/index.ts b/src/loaders/postcss/modules/index.ts index 86bc93db..d9442764 100644 --- a/src/loaders/postcss/modules/index.ts +++ b/src/loaders/postcss/modules/index.ts @@ -26,8 +26,6 @@ export interface ModulesOptions { * @default "[name]_[local]__[hash:8]" */ generateScopedName?: string | ((name: string, file: string, css: string) => string); - /** Function for resulting replacements extraction */ - getReplacements?: (file: string, replacements: Record, out?: string) => void; } export default (options: ModulesOptions): (postcss.Transformer | postcss.Processor)[] => {