UNPKG

beesbuild

Version:

构建工具链

190 lines (189 loc) 5.71 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 = (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, { esbuildBasePlugin: () => esbuildBasePlugin, esbuildCustomPlugin: () => esbuildCustomPlugin, esbuildPlugin: () => esbuildPlugin, printWarnings: () => printWarnings }); module.exports = __toCommonJS(stdin_exports); var import_pathe = require("pathe"); var import_esbuild = require("esbuild"); var import_utils = require("@beesbuild/utils"); var import_rollup_plugin_esbuild = __toESM(require("rollup-plugin-esbuild")); var import_pluginutils = require("@rollup/pluginutils"); var import_lodash_unified = require("lodash-unified"); const DefaultLoaders = { ".js": "js", ".mjs": "js", ".cjs": "js", ".ts": "ts", ".mts": "ts", ".cts": "ts", ".tsx": "tsx", ".jsx": "jsx", ".vue": "ts" }; function printWarnings(id, result, plugin) { if (result.warnings) { for (const warning of result.warnings) { let message = "[esbuild]"; if (warning.location) { message += ` (${(0, import_pathe.relative)(process.cwd(), id)}:${warning.location.line}:${warning.location.column})`; } message += ` ${warning.text}`; plugin.warn(message); } } } function esbuildCustomPlugin(option, ctx) { var _a; if ((option != null ? option : false) === false) return []; if (typeof option === "boolean") { option = {}; } const minify = (_a = option == null ? void 0 : option.minify) != null ? _a : false; const { include = /\.(ts|js|tsx|jsx)$/, exclude = /node_modules/, loaders: loaderOptions, ...esbuildOptions } = option; const loaders = { ...DefaultLoaders }; if (loaderOptions) { for (const [key, value] of Object.entries(loaderOptions)) { if (typeof value === "string") { loaders[key] = value; } else if (!value) { delete loaders[key]; } } } const getLoader = (id = "") => { return loaders[(0, import_pathe.extname)(id)]; }; const filter = (0, import_pluginutils.createFilter)(include, exclude); return { name: "esbuild", async transform(code, id) { if (!filter(id)) { return null; } const loader = getLoader(id); if (!loader) { return null; } const result = await (0, import_esbuild.transform)(code, { ...esbuildOptions, loader, sourcefile: id }); printWarnings(id, result, this); if (result.code) { return { code: result.code, map: result.map || null }; } }, async renderChunk(code, { fileName }) { if (!minify) { return null; } if (/\.d\.([cm])?tsx?$/.test(fileName)) { return null; } const loader = getLoader(fileName); if (!loader) { return null; } const result = await (0, import_esbuild.transform)(code, { ...esbuildOptions, loader, sourcefile: fileName, minify: true, drop: ["console", "debugger"] }); if (result.code) { return { code: result.code, map: result.map || null }; } } }; } function esbuildBasePlugin(options, ctx) { var _a; if ((0, import_lodash_unified.isBoolean)(options) || !options) options = {}; const minify = ctx.options.minify ? false : (_a = options.minify) != null ? _a : false; const plugins = [(0, import_rollup_plugin_esbuild.default)((0, import_lodash_unified.mergeWith)({ exclude: [], sourceMap: true, target: import_utils.target, loaders: { ".vue": "ts" }, define: { "process.env.NODE_ENV": JSON.stringify("production") }, treeShaking: true }, options))]; if (minify) { plugins.push((0, import_rollup_plugin_esbuild.minify)({ target: import_utils.target, sourceMap: true, drop: ["debugger", "console"] })); } return plugins; } function esbuildPlugin(options, ctx) { const rollup = ctx.options.rollup; const isVue = (0, import_lodash_unified.isBoolean)(rollup.vue) ? rollup.vue : Boolean(rollup.vue); return isVue ? esbuildBasePlugin(options, ctx) : esbuildCustomPlugin(options, ctx); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { esbuildBasePlugin, esbuildCustomPlugin, esbuildPlugin, printWarnings });