weapp-tailwindcss
Version:
把 tailwindcss 原子化样式思想,带给小程序开发者们! bring tailwindcss to miniprogram developers!
231 lines (218 loc) • 7.99 kB
JavaScript
;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 _chunkLTJQUORKjs = require('./chunk-LTJQUORK.js');
var _chunkIGTIMGCPjs = require('./chunk-IGTIMGCP.js');
var _chunkA2OSQ5CVjs = require('./chunk-A2OSQ5CV.js');
// src/bundlers/gulp/index.ts
var _buffer = require('buffer');
var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);
var _path = require('path'); var _path2 = _interopRequireDefault(_path);
var _process = require('process'); var _process2 = _interopRequireDefault(_process);
var _stream = require('stream'); var _stream2 = _interopRequireDefault(_stream);
var debug = _chunkA2OSQ5CVjs.createDebug.call(void 0, );
var Transform = _stream2.default.Transform;
function createPlugins(options = {}) {
const opts = _chunkA2OSQ5CVjs.getCompilerContext.call(void 0, options);
const { templateHandler, styleHandler, jsHandler, cache, twPatcher: initialTwPatcher, refreshTailwindcssPatcher } = opts;
const patchRecorderState = _chunkIGTIMGCPjs.setupPatchRecorder.call(void 0, initialTwPatcher, opts.tailwindcssBasedir, {
source: "runtime",
cwd: _nullishCoalesce(opts.tailwindcssBasedir, () => ( _process2.default.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 _chunkA2OSQ5CVjs.refreshTailwindRuntimeState.call(void 0, runtimeState, force);
}
async function refreshRuntimeSet(force = false) {
await refreshRuntimeState(force);
await runtimeState.patchPromise;
if (!force && runtimeSetInitialized && runtimeSet.size > 0) {
return runtimeSet;
}
runtimeSet = await _chunkA2OSQ5CVjs.collectRuntimeClassSet.call(void 0, runtimeState.twPatcher, { force, skipRefresh: force });
runtimeSetInitialized = true;
return runtimeSet;
}
function resolveWithExtensions(base) {
for (const ext of MODULE_EXTENSIONS) {
const candidate = `${base}${ext}`;
try {
if (_fs2.default.statSync(candidate).isFile()) {
return candidate;
}
} catch (e) {
continue;
}
}
return void 0;
}
function resolveLocalModuleCandidate(base) {
try {
const stat = _fs2.default.statSync(base);
if (stat.isFile()) {
return base;
}
if (stat.isDirectory()) {
const resolvedIndex = resolveWithExtensions(_path2.default.join(base, "index"));
if (resolvedIndex) {
return resolvedIndex;
}
}
} catch (e2) {
}
if (!_path2.default.extname(base)) {
return resolveWithExtensions(base);
}
return void 0;
}
function createModuleGraphOptionsFor() {
return {
resolve(specifier, importer) {
if (!specifier) {
return void 0;
}
if (!specifier.startsWith(".") && !_path2.default.isAbsolute(specifier)) {
return void 0;
}
const normalized = _path2.default.resolve(_path2.default.dirname(importer), specifier);
return resolveLocalModuleCandidate(normalized);
},
load(id) {
try {
return _fs2.default.readFileSync(id, "utf8");
} catch (e3) {
return void 0;
}
},
filter(id) {
const relative = _path2.default.relative(_process2.default.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 _chunkLTJQUORKjs.processCachedTask.call(void 0, {
cache,
cacheKey: file.path,
rawSource,
applyResult(source) {
file.contents = _buffer.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 = _path2.default.resolve(file.path);
const moduleGraph = _nullishCoalesce(options2.moduleGraph, () => ( createModuleGraphOptionsFor()));
const handlerOptions = {
...options2,
filename,
moduleGraph,
babelParserOptions: {
..._nullishCoalesce(_optionalChain([options2, 'optionalAccess', _ => _.babelParserOptions]), () => ( {})),
sourceFilename: filename
}
};
const rawSource = file.contents.toString();
await _chunkLTJQUORKjs.processCachedTask.call(void 0, {
cache,
cacheKey: file.path,
rawSource,
applyResult(source) {
file.contents = _buffer.Buffer.from(source);
},
onCacheHit() {
debug("js cache hit: %s", file.path);
},
async transform() {
await runtimeState.patchPromise;
const currentSource = _nullishCoalesce(_optionalChain([file, 'access', _2 => _2.contents, 'optionalAccess', _3 => _3.toString, 'call', _4 => _4()]), () => ( 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 _chunkLTJQUORKjs.processCachedTask.call(void 0, {
cache,
cacheKey: file.path,
rawSource,
applyResult(source) {
file.contents = _buffer.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
};
}
exports.createPlugins = createPlugins;