beesbuild
Version:
构建工具链
300 lines (299 loc) • 11.4 kB
JavaScript
;
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 = (target2, all) => {
for (var name in all) __defProp(target2, 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, target2) => (target2 = 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(target2, "default", {
value: mod,
enumerable: true
}) : target2, mod));
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
value: true
}), mod);
var stdin_exports = {};
__export(stdin_exports, {
copyBundles: () => copyBundles,
getRollupOptions: () => getRollupOptions,
rollupBuild: () => rollupBuild,
writeBundles: () => writeBundles
});
module.exports = __toCommonJS(stdin_exports);
var import_path = __toESM(require("path"));
var import_chalk_unified = __toESM(require("chalk-unified"));
var import_utils = require("@beesbuild/utils");
var import_fs_extra = require("fs-extra");
var import_rollup = require("rollup");
var import_defu = require("defu");
var import_lodash_unified = require("lodash-unified");
var import_plugin_terser = __toESM(require("@rollup/plugin-terser"));
var import_rollup_plugin_esbuild = require("rollup-plugin-esbuild");
var import_utils2 = require("../utils.cjs");
var import_variables = require("../variables.cjs");
var import_plugins = __toESM(require("./plugins/index.cjs"));
var import_watch = require("./watch.cjs");
function getRollupOptions(ctx) {
var _a, _b, _c;
const rollup2 = ctx.options.rollup;
const external = (0, import_utils.generateExternal)({
full: false,
dependencies: ctx.options.dependencies,
peerDependencies: ctx.options.peerDependencies
});
const plugins = (0, import_plugins.default)(ctx);
const entries = ctx.options.entries;
const externals = ctx.options.externals;
const input = entries.filter(entry => entry.builder === "rollup").map(entry => import_path.default.resolve(ctx.options.rootDir, entry.input));
let logLevel = "warn";
if (((_a = ctx.options) == null ? void 0 : _a.debug) === true) {
logLevel = "info";
} else if (((_b = ctx.options) == null ? void 0 : _b.verbose) === true) {
logLevel = "warn";
}
return {
input,
plugins: plugins.length > 0 ? plugins : null,
external(id, importer, isResolved) {
var _a2;
const pkg = (0, import_utils2.getpkg)(id);
const isExplicitExternal = (0, import_utils2.arrayIncludes)(externals, pkg) || (0, import_utils2.arrayIncludes)(externals, id);
if (isExplicitExternal) {
return true;
}
if (rollup2.inlineDependencies || id[0] === "." || import_path.default.isAbsolute(id) || /src[/\\]/.test(id) || id.startsWith(ctx.pkg.name)) {
return external(id, importer, isResolved);
}
if (!isExplicitExternal) {
((_a2 = ctx.options) == null ? void 0 : _a2.debug) && import_utils.consola.warn(`Inlined implicit external ${id}`);
return external(id, importer, isResolved);
}
return isExplicitExternal;
},
treeshake: (_c = ctx.options) == null ? void 0 : _c.treeshake,
logLevel
};
}
async function rollupBuild(ctx) {
var _a, _b;
const options = getRollupOptions(ctx);
await ctx.hooks.callHook("rollup:options", ctx, options);
if (Object.keys(options.input).length === 0) {
return;
}
const rollupBundle = await (0, import_rollup.rollup)(options);
await ctx.hooks.callHook("rollup:build", ctx, rollupBundle);
const rollupOptions = ctx.options.rollup;
const entries = ctx.options.entries;
const rootDir = ctx.options.rootDir;
const outDir = ctx.options.outDir || ".";
const entryFile = entries.find(({
isEntry
}) => isEntry);
const buildEntries = (0, import_utils.getBuildConfig)({
rootDir,
outDir,
manifest: (0, import_lodash_unified.merge)({}, ctx.pkg, ctx.pkg.publishConfig),
rollup: (0, import_lodash_unified.omit)(rollupOptions, "vue", "vueJsx", "vueMacros"),
entryFile: entryFile == null ? void 0 : entryFile.input
});
const rollupOutput = (_a = rollupOptions == null ? void 0 : rollupOptions.output) != null ? _a : {};
import_utils.consola.info(buildEntries.map(([module2]) => module2), "buildEntries");
const preserveModulesRoot = import_path.default.join(rootDir, "src");
((_b = ctx == null ? void 0 : ctx.argv) == null ? void 0 : _b.debug) && import_utils.consola.info(import_chalk_unified.default.gray(preserveModulesRoot, "preserveModulesRoot"));
const outputs = buildEntries.map(([module2, {
format,
root,
optionConfig,
bundle: bundle2,
ext,
output: {
name: outputName
}
}]) => {
var _a2, _b2, _c, _d;
const options2 = (_a2 = rollupOutput == null ? void 0 : rollupOutput[module2]) != null ? _a2 : {};
const output = (0, import_defu.defu)({
_module: module2
}, options2, optionConfig, {
preserveModules: true,
preserveModulesRoot
});
output.entryFileNames = `[name].${ext}`;
if (module2 !== "umd") {
output.dir = import_path.default.resolve(root, ".", import_variables.BUILD_CACHE_DIR, outputName);
} else {
output.file = bundle2.path;
}
if (optionConfig == null ? void 0 : optionConfig.exports) {
output.exports = optionConfig.exports;
}
if ((_b2 = ctx.options) == null ? void 0 : _b2.sourcemap) {
output.sourcemap = true;
}
if (((_c = ctx.options) == null ? void 0 : _c.minify) && ["cjs", "umd"].includes(format)) {
const minify = (_d = ctx.options) == null ? void 0 : _d.minify;
const plugins = (0, import_lodash_unified.flattenDeep)([output.plugins]);
switch (true) {
case minify === true:
case minify === "esbuild":
output.plugins = [(0, import_rollup_plugin_esbuild.minify)({
target: import_utils.target,
drop: ["debugger", "console"]
})].concat(plugins);
break;
default:
output.plugins = [(0, import_plugin_terser.default)({
compress: {
drop_console: true,
drop_debugger: true
// // 在 `compress` 选项中,你可以指定自定义的规则来处理 `console.log`
// global_defs: {
// 'console.log': () => '', // 将 `console.log` 替换为一个空函数
// },
}
})].concat(plugins);
}
output.plugins = output.plugins.filter(Boolean);
}
return output;
});
const watchOptions = {
...rollupOptions,
output: outputs
};
function getBundleOptions(module2, files) {
const {
root,
output,
ext
} = bundle.buildConfig[module2];
const src = import_path.default.resolve(root, ".", import_variables.BUILD_CACHE_DIR, output.name);
let filesPaths = [];
if (files && files.length > 0) {
filesPaths = files.map(filePath => {
const {
dir,
name
} = (0, import_utils.parseManifest)(filePath, rootDir);
const relativeFilePath = import_path.default.join(import_path.default.relative(preserveModulesRoot, dir), `${name}.${ext}`);
import_utils.consola.info(import_chalk_unified.default.cyan(`Start for copy file: ${relativeFilePath} generated`));
return {
cacheFilePath: import_path.default.resolve(src, relativeFilePath),
outputFilePath: import_path.default.resolve(output.path, relativeFilePath)
};
});
}
return {
cacheDir: src,
outputDir: output.path,
filesPaths
};
}
const bundle = {
...rollupBundle,
buildConfig: (0, import_lodash_unified.fromPairs)(buildEntries),
delete: (module2, files) => {
const {
cacheDir,
outputDir,
filesPaths
} = getBundleOptions(module2, files);
const paths = [];
if (filesPaths) {
for (const {
cacheFilePath,
outputFilePath
} of filesPaths) {
paths.push(cacheFilePath, outputFilePath);
}
} else {
paths.push(cacheDir, outputDir);
}
return Promise.allSettled(paths.map(file => {
if ((0, import_fs_extra.existsSync)(file)) {
import_utils.consola.info(import_chalk_unified.default.cyan(`Start for delete file: `, import_path.default.relative(ctx.options.rootDir, file), ` generated`));
return (0, import_fs_extra.rm)(file, {
recursive: true
});
}
return Promise.resolve();
}));
},
copy: (module2, files) => {
const {
cacheDir,
outputDir,
filesPaths
} = getBundleOptions(module2, files);
const paths = [];
if (filesPaths && filesPaths.length > 0) {
for (const {
cacheFilePath,
outputFilePath
} of filesPaths) {
paths.push([cacheFilePath, outputFilePath]);
}
} else {
paths.push([cacheDir, outputDir]);
}
import_utils.consola.info(import_chalk_unified.default.cyan("Start for copy file!"));
return Promise.allSettled(paths.map(([form, to]) => {
import_utils.consola.info(import_chalk_unified.default.cyan(`Start for copy \u6765\u6E90: `, import_path.default.relative(ctx.options.rootDir, form), ` ==> `, import_path.default.relative(ctx.options.rootDir, to), ` generated`));
return (0, import_fs_extra.copy)(form, to, {
overwrite: true
});
}));
},
build: () => writeBundles(rollupBundle, outputs)
};
return {
watchOptions,
build: () => writeBundles(rollupBundle, outputs),
copy: () => copyBundles(bundle, buildEntries),
watch: () => (0, import_watch.rollupWatch)(ctx, watchOptions, bundle)
};
}
const copyBundles = (bundle, options) => {
return Promise.allSettled(options.map(async ([module2]) => {
(bundle == null ? void 0 : bundle.copy) && (await bundle.copy(module2));
return Promise.resolve();
}));
};
async function writeBundles(bundle, options) {
await Promise.allSettled(options.map(async option => {
option.dir && (0, import_fs_extra.existsSync)(option.dir) && (0, import_fs_extra.rmSync)(option.dir, {
recursive: true
});
(bundle == null ? void 0 : bundle.write) && (await bundle.write(option));
return Promise.resolve();
}));
return Promise.resolve();
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
copyBundles,
getRollupOptions,
rollupBuild,
writeBundles
});