UNPKG

weapp-tailwindcss

Version:

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

784 lines (749 loc) 27.2 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _chunkORSWL3MIjs = require('./chunk-ORSWL3MI.js'); var _chunkLTJQUORKjs = require('./chunk-LTJQUORK.js'); var _chunkIGTIMGCPjs = require('./chunk-IGTIMGCP.js'); var _chunkA2OSQ5CVjs = require('./chunk-A2OSQ5CV.js'); var _chunkFMBPNII7js = require('./chunk-FMBPNII7.js'); // src/bundlers/vite/index.ts var _buffer = require('buffer'); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _process = require('process'); var _process2 = _interopRequireDefault(_process); var _htmltransform = require('@weapp-tailwindcss/postcss/html-transform'); var _htmltransform2 = _interopRequireDefault(_htmltransform); // src/uni-app-x/transform.ts var _compilerdom = require('@vue/compiler-dom'); var _compilersfc = require('@vue/compiler-sfc'); var _magicstring = require('magic-string'); var _magicstring2 = _interopRequireDefault(_magicstring); function traverse(node, visitor) { visitor(node); if (Array.isArray(node.children)) { for (const child of node.children) { if (child && typeof child === "object" && "type" in child) { traverse(child, visitor); } } } } function updateStaticAttribute(ms, prop) { if (!prop.value) { return; } const start = prop.value.loc.start.offset + 1; const end = prop.value.loc.end.offset - 1; if (start < end) { ms.update(start, end, _chunkA2OSQ5CVjs.replaceWxml.call(void 0, prop.value.content)); } } function updateDirectiveExpression(ms, prop, jsHandler, runtimeSet) { if (_optionalChain([prop, 'access', _ => _.exp, 'optionalAccess', _2 => _2.type]) !== _compilerdom.NodeTypes.SIMPLE_EXPRESSION) { return; } const start = prop.exp.loc.start.offset; const end = prop.exp.loc.end.offset; if (start >= end) { return; } const generated = _chunkA2OSQ5CVjs.generateCode.call(void 0, prop.exp.content, { jsHandler, runtimeSet, wrapExpression: true }); ms.update(start, end, generated); } function shouldHandleAttribute(tag, attrName, disabledDefaultTemplateHandler, matchCustomAttribute) { const lowerName = attrName.toLowerCase(); const shouldHandleDefault = !disabledDefaultTemplateHandler && lowerName === "class"; const shouldHandleCustom = _nullishCoalesce(_optionalChain([matchCustomAttribute, 'optionalCall', _3 => _3(tag, attrName)]), () => ( false)); return { shouldHandleDefault, shouldHandleCustom, shouldHandle: shouldHandleDefault || shouldHandleCustom }; } var defaultCreateJsHandlerOptions = { babelParserOptions: { plugins: [ "typescript" ] } }; function transformUVue(code, id, jsHandler, runtimeSet, options = {}) { if (!/\.(?:uvue|nvue)(?:\?.*)?$/.test(id)) { return; } const { customAttributesEntities, disabledDefaultTemplateHandler = false } = options; const matchCustomAttribute = _chunkA2OSQ5CVjs.createAttributeMatcher.call(void 0, customAttributesEntities); const ms = new (0, _magicstring2.default)(code); const { descriptor, errors } = _compilersfc.parse.call(void 0, code); if (errors.length === 0) { if (_optionalChain([descriptor, 'access', _4 => _4.template, 'optionalAccess', _5 => _5.ast])) { traverse(descriptor.template.ast, (node) => { if (node.type !== _compilerdom.NodeTypes.ELEMENT) { return; } const tag = node.tag; for (const prop of node.props) { if (prop.type === _compilerdom.NodeTypes.ATTRIBUTE) { const { shouldHandle, shouldHandleDefault } = shouldHandleAttribute( tag, prop.name, disabledDefaultTemplateHandler, matchCustomAttribute ); if (!shouldHandle) { continue; } updateStaticAttribute(ms, prop); if (shouldHandleDefault) { continue; } } else if (prop.type === _compilerdom.NodeTypes.DIRECTIVE && prop.name === "bind" && _optionalChain([prop, 'access', _6 => _6.arg, 'optionalAccess', _7 => _7.type]) === _compilerdom.NodeTypes.SIMPLE_EXPRESSION && prop.arg.isStatic) { const attrName = prop.arg.content; const { shouldHandle } = shouldHandleAttribute( tag, attrName, disabledDefaultTemplateHandler, matchCustomAttribute ); if (!shouldHandle) { continue; } updateDirectiveExpression(ms, prop, jsHandler, runtimeSet); } } }); } if (descriptor.script) { const { code: code2 } = jsHandler(descriptor.script.content, _nullishCoalesce(runtimeSet, () => ( /* @__PURE__ */ new Set())), defaultCreateJsHandlerOptions); ms.update(descriptor.script.loc.start.offset, descriptor.script.loc.end.offset, code2); } if (descriptor.scriptSetup) { const { code: code2 } = jsHandler(descriptor.scriptSetup.content, _nullishCoalesce(runtimeSet, () => ( /* @__PURE__ */ new Set())), defaultCreateJsHandlerOptions); ms.update(descriptor.scriptSetup.loc.start.offset, descriptor.scriptSetup.loc.end.offset, code2); } } return { code: ms.toString(), // @ts-ignore get map() { return ms.generateMap(); } }; } // src/bundlers/vite/query.ts function parseVueRequest(id) { const [filename, rawQuery] = id.split(`?`, 2); const searchParams = new URLSearchParams(rawQuery); const query = Object.fromEntries(searchParams); if (query.vue != null) { query.vue = true; } if (query.index != null) { query.index = Number(query.index); } if (query.raw != null) { query.raw = true; } if (query.url != null) { query.url = true; } if (query.scoped != null) { query.scoped = true; } const langTypeMatch = [...searchParams.keys()].find((key) => key.startsWith("lang.")); const langType = query.lang || (langTypeMatch ? langTypeMatch.slice("lang.".length) : void 0); if (langType) { query.lang = langType; } return { filename, query }; } // src/bundlers/vite/utils.ts function slash(p) { return p.replace(/\\/g, "/"); } var isWindows = _process2.default.platform === "win32"; var cssLangs = `\\.(css|less|sass|scss|styl|stylus|pcss|postcss)($|\\?)`; var cssLangRE = new RegExp(cssLangs); function isCSSRequest(request) { return cssLangRE.test(request); } function normalizePath(id) { return _path2.default.posix.normalize(isWindows ? slash(id) : id); } var postfixRE = /[?#].*$/; function cleanUrl(url) { return url.replace(postfixRE, ""); } async function formatPostcssSourceMap(rawMap, file) { const inputFileDir = _path2.default.dirname(file); const sources = rawMap.sources.map((source) => { const cleanSource = cleanUrl(decodeURIComponent(source)); if (cleanSource[0] === "<" && cleanSource.endsWith(">")) { return `\0${cleanSource}`; } return normalizePath(_path2.default.resolve(inputFileDir, cleanSource)); }); return { file, mappings: rawMap.mappings, names: rawMap.names, sources, sourcesContent: rawMap.sourcesContent, version: rawMap.version }; } // src/uni-app-x/vite.ts var preprocessorLangs = /* @__PURE__ */ new Set(["scss", "sass", "less", "styl", "stylus"]); function isPreprocessorRequest(id, lang) { if (lang && preprocessorLangs.has(lang)) { return true; } return /\.(?:scss|sass|less|styl|stylus)(?:\?|$)/.test(id); } function createUniAppXPlugins(options) { const { appType, customAttributesEntities, disabledDefaultTemplateHandler, mainCssChunkMatcher, runtimeState, styleHandler, jsHandler, ensureRuntimeClassSet, getResolvedConfig } = options; const cssPlugins = [void 0, "pre"].map((enforce) => ({ name: `weapp-tailwindcss:uni-app-x:css${enforce ? `:${enforce}` : ""}`, enforce, async transform(code, id) { await runtimeState.patchPromise; const { query } = parseVueRequest(id); const lang = query.lang; if (enforce === "pre" && isPreprocessorRequest(id, lang)) { return; } if (isCSSRequest(id) || query.vue && query.type === "style") { const postcssResult = await styleHandler(code, { isMainChunk: mainCssChunkMatcher(id, appType), postcssOptions: { options: { from: id, map: { inline: false, annotation: false, // PostCSS 可能返回虚拟文件,因此需要启用这一项以获取源内容 sourcesContent: true // 若上游预处理器已经生成 source map,sources 中可能出现重复条目 } } } }); const rawPostcssMap = postcssResult.map.toJSON(); const postcssMap = await formatPostcssSourceMap( rawPostcssMap, cleanUrl(id) ); return { code: postcssResult.css, map: postcssMap }; } } })); const nvuePlugin = { name: "weapp-tailwindcss:uni-app-x:nvue", enforce: "pre", async buildStart() { await ensureRuntimeClassSet(true); }, async transform(code, id) { if (!/\.(?:uvue|nvue)(?:\?.*)?$/.test(id)) { return; } const resolvedConfig = getResolvedConfig(); const isServeCommand = _optionalChain([resolvedConfig, 'optionalAccess', _8 => _8.command]) === "serve"; const isWatchBuild = _optionalChain([resolvedConfig, 'optionalAccess', _9 => _9.command]) === "build" && !!_optionalChain([resolvedConfig, 'access', _10 => _10.build, 'optionalAccess', _11 => _11.watch]); const isNonWatchBuild = _optionalChain([resolvedConfig, 'optionalAccess', _12 => _12.command]) === "build" && !_optionalChain([resolvedConfig, 'access', _13 => _13.build, 'optionalAccess', _14 => _14.watch]); const shouldForceRefresh = isServeCommand || isWatchBuild || isNonWatchBuild; const currentRuntimeSet = shouldForceRefresh ? await ensureRuntimeClassSet(true) : await ensureRuntimeClassSet(); const extraOptions = customAttributesEntities.length > 0 || disabledDefaultTemplateHandler ? { customAttributesEntities, disabledDefaultTemplateHandler } : void 0; if (extraOptions) { return transformUVue(code, id, jsHandler, currentRuntimeSet, extraOptions); } return transformUVue(code, id, jsHandler, currentRuntimeSet); }, async handleHotUpdate(ctx) { const resolvedConfig = getResolvedConfig(); if (_optionalChain([resolvedConfig, 'optionalAccess', _15 => _15.command]) !== "serve") { return; } if (!/\.(?:uvue|nvue)$/.test(ctx.file)) { return; } await ensureRuntimeClassSet(true); }, async watchChange(id) { const resolvedConfig = getResolvedConfig(); if (_optionalChain([resolvedConfig, 'optionalAccess', _16 => _16.command]) !== "build" || !_optionalChain([resolvedConfig, 'access', _17 => _17.build, 'optionalAccess', _18 => _18.watch])) { return; } if (!/\.(?:uvue|nvue)(?:\?.*)?$/.test(id)) { return; } await ensureRuntimeClassSet(true); } }; return [ ...cssPlugins, nvuePlugin ]; } function createUniAppXAssetTask(file, originalSource, outDir, options) { return async () => { const { cache, createHandlerOptions, debug: debug2, jsHandler, onUpdate, runtimeSet, applyLinkedResults: applyLinkedResults2 } = options; const absoluteFile = _chunkORSWL3MIjs.toAbsoluteOutputPath.call(void 0, file, outDir); const rawSource = originalSource.source.toString(); await _chunkLTJQUORKjs.processCachedTask.call(void 0, { cache, cacheKey: file, rawSource, applyResult(source) { originalSource.source = source; }, onCacheHit() { debug2("js cache hit: %s", file); }, async transform() { const currentSource = originalSource.source.toString(); const { code, linked } = await jsHandler(currentSource, runtimeSet, createHandlerOptions(absoluteFile, { uniAppX: _nullishCoalesce(options.uniAppX, () => ( true)), babelParserOptions: { plugins: [ "typescript" ], sourceType: "unambiguous" } })); onUpdate(file, currentSource, code); debug2("js handle: %s", file); applyLinkedResults2(linked); return { result: code }; } }); }; } // src/bundlers/vite/index.ts var debug = _chunkA2OSQ5CVjs.createDebug.call(void 0, ); var weappTailwindcssPackageDir = _chunkORSWL3MIjs.resolvePackageDir.call(void 0, "weapp-tailwindcss"); var weappTailwindcssDirPosix = slash(weappTailwindcssPackageDir); function joinPosixPath(base, subpath) { if (base.endsWith("/")) { return `${base}${subpath}`; } return `${base}/${subpath}`; } function isCssLikeImporter(importer) { if (!importer) { return false; } const normalized = cleanUrl(importer); return isCSSRequest(normalized); } function readOutputEntry(entry) { if (entry.output.type === "chunk") { return entry.output.code; } const source = entry.output.source; if (typeof source === "string") { return source; } if (source instanceof Uint8Array) { return _buffer.Buffer.from(source).toString(); } const fallbackSource = source; if (fallbackSource == null) { return void 0; } if (typeof fallbackSource.toString === "function") { return fallbackSource.toString(); } return void 0; } function isJavaScriptEntry(entry) { if (entry.output.type === "chunk") { return true; } return entry.fileName.endsWith(".js"); } function createBundleModuleGraphOptions(outputDir, entries) { return { resolve(specifier, importer) { return _chunkORSWL3MIjs.resolveOutputSpecifier.call(void 0, specifier, importer, outputDir, (candidate) => entries.has(candidate)); }, load(id) { const entry = entries.get(id); if (!entry) { return void 0; } return readOutputEntry(entry); }, filter(id) { return entries.has(id); } }; } function applyLinkedResults(linked, entries, onLinkedUpdate, onApplied) { if (!linked) { return; } for (const [id, { code }] of Object.entries(linked)) { const entry = entries.get(id); if (!entry) { continue; } const previous = readOutputEntry(entry); if (previous == null || previous === code) { continue; } if (entry.output.type === "chunk") { entry.output.code = code; } else { entry.output.source = code; } _optionalChain([onApplied, 'optionalCall', _19 => _19(entry, code)]); onLinkedUpdate(entry.fileName, previous, code); } } function UnifiedViteWeappTailwindcssPlugin(options = {}) { const rewriteCssImportsSpecified = Object.prototype.hasOwnProperty.call(options, "rewriteCssImports"); const opts = _chunkA2OSQ5CVjs.getCompilerContext.call(void 0, options); const { disabled, customAttributes, onEnd, onLoad, onStart, onUpdate, templateHandler, styleHandler, jsHandler, mainCssChunkMatcher, appType, cache, twPatcher: initialTwPatcher, refreshTailwindcssPatcher, uniAppX, disabledDefaultTemplateHandler } = opts; const disabledOptions = _chunkORSWL3MIjs.resolveDisabledOptions.call(void 0, disabled); const tailwindcssMajorVersion = _nullishCoalesce(initialTwPatcher.majorVersion, () => ( 0)); const shouldRewriteCssImports = opts.rewriteCssImports !== false && !disabledOptions.rewriteCssImports && (rewriteCssImportsSpecified || tailwindcssMajorVersion >= 4); const rewritePlugins = !shouldRewriteCssImports ? [] : [ { name: `${_chunkA2OSQ5CVjs.vitePluginName}:rewrite-css-imports`, enforce: "pre", resolveId: { order: "pre", handler(id, importer) { const replacement = _chunkORSWL3MIjs.resolveTailwindcssImport.call(void 0, id, weappTailwindcssDirPosix, { join: joinPosixPath, appType }); if (!replacement) { return null; } if (importer && !isCssLikeImporter(importer)) { return null; } return replacement; } }, transform: { order: "pre", handler(code, id) { if (!isCSSRequest(id)) { return null; } const rewritten = _chunkORSWL3MIjs.rewriteTailwindcssImportsInCode.call(void 0, code, weappTailwindcssDirPosix, { join: joinPosixPath, appType }); if (!rewritten) { return null; } return { code: rewritten, map: null }; } } } ]; if (disabledOptions.plugin) { return rewritePlugins.length ? rewritePlugins : void 0; } const customAttributesEntities = _chunkA2OSQ5CVjs.toCustomAttributesEntities.call(void 0, customAttributes); const patchRecorderState = _chunkIGTIMGCPjs.setupPatchRecorder.call(void 0, initialTwPatcher, opts.tailwindcssBasedir, { source: "runtime", cwd: _nullishCoalesce(opts.tailwindcssBasedir, () => ( _process2.default.cwd())) }); const runtimeState = { twPatcher: initialTwPatcher, patchPromise: patchRecorderState.patchPromise, refreshTailwindcssPatcher, onPatchCompleted: patchRecorderState.onPatchCompleted }; let runtimeSet; let runtimeSetPromise; let resolvedConfig; async function refreshRuntimeState(force) { const refreshed = await _chunkA2OSQ5CVjs.refreshTailwindRuntimeState.call(void 0, runtimeState, force); if (refreshed) { runtimeSet = void 0; runtimeSetPromise = void 0; } } async function ensureRuntimeClassSet(force = false) { await refreshRuntimeState(force); await runtimeState.patchPromise; if (!force && runtimeSet) { return runtimeSet; } if (force || !runtimeSetPromise) { const task2 = _chunkA2OSQ5CVjs.collectRuntimeClassSet.call(void 0, runtimeState.twPatcher, { force: force || !runtimeSet, skipRefresh: force }); runtimeSetPromise = task2; } const task = runtimeSetPromise; try { runtimeSet = await task; return runtimeSet; } finally { if (runtimeSetPromise === task) { runtimeSetPromise = void 0; } } } onLoad(); const getResolvedConfig = () => resolvedConfig; const uniAppXPlugins = uniAppX ? createUniAppXPlugins({ appType, customAttributesEntities, disabledDefaultTemplateHandler, mainCssChunkMatcher, runtimeState, styleHandler, jsHandler, ensureRuntimeClassSet, getResolvedConfig }) : void 0; const plugins = [ ...rewritePlugins, { name: `${_chunkA2OSQ5CVjs.vitePluginName}:post`, enforce: "post", configResolved(config) { resolvedConfig = 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, _htmltransform2.default.call(void 0, )); debug("remove postcss-html-transform plugin from vite config"); } } }, async generateBundle(_opt, bundle) { await runtimeState.patchPromise; debug("start"); onStart(); const entries = Object.entries(bundle); const rootDir = _optionalChain([resolvedConfig, 'optionalAccess', _20 => _20.root]) ? _path2.default.resolve(resolvedConfig.root) : _process2.default.cwd(); const outDir = _optionalChain([resolvedConfig, 'optionalAccess', _21 => _21.build, 'optionalAccess', _22 => _22.outDir]) ? _path2.default.resolve(rootDir, resolvedConfig.build.outDir) : rootDir; const jsEntries = /* @__PURE__ */ new Map(); for (const [fileName, output] of entries) { const entry = { fileName, output }; if (isJavaScriptEntry(entry)) { const absolute = _chunkORSWL3MIjs.toAbsoluteOutputPath.call(void 0, fileName, outDir); jsEntries.set(absolute, entry); } } const moduleGraphOptions = createBundleModuleGraphOptions(outDir, jsEntries); const groupedEntries = _chunkFMBPNII7js.getGroupedEntries.call(void 0, entries, opts); const runtime = await ensureRuntimeClassSet(true); debug("get runtimeSet, class count: %d", runtime.size); const handleLinkedUpdate = (fileName, previous, next) => { onUpdate(fileName, previous, next); debug("js linked handle: %s", fileName); }; const pendingLinkedUpdates = []; const scheduleLinkedApply = (entry, code) => { pendingLinkedUpdates.push(() => { if (entry.output.type === "chunk") { entry.output.code = code; } else { entry.output.source = code; } }); }; const applyLinkedUpdates = (linked) => { applyLinkedResults(linked, jsEntries, handleLinkedUpdate, scheduleLinkedApply); }; const createHandlerOptions = (absoluteFilename, extra) => ({ ...extra, filename: absoluteFilename, moduleGraph: moduleGraphOptions, babelParserOptions: { ..._nullishCoalesce(_optionalChain([extra, 'optionalAccess', _23 => _23.babelParserOptions]), () => ( {})), sourceFilename: absoluteFilename } }); const tasks = []; if (Array.isArray(groupedEntries.html)) { for (const [file, originalSource] of groupedEntries.html) { const rawSource = originalSource.source.toString(); tasks.push( _chunkLTJQUORKjs.processCachedTask.call(void 0, { cache, cacheKey: file, rawSource, applyResult(source) { originalSource.source = source; }, onCacheHit() { debug("html cache hit: %s", file); }, async transform() { const transformed = await templateHandler(rawSource, { runtimeSet: runtime }); onUpdate(file, rawSource, transformed); debug("html handle: %s", file); return { result: transformed }; } }) ); } } const jsTaskFactories = []; if (Array.isArray(groupedEntries.js)) { for (const [file, originalSource] of groupedEntries.js) { if (originalSource.type === "chunk") { const absoluteFile = _chunkORSWL3MIjs.toAbsoluteOutputPath.call(void 0, file, outDir); const initialRawSource = originalSource.code; jsTaskFactories.push(async () => { await _chunkLTJQUORKjs.processCachedTask.call(void 0, { cache, cacheKey: file, rawSource: initialRawSource, applyResult(source) { originalSource.code = source; }, onCacheHit() { debug("js cache hit: %s", file); }, async transform() { const rawSource = originalSource.code; const { code, linked } = await jsHandler(rawSource, runtime, createHandlerOptions(absoluteFile)); onUpdate(file, rawSource, code); debug("js handle: %s", file); applyLinkedUpdates(linked); return { result: code }; } }); }); } else if (uniAppX && originalSource.type === "asset") { jsTaskFactories.push( createUniAppXAssetTask( file, originalSource, outDir, { cache, createHandlerOptions, debug, jsHandler, onUpdate, runtimeSet: runtime, applyLinkedResults: applyLinkedUpdates, uniAppX } ) ); } } } if (Array.isArray(groupedEntries.css)) { for (const [file, originalSource] of groupedEntries.css) { const rawSource = originalSource.source.toString(); tasks.push( _chunkLTJQUORKjs.processCachedTask.call(void 0, { cache, cacheKey: file, rawSource, applyResult(source) { originalSource.source = source; }, onCacheHit() { debug("css cache hit: %s", file); }, async transform() { await runtimeState.patchPromise; const { css } = await styleHandler(rawSource, { isMainChunk: mainCssChunkMatcher(originalSource.fileName, appType), postcssOptions: { options: { from: file } }, majorVersion: runtimeState.twPatcher.majorVersion }); onUpdate(file, rawSource, css); debug("css handle: %s", file); return { result: css }; } }) ); } } _chunkORSWL3MIjs.pushConcurrentTaskFactories.call(void 0, tasks, jsTaskFactories); await Promise.all(tasks); for (const apply of pendingLinkedUpdates) { apply(); } onEnd(); debug("end"); } } ]; if (uniAppXPlugins) { plugins.push(...uniAppXPlugins); } return plugins; } exports.UnifiedViteWeappTailwindcssPlugin = UnifiedViteWeappTailwindcssPlugin;