ESBuild - SWC 淺談: 新一代構建工具

首先, ESBuild & swc 是什麼?

爲什麼要關注這兩個工具?

ESBuild/swc 在前端生態中的定位

ESBuild/SWC 爲何這麼快?

ESBuild 的實現 (參考 ESBuild FAQ[3])

swc 的實現

一點總結

如何用 ESBuild/swc 提效?

使用 ESBuild

require('esbuild').buildSync({
  entryPoints: ['app.js'],
  bundle: true,
  loader: { '.js''jsx' },
  outfile: 'out.js',
})
// 來自於官網的插件示範
let envPlugin = {
  name: 'env',
  setup(build) {
    // Intercept import paths called "env" so esbuild doesn't attempt
    // to map them to a file system location. Tag them with the "env-ns"
    // namespace to reserve them for this plugin.
    build.onResolve({ filter: /^env$/ }, args => ({
      path: args.path,
      namespace: 'env-ns',
    }))

    // Load paths tagged with the "env-ns" namespace and behave as if
    // they point to a JSON file containing the environment variables.
    build.onLoad({ filter: /.*/, namespace: 'env-ns' }, () => ({
      contents: JSON.stringify(process.env),
      loader: 'json',
    }))
  },
}

// 使用插件
require('esbuild').build({
  entryPoints: ['app.js'],
  bundle: true,
  outfile: 'out.js',
  plugins: [envPlugin],
}).catch(() => process.exit(1))

使用 Vite

使用 swc

一點點總結和思考

全文總結

延伸思考

❤️感謝收看❤️

參考資料

參考資料

[1]

ESBuild: https://esbuild.github.io/

[2]

SWC: https://swc.rs/

[3]

FAQ: https://esbuild.github.io/faq/

[4]

博客: https://swc.rs/blog/perf-swc-vs-babel

[5]

esbuild-loader: https://github.com/privatenumber/esbuild-loader

[6]

配置文件: https://swc.rs/docs/configuration/swcrc

[7]

spack.config.js: https://swc.rs/docs/configuration/bundling

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/9VaUq9FOm2_nKNCGaH-7rw