UNPKG

beesbuild

Version:

构建工具链

152 lines (151 loc) 3.81 kB
import { extname, relative } from "pathe"; import { transform as esbuildTransform } from "esbuild"; import { target } from "@beesbuild/utils"; import esbuild, { minify as minifyPlugin } from "rollup-plugin-esbuild"; import { createFilter } from "@rollup/pluginutils"; import { isBoolean, mergeWith } from "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 += ` (${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[extname(id)]; }; const filter = 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 esbuildTransform(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 esbuildTransform(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 (isBoolean(options) || !options) options = {}; const minify = ctx.options.minify ? false : (_a = options.minify) != null ? _a : false; const plugins = [ esbuild( mergeWith( { exclude: [], sourceMap: true, target, loaders: { ".vue": "ts" }, define: { "process.env.NODE_ENV": JSON.stringify("production") }, treeShaking: true }, options ) ) ]; if (minify) { plugins.push( minifyPlugin({ target, sourceMap: true, drop: ["debugger", "console"] }) ); } return plugins; } function esbuildPlugin(options, ctx) { const rollup = ctx.options.rollup; const isVue = isBoolean(rollup.vue) ? rollup.vue : Boolean(rollup.vue); return isVue ? esbuildBasePlugin(options, ctx) : esbuildCustomPlugin(options, ctx); } export { esbuildBasePlugin, esbuildCustomPlugin, esbuildPlugin, printWarnings };