V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
爱意满满的作品展示区。
Exuanbo

写了个 rollup 小插件, rollup-plugin-esbuild-transform

  •  1
     
  •   Exuanbo ·
    exuanbo · Aug 27, 2021 · 1943 views
    This topic created in 1705 days ago, the information mentioned may be changed or developed.

    esbuildrollup 构建中转译 JavaScript, TypeScript, JSX, JSON 等文件内容类型, 支持 esbuild Transform API 所有的选项。

    项目仓库

    rollup-plugin-esbuild-transform

    为什么

    esbuild 在打包时有一些问题,比如从去年至今依然没有修复的 #475

    EGOIST 大佬写的 rollup-plugin-esbuild 很棒,但是如果需要多个 loader 每个不同选项就有些麻烦, 而且不知为什么没有暴露出 esbuild.transform 完整的 API.

    示例

    // rollup.config.js
    
    import esbuild from 'rollup-plugin-esbuild-transform'
    
    export default {
      plugins: [
        esbuild([
          {
            loader: 'json',
            minifyWhitespace: true
          },
          {
            loader: 'ts'
          },
          {
            loader: 'tsx',
            banner: "import * as React from 'react'"
          },
          {
            loader: 'js',
            include: /\.[jt]sx?$/,
            target: 'es2017'
          }
        ]) 
      ]
    }
    

    include 选项

    默认值为 new RegExp(`\\.${loader === 'js' ? '(?:js|cjs|mjs)' : loader}$`)

    如果一个文件被多个选项匹配 (像上面的例子中), 除了 loader 以外的其他选项会被 Object.assign() 合并。

    // 比如文件 index.tsx
    [
      {
        loader: 'tsx',
        banner: "import * as React from 'react'"
      },
      {
        loader: 'js',
        include: /\.[jt]sx?$/,
        target: 'es2017'
      }
    ]
    
    // 最终传到 esbuild.transform 的选项会变成
    {
      loader: 'tsx',
      banner: "import * as React from 'react'",
      target: 'es2017'
    }
    

    exclude 选项

    默认值为 /node_modules/, 优先于 include 匹配。

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1004 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 19:19 · PVG 03:19 · LAX 12:19 · JFK 15:19
    ♥ Do have faith in what you're doing.