UNPKG

beesbuild

Version:

构建工具链

173 lines (171 loc) 6.37 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var stdin_exports = {}; __export(stdin_exports, { createChokidarWatcher: () => createChokidarWatcher, createRollupWatcher: () => createRollupWatcher, rollupWatch: () => rollupWatch }); module.exports = __toCommonJS(stdin_exports); var import_path = __toESM(require("path")); var import_rollup = require("rollup"); var import_chalk_unified = __toESM(require("chalk-unified")); var import_utils = require("@beesbuild/utils"); var import_fs_extra = require("fs-extra"); var import_defu = require("defu"); var import_chokidar = __toESM(require("chokidar")); var import_utils2 = require("./utils.cjs"); const createRollupWatcher = (ctx, options, bundle) => { const script = [import_utils.pathExtra.resolve(ctx.options.rootDir, "src/**/*"), import_utils.pathExtra.resolve(ctx.options.rootDir, "index.*")]; const includes = script.concat(options.input); const output = options.output.find(output2 => output2._module === "esm"); if (!output) return {}; const watchOptions = (0, import_defu.defu)({ output: [output], cache: true, logLevel: "silent", watch: { buildDelay: 1e3, // chokidar: { // cwd: ctx.options.rootDir, // }, clearScreen: true, // skipWrite: false, exclude: "node_modules/**", include: script } }, options); const { output: outputs, ...opts } = watchOptions; const writeBundle = async files => { var _a; const dir = (_a = output.dir) != null ? _a : ""; (0, import_fs_extra.existsSync)(dir) && (0, import_fs_extra.rmSync)(dir, { recursive: true }); const rollupBundle = await (0, import_rollup.rollup)((0, import_defu.defu)({ input: files }, opts)); await rollupBundle.write(output); (bundle == null ? void 0 : bundle.copy) && (await bundle.copy("esm", files)); }; const watcher = (0, import_rollup.watch)(watchOptions); let first = true; let changeFiles = []; watcher.on("event", async event => { const code = first ? `FIRST_${event.code}` : event.code; if (event.code == "BUNDLE_END" && code !== "FIRST_BUNDLE_END") { import_utils.consola.info(`\u5B8C\u6210\u6784\u5EFA\u65F6\u95F4\uFF1A${event.duration}`); const { watchFiles } = event.result; event.result.watchFiles = (0, import_utils2.handleBuildFiles)(ctx, ctx == null ? void 0 : ctx.argv).map(file => typeof file === "string" ? file : file == null ? void 0 : file.input).filter(file => typeof file === "string"); for (const file of event.result.watchFiles.filter(value => !watchFiles.includes(value))) { import_utils.consola.info(`Add listening file\uFF1A${import_utils.pathExtra.relative(ctx.options.rootDir, file)}`); } } if (event.code === "END") { import_utils.consola.success(import_chalk_unified.default.green("\u5B8C\u6210\u6240\u6709\u4EA7\u7269\u7684\u6784\u5EFA")); code === "FIRST_END" && (first = false); if (bundle.copy) { import_utils.consola.info(import_chalk_unified.default.cyan("Start for copy file!")); return bundle.copy("esm", [...changeFiles]).then(r => { changeFiles = []; }); } } switch (code) { case "START": import_utils.consola.info(import_chalk_unified.default.yellow("\u76D1\u89C6\u5668\u6B63\u5728\uFF08\u91CD\u65B0\uFF09\u542F\u52A8")); break; } import_utils.consola.warn(import_chalk_unified.default.yellow(`\u6267\u884C code:${code}`)); return Promise.resolve(); }); watcher.on("change", async (id, { event }) => { import_utils.consola.info(id, "change", event); import_utils.consola.info(import_chalk_unified.default.gray(`File ${import_path.default.relative(ctx.options.rootDir, id)} break out changed:${event}`)); if (event !== "delete") { changeFiles.push(id); } else if (event === "delete") { await bundle.delete("esm", [id]); } }); watcher.on("restart", () => { import_utils.consola.info("restart"); }); watcher.on("close", () => { import_utils.consola.info("close"); watcher.close().then(r => r); }); return { script, includes, watchOptions, watcher, writeBundle // chokidar, }; }; const createChokidarWatcher = options => { const watcher = import_chokidar.default.watch(options.script, { persistent: true }); watcher.on("change", async path2 => { import_utils.consola.info(import_chalk_unified.default.gray(`File ${path2} has been changed`)); }); watcher.on("unlink", async path2 => { import_utils.consola.log(`File ${path2} changed unlink`); }); return watcher; }; const rollupWatch = async (ctx, options, bundle) => { const { script, watcher: rollupWatcher } = createRollupWatcher(ctx, options, bundle); if (!script) return; return rollupWatcher; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createChokidarWatcher, createRollupWatcher, rollupWatch });