0
# Rollup Plugin ESBuild
1
2
Rollup Plugin ESBuild is a high-performance Rollup plugin that leverages esbuild's fast TypeScript/JavaScript compilation and minification capabilities. It serves as a comprehensive replacement for multiple traditional Rollup plugins including rollup-plugin-typescript2, @rollup/plugin-typescript, and rollup-plugin-terser.
3
4
## Package Information
5
6
- **Package Name**: rollup-plugin-esbuild
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install rollup-plugin-esbuild esbuild --save-dev`
10
11
## Core Imports
12
13
```typescript
14
import esbuild, { minify } from "rollup-plugin-esbuild";
15
```
16
17
For CommonJS:
18
19
```javascript
20
const esbuild = require("rollup-plugin-esbuild").default;
21
const { minify } = require("rollup-plugin-esbuild");
22
```
23
24
## Basic Usage
25
26
```typescript
27
import esbuild from "rollup-plugin-esbuild";
28
29
export default {
30
input: "src/index.ts",
31
output: {
32
file: "dist/bundle.js",
33
format: "es",
34
},
35
plugins: [
36
esbuild({
37
include: /\.[jt]sx?$/,
38
exclude: /node_modules/,
39
sourceMap: true,
40
minify: process.env.NODE_ENV === "production",
41
target: "es2017",
42
jsx: "transform",
43
tsconfig: "tsconfig.json",
44
}),
45
],
46
};
47
```
48
49
## Architecture
50
51
Rollup Plugin ESBuild is built around several key components:
52
53
- **Main Plugin Function**: The default export creates a full-featured Rollup plugin with transformation, compilation, and optional minification
54
- **Standalone Minify Plugin**: The `minify` function provides minification-only functionality for projects that only need code compression
55
- **TypeScript Integration**: Automatic tsconfig.json detection and configuration inheritance for seamless TypeScript compilation
56
- **Dependency Optimization**: Experimental pre-bundling feature for optimizing external dependencies using esbuild's bundling capabilities
57
- **Flexible Loaders**: Configurable file type processing with extensible loader system for different file formats
58
59
## Capabilities
60
61
### Main Plugin Function
62
63
Core esbuild transformation plugin providing TypeScript/JavaScript compilation, JSX transformation, and optional minification within the Rollup build pipeline.
64
65
```typescript { .api }
66
function esbuild(options?: Options): RollupPlugin;
67
68
interface Options {
69
include?: FilterPattern;
70
exclude?: FilterPattern;
71
sourceMap?: boolean;
72
optimizeDeps?: OptimizeDepsOptions;
73
tsconfig?: string | false;
74
loaders?: { [ext: string]: Loader | false };
75
// Plus all esbuild TransformOptions except 'sourcemap' and 'loader'
76
minify?: boolean;
77
minifyWhitespace?: boolean;
78
minifyIdentifiers?: boolean;
79
minifySyntax?: boolean;
80
target?: string | string[];
81
jsx?: "transform" | "preserve" | "automatic";
82
jsxFactory?: string;
83
jsxFragment?: string;
84
jsxImportSource?: string;
85
jsxDev?: boolean;
86
define?: { [key: string]: string };
87
charset?: "ascii" | "utf8";
88
treeShaking?: boolean;
89
ignoreAnnotations?: boolean;
90
legalComments?: "none" | "inline" | "eof" | "linked" | "external";
91
keepNames?: boolean;
92
format?: "iife" | "cjs" | "esm";
93
globalName?: string;
94
mangleProps?: RegExp;
95
reserveProps?: RegExp;
96
mangleQuoted?: boolean;
97
mangleCache?: { [key: string]: string | false };
98
drop?: ("console" | "debugger")[];
99
dropLabels?: string[];
100
}
101
102
type FilterPattern = string | RegExp | (string | RegExp)[];
103
```
104
105
[Main Plugin](./main-plugin.md)
106
107
### Standalone Minification
108
109
Standalone minification plugin for projects that only need code compression without transformation or compilation features.
110
111
```typescript { .api }
112
function minify(options?: MinifyOptions): RollupPlugin;
113
114
interface MinifyOptions {
115
sourceMap?: boolean;
116
// Plus all esbuild TransformOptions except 'sourcemap'
117
minify?: boolean;
118
minifyWhitespace?: boolean;
119
minifyIdentifiers?: boolean;
120
minifySyntax?: boolean;
121
target?: string | string[];
122
charset?: "ascii" | "utf8";
123
treeShaking?: boolean;
124
ignoreAnnotations?: boolean;
125
legalComments?: "none" | "inline" | "eof" | "linked" | "external";
126
keepNames?: boolean;
127
format?: "iife" | "cjs" | "esm";
128
globalName?: string;
129
mangleProps?: RegExp;
130
reserveProps?: RegExp;
131
mangleQuoted?: boolean;
132
mangleCache?: { [key: string]: string | false };
133
drop?: ("console" | "debugger")[];
134
dropLabels?: string[];
135
}
136
```
137
138
[Minification](./minification.md)
139
140
### Dependency Optimization
141
142
Experimental pre-bundling feature for optimizing external dependencies using esbuild's fast bundling capabilities, eliminating the need for additional CommonJS and Node.js resolution plugins.
143
144
```typescript { .api }
145
interface OptimizeDepsOptions {
146
include: string[];
147
exclude?: string[];
148
cwd: string;
149
esbuildOptions?: EsbuildBuildOptions;
150
sourceMap: boolean;
151
}
152
153
interface OptimizeDepsResult {
154
optimized: Map<string, { file: string }>;
155
cacheDir: string;
156
}
157
```
158
159
[Dependency Optimization](./dependency-optimization.md)
160
161
## Types
162
163
### Core Types
164
165
```typescript { .api }
166
// Re-export from esbuild for convenience
167
type Loader =
168
| "js"
169
| "jsx"
170
| "ts"
171
| "tsx"
172
| "css"
173
| "json"
174
| "text"
175
| "base64"
176
| "binary"
177
| "dataurl";
178
179
// Rollup plugin interface
180
interface RollupPlugin {
181
name: string;
182
options?: (opts: InputOptions) => InputOptions | null;
183
buildStart?: (opts: NormalizedInputOptions) => void;
184
resolveId?: (id: string, importer?: string) => string | null;
185
transform?: (code: string, id: string) => TransformResult | null;
186
renderChunk?: (code: string, chunk: RenderedChunk, options: NormalizedOutputOptions) => TransformResult | null;
187
}
188
189
interface TransformResult {
190
code: string;
191
map?: string | SourceMap | null;
192
}
193
```