0
# @rollup/plugin-babel
1
2
@rollup/plugin-babel provides seamless integration between Rollup and Babel, enabling developers to transpile ES6/7 code during the bundling process. It offers multiple helper handling strategies and supports both input and output transformation modes with comprehensive configuration options.
3
4
## Package Information
5
6
- **Package Name**: @rollup/plugin-babel
7
- **Package Type**: npm
8
- **Language**: JavaScript/TypeScript
9
- **Installation**: `npm install @rollup/plugin-babel --save-dev`
10
11
## Core Imports
12
13
```javascript
14
import { babel } from '@rollup/plugin-babel';
15
```
16
17
For specific functions:
18
19
```javascript
20
import {
21
babel,
22
getBabelInputPlugin,
23
getBabelOutputPlugin,
24
createBabelInputPluginFactory,
25
createBabelOutputPluginFactory
26
} from '@rollup/plugin-babel';
27
```
28
29
Using default export:
30
31
```javascript
32
import babel from '@rollup/plugin-babel';
33
// babel is an alias for getBabelInputPlugin
34
```
35
36
For CommonJS:
37
38
```javascript
39
const { babel, getBabelInputPlugin, getBabelOutputPlugin } = require('@rollup/plugin-babel');
40
// or
41
const babel = require('@rollup/plugin-babel').default;
42
```
43
44
## Basic Usage
45
46
```javascript
47
import { babel } from '@rollup/plugin-babel';
48
49
const config = {
50
input: 'src/index.js',
51
output: {
52
dir: 'output',
53
format: 'es'
54
},
55
plugins: [babel({ babelHelpers: 'bundled' })]
56
};
57
58
export default config;
59
```
60
61
## Architecture
62
63
@rollup/plugin-babel is built around several key components:
64
65
- **Input Transformation**: `babel()` and `getBabelInputPlugin()` transform source files during the input phase
66
- **Output Transformation**: `getBabelOutputPlugin()` transforms generated chunks during the output phase
67
- **Helper Strategies**: Four different approaches for handling Babel helpers (bundled, runtime, inline, external)
68
- **Factory Functions**: `createBabelInputPluginFactory()` and `createBabelOutputPluginFactory()` for custom plugin creation
69
- **Configuration System**: Comprehensive options handling with validation and defaults
70
- **Filter System**: Advanced file filtering with include/exclude patterns and custom filter functions
71
72
## Capabilities
73
74
### Input Plugin
75
76
Primary plugin function for transforming source files during input processing. Supports comprehensive Babel configuration and file filtering.
77
78
```javascript { .api }
79
function babel(options?: RollupBabelInputPluginOptions): Plugin;
80
function getBabelInputPlugin(options?: RollupBabelInputPluginOptions): Plugin;
81
82
interface RollupBabelInputPluginOptions extends Omit<BabelTransformOptions, 'include' | 'exclude'> {
83
include?: FilterPattern;
84
exclude?: FilterPattern;
85
filter?: ReturnType<CreateFilter>;
86
extensions?: string[];
87
babelHelpers?: 'bundled' | 'runtime' | 'inline' | 'external';
88
skipPreflightCheck?: boolean;
89
}
90
```
91
92
[Input Plugin](./input-plugin.md)
93
94
### Output Plugin
95
96
Plugin for transforming output chunks after bundle generation. Ideal for compatibility transformations and format-specific optimizations.
97
98
```javascript { .api }
99
function getBabelOutputPlugin(options?: RollupBabelOutputPluginOptions): Plugin;
100
101
interface RollupBabelOutputPluginOptions extends Omit<BabelTransformOptions, 'include' | 'exclude'> {
102
allowAllFormats?: boolean;
103
}
104
```
105
106
[Output Plugin](./output-plugin.md)
107
108
### Custom Plugin Factories
109
110
Advanced factory functions for creating custom Babel plugins with specialized configuration and processing logic.
111
112
```javascript { .api }
113
function createBabelInputPluginFactory(
114
customCallback?: RollupBabelCustomInputPluginBuilder
115
): typeof getBabelInputPlugin;
116
117
function createBabelOutputPluginFactory(
118
customCallback?: RollupBabelCustomOutputPluginBuilder
119
): typeof getBabelOutputPlugin;
120
121
type RollupBabelCustomInputPluginBuilder = (
122
babel: typeof babelCore
123
) => RollupBabelCustomInputPlugin;
124
125
type RollupBabelCustomOutputPluginBuilder = (
126
babel: typeof babelCore
127
) => RollupBabelCustomOutputPlugin;
128
```
129
130
[Custom Plugin Factories](./custom-plugin-factories.md)
131
132
## Core Types
133
134
```javascript { .api }
135
// From @rollup/pluginutils
136
type FilterPattern = string | RegExp | Array<string | RegExp>;
137
type CreateFilter = (include?: FilterPattern, exclude?: FilterPattern, options?: FilterOptions) => (id: string | unknown) => boolean;
138
139
interface FilterOptions {
140
resolve?: string | false | null;
141
}
142
143
// From rollup
144
interface Plugin {
145
name: string;
146
options?(): void;
147
resolveId?(id: string): string | null;
148
load?(id: string): string | null;
149
transform?(code: string, filename: string): TransformResult | null;
150
renderStart?(outputOptions: OutputOptions): void;
151
renderChunk?(code: string, chunk: RenderedChunk, outputOptions: OutputOptions): TransformResult | null;
152
}
153
154
interface TransformPluginContext {
155
error(message: string): never;
156
warn(message: string): void;
157
// ... other context methods
158
}
159
160
interface PluginContext {
161
error(message: string): never;
162
warn(message: string): void;
163
// ... other context methods
164
}
165
166
interface TransformResult {
167
code: string;
168
map?: SourceMap;
169
}
170
171
interface SourceMap {
172
version: number;
173
sources: string[];
174
names: string[];
175
mappings: string;
176
file?: string;
177
sourcesContent?: string[];
178
}
179
180
// From @babel/core
181
interface BabelTransformOptions {
182
filename?: string;
183
filenameRelative?: string;
184
presets?: any[];
185
plugins?: any[];
186
sourceMaps?: boolean;
187
sourceType?: 'script' | 'module' | 'unambiguous';
188
// ... other Babel options
189
}
190
191
interface BabelPartialConfig {
192
options: BabelTransformOptions;
193
hasFilesystemConfig(): boolean;
194
}
195
196
interface BabelFileResult {
197
code: string;
198
map?: object;
199
ast?: object;
200
}
201
202
// Custom plugin interfaces
203
interface RollupBabelCustomInputPlugin {
204
options?: RollupBabelCustomInputPluginOptions;
205
config?: RollupBabelCustomInputPluginConfig;
206
result?: RollupBabelCustomInputPluginResult;
207
}
208
209
interface RollupBabelCustomOutputPlugin {
210
options?: RollupBabelCustomOutputPluginOptions;
211
config?: RollupBabelCustomOutputPluginConfig;
212
result?: RollupBabelCustomOutputPluginResult;
213
}
214
215
type RollupBabelCustomInputPluginOptions = (
216
options: RollupBabelInputPluginOptions & Record<string, any>
217
) => {
218
customOptions: Record<string, any>;
219
pluginOptions: RollupBabelInputPluginOptions;
220
};
221
222
type RollupBabelCustomOutputPluginOptions = (
223
options: RollupBabelOutputPluginOptions & Record<string, any>
224
) => {
225
customOptions: Record<string, any>;
226
pluginOptions: RollupBabelOutputPluginOptions;
227
};
228
229
interface RollupBabelCustomPluginConfigOptions {
230
code: string;
231
customOptions: Record<string, any>;
232
}
233
234
interface RollupBabelCustomPluginResultOptions {
235
code: string;
236
customOptions: Record<string, any>;
237
config: BabelPartialConfig;
238
transformOptions: BabelTransformOptions;
239
}
240
241
type RollupBabelCustomInputPluginConfig = (
242
this: TransformPluginContext,
243
cfg: BabelPartialConfig,
244
options: RollupBabelCustomPluginConfigOptions
245
) => BabelTransformOptions;
246
247
type RollupBabelCustomInputPluginResult = (
248
this: TransformPluginContext,
249
result: BabelFileResult,
250
options: RollupBabelCustomPluginResultOptions
251
) => BabelFileResult;
252
253
type RollupBabelCustomOutputPluginConfig = (
254
this: PluginContext,
255
cfg: BabelPartialConfig,
256
options: RollupBabelCustomPluginConfigOptions
257
) => BabelTransformOptions;
258
259
type RollupBabelCustomOutputPluginResult = (
260
this: PluginContext,
261
result: BabelFileResult,
262
options: RollupBabelCustomPluginResultOptions
263
) => BabelFileResult;
264
```