UNPKG

vite-plugin-pretty-module-classnames

Version:

Adds the filename without the -module suffix to the class names of CSS modules.

3 lines (2 loc) 1.21 kB
"use strict";const m=require("crypto");function u(n){return m.createHash("sha256").update(n).digest("hex").slice(0,5)}function g(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("/")),r=`${i}__${n}`,a=u(`${c}-${r}`),l=e!==void 0?`_${e}`:"";return`${r}_${a}${l}`}function f(n={}){return{name:"vite-plugin-pretty-module-classnames",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,r)=>{let a;if(n.lineNumber){const l=r.split(` `),p=new RegExp(`\\.${i}\\b`);a=l.findIndex(d=>p.test(d))+1}return g(i,c,a)}}};return{...t,css:o}}}}module.exports=f;