0
# Custom Plugin Factories
1
2
Custom plugin factories allow you to create specialized Babel plugins with custom handling of configuration and transformation logic. They provide access to the exact same Babel core instance used by the plugin and enable advanced customization scenarios.
3
4
## Capabilities
5
6
### Input Plugin Factory
7
8
Creates a factory function for custom input plugins with specialized configuration handling.
9
10
```javascript { .api }
11
/**
12
* Creates a factory for custom input plugins
13
* @param customCallback - Callback function for customizing plugin behavior
14
* @returns Factory function that creates input plugins
15
*/
16
function createBabelInputPluginFactory(
17
customCallback?: RollupBabelCustomInputPluginBuilder
18
): typeof getBabelInputPlugin;
19
20
type RollupBabelCustomInputPluginBuilder = (
21
babel: typeof babelCore
22
) => RollupBabelCustomInputPlugin;
23
```
24
25
**Usage Examples:**
26
27
```javascript
28
import { createBabelInputPluginFactory } from '@rollup/plugin-babel';
29
30
// Create a custom input plugin factory
31
const customBabelPlugin = createBabelInputPluginFactory((babelCore) => {
32
function myCustomPlugin() {
33
return {
34
visitor: {
35
// Custom Babel plugin logic
36
FunctionDeclaration(path) {
37
// Transform function declarations
38
}
39
}
40
};
41
}
42
43
return {
44
// Custom options processing
45
options({ customOption, ...pluginOptions }) {
46
return {
47
customOptions: { customOption },
48
pluginOptions
49
};
50
},
51
52
// Custom Babel configuration
53
config(cfg, { code, customOptions }) {
54
if (cfg.hasFilesystemConfig()) {
55
return cfg.options;
56
}
57
58
return {
59
...cfg.options,
60
plugins: [
61
...(cfg.options.plugins || []),
62
myCustomPlugin
63
]
64
};
65
},
66
67
// Custom result processing
68
result(result, { code, customOptions, config, transformOptions }) {
69
return {
70
...result,
71
code: result.code + '\n// Generated by custom plugin'
72
};
73
}
74
};
75
});
76
77
// Use the custom plugin
78
export default {
79
input: 'src/index.js',
80
plugins: [
81
customBabelPlugin({
82
babelHelpers: 'bundled',
83
customOption: 'value'
84
})
85
]
86
};
87
```
88
89
### Output Plugin Factory
90
91
Creates a factory function for custom output plugins.
92
93
```javascript { .api }
94
/**
95
* Creates a factory for custom output plugins
96
* @param customCallback - Callback function for customizing plugin behavior
97
* @returns Factory function that creates output plugins
98
*/
99
function createBabelOutputPluginFactory(
100
customCallback?: RollupBabelCustomOutputPluginBuilder
101
): typeof getBabelOutputPlugin;
102
103
type RollupBabelCustomOutputPluginBuilder = (
104
babel: typeof babelCore
105
) => RollupBabelCustomOutputPlugin;
106
```
107
108
**Usage Examples:**
109
110
```javascript
111
import { createBabelOutputPluginFactory } from '@rollup/plugin-babel';
112
113
// Create a custom output plugin factory
114
const customOutputPlugin = createBabelOutputPluginFactory((babelCore) => {
115
return {
116
options({ format, ...pluginOptions }) {
117
return {
118
customOptions: { format },
119
pluginOptions
120
};
121
},
122
123
config(cfg, { code, customOptions }) {
124
return {
125
...cfg.options,
126
presets: [
127
['@babel/preset-env', {
128
modules: customOptions.format === 'es' ? false : 'auto'
129
}]
130
]
131
};
132
}
133
};
134
});
135
136
// Use the custom plugin
137
export default {
138
input: 'main.js',
139
output: {
140
file: 'bundle.js',
141
format: 'cjs',
142
plugins: [customOutputPlugin({ format: 'cjs' })]
143
}
144
};
145
```
146
147
## Custom Plugin Interfaces
148
149
### Input Plugin Interface
150
151
```javascript { .api }
152
interface RollupBabelCustomInputPlugin {
153
/**
154
* Custom options processing function
155
*/
156
options?: RollupBabelCustomInputPluginOptions;
157
158
/**
159
* Custom Babel configuration function
160
*/
161
config?: RollupBabelCustomInputPluginConfig;
162
163
/**
164
* Custom result processing function
165
*/
166
result?: RollupBabelCustomInputPluginResult;
167
}
168
169
type RollupBabelCustomInputPluginOptions = (
170
options: RollupBabelInputPluginOptions & Record<string, any>
171
) => {
172
customOptions: Record<string, any>;
173
pluginOptions: RollupBabelInputPluginOptions;
174
};
175
176
type RollupBabelCustomInputPluginConfig = (
177
this: TransformPluginContext,
178
cfg: BabelPartialConfig,
179
options: RollupBabelCustomPluginConfigOptions
180
) => BabelTransformOptions;
181
182
type RollupBabelCustomInputPluginResult = (
183
this: TransformPluginContext,
184
result: BabelFileResult,
185
options: RollupBabelCustomPluginResultOptions
186
) => BabelFileResult;
187
```
188
189
### Output Plugin Interface
190
191
```javascript { .api }
192
interface RollupBabelCustomOutputPlugin {
193
/**
194
* Custom options processing function
195
*/
196
options?: RollupBabelCustomOutputPluginOptions;
197
198
/**
199
* Custom Babel configuration function
200
*/
201
config?: RollupBabelCustomOutputPluginConfig;
202
203
/**
204
* Custom result processing function
205
*/
206
result?: RollupBabelCustomOutputPluginResult;
207
}
208
209
type RollupBabelCustomOutputPluginOptions = (
210
options: RollupBabelOutputPluginOptions & Record<string, any>
211
) => {
212
customOptions: Record<string, any>;
213
pluginOptions: RollupBabelOutputPluginOptions;
214
};
215
216
type RollupBabelCustomOutputPluginConfig = (
217
this: PluginContext,
218
cfg: BabelPartialConfig,
219
options: RollupBabelCustomPluginConfigOptions
220
) => BabelTransformOptions;
221
222
type RollupBabelCustomOutputPluginResult = (
223
this: PluginContext,
224
result: BabelFileResult,
225
options: RollupBabelCustomPluginResultOptions
226
) => BabelFileResult;
227
```
228
229
## Shared Types and Interfaces
230
231
```javascript { .api }
232
interface RollupBabelCustomPluginConfigOptions {
233
/**
234
* Source code being transformed
235
*/
236
code: string;
237
238
/**
239
* Custom options extracted from plugin options
240
*/
241
customOptions: Record<string, any>;
242
}
243
244
interface RollupBabelCustomPluginResultOptions {
245
/**
246
* Source code being transformed
247
*/
248
code: string;
249
250
/**
251
* Custom options extracted from plugin options
252
*/
253
customOptions: Record<string, any>;
254
255
/**
256
* Babel partial configuration object
257
*/
258
config: BabelPartialConfig;
259
260
/**
261
* Final Babel transform options
262
*/
263
transformOptions: BabelTransformOptions;
264
}
265
```
266
267
## Advanced Usage Patterns
268
269
### Framework Integration
270
271
Creating a plugin for specific framework needs:
272
273
```javascript
274
const createReactPlugin = createBabelInputPluginFactory((babel) => {
275
return {
276
options({ reactVersion, ...options }) {
277
return {
278
customOptions: { reactVersion },
279
pluginOptions: options
280
};
281
},
282
283
config(cfg, { customOptions }) {
284
const reactPreset = customOptions.reactVersion === '17'
285
? ['@babel/preset-react', { runtime: 'automatic' }]
286
: ['@babel/preset-react'];
287
288
return {
289
...cfg.options,
290
presets: [
291
...(cfg.options.presets || []),
292
reactPreset
293
]
294
};
295
}
296
};
297
});
298
```
299
300
### Development vs Production
301
302
Different behavior for different environments:
303
304
```javascript
305
const createEnvironmentAwarePlugin = createBabelInputPluginFactory((babel) => {
306
return {
307
config(cfg, { code }) {
308
const isProduction = process.env.NODE_ENV === 'production';
309
310
return {
311
...cfg.options,
312
plugins: [
313
...(cfg.options.plugins || []),
314
...(isProduction ? [] : ['@babel/plugin-transform-react-jsx-source'])
315
]
316
};
317
},
318
319
result(result, { customOptions }) {
320
if (process.env.NODE_ENV === 'development') {
321
// Add development helpers
322
return {
323
...result,
324
code: result.code + '\n// Development build'
325
};
326
}
327
return result;
328
}
329
};
330
});
331
```
332
333
## Base Type Definitions
334
335
```javascript { .api }
336
// From @babel/core
337
interface BabelTransformOptions {
338
filename?: string;
339
filenameRelative?: string;
340
presets?: any[];
341
plugins?: any[];
342
sourceMaps?: boolean;
343
sourceType?: 'script' | 'module' | 'unambiguous';
344
// ... other Babel options
345
}
346
347
interface BabelPartialConfig {
348
options: BabelTransformOptions;
349
hasFilesystemConfig(): boolean;
350
}
351
352
interface BabelFileResult {
353
code: string;
354
map?: object;
355
ast?: object;
356
}
357
358
// From rollup
359
interface TransformPluginContext {
360
error(message: string): never;
361
warn(message: string): void;
362
// ... other context methods
363
}
364
365
interface PluginContext {
366
error(message: string): never;
367
warn(message: string): void;
368
// ... other context methods
369
}
370
371
// Input plugin options interface
372
interface RollupBabelInputPluginOptions extends Omit<BabelTransformOptions, 'include' | 'exclude'> {
373
include?: FilterPattern;
374
exclude?: FilterPattern;
375
filter?: ReturnType<CreateFilter>;
376
extensions?: string[];
377
babelHelpers?: 'bundled' | 'runtime' | 'inline' | 'external';
378
skipPreflightCheck?: boolean;
379
}
380
381
// Output plugin options interface
382
interface RollupBabelOutputPluginOptions extends Omit<BabelTransformOptions, 'include' | 'exclude'> {
383
allowAllFormats?: boolean;
384
}
385
386
// Utility types
387
type FilterPattern = string | RegExp | Array<string | RegExp>;
388
type CreateFilter = (include?: FilterPattern, exclude?: FilterPattern, options?: FilterOptions) => (id: string | unknown) => boolean;
389
390
interface FilterOptions {
391
resolve?: string | false | null;
392
}
393
```