UNPKG

weapp-tailwindcss

Version:

把 tailwindcss 原子化样式思想,带给小程序开发者们! bring tailwindcss to miniprogram developers!

172 lines (169 loc) 5.13 kB
import { createDebug } from "./chunk-3AUX4FGE.mjs"; import { vitePluginName } from "./chunk-CMUA5KCO.mjs"; import { getCompilerContext } from "./chunk-XRPBGBW5.mjs"; import { getGroupedEntries } from "./chunk-JXBLHLFR.mjs"; // src/bundlers/vite/index.ts var debug = createDebug(); function UnifiedViteWeappTailwindcssPlugin(options = {}) { const opts = getCompilerContext(options); const { disabled, onEnd, onLoad, onStart, onUpdate, templateHandler, styleHandler, jsHandler, mainCssChunkMatcher, appType, setMangleRuntimeSet, cache, twPatcher } = opts; if (disabled) { return; } twPatcher.patch(); onLoad(); return { name: vitePluginName, enforce: "post", async generateBundle(_opt, bundle) { debug("start"); onStart(); const entries = Object.entries(bundle); const groupedEntries = getGroupedEntries(entries, opts); const runtimeSet = await twPatcher.getClassSet(); setMangleRuntimeSet(runtimeSet); debug("get runtimeSet, class count: %d", runtimeSet.size); const promises = []; if (Array.isArray(groupedEntries.html)) { for (const element of groupedEntries.html) { const [file, originalSource] = element; const oldVal = originalSource.source.toString(); const hash = cache.computeHash(oldVal); cache.calcHashValueChanged(file, hash); promises.push( cache.process( file, () => { const source = cache.get(file); if (source) { originalSource.source = source; debug("html cache hit: %s", file); } else { return false; } }, async () => { originalSource.source = await templateHandler(oldVal, { runtimeSet }); onUpdate(file, oldVal, originalSource.source); debug("html handle: %s", file); return { key: file, source: originalSource.source }; } ) ); } } if (Array.isArray(groupedEntries.js)) { for (const element of groupedEntries.js.filter((x) => x[1].type === "chunk")) { const [file, originalSource] = element; const rawSource = originalSource.code; const hash = cache.computeHash(rawSource); cache.calcHashValueChanged(file, hash); promises.push( cache.process( file, () => { const source = cache.get(file); if (source) { originalSource.code = source; debug("js cache hit: %s", file); } else { return false; } }, async () => { const mapFilename = `${file}.map`; const hasSourceMap = Boolean(bundle[mapFilename]); const { code, map } = await jsHandler(rawSource, runtimeSet, { generateMap: hasSourceMap }); originalSource.code = code; onUpdate(file, rawSource, code); debug("js handle: %s", file); if (hasSourceMap && map) { ; bundle[mapFilename].source = map.toString(); } return { key: file, source: code }; } ) ); } } if (Array.isArray(groupedEntries.css)) { for (const element of groupedEntries.css) { const [file, originalSource] = element; const rawSource = originalSource.source.toString(); const hash = cache.computeHash(rawSource); cache.calcHashValueChanged(file, hash); promises.push( cache.process( file, () => { const source = cache.get(file); if (source) { originalSource.source = source; debug("css cache hit: %s", file); } else { return false; } }, async () => { const { css } = await styleHandler(rawSource, { isMainChunk: mainCssChunkMatcher(originalSource.fileName, appType), postcssOptions: { options: { from: file } } }); originalSource.source = css; onUpdate(file, rawSource, css); debug("css handle: %s", file); return { key: file, source: css }; } ) ); } } await Promise.all(promises); onEnd(); debug("end"); } }; } export { UnifiedViteWeappTailwindcssPlugin };