@umijs/plugins
Version:
81 lines (79 loc) • 2.84 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/unocss.ts
var unocss_exports = {};
__export(unocss_exports, {
default: () => unocss_default
});
module.exports = __toCommonJS(unocss_exports);
var import_utils = require("@umijs/utils");
var import_fs = require("fs");
var import_path = require("path");
var import_plugin_utils = require("umi/plugin-utils");
var unocss_default = (api) => {
api.describe({
key: "unocss",
config: {
schema({ zod }) {
return zod.object({
watch: zod.array(zod.any())
});
}
},
enableBy: api.EnableBy.config
});
const outputPath = "uno.css";
api.onBeforeCompiler(async () => {
if (process.env.IS_UMI_BUILD_WORKER) return;
if (!(0, import_fs.existsSync)((0, import_path.join)(api.paths.cwd, "unocss.config.ts")))
api.logger.warn(
"请在项目目录中添加 unocss.config.ts 文件,并配置需要的 unocss presets,否则插件将没有效果!"
);
const generatedPath = (0, import_path.join)(api.paths.absTmpPath, outputPath);
const binPath = (0, import_path.join)(api.cwd, "node_modules/.bin/unocss");
const watchDirs = api.config.unocss.watch || [];
const isDev = api.env === "development";
const args = [
...watchDirs,
"--out-file",
generatedPath,
isDev ? "--watch" : ""
].filter(Boolean);
const execaRes = import_utils.execa.execa(binPath, args, {
cwd: api.cwd,
stdio: isDev ? "pipe" : "inherit"
});
if (isDev) {
await new Promise((resolve) => {
const { stdout } = execaRes;
function dataHandler() {
resolve();
stdout == null ? void 0 : stdout.off("data", dataHandler);
}
stdout == null ? void 0 : stdout.on("data", dataHandler);
});
api.logger.info("unocss watch mode is running!");
} else {
await execaRes;
}
});
api.addEntryImports(() => {
const generatedPath = (0, import_plugin_utils.winPath)((0, import_path.join)(api.paths.absTmpPath, outputPath));
return [{ source: generatedPath }];
});
};