weapp-tailwindcss
Version:
把 tailwindcss 原子化样式思想,带给小程序开发者们! bring tailwindcss to miniprogram developers!
172 lines (166 loc) • 4.97 kB
JavaScript
// src/bundlers/shared/module-graph.ts
import path from "path";
var QUERY_HASH_RE = /[?#].*$/u;
var PROTOCOL_RE = /^[a-z][a-z+.-]*:/iu;
var VIRTUAL_PREFIX = "\0";
var JS_EXTENSIONS = [".js", ".mjs", ".cjs"];
function stripQueryAndHash(specifier) {
return specifier.replace(QUERY_HASH_RE, "");
}
function isResolvableSpecifier(specifier) {
if (!specifier) {
return false;
}
const normalized = stripQueryAndHash(specifier);
if (normalized.startsWith(VIRTUAL_PREFIX)) {
return false;
}
return !PROTOCOL_RE.test(normalized);
}
function toAbsoluteOutputPath(fileName, outDir) {
if (path.isAbsolute(fileName)) {
return fileName;
}
return path.resolve(outDir, fileName);
}
function matchWithExtensions(candidate, hasOutput) {
if (hasOutput(candidate)) {
return candidate;
}
if (!path.extname(candidate)) {
for (const ext of JS_EXTENSIONS) {
const extended = `${candidate}${ext}`;
if (hasOutput(extended)) {
return extended;
}
}
}
return void 0;
}
function resolveOutputSpecifier(specifier, importer, outDir, hasOutput) {
if (!isResolvableSpecifier(specifier)) {
return void 0;
}
const normalized = stripQueryAndHash(specifier);
let candidate;
if (path.isAbsolute(normalized)) {
candidate = normalized;
} else if (normalized.startsWith("/")) {
candidate = path.resolve(outDir, normalized.slice(1));
} else {
candidate = path.resolve(path.dirname(importer), normalized);
}
return matchWithExtensions(candidate, hasOutput);
}
// src/utils/disabled.ts
function resolveDisabledOptions(disabled) {
if (disabled === true) {
return { plugin: true, rewriteCssImports: false };
}
if (disabled === false || disabled == null) {
return { plugin: false, rewriteCssImports: false };
}
return {
plugin: disabled.plugin ?? false,
rewriteCssImports: disabled.rewriteCssImports ?? false
};
}
// src/utils/resolve-package.ts
import { createRequire } from "module";
import path2 from "path";
var require2 = createRequire(import.meta.url);
function resolvePackageDir(name) {
const pkgPath = require2.resolve(`${name}/package.json`);
return path2.dirname(pkgPath);
}
// src/bundlers/shared/css-imports.ts
import path3 from "path";
var tailwindcssImportRE = /^tailwindcss(?:\/.*)?$/;
var tailwindcssCssImportStatementRE = /(@import\s+(?:url\(\s*)?)(["'])(tailwindcss(?:\/[^"']*)?\$?)(\2\s*\)?)/gi;
function normalizeTailwindcssSpecifier(specifier) {
if (specifier === "tailwindcss$") {
return "tailwindcss";
}
return specifier;
}
function getTailwindcssSubpath(specifier) {
if (specifier === "tailwindcss") {
return "index.css";
}
return specifier.slice("tailwindcss/".length);
}
function resolveTailwindcssImport(specifier, pkgDir, options) {
const normalized = normalizeTailwindcssSpecifier(specifier);
if (!tailwindcssImportRE.test(normalized)) {
return null;
}
if (normalized === "tailwindcss") {
return "weapp-tailwindcss/index.css";
}
const join = options?.join ?? path3.join;
const subpath = getTailwindcssSubpath(normalized);
return join(pkgDir, subpath);
}
function rewriteTailwindcssImportsInCode(code, pkgDir, options) {
let hasReplacements = false;
const rewritten = code.replace(
tailwindcssCssImportStatementRE,
(full, prefix, quote, specifier, suffix) => {
const replacement = resolveTailwindcssImport(specifier, pkgDir, options);
if (!replacement) {
return full;
}
hasReplacements = true;
return `${prefix}${quote}${replacement}${suffix}`;
}
);
return hasReplacements ? rewritten : void 0;
}
// src/bundlers/shared/run-tasks.ts
async function runWithConcurrency(factories, limit = Math.min(4, Math.max(1, factories.length))) {
if (factories.length === 0) {
return [];
}
const results = Array.from({ length: factories.length });
const executing = /* @__PURE__ */ new Set();
let cursor = 0;
const effectiveLimit = Math.max(1, limit);
const scheduleNext = () => {
if (cursor >= factories.length) {
return;
}
const currentIndex = cursor++;
const wrapped = Promise.resolve(factories[currentIndex]()).then((value) => {
results[currentIndex] = value;
}).finally(() => {
executing.delete(wrapped);
});
executing.add(wrapped);
};
while (cursor < factories.length && executing.size < effectiveLimit) {
scheduleNext();
}
while (cursor < factories.length) {
await Promise.race(executing);
scheduleNext();
}
await Promise.all(executing);
return results;
}
function pushConcurrentTaskFactories(queue, factories, limit) {
if (factories.length === 0) {
return;
}
queue.push(
runWithConcurrency(factories, limit).then(() => void 0)
);
}
export {
toAbsoluteOutputPath,
resolveOutputSpecifier,
resolveDisabledOptions,
resolvePackageDir,
resolveTailwindcssImport,
rewriteTailwindcssImportsInCode,
pushConcurrentTaskFactories
};