weapp-tailwindcss
Version:
把 tailwindcss 原子化样式思想,带给小程序开发者们! bring tailwindcss to miniprogram developers!
231 lines (228 loc) • 6.44 kB
JavaScript
import {
processCachedTask
} from "./chunk-RRHPTTCP.mjs";
import {
setupPatchRecorder
} from "./chunk-GIUNRP65.mjs";
import {
collectRuntimeClassSet,
createDebug,
getCompilerContext,
refreshTailwindRuntimeState
} from "./chunk-4Z6MHSEO.mjs";
// src/bundlers/gulp/index.ts
import { Buffer } from "buffer";
import fs from "fs";
import path from "path";
import process from "process";
import stream from "stream";
var debug = createDebug();
var Transform = stream.Transform;
function createPlugins(options = {}) {
const opts = getCompilerContext(options);
const { templateHandler, styleHandler, jsHandler, cache, twPatcher: initialTwPatcher, refreshTailwindcssPatcher } = opts;
const patchRecorderState = setupPatchRecorder(initialTwPatcher, opts.tailwindcssBasedir, {
source: "runtime",
cwd: opts.tailwindcssBasedir ?? process.cwd()
});
let runtimeSet = /* @__PURE__ */ new Set();
const runtimeState = {
twPatcher: initialTwPatcher,
patchPromise: patchRecorderState.patchPromise,
refreshTailwindcssPatcher,
onPatchCompleted: patchRecorderState.onPatchCompleted
};
const MODULE_EXTENSIONS = [".js", ".mjs", ".cjs", ".ts", ".tsx", ".jsx"];
let runtimeSetInitialized = false;
async function refreshRuntimeState(force) {
await refreshTailwindRuntimeState(runtimeState, force);
}
async function refreshRuntimeSet(force = false) {
await refreshRuntimeState(force);
await runtimeState.patchPromise;
if (!force && runtimeSetInitialized && runtimeSet.size > 0) {
return runtimeSet;
}
runtimeSet = await collectRuntimeClassSet(runtimeState.twPatcher, { force, skipRefresh: force });
runtimeSetInitialized = true;
return runtimeSet;
}
function resolveWithExtensions(base) {
for (const ext of MODULE_EXTENSIONS) {
const candidate = `${base}${ext}`;
try {
if (fs.statSync(candidate).isFile()) {
return candidate;
}
} catch {
continue;
}
}
return void 0;
}
function resolveLocalModuleCandidate(base) {
try {
const stat = fs.statSync(base);
if (stat.isFile()) {
return base;
}
if (stat.isDirectory()) {
const resolvedIndex = resolveWithExtensions(path.join(base, "index"));
if (resolvedIndex) {
return resolvedIndex;
}
}
} catch {
}
if (!path.extname(base)) {
return resolveWithExtensions(base);
}
return void 0;
}
function createModuleGraphOptionsFor() {
return {
resolve(specifier, importer) {
if (!specifier) {
return void 0;
}
if (!specifier.startsWith(".") && !path.isAbsolute(specifier)) {
return void 0;
}
const normalized = path.resolve(path.dirname(importer), specifier);
return resolveLocalModuleCandidate(normalized);
},
load(id) {
try {
return fs.readFileSync(id, "utf8");
} catch {
return void 0;
}
},
filter(id) {
const relative = path.relative(process.cwd(), id);
return opts.jsMatcher(relative) || opts.wxsMatcher(relative);
}
};
}
function createVinylTransform(handler) {
return new Transform({
objectMode: true,
async transform(file, _encoding, callback) {
try {
await handler(file);
callback(null, file);
} catch (error) {
callback(error, file);
}
}
});
}
const transformWxss = (options2 = {}) => createVinylTransform(async (file) => {
if (!file.contents) {
return;
}
await refreshRuntimeSet(true);
await runtimeState.patchPromise;
const rawSource = file.contents.toString();
await processCachedTask({
cache,
cacheKey: file.path,
rawSource,
applyResult(source) {
file.contents = Buffer.from(source);
},
onCacheHit() {
debug("css cache hit: %s", file.path);
},
async transform() {
await runtimeState.patchPromise;
const { css } = await styleHandler(rawSource, {
isMainChunk: true,
majorVersion: runtimeState.twPatcher.majorVersion,
...options2
});
debug("css handle: %s", file.path);
return {
result: css
};
}
});
});
const transformJs = (options2 = {}) => createVinylTransform(async (file) => {
if (!file.contents) {
return;
}
await refreshRuntimeSet(runtimeSet.size === 0);
await runtimeState.patchPromise;
const filename = path.resolve(file.path);
const moduleGraph = options2.moduleGraph ?? createModuleGraphOptionsFor();
const handlerOptions = {
...options2,
filename,
moduleGraph,
babelParserOptions: {
...options2?.babelParserOptions ?? {},
sourceFilename: filename
}
};
const rawSource = file.contents.toString();
await processCachedTask({
cache,
cacheKey: file.path,
rawSource,
applyResult(source) {
file.contents = Buffer.from(source);
},
onCacheHit() {
debug("js cache hit: %s", file.path);
},
async transform() {
await runtimeState.patchPromise;
const currentSource = file.contents?.toString() ?? rawSource;
const { code } = await jsHandler(currentSource, runtimeSet, handlerOptions);
debug("js handle: %s", file.path);
return {
result: code
};
}
});
});
const transformWxml = (options2 = {}) => createVinylTransform(async (file) => {
if (!file.contents) {
return;
}
await refreshRuntimeSet(runtimeSet.size === 0);
await runtimeState.patchPromise;
const rawSource = file.contents.toString();
await processCachedTask({
cache,
cacheKey: file.path,
rawSource,
applyResult(source) {
file.contents = Buffer.from(source);
},
onCacheHit() {
debug("html cache hit: %s", file.path);
},
async transform() {
await runtimeState.patchPromise;
const code = await templateHandler(rawSource, {
runtimeSet,
...options2
});
debug("html handle: %s", file.path);
return {
result: code
};
}
});
});
return {
transformWxss,
transformWxml,
transformJs
};
}
export {
createPlugins
};