Babel plugin that compiles ES2015 default and rest parameters to ES5-compatible code
91
Build a Babel plugin that transforms import paths in JavaScript modules based on configurable pattern mappings.
Create a Babel plugin that rewrites import declarations to match path aliases. The plugin should:
The plugin accepts a configuration object with path mappings:
{
"pathMappings": {
"@utils/*": "./src/utilities/*",
"@components/*": "./src/components/*"
}
}When the plugin encounters an import matching a pattern (e.g., @utils/logger), it transforms it to the mapped path (e.g., ./src/utilities/logger). The * wildcard matches any path segment.
The plugin must preserve all aspects of the import:
The plugin should validate that the configuration object is properly structured. If the configuration is missing or invalid, the plugin should skip transformation gracefully without throwing errors.
Given an import statement import { log } from '@utils/logger' and a mapping "@utils/*": "./src/utilities/*", the plugin transforms it to import { log } from './src/utilities/logger' @test
Given an import statement import Button from '@components/Button' and a mapping "@components/*": "./src/components/*", the plugin transforms it to import Button from './src/components/Button' @test
Given an import statement import * as utils from '@utils/helpers' and a mapping "@utils/*": "./src/utilities/*", the plugin transforms it to import * as utils from './src/utilities/helpers' @test
Given an import statement import '@utils/polyfill' and a mapping "@utils/*": "./src/utilities/*", the plugin transforms it to import './src/utilities/polyfill' @test
Given multiple import statements with different patterns, the plugin transforms only those matching configured mappings and leaves others unchanged @test
Given a plugin configuration without pathMappings or with an empty object, the plugin processes the code without transformations and without errors @test
@generates
/**
* Babel plugin that transforms import paths based on configured mappings.
*
* @returns {Object} Babel plugin object with visitor methods
*/
export default function importPathTransformer() {
return {
name: "import-path-transformer",
visitor: {
// Plugin implementation
}
};
}Provides the core Babel transformation API and plugin system infrastructure.
Provides utilities for declaring Babel plugins with proper configuration handling.
Provides builders and validators for AST node manipulation.
Install with Tessl CLI
npx tessl i tessl/npm-babel--plugin-transform-parametersdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10