UNPKG

weapp-tailwindcss-webpack-plugin

Version:

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

270 lines (256 loc) 8.91 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); var _chunkLSSLYD6Bjs = require('./chunk-LSSLYD6B.js'); var _chunkOGROHM4Ljs = require('./chunk-OGROHM4L.js'); var _chunk2B6ZPTTSjs = require('./chunk-2B6ZPTTS.js'); var _chunkO335YLYHjs = require('./chunk-O335YLYH.js'); // src/bundlers/vite/index.ts var debug = _chunkLSSLYD6Bjs.createDebug.call(void 0, ); var cssLangs = `\\.(css|less|sass|scss|styl|stylus|pcss|postcss)($|\\?)`; var cssLangRE = new RegExp(cssLangs); function isCSSRequest(request) { return cssLangRE.test(request); } function UnifiedViteWeappTailwindcssPlugin(options = {}) { const opts = _chunk2B6ZPTTSjs.getCompilerContext.call(void 0, options); const { disabled, onEnd, onLoad, onStart, onUpdate, templateHandler, styleHandler, jsHandler, mainCssChunkMatcher, appType, setMangleRuntimeSet, cache, twPatcher, uniAppX } = opts; if (disabled) { return; } twPatcher.patch(); onLoad(); const plugins = [ // { // name: `${vitePluginName}:pre`, // enforce: 'pre', // }, { name: `${_chunkOGROHM4Ljs.vitePluginName}:post`, enforce: "post", configResolved(config) { if (typeof config.css.postcss === "object" && Array.isArray(config.css.postcss.plugins)) { const idx = config.css.postcss.plugins.findIndex((x) => ( // @ts-ignore x.postcssPlugin === "postcss-html-transform" )); if (idx !== -1) { config.css.postcss.plugins.splice(idx, 1); debug("remove postcss-html-transform plugin from vite config"); } } }, async generateBundle(_opt, bundle) { debug("start"); onStart(); const entries = Object.entries(bundle); const groupedEntries = _chunkO335YLYHjs.getGroupedEntries.call(void 0, 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 (uniAppX) { for (const element of groupedEntries.js.filter((x) => x[1].type === "asset")) { 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("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, uniAppX, babelParserOptions: { plugins: [ "typescript" ], sourceType: "unambiguous" } }); originalSource.source = 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 } }, majorVersion: twPatcher.majorVersion }); originalSource.source = css; onUpdate(file, rawSource, css); debug("css handle: %s", file); return { key: file, source: css }; } ) ); } } await Promise.all(promises); onEnd(); debug("end"); } } ]; if (uniAppX) { plugins.push( { name: "weapp-tailwindcss:uni-app-x:css", async transform(code, id) { if (isCSSRequest(id)) { const { css } = await styleHandler(code, { isMainChunk: mainCssChunkMatcher(id, appType), postcssOptions: { options: { from: id } } }); return { code: css // map, }; } } } ); } return plugins; } exports.cssLangRE = cssLangRE; exports.isCSSRequest = isCSSRequest; exports.UnifiedViteWeappTailwindcssPlugin = UnifiedViteWeappTailwindcssPlugin;