weapp-tailwindcss
Version:
把 tailwindcss 原子化样式思想,带给小程序开发者们! bring tailwindcss to miniprogram developers!
98 lines (97 loc) • 4.11 kB
JavaScript
const require_rolldown_runtime = require("./rolldown-runtime-CVvi-lCc.cjs");
const require_css_loader_runtime = require("./css-loader-runtime-CSv0lKqV.cjs");
const require_runtime_registry = require("./runtime-registry-Y7NrD2WH.cjs");
const require_watch_dependencies = require("./watch-dependencies-BZGubvuJ.cjs");
let node_fs = require("node:fs");
let node_path = require("node:path");
node_path = require_rolldown_runtime.__toESM(node_path, 1);
let node_process = require("node:process");
node_process = require_rolldown_runtime.__toESM(node_process, 1);
let _weapp_tailwindcss_postcss = require("@weapp-tailwindcss/postcss");
let node_buffer = require("node:buffer");
//#region src/bundlers/webpack/loaders/weapp-tw-runtime-classset-loader.ts
function isPromiseLike(value) {
return Boolean(value && typeof value.then === "function");
}
function normalizeRuntimeCssSource(source) {
if (node_buffer.Buffer.isBuffer(source)) {
const css = source.toString("utf8");
return shouldCleanRuntimeCss(css) ? node_buffer.Buffer.from(cleanRuntimeCss(css)) : source;
}
return shouldCleanRuntimeCss(source) ? cleanRuntimeCss(source) : source;
}
function shouldCleanRuntimeCss(css) {
return css.includes("@layer") || css.includes("@theme");
}
function cleanRuntimeCss(css) {
const root = _weapp_tailwindcss_postcss.postcss.parse(css);
(0, _weapp_tailwindcss_postcss.removeUnsupportedCascadeLayers)(root);
removeUnsupportedThemeKeyframes(root);
return root.toString();
}
function removeUnsupportedThemeKeyframes(root) {
root.walkAtRules("theme", (themeRule) => {
themeRule.walkAtRules((atRule) => {
if (atRule.name.startsWith("-") && atRule.name.endsWith("keyframes")) atRule.remove();
});
});
}
function resolveOriginalCssSource(file, source) {
if (isPlainCssResource(file)) try {
return (0, node_fs.readFileSync)(file, "utf8");
} catch {}
return node_buffer.Buffer.isBuffer(source) ? source.toString("utf8") : source;
}
function resolveRegisteredCssSource(file, source) {
if (!require_css_loader_runtime.isWebpackCssLoaderRuntimeSource(node_buffer.Buffer.isBuffer(source) ? source.toString("utf8") : source)) return resolveOriginalCssSource(file, source);
if (isPlainCssResource(file)) try {
return (0, node_fs.readFileSync)(file, "utf8");
} catch {
return;
}
}
function isPlainCssResource(file) {
return (/* @__PURE__ */ new Set([
".acss",
".css",
".jxss",
".qss",
".ttss",
".wxss"
])).has(node_path.default.extname(file.replace(/[?#].*$/, "")).toLowerCase());
}
const WeappTwRuntimeClassSetLoader = function(source) {
if (node_process.default.env["WEAPP_TW_LOADER_DEBUG"]) node_process.default.stdout.write(`[weapp-tw-runtime-classset-loader] executing for ${this.resourcePath}\n`);
const rawOptions = this.getOptions();
const opt = require_runtime_registry.getWebpackLoaderRuntime(rawOptions?.weappTailwindcssRuntimeKey)?.classSet ?? rawOptions;
if (this.resourcePath) {
opt?.updateGeneratedCss?.({
file: this.resourcePath,
css: node_buffer.Buffer.isBuffer(source) ? source.toString("utf8") : source
});
opt?.registerCssSourceFile?.({
file: this.resourcePath,
css: resolveRegisteredCssSource(this.resourcePath, source)
});
}
const maybePromise = opt?.getClassSet?.();
const applyWatchDependencies = (dependencies) => {
for (const file of dependencies?.files ?? []) require_watch_dependencies.registerWebpackWatchFile(this, file);
for (const context of dependencies?.contexts ?? []) require_watch_dependencies.registerWebpackWatchContext(this, context);
};
const resolveWatchDependencies = () => {
const dependencies = opt?.getWatchDependencies?.();
if (isPromiseLike(dependencies)) return Promise.resolve(dependencies).then((value) => {
applyWatchDependencies(value);
});
applyWatchDependencies(dependencies);
};
if (isPromiseLike(maybePromise)) return Promise.resolve(maybePromise).then(async () => {
await resolveWatchDependencies();
return normalizeRuntimeCssSource(source);
});
resolveWatchDependencies();
return normalizeRuntimeCssSource(source);
};
//#endregion
module.exports = WeappTwRuntimeClassSetLoader;