库开发配置

库打包、多格式输出


📋 学习目标

  • ✅ 掌握使用 ESBuild 打包库
  • ✅ 理解多格式输出配置
  • ✅ 了解库开发最佳实践
  • ✅ 掌握外部依赖处理

库打包基础

基础配置

require('esbuild').build({
  entryPoints: ['src/index.js'],
  bundle: true,
  outfile: 'dist/library.js',
  format: 'esm',
  platform: 'neutral'
})

多格式输出

同时输出多种格式

// ESM
require('esbuild').build({
  entryPoints: ['src/index.js'],
  bundle: true,
  outfile: 'dist/library.esm.js',
  format: 'esm'
})
 
// CommonJS
require('esbuild').build({
  entryPoints: ['src/index.js'],
  bundle: true,
  outfile: 'dist/library.cjs.js',
  format: 'cjs'
})
 
// UMD
require('esbuild').build({
  entryPoints: ['src/index.js'],
  bundle: true,
  outfile: 'dist/library.umd.js',
  format: 'iife',
  globalName: 'MyLibrary'
})

外部依赖

排除依赖

{
  external: ['react', 'react-dom'],
  bundle: true
}

相关链接


最后更新:2025


ESBuild 库开发