'use strict'; const changeCase = require('change-case'); const esModuleLexer = require('es-module-lexer'); const fs = require('fs'); const module$1 = require('module'); const path = require('path'); const vite = require('vite'); function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; } function _interopNamespaceCompat(e) { if (e && typeof e === 'object' && 'default' in e) return e; const n = Object.create(null); if (e) { for (const k in e) { n[k] = e[k]; } } n.default = e; return n; } const changeCase__namespace = /*#__PURE__*/_interopNamespaceCompat(changeCase); const fs__default = /*#__PURE__*/_interopDefaultCompat(fs); const path__default = /*#__PURE__*/_interopDefaultCompat(path); function fileExists(file) { try { return fs__default.statSync(file).isFile(); } catch (e) { console.error(`${file} not found`); return false; } } function isFunction(value) { return value && Object.prototype.toString.call(value) === "[object Function]"; } function resolveNodeModules(libName) { const esRequire = module$1.createRequire(process.cwd() + path__default.sep + "node_modules"); let modulePath = ""; try { modulePath = vite.normalizePath(esRequire.resolve(libName)); } catch (error) { return ""; } return modulePath; } const asRE = /\s+as\s+\w+,?/g; function createImportPlugin(options) { const libs = Array.isArray(options) ? options : [options]; const libMap = /* @__PURE__ */ new Map(); libs.forEach((item) => { libMap.set(item.libraryName, item); }); let viteConfig; let needSourcemap = false; return { name: "vitejs:importer", async configResolved(resolvedConfig) { viteConfig = resolvedConfig; needSourcemap = !!viteConfig.build?.sourcemap; }, async transform(code) { if (!code || !needTransform(code, libs)) { return null; } await esModuleLexer.init; let handledCode = code; let imports = []; try { imports = esModuleLexer.parse(code)[0]; } catch (e) { console.error(e); } if (!imports.length) { return null; } for (const importer of imports) { const { n, se, ss } = importer; if (!n) continue; const lib = libMap.get(n); if (!lib) continue; const importStr = code.slice(ss, se); let importVariables = transformImportVar(importStr); importVariables = filterImportVariables(importVariables, lib.importTest); const importCssStrList = await transformComponentCss(lib, importVariables, viteConfig); const { base = "" } = lib; let baseImporter = base ? ` import '${base}';` : "\n"; if (code.includes(base)) { baseImporter = "\n"; } importCssStrList.unshift(baseImporter); handledCode = handledCode.replace(new RegExp(importStr, "ig"), importStr + importCssStrList.join("")); } return { map: needSourcemap ? this.getCombinedSourcemap() : null, code: handledCode }; } }; } function filterImportVariables(importVars, reg) { if (!reg) { return importVars; } return importVars.filter((item) => reg.test(item)); } async function transformComponentCss(lib, importVariables, viteConfig) { const { libraryName, resolveStyle, libraryNameChangeCase = "paramCase", ensureStyleFile = false } = lib; const ensureStyleFileFound = ensureStyleFile || viteConfig.command === "build"; if (!isFunction(resolveStyle) || !libraryName) { return []; } const set = /* @__PURE__ */ new Set(); for (const importVariable of importVariables) { const name = getChangeCaseFileName(importVariable, libraryNameChangeCase); let importStr = resolveStyle(name); if (!importStr) { continue; } importStr = resolveNodeModules(importStr); let isAdd = true; if (ensureStyleFileFound) { isAdd = fileExists(importStr); } isAdd && set.add(`import '${importStr}'; `); } return Array.from(set); } function transformImportVar(importStr) { if (!importStr) { return []; } const exportStr = importStr.replace("import", "export").replace(asRE, ","); let importVariables = []; try { importVariables = esModuleLexer.parse(exportStr)[1].map((item) => item.n); } catch (error) { console.error(error); } return importVariables; } function getChangeCaseFileName(importedName, libraryNameChangeCase) { try { return changeCase__namespace[libraryNameChangeCase](importedName); } catch (error) { return importedName; } } function needTransform(code, libs) { return !libs.every(({ libraryName }) => { return !new RegExp(`('${libraryName}')|("${libraryName}")`).test(code); }); } module.exports = createImportPlugin;