A Parcel transformer plugin that transforms JavaScript code using Babel with automatic configuration discovery and intelligent defaults for modern JavaScript, TypeScript, JSX, and Flow.
npx @tessl/cli install tessl/npm-parcel--transformer-babel@2.15.0@parcel/transformer-babel is a Parcel transformer plugin that integrates Babel compilation into the Parcel build pipeline. It automatically discovers and applies Babel configuration files or uses intelligent defaults when no configuration is found, supporting modern JavaScript features, TypeScript, JSX, and Flow type stripping.
npm install @parcel/transformer-babel// This is a Parcel plugin - not directly imported by users
// Parcel automatically loads and uses this transformer based on configuration
// For plugin development or advanced use cases:
const BabelTransformer = require("@parcel/transformer-babel");
// BabelTransformer is the default export - a pre-configured Transformer instance
// Named exports for specific functionality:
const { load } = require("@parcel/transformer-babel/lib/config");
const { babelErrorEnhancer } = require("@parcel/transformer-babel/lib/babelErrorUtils");This transformer is used internally by Parcel and not directly by user code. Parcel automatically applies this transformer to JavaScript, TypeScript, and JSX files based on configuration.
Automatic Babel Configuration Discovery:
// .babelrc or babel.config.js in your project
{
"presets": ["@babel/preset-env"],
"plugins": ["@babel/plugin-proposal-class-properties"]
}Default Behavior (no config):
flow-bin is detected.ts and .tsx files@babel/preset-env with browserslist targets from package.json@parcel/transformer-babel is built around several key components:
The core transformer that integrates with Parcel's build pipeline to apply Babel transformations to JavaScript assets.
// Default export - Pre-configured Transformer instance (not a class)
export default BabelTransformer: Transformer;
interface Transformer {
loadConfig(options: {
config: Config;
options: PluginOptions;
logger: PluginLogger;
}): Promise<BabelConfigResult | null>;
canReuseAST(options: { ast: AST }): boolean;
transform(options: {
asset: MutableAsset;
config: BabelConfigResult | null;
logger: PluginLogger;
options: PluginOptions;
tracer: PluginTracer;
}): Promise<MutableAsset[]>;
generate(options: {
asset: MutableAsset;
ast: AST;
options: PluginOptions;
}): Promise<{ content: string; map: SourceMap }>;
}Babel configuration loading, validation, and default configuration generation with automatic dependency detection.
function load(
config: Config,
options: PluginOptions,
logger: PluginLogger
): Promise<BabelConfigResult | null>;
interface BabelConfigResult {
internal: boolean;
config: BabelConfig;
targets?: mixed;
syntaxPlugins?: mixed;
}
interface BabelConfig {
plugins?: Array<any>;
presets?: Array<any>;
}Core Babel 7 processing functionality that performs the actual code transformation with AST management.
function babel7(opts: Babel7TransformOptions): Promise<AST | null>;
interface Babel7TransformOptions {
asset: MutableAsset;
options: PluginOptions;
logger: PluginLogger;
babelOptions: any;
additionalPlugins?: Array<any>;
tracer: PluginTracer;
}Enhanced error reporting and diagnostics for Babel compilation errors.
function babelErrorEnhancer(
error: BabelError,
asset: BaseAsset
): Promise<BabelError>;
interface BabelError extends Error {
loc?: {
line: number;
column: number;
};
source?: string;
filePath?: string;
}Internal helper functions for target conversion, JSX detection, Flow configuration, and AST manipulation. These are not exported and are used internally by the transformer.
// Internal utility functions (not exported)
function enginesToBabelTargets(env: Environment): BabelTargets; // @internal
function isJSX(options: PluginOptions, config: Config): Promise<boolean>; // @internal
function getFlowOptions(config: Config, options: PluginOptions): Promise<BabelConfig | null>; // @internal
function remapAstLocations(t: BabelTypes, ast: BabelNodeFile, map: SourceMap): void; // @internal// Core Parcel types used throughout the transformer
interface Config {
isSource: boolean;
searchPath: string;
invalidateOnFileCreate(options: { fileName: string; aboveFilePath: string }): void;
invalidateOnFileChange(filePath: string): void;
invalidateOnStartup(): void;
invalidateOnEnvChange(envVar: string): void;
addDevDependency(dependency: DevDependency): void;
setCacheKey(key: string): void;
getPackage(): Promise<PackageJSON | null>;
getConfigFrom<T>(searchPath: string, filenames: string[]): Promise<ConfigResult<T> | null>;
}
interface PluginOptions {
inputFS: FileSystem;
packageManager: PackageManager;
projectRoot: string;
env: { [key: string]: string };
mode: string;
shouldAutoInstall: boolean;
}
interface MutableAsset {
filePath: string;
env: Environment;
meta: AssetMeta;
getAST(): Promise<AST | null>;
setAST(ast: AST): void;
getCode(): Promise<string>;
getMap(): Promise<SourceMap | null>;
isASTDirty(): boolean;
invalidateOnFileChange(filePath: string): void;
invalidateOnFileCreate(options: { filePath: string }): void;
}
interface Environment {
engines: { [key: string]: string };
outputFormat: string;
sourceMap: boolean;
isBrowser(): boolean;
}