UNPKG

vite-plugin-load-css-module

Version:

A plugin to extend vite's css module rules, not just ".module" suffix

112 lines (110 loc) 3.78 kB
var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); // src/index.ts import { posix as path } from "path"; import { promises as fs } from "fs"; var queryRE = /\?.*$/s; var hashRE = /#.*$/s; var cleanUrl = (url) => url.replace(hashRE, "").replace(queryRE, ""); var resolvedConfig; var defaultResolvePlugin; var cssModuleMap = /* @__PURE__ */ new Map(); var originFileMap = /* @__PURE__ */ new Map(); var watchFiles = /* @__PURE__ */ new Set(); function getFullPath(id) { return path.join(resolvedConfig.root, id); } function getFilePath(id) { const fullPath = getFullPath(id); if (cssModuleMap.has(id)) return cssModuleMap.get(id); if (id.startsWith("/") && cssModuleMap.has(fullPath)) return cssModuleMap.get(fullPath); } function loadCssModuleFile(options) { const { include } = options; return { name: "load-css-module", enforce: "pre", configResolved(config) { defaultResolvePlugin = config.plugins.find((i) => i.name === "vite:resolve"); resolvedConfig = config; }, async resolveId(id, importer, resolveOpts) { if (!(defaultResolvePlugin == null ? void 0 : defaultResolvePlugin.resolveId)) return null; if (id.startsWith("/") && cssModuleMap.has(getFullPath(id))) return getFullPath(id); if (!include(id)) return null; if (id.startsWith("/")) { const fullPath = getFullPath(id); if (cssModuleMap.has(fullPath)) return fullPath; } const result = await defaultResolvePlugin.resolveId.call(this, id, importer, resolveOpts || {}); if (!result) return null; let resolvedPath; if (typeof result === "string") resolvedPath = result; else resolvedPath = result.id; const p = path.parse(resolvedPath); const newPath = path.format(__spreadProps(__spreadValues({}, p), { base: `${p.name}.module${p.ext}` })); cssModuleMap.set(newPath, resolvedPath); originFileMap.set(resolvedPath, newPath); if (!watchFiles.has(resolvedPath)) { watchFiles.add(resolvedPath); this.addWatchFile(resolvedPath); } return newPath; }, handleHotUpdate(hmrContext) { const { file, server } = hmrContext; if (!originFileMap.has(file)) return; const { config: { root } } = server; const cssModuleFile = originFileMap.get(file); let modules = server.moduleGraph.getModulesByFile(cssModuleFile); if (!modules && cssModuleFile.startsWith(root)) { modules = server.moduleGraph.getModulesByFile(cssModuleFile.replace(root, "")); } return [...modules || []]; }, async load(id) { const filePath = getFilePath(id); if (filePath) { try { return await fs.readFile(cleanUrl(filePath), "utf-8"); } catch (e) { return fs.readFile(filePath, "utf-8"); } } } }; } export { loadCssModuleFile as default };