vite-plugin-pretty-module-classnames
Version:
Adds the filename without the -module suffix to the class names of CSS modules.
59 lines (58 loc) • 2.22 kB
JavaScript
import { createHash as d } from "crypto";
function u(n) {
return d("sha256").update(n).digest("hex").slice(0, 5);
}
function f(n, t, e) {
if (typeof t != "string")
throw new Error("The filename must be string and cannot be undefined.");
const o = t.split("?")[0].split("/"), s = o.pop();
if (!s)
throw new Error("Filename must include a valid file name.");
const i = s.replace(/(\.vue|\.module)?(\.\w+)$/, ""), c = u(o.join("/")), a = `${i}__${n}`, r = u(`${c}-${a}`), l = e !== void 0 ? `_${e}` : "";
return `${a}_${r}${l}`;
}
function h(n = {}) {
return {
name: "vite-plugin-pretty-module-classnames",
/**
* Modifies the Vite configuration object to include custom settings for CSS module class name generation.
* It checks if generateScopedName is already set by the user and throws an error if so.
* If not set, it applies a custom generateScopedName function.
*
* @param {UserConfig} config - The original Vite configuration provided by the user.
* @throws {Error} Throws an error if generateScopedName is already defined in the user's configuration.
* @returns {UserConfig} A modified Vite configuration object with custom settings for CSS module class name generation.
*/
config(t) {
var s;
const e = (s = t.css) == null ? void 0 : s.modules;
if (process.env.VITEST)
return {};
e && "generateScopedName" in e && e.generateScopedName && console.warn(
"[vite-plugin-pretty-module-classnames]:: The 'generateScopedName' configuration has already been set. Your vite.config configuration or other plugins might be attempting to override this setting, which could affect the proper functioning of vite-plugin-pretty-module-classnames."
);
const o = {
...t.css,
modules: {
...e,
generateScopedName: (i, c, a) => {
let r;
if (n.lineNumber) {
const l = a.split(`
`), m = new RegExp(`\\.${i}\\b`);
r = l.findIndex((p) => m.test(p)) + 1;
}
return f(i, c, r);
}
}
};
return {
...t,
css: o
};
}
};
}
export {
h as default
};