mirror of
https://github.com/Sevichecc/Urara-Blog.git
synced 2025-05-20 17:39:14 +08:00
126 lines
3.7 KiB
Text
126 lines
3.7 KiB
Text
import { promises, existsSync } from 'fs';
|
|
import { resolve, dirname, relative, basename } from 'pathe';
|
|
import fg from 'fast-glob';
|
|
import consola from 'consola';
|
|
import { cyan, green, dim } from 'colorette';
|
|
import { debounce } from 'perfect-debounce';
|
|
import { createGenerator, toArray } from '@unocss/core';
|
|
import { loadConfig } from '@unocss/config';
|
|
import presetUno from '@unocss/preset-uno';
|
|
|
|
const version = "0.45.6";
|
|
|
|
class PrettyError extends Error {
|
|
constructor(message) {
|
|
super(message);
|
|
this.name = this.constructor.name;
|
|
if (typeof Error.captureStackTrace === "function")
|
|
Error.captureStackTrace(this, this.constructor);
|
|
else
|
|
this.stack = new Error(message).stack;
|
|
}
|
|
}
|
|
function handleError(error) {
|
|
if (error instanceof PrettyError)
|
|
consola.error(error.message);
|
|
process.exitCode = 1;
|
|
}
|
|
|
|
const defaultConfig = {
|
|
envMode: "build",
|
|
presets: [
|
|
presetUno()
|
|
]
|
|
};
|
|
|
|
const name = "unocss";
|
|
async function resolveOptions(options) {
|
|
if (!options.patterns?.length) {
|
|
throw new PrettyError(
|
|
`No glob patterns, try ${cyan(`${name} <path/to/**/*>`)}`
|
|
);
|
|
}
|
|
return options;
|
|
}
|
|
async function build(_options) {
|
|
const fileCache = /* @__PURE__ */ new Map();
|
|
const cwd = _options.cwd || process.cwd();
|
|
const options = await resolveOptions(_options);
|
|
const { config, sources: configSources } = await loadConfig(cwd, options.config);
|
|
const uno = createGenerator(
|
|
config,
|
|
defaultConfig
|
|
);
|
|
const files = await fg(options.patterns, { cwd, absolute: true });
|
|
await Promise.all(
|
|
files.map(async (file) => {
|
|
fileCache.set(file, await promises.readFile(file, "utf8"));
|
|
})
|
|
);
|
|
consola.log(green(`${name} v${version}`));
|
|
consola.start(`UnoCSS ${options.watch ? "in watch mode..." : "for production..."}`);
|
|
const debouncedBuild = debounce(
|
|
async () => {
|
|
generate(options).catch(handleError);
|
|
},
|
|
100
|
|
);
|
|
const startWatcher = async () => {
|
|
if (!options.watch)
|
|
return;
|
|
const { watch } = await import('chokidar');
|
|
const { patterns } = options;
|
|
const ignored = ["**/{.git,node_modules}/**"];
|
|
const watcher = watch(patterns, {
|
|
ignoreInitial: true,
|
|
ignorePermissionErrors: true,
|
|
ignored,
|
|
cwd
|
|
});
|
|
if (configSources.length)
|
|
watcher.add(configSources);
|
|
watcher.on("all", async (type, file) => {
|
|
if (configSources.includes(file)) {
|
|
uno.setConfig((await loadConfig()).config);
|
|
consola.info(`${cyan(basename(file))} changed, setting new config`);
|
|
} else {
|
|
consola.log(`${green(type)} ${dim(file)}`);
|
|
const absolutePath = resolve(cwd, file);
|
|
if (type.startsWith("unlink"))
|
|
fileCache.delete(absolutePath);
|
|
else
|
|
fileCache.set(absolutePath, await promises.readFile(absolutePath, "utf8"));
|
|
}
|
|
debouncedBuild();
|
|
});
|
|
consola.info(
|
|
`Watching for changes in ${toArray(patterns).map((i) => cyan(i)).join(", ")}`
|
|
);
|
|
};
|
|
await generate(options);
|
|
startWatcher();
|
|
async function generate(options2) {
|
|
const outFile = resolve(options2.cwd || process.cwd(), options2.outFile ?? "uno.css");
|
|
const { css, matched } = await uno.generate(
|
|
[...fileCache].join("\n"),
|
|
{
|
|
preflights: options2.preflights,
|
|
minify: options2.minify
|
|
}
|
|
);
|
|
const dir = dirname(outFile);
|
|
if (!existsSync(dir))
|
|
await promises.mkdir(dir, { recursive: true });
|
|
await promises.writeFile(outFile, css, "utf-8");
|
|
if (!options2.watch) {
|
|
consola.success(
|
|
`${[...matched].length} utilities generated to ${cyan(
|
|
relative(process.cwd(), outFile)
|
|
)}
|
|
`
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
export { build as b, handleError as h, resolveOptions as r, version as v };
|