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
Enhanced error reporting and diagnostics for Babel compilation errors with source context and improved developer experience.
Enhances Babel compilation errors with additional context and source information for better debugging experience.
/**
* Enhances Babel errors with source context and file information
* Improves error messages by removing redundant path information and adding source code
* @param error - The original Babel error
* @param asset - The asset being processed when the error occurred
* @returns Promise resolving to enhanced error with additional context
*/
export async function babelErrorEnhancer(
error: BabelError,
asset: BaseAsset
): Promise<BabelError>;
interface BabelError extends Error {
loc?: {
line: number; // Line number where error occurred
column: number; // Column number where error occurred
};
source?: string; // Source code content (added by enhancer)
filePath?: string; // File path where error occurred (added by enhancer)
}The enhancer improves error message readability by removing redundant information:
// Error message processing logic
if (error.loc) {
// Remove file path prefix if present to avoid duplication
let start = error.message.startsWith(asset.filePath)
? asset.filePath.length + 1 // Skip path and separator
: 0;
// Extract clean error message (first line only)
error.message = error.message.slice(start).split('\n')[0].trim();
}Before Enhancement:
/path/to/project/src/components/Button.jsx: Unexpected token, expected "}" (15:8)After Enhancement:
Unexpected token, expected "}"Adds complete source code and file path information for enhanced debugging:
// Source context enhancement
error.source = await asset.getCode(); // Full source code content
error.filePath = asset.filePath; // Absolute file pathThis allows error reporting tools and IDEs to:
Common JavaScript/TypeScript/JSX syntax errors enhanced with source context:
// Example: Missing closing brace
const Component = () => {
return <div>Hello World</div>
// Missing closing brace causes Babel errorEnhanced Error Information:
{ line: 3, column: 1 }Errors from Babel plugins during transformation:
// Example: TypeScript transformation error
interface Props {
invalid syntax here
}Enhanced Error Information:
Errors related to Babel configuration or plugin setup:
// Example: Invalid preset configuration
{
"presets": [
["@babel/preset-env", { invalidOption: true }]
]
}Enhanced Error Information:
The enhanced errors integrate seamlessly with Parcel's diagnostic system:
// Usage in transformer
try {
await babel7({
asset,
options,
logger,
babelOptions: config,
tracer
});
} catch (error) {
// Enhance error before throwing
throw await babelErrorEnhancer(error, asset);
}// Original Babel error
{
name: 'SyntaxError',
message: '/Users/dev/project/src/Button.jsx: Unexpected token (15:8)\n\n 13 | return (\n 14 | <div className="button"\n> 15 | onClick={handleClick\n | ^\n 16 | >\n 17 | {children}\n 18 | </div>',
loc: { line: 15, column: 8 }
}// Enhanced error with context
{
name: 'SyntaxError',
message: 'Unexpected token',
loc: { line: 15, column: 8 },
source: 'import React from "react";\n\nconst Button = ({ onClick, children }) => {\n const handleClick = () => {\n onClick?.();\n };\n\n return (\n <div className="button"\n onClick={handleClick\n >\n {children}\n </div>\n );\n};\n\nexport default Button;',
filePath: '/Users/dev/project/src/Button.jsx'
}Usage Example:
// Internal error enhancement flow
try {
// Babel transformation attempt
const result = await babelCore.transformAsync(sourceCode, config);
} catch (originalError) {
// Enhance error with source context
const enhancedError = await babelErrorEnhancer(originalError, asset);
// Enhanced error now contains:
console.log(enhancedError.message); // Clean error message
console.log(enhancedError.loc); // { line: 15, column: 8 }
console.log(enhancedError.source); // Complete source code
console.log(enhancedError.filePath); // Absolute file path
// Throw enhanced error for Parcel to handle
throw enhancedError;
}Install with Tessl CLI
npx tessl i tessl/npm-parcel--transformer-babel