A Parcel transformer plugin that transforms JavaScript code using Babel with automatic configuration discovery and intelligent defaults for modern JavaScript, TypeScript, JSX, and Flow.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Internal helper functions for target conversion, JSX detection, Flow configuration, and AST manipulation that support the core Babel transformation functionality. These functions are not directly exported but are used internally by the transformer.
Converts Parcel's engine specifications to Babel preset-env targets format.
/**
* Converts Parcel's engine specifications to Babel preset-env targets
* Handles browser targets, Node.js versions, and ES module output optimization
* @param env - Parcel environment object with engine specifications
* @returns BabelTargets object compatible with @babel/preset-env
* @internal - Used internally by the configuration system
*/
function enginesToBabelTargets(env: Environment): BabelTargets;
interface Environment {
engines: { [engineName: string]: string }; // Engine version specifications
outputFormat: string; // Output format (esm, cjs, etc.)
isBrowser(): boolean; // Browser environment check
}
interface BabelTargets {
[engineName: string]: string | boolean | Array<string>;
browsers?: string | Array<string>; // Browser target queries
esmodules?: boolean; // ES modules target flag
}Supported Target Names:
const TargetNames = {
node: 'node',
chrome: 'chrome',
opera: 'opera',
edge: 'edge',
firefox: 'firefox',
safari: 'safari',
ie: 'ie',
ios: 'ios',
android: 'android',
electron: 'electron',
samsung: 'samsung',
rhino: 'rhino'
};ES Module Browser Exclusions:
const ESMODULE_BROWSERS = [
'not ie <= 11',
'not edge < 16',
'not firefox < 60',
'not chrome < 61',
'not safari < 11',
'not opera < 48',
'not ios_saf < 11',
'not op_mini all',
'not android < 76',
'not blackberry > 0',
'not op_mob > 0',
'not and_chr < 76',
'not and_ff < 68',
'not ie_mob > 0',
'not and_uc > 0',
'not samsung < 8.2',
'not and_qq > 0',
'not baidu > 0',
'not kaios > 0'
];Usage Examples:
// Convert Parcel engines to Babel targets
const env = {
engines: {
node: '>=16.0.0',
browsers: ['> 1%', 'last 2 versions']
},
outputFormat: 'cjs',
isBrowser: () => false
};
const targets = enginesToBabelTargets(env);
// Result: { node: '16.0.0', browsers: ['> 1%', 'last 2 versions'] }
// ES module output with browser targets
const esmEnv = {
engines: { browsers: '> 1%' },
outputFormat: 'esmodule',
isBrowser: () => true
};
const esmTargets = enginesToBabelTargets(esmEnv);
// Result: { browsers: ['> 1%', 'not ie <= 11', 'not edge < 16', ...] }Determines if a file likely contains JSX syntax based on file extension and project dependencies.
/**
* Detects if an asset likely contains JSX syntax
* Checks file extensions and React-like dependencies in package.json
* @param options - Plugin options for dependency resolution
* @param config - Parcel configuration object
* @returns Promise resolving to true if JSX is likely present
* @internal - Used internally by the configuration system
*/
async function isJSX(
options: PluginOptions,
config: Config
): Promise<boolean>;JSX Detection Logic:
.jsx and .tsx filesSupported Extensions:
const JSX_EXTENSIONS = new Set(['.jsx', '.tsx']);Detected Libraries:
const JSX_LIBRARIES = ['react', 'preact', 'nervejs', 'hyperapp'];Usage Examples:
// Check if file contains JSX
const hasJSX = await isJSX(pluginOptions, parcelConfig);
// File extension detection
// Button.jsx -> true
// Component.tsx -> true
// utils.js -> depends on dependencies
// Dependency detection
// package.json with "react": "^18.0.0" -> true for .js files
// package.json with "preact": "^10.0.0" -> true for .js files
// No React-like deps -> false for .js files
// Alias detection
// package.json: { "alias": { "react": "preact/compat" } } -> trueGenerates Babel configuration for stripping Flow type annotations when Flow is detected in the project.
/**
* Generates Babel configuration for Flow type stripping
* Only applies when flow-bin is detected as a project dependency
* @param config - Parcel configuration object
* @param options - Plugin options for dependency resolution
* @returns Promise resolving to BabelConfig for Flow or null if not needed
* @internal - Used internally by the configuration system
*/
async function getFlowOptions(
config: Config,
options: PluginOptions
): Promise<BabelConfig | null>;
interface BabelConfig {
plugins?: Array<any>;
presets?: Array<any>;
}Flow Detection Process:
flow-bin in dependencies or devDependenciesUsage Examples:
// Generate Flow configuration
const flowConfig = await getFlowOptions(parcelConfig, pluginOptions);
// With flow-bin dependency:
// {
// plugins: [
// ['@babel/plugin-transform-flow-strip-types', { requireDirective: true }]
// ]
// }
// Without flow-bin dependency:
// null
// Example Flow code transformation:
// Input: function add(a: number, b: number): number { return a + b; }
// Output: function add(a, b) { return a + b; }Remaps AST node locations using source maps for accurate debugging information.
/**
* Remaps AST node locations using source map for accurate sourcemap generation
* Improves sourcemap accuracy and fixes sourcemaps when scope-hoisting
* @param t - Babel types utility object
* @param ast - Babel AST file node
* @param map - Source map for location mapping
* @internal - Used internally by the Babel processing engine
*/
function remapAstLocations(
t: BabelTypes,
ast: BabelNodeFile,
map: SourceMap
): void;
interface BabelNodeFile {
program: any; // AST program node
}
interface SourceMap {
findClosestMapping(line: number, column: number): Mapping | null;
}
interface Mapping {
original: {
line: number; // Original source line
column: number; // Original source column
};
source: string; // Original source file name
}Remapping Process:
Usage Examples:
// Remap AST locations after transformation
const sourceMap = await asset.getMap();
if (sourceMap && transformedAST) {
remapAstLocations(babelTypes, transformedAST, sourceMap);
}
// Before remapping (compiled locations):
// Node at line 10, column 5 in compiled output
// After remapping (original locations):
// Node at line 8, column 2 in original source
// Filename: 'src/components/Button.jsx'Helper function for traversing all nodes in an AST tree.
/**
* Recursively traverses all nodes in an AST tree
* Used internally by remapAstLocations for complete tree processing
* @param t - Babel types utility object
* @param node - Current AST node to traverse
* @param visitor - Visitor function called for each node
*/
function traverseAll(
t: BabelTypes,
node: Node,
visitor: (node: Node) => void
): void;Traversal Features:
const BABEL_CORE_RANGE = '^7.12.0'; // Required @babel/core versionconst TYPESCRIPT_EXTNAME_RE = /\.tsx?$/; // TypeScript file detection
const JS_EXTNAME_RE = /^\.(js|cjs|mjs)$/; // JavaScript file detectionAll utility functions include appropriate error handling:
Install with Tessl CLI
npx tessl i tessl/npm-parcel--transformer-babel