温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/docs/bundler/esbuild
点击访问原文链接
esbuild - BunSkip to main contentBun home pageSearch...⌘KInstall BunSearch...NavigationMigrationesbuildRuntimePackage ManagerBundlerTest RunnerGuidesReferenceBlogFeedbackdiv:first-child]:!hidden peer-[.is-custom]:[&>div:first-child]:sm:!hidden peer-[.is-custom]:[&>div:first-child]:md:!hidden peer-[.is-custom]:[&>div:first-child]:lg:!hidden peer-[.is-custom]:[&>div:first-child]:xl:!hidden">CoreBundlerDevelopment ServerFullstack dev serverHot reloadingAsset ProcessingHTML & static sitesStandalone HTMLCSSLoadersSingle File ExecutableSingle-file executableExtensionsPluginsMacrosOptimizationBytecode CachingMinifierMigrationesbuildOn this pagePerformanceCLI APIJavaScript APIPlugin APIonResolveonLoadMigrationesbuildCopy pagespan]:line-clamp-1 overflow-hidden group flex items-center py-0.5 gap-1 text-sm text-gray-950/50 dark:text-white/50 group-hover:text-gray-950/70 dark:group-hover:text-white/70 rounded-none rounded-r-xl border px-3 border-gray-200 aspect-square dark:border-white/[0.07] bg-background-light dark:bg-background-dark hover:bg-gray-600/5 dark:hover:bg-gray-200/5" aria-label="More actions" type="button" id="radix-_R_2shjinpfd9rqaabsnpfdb_" aria-haspopup="menu" aria-expanded="false" data-state="closed">*]:[overflow-wrap:anywhere]">Migration guide from esbuild to Bun’s bundler

Copy pagespan]:line-clamp-1 overflow-hidden group flex items-center py-0.5 gap-1 text-sm text-gray-950/50 dark:text-white/50 group-hover:text-gray-950/70 dark:group-hover:text-white/70 rounded-none rounded-r-xl border px-3 border-gray-200 aspect-square dark:border-white/[0.07] bg-background-light dark:bg-background-dark hover:bg-gray-600/5 dark:hover:bg-gray-200/5" aria-label="More actions" type="button" id="radix-_R_5hjinpfd9rqaabsnpfdb_" aria-haspopup="menu" aria-expanded="false" data-state="closed">Bun’s bundler API is inspired heavily by esbuild. Migrating to Bun’s bundler from esbuild should be relatively painless. This guide will briefly explain why you might consider migrating to Bun’s bundler and provide a side-by-side API comparison reference for those who are already familiar with esbuild’s API. There are a few behavioral differences to note. Bundling by default. Unlike esbuild, Bun always bundles by default. This is why the --bundle flag isn’t necessary in the Bun example. To transpile each file individually, use Bun.Transpiler. It’s just a bundler. Unlike esbuild, Bun’s bundler does not include a built-in development server or file watcher. It’s just a bundler. The bundler is intended for use in conjunction with Bun.serve and other runtime APIs to achieve the same effect. As such, all options relating to HTTP/file watching are not applicable. ​Performance With a performance-minded API coupled with the extensively optimized Zig-based JS/TS parser, Bun’s bundler is 1.75x faster than esbuild on esbuild’s three.js benchmark. Bundling 10 copies of three.js from scratch, with sourcemaps and minification ​CLI API Bun and esbuild both provide a command-line interface. terminalCopy# esbuild esbuild entrypoint> --outdir=out --bundle # bun bun build entrypoint> --outdir=out In Bun’s CLI, simple boolean flags like --minify do not accept an argument. Other flags like --outdir do accept an argument; these flags can be written as --outdir out or --outdir=out. Some flags like --define can be specified several times: --define foo=bar --define bar=baz. esbuildbun buildNotes--bundlen/aBun always bundles, use --no-bundle to disable this behavior.--define:K=V--define K=VSmall syntax difference; no colon.
esbuild --define:foo=bar
bun build --define foo=bar--external:--external Small syntax difference; no colon.
esbuild --external:react
bun build --external react--format--formatBun supports "esm" and "cjs" currently, but more module formats are planned. esbuild defaults to "iife".--loader:.ext=loader--loader .ext:loaderBun supports a different set of built-in loaders than esbuild; see Bundler > Loaders for a complete reference. The esbuild loaders dataurl, binary, base64, copy, and empty are not yet implemented.

The syntax for --loader is slightly different.
esbuild app.ts --bundle --loader:.svg=text
bun build app.ts --loader .svg:text--minify--minifyNo differences--outdir--outdirNo differences--outfile--outfileNo differences--packages--packagesNo differences--platform--targetRenamed to --target for consistency with tsconfig. Does not support neutral.--serven/aNot applicable--sourcemap--sourcemapNo differences--splitting--splittingNo differences--targetn/aNot supported. Bun’s bundler performs no syntactic down-leveling at this time.--watch--watchNo differences--allow-overwriten/aOverwriting is never allowed--analyzen/aNot supported--asset-names--asset-namingRenamed for consistency with naming in JS API--banner--bannerOnly applies to js bundles--footer--footerOnly applies to js bundles--certfilen/aNot applicable--charset=utf8n/aNot supported--chunk-names--chunk-namingRenamed for consistency with naming in JS API--colorn/aAlways enabled--drop--dropn/a--featureBun-specific. Enable feature flags for compile-time dead-code elimination via import { feature } from "bun:bundle"--entry-names--entry-namingRenamed for consistency with naming in JS API--global-namen/aNot applicable, Bun does not support iife output at this time--ignore-annotations--ignore-dce-annotations--injectn/aNot supported--jsx--jsx-runtime Supports "automatic" (uses jsx transform) and "classic" (uses React.createElement)--jsx-devn/aBun reads compilerOptions.jsx from tsconfig.json to determine a default. If compilerOptions.jsx is "react-jsx", or if NODE_ENV=production, Bun will use the jsx transform. Otherwise, it uses jsxDEV. The bundler does not support preserve.--jsx-factory--jsx-factory--jsx-fragment--jsx-fragment--jsx-import-source--jsx-import-source--jsx-side-effectsn/aJSX is always assumed to be side-effect-free--keep-namesn/aNot supported--keyfilen/aNot applicable--legal-commentsn/aNot supported--log-leveln/aNot supported. This can be set in bunfig.toml as logLevel.--log-limitn/aNot supported--log-override:X=Yn/aNot supported--main-fieldsn/aNot supported--mangle-cachen/aNot supported--mangle-propsn/aNot supported--mangle-quotedn/aNot supported--metafilen/aNot supported--minify-whitespace--minify-whitespace--minify-identifiers--minify-identifiers--minify-syntax--minify-syntax--out-extensionn/aNot supported--outbase--root--preserve-symlinksn/aNot supported--public-path--public-path--puren/aNot supported--reserve-propsn/aNot supported--resolve-extensionsn/aNot supported--servedirn/aNot applicable--source-rootn/aNot supported--sourcefilen/aNot supported. Bun does not support stdin input yet.--sourcemap--sourcemapNo differences--sources-contentn/aNot supported--supportedn/aNot supported--tree-shakingn/aAlways true--tsconfig--tsconfig-override--versionn/aRun bun --version to see the version of Bun. ​JavaScript API esbuild.build()Bun.build()NotesabsWorkingDirn/aAlways set to process.cwd()aliasn/aNot supportedallowOverwriten/aAlways falseassetNamesnaming.assetUses same templating syntax as esbuild, but [ext] must be included explicitly.

ts
Bun.build({
entrypoints: ["./index.tsx"],
naming: {
asset: "[name].[ext]",
},
});
bannern/aNot supportedbundlen/aAlways true. Use Bun.Transpiler to transpile without bundling.charsetn/aNot supportedchunkNamesnaming.chunkUses same templating syntax as esbuild, but [ext] must be included explicitly.

ts
Bun.build({
entrypoints: ["./index.tsx"],
naming: {
chunk: "[name].[ext]",
},
});
colorn/aBun returns logs in the logs property of the build result.conditionsn/aNot supported. Export conditions priority is determined by target.definedefinedropn/aNot supportedentryNamesnaming or naming.entryBun supports a naming key that can either be a string or an object. Uses same templating syntax as esbuild, but [ext] must be included explicitly.

ts
Bun.build({
entrypoints: ["./index.tsx"],
// when string, this is equivalent to entryNames
naming: "[name].[ext]",

// granular naming options
naming: {
entry: "[name].[ext]",
asset: "[name].[ext]",
chunk: "[name].[ext]",
},
});
entryPointsentrypointsCapitalization differenceexternalexternalNo differencesfootern/aNot supportedformatformatOnly supports "esm" currently. Support for "cjs" and "iife" is planned.globalNamen/aNot supportedignoreAnnotationsn/aNot supportedinjectn/aNot supportedjsxjsxNot supported in JS API, configure in tsconfig.jsonjsxDevjsxDevNot supported in JS API, configure in tsconfig.jsonjsxFactoryjsxFactoryNot supported in JS API, configure in tsconfig.jsonjsxFragmentjsxFragmentNot supported in JS API, configure in tsconfig.jsonjsxImportSourcejsxImportSourceNot supported in JS API, configure in tsconfig.jsonjsxSideEffectsjsxSideEffectsNot supported in JS API, configure in tsconfig.jsonkeepNamesn/aNot supportedlegalCommentsn/aNot supportedloaderloaderBun supports a different set of built-in loaders than esbuild; see Bundler > Loaders for a complete reference. The esbuild loaders dataurl, binary, base64, copy, and empty are not yet implemented.logLeveln/aNot supportedlogLimitn/aNot supportedlogOverriden/aNot supportedmainFieldsn/aNot supportedmangleCachen/aNot supportedmanglePropsn/aNot supportedmangleQuotedn/aNot supportedmetafilen/aNot supportedminifyminifyIn Bun, minify can be a boolean or an object.

ts
await Bun.build({
entrypoints: ['./index.tsx'],
// enable all minification
minify: true

// granular options
minify: {
identifiers: true,
syntax: true,
whitespace: true
}
})
minifyIdentifiersminify.identifiersSee minifyminifySyntaxminify.syntaxSee minifyminifyWhitespaceminify.whitespaceSee minifynodePathsn/aNot supportedoutExtensionn/aNot supportedoutbaserootDifferent nameoutdiroutdirNo differencesoutfileoutfileNo differencespackagesn/aNot supported, use externalplatformtargetSupports "bun", "node" and "browser" (the default). Does not support "neutral".pluginspluginsBun’s plugin API is a subset of esbuild’s. Some esbuild plugins will work out of the box with Bun.preserveSymlinksn/aNot supportedpublicPathpublicPathNo differencespuren/aNot supportedreservePropsn/aNot supportedresolveExtensionsn/aNot supportedsourceRootn/aNot supportedsourcemapsourcemapSupports "inline", "external", and "none"sourcesContentn/aNot supportedsplittingsplittingNo differencesstdinn/aNot supportedsupportedn/aNot supportedtargetn/aNo support for syntax downlevelingtreeShakingn/aAlways truetsconfign/aNot supportedwriten/aSet to true if outdir/outfile is set, otherwise false ​Plugin API Bun’s plugin API is designed to be esbuild compatible. Bun doesn’t support esbuild’s entire plugin API surface, but the core functionality is implemented. Many third-party esbuild plugins will work out of the box with Bun. Long term, we aim for feature parity with esbuild’s API, so if something doesn’t work please file an issue to help us prioritize. Plugins in Bun and esbuild are defined with a builder object. myPlugin.tsCopyimport type { BunPlugin } from "bun"; const myPlugin: BunPlugin = { name: "my-plugin", setup(builder) { // define plugin }, }; The builder object provides some methods for hooking into parts of the bundling process. Bun implements onStart, onEnd, onResolve, and onLoad. It does not yet implement the esbuild hooks onDispose and resolve. initialOptions is partially implemented, being read-only and only having a subset of esbuild’s options; use config (same thing but with Bun’s BuildConfig format) instead. myPlugin.tsCopyimport type { BunPlugin } from "bun"; const myPlugin: BunPlugin = { name: "my-plugin", setup(builder) { builder.onStart(() => { /* called when the bundle starts */ }); builder.onResolve( { /* onResolve.options */ }, args => { return { /* onResolve.results */ }; }, ); builder.onLoad( { /* onLoad.options */ }, args => { return { /* onLoad.results */ }; }, ); builder.onEnd(result => { /* called when the bundle is complete */ }); }, }; ​onResolve options arguments results 🟢 filter 🟢 namespace 🟢 path 🟢 importer 🔴 namespace 🔴 resolveDir 🔴 kind 🔴 pluginData 🟢 namespace 🟢 path 🔴 errors 🔴 external 🔴 pluginData 🔴 pluginName 🔴 sideEffects 🔴 suffix 🔴 warnings 🔴 watchDirs 🔴 watchFiles ​onLoad options arguments results 🟢 filter 🟢 namespace 🟢 path 🔴 namespace 🔴 suffix 🔴 pluginData 🟢 contents 🟢 loader 🔴 errors 🔴 pluginData 🔴 pluginName 🔴 resolveDir 🔴 warnings 🔴 watchDirs 🔴 watchFiles

Was this page helpful?

YesNoSuggest editsRaise issuePrevious⌘IxgithubdiscordyoutubePowered by

智能索引记录