Détail du package

rollup-plugin-esbuild-transform

exuanbo1.4kMIT1.5.0

Use esbuild with Rollup to transform any supported content types.

rollup, esbuild, rollup-plugin, rollup-plugin-esbuild

readme

rollup-plugin-esbuild-transform

Use esbuild with Rollup to transform any supported content types.

npm GitHub Workflow Status (branch) Codecov branch libera manifesto

Why

esbuild as a bundler has some problems such as #475 which has still not been fixed since Oct, 2020.

rollup-plugin-esbuild is great but there is no simpler way to use multiple loader with different options, and for some reason it does not provide all available options from esbuild transform API.

Install

npm install -D esbuild rollup-plugin-esbuild-transform

Example

// rollup.config.js

import { join } from 'path'
import esbuild from 'rollup-plugin-esbuild-transform'

export default {
  // ...
  plugins: [
    esbuild([
      {
        loader: 'json'
      },
      {
        loader: 'tsx',
        legalComments: 'eof'
      },
      {
        loader: 'ts',
        include: /\.tsx?$/,
        tsconfig: join(__dirname, 'tsconfig.json')
      },
      {
        output: true,
        minify: true,
        target: 'es2015'
      }
    ])
  ]
}

Options

// index.d.ts

import { TransformOptions as EsbuildTransformOptions } from 'esbuild'
import { FilterPattern } from '@rollup/pluginutils'
import { Plugin } from 'rollup'

export interface TransformOptions extends EsbuildTransformOptions {
  tsconfig?: string
}

export interface Options extends TransformOptions {
  output?: boolean
  include?: FilterPattern
  exclude?: FilterPattern
}

declare function esbuildTransform(options?: Options | Options[]): Plugin
export default esbuildTransform

This plugin uses the same options from esbuild transform API.

tsconfig is the path to tsconfig.json file relative to process.cwd(). It will not be used if tsconfigRaw is provided.

output is for indicating whether this transformation should be performed after the chunk (bundle) has been rendered.

include and exclude are picomatch patterns. They can be string | RegExp | Array<string | RegExp>. When supplied they will override the default values.

If output: true, then the options include and exclude will be applied to the chunk's filename from RollupOptions.output.file.

include

Default to new RegExp(`\\.(?:\${loaderExtensions.join('|')})\$`) (supports .cjs, .mjs, .cts, .mts), or undefined (match any filename) if output: true.

If a file is matched by more than one pattern (as the example below), the options other than loader will be shallowly merged into and possibly override the previous ones.

// options
[
  {
    loader: 'tsx',
    legalComments: 'eof'
  },
  {
    loader: 'ts',
    include: /\.tsx?$/,
    tsconfig: join(__dirname, 'tsconfig.json')
  }
]

// the final transform options for `index.tsx` will become
{
  loader: 'tsx',
  legalComments: 'eof',
  tsconfig: join(__dirname, 'tsconfig.json')
}

exclude

Default to /node_modules/, or undefined if output: true.

It takes priority over include.

Other default options

// output: false | undefined
{
  format: options.loader === 'json' ? 'esm' : undefined,
  sourcefile: id, // the resolved file path
  sourcemap: true,
  ...options
}

// output: true
{
  sourcefile: chunk.fileName,
  sourcemap: true,
  ...options
}

License

MIT License © 2021 Exuanbo

changelog

Changelog

1.5.0 (2022-08-13)

Features

  • Support TypeScript 4.7 "module": "Node16" resolution.

Bug Fixes

1.4.1 (2022-07-17)

Bug Fixes

  • Loader default was not handled correctly.

1.4.0 (2022-05-16)

Features

  • Add Options.tsconfig to specify tsconfig.json file. It will not be used if tsconfigRaw is provided.
  • Export extended TransformOptions.

1.3.2 (2021-11-27)

Bug Fixes

  • Import of TypeScript files with extension cts and mts could not be resolved.

1.3.1 (2021-11-26)

Chores

  • Add JSDoc to interface Options.

Code Refactoring

  • Replace regex with path.isAbsolute() when testing path.
  • Remove unnecessary overload function signatures.

1.3.0 (2021-11-20)

Bug Fixes

  • Absolute path import on Windows such as C:\foo could not be resolved.

Features

  • Parameter options is now optional and Options.loader is not required anymore.
  • Add Options.output for indicating whether this transformation should be performed after the chunk (bundle) has been rendered.

1.2.0 (2021-11-06)

Features

  • Support resolve TypeScript files with extension cts and mts (requires esbuild >= 0.13.4).

1.1.1 (2021-08-26)

Bug Fixes

  • In the previous versions resolveId() hook uses path.sep to check whether the argument is an absolute path, which will not work as expected if used on Windows and the path starts with posix path separator.

1.1.0 (2021-08-25)

BREAKING CHANGES

  • From this version the plugin will not transform the file multiple times if more than one pattern is matched, but will merge the options. See include for more details.

Bug Fixes

  • Extensions .cjs and .mjs are not resolved by default