Seamless integration between Rollup and Babel enabling transpilation during bundling with multiple helper strategies.
—
The input plugin transforms source files during the input processing phase of Rollup. It's the primary way to use Babel with Rollup and supports comprehensive configuration options for file filtering, helper handling, and Babel integration.
The default export and primary plugin function for input transformation.
/**
* A Rollup plugin for seamless integration between Rollup and Babel
* @param options - Plugin options extending Babel transform options
* @returns Plugin instance for input transformation
*/
function babel(options?: RollupBabelInputPluginOptions): Plugin;
// Alias for babel function
function getBabelInputPlugin(options?: RollupBabelInputPluginOptions): Plugin;Usage Examples:
import { babel } from '@rollup/plugin-babel';
// Basic usage with bundled helpers
export default {
input: 'src/index.js',
plugins: [babel({ babelHelpers: 'bundled' })],
output: { file: 'dist/bundle.js', format: 'es' }
};
// Advanced configuration
export default {
input: 'src/index.js',
plugins: [
babel({
babelHelpers: 'runtime',
extensions: ['.js', '.jsx', '.ts', '.tsx'],
exclude: 'node_modules/**',
include: 'src/**/*',
skipPreflightCheck: false,
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: ['@babel/plugin-transform-runtime']
})
],
output: { file: 'dist/bundle.js', format: 'es' }
};Complete configuration interface for input plugin.
interface RollupBabelInputPluginOptions extends Omit<BabelTransformOptions, 'include' | 'exclude'> {
/**
* A picomatch pattern, or array of patterns, which specifies the files to include
* @default undefined
*/
include?: FilterPattern;
/**
* A picomatch pattern, or array of patterns, which specifies the files to exclude
* @default undefined
*/
exclude?: FilterPattern;
/**
* Custom filter function for determining which modules should be operated upon
* @default undefined
*/
filter?: ReturnType<CreateFilter>;
/**
* An array of file extensions that Babel should transpile
* @default ['.js', '.jsx', '.es6', '.es', '.mjs']
*/
extensions?: string[];
/**
* How babel helpers are inserted into the code
* @default 'bundled'
*/
babelHelpers?: 'bundled' | 'runtime' | 'inline' | 'external';
/**
* Skip validation checks for better performance in large projects
* @default false
*/
skipPreflightCheck?: boolean;
}Usage Examples:
Control which files are processed by the plugin:
import { babel } from '@rollup/plugin-babel';
import { createFilter } from '@rollup/pluginutils';
// Using include/exclude patterns
babel({
babelHelpers: 'bundled',
include: ['src/**/*', 'lib/**/*'],
exclude: ['**/*.test.js', 'node_modules/**']
});
// Using custom filter function
const filter = createFilter('src/**/*.js', 'src/**/*.test.js');
babel({
babelHelpers: 'bundled',
filter: filter
});
// Using extensions
babel({
babelHelpers: 'bundled',
extensions: ['.js', '.jsx', '.ts', '.tsx']
});Different approaches for handling Babel helpers:
// Bundled helpers (recommended for applications)
babel({ babelHelpers: 'bundled' });
// Runtime helpers (recommended for libraries)
babel({
babelHelpers: 'runtime',
plugins: ['@babel/plugin-transform-runtime']
});
// External helpers (requires global babelHelpers)
babel({
babelHelpers: 'external',
plugins: ['@babel/plugin-external-helpers']
});
// Inline helpers (not recommended - causes duplication)
babel({ babelHelpers: 'inline' });Options for improving build performance:
// Skip preflight checks for better performance
babel({
babelHelpers: 'bundled',
skipPreflightCheck: true, // Only use if configuration is stable
exclude: 'node_modules/**' // Exclude dependencies
});Proper plugin ordering, especially with CommonJS:
import commonjs from '@rollup/plugin-commonjs';
import { babel } from '@rollup/plugin-babel';
export default {
plugins: [
// CommonJS must come before Babel
commonjs(),
babel({ babelHelpers: 'bundled' })
]
};// From @rollup/pluginutils
type FilterPattern = string | RegExp | Array<string | RegExp>;
type CreateFilter = (include?: FilterPattern, exclude?: FilterPattern, options?: FilterOptions) => (id: string | unknown) => boolean;
interface FilterOptions {
resolve?: string | false | null;
}
// From @babel/core
interface BabelTransformOptions {
filename?: string;
filenameRelative?: string;
presets?: any[];
plugins?: any[];
sourceMaps?: boolean;
sourceType?: 'script' | 'module' | 'unambiguous';
// ... other Babel options
}
// From rollup
interface Plugin {
name: string;
options?(): void;
resolveId?(id: string): string | null;
load?(id: string): string | null;
transform?(code: string, filename: string): TransformResult | null;
}
interface TransformResult {
code: string;
map?: SourceMap;
}
interface SourceMap {
version: number;
sources: string[];
names: string[];
mappings: string;
file?: string;
sourcesContent?: string[];
}Install with Tessl CLI
npx tessl i tessl/npm-rollup--plugin-babel