A Babel plugin that replaces file extensions in import statements and dynamic imports during transpilation. Current stable version is 1.1.5, with irregular release cadence. It solves the problem of co-existing ESModule and CommonJS builds by rewriting relative imports (starting with ./ or ../) so that, for example, .js becomes .mjs or .cjs. Unlike other extension replacement approaches, it handles dynamic imports by injecting a __transformExtension helper and avoids changes to bare specifiers (node_modules). It works with @babel/transform-modules-commonjs and is configured via an extMapping option.
npm install babel-plugin-replace-import-extensionVerified import paths — ran on the pinned version, not inferred.
Shows basic usage: install plugin, configure extMapping in Babel config, and expected transformation of static and dynamic imports.
Ensure all local file imports use relative paths if you expect extension replacement.
Update to v1.1.4+ which handles constant dynamic imports. For fully dynamic, consider runtime helper.
Update to v1.1.3+ to avoid potential infinite loops.
Avoid using __transformExtension in your codebase, or check if plugin version allows renaming (not available by default).
Test with your Babel version; if issues, consider alternatives like babel-plugin-transform-import-extension or manual string replacement.
Run 'npm install --save-dev babel-plugin-replace-import-extension' and use plugin name 'replace-import-extension' (without the 'babel-plugin-' prefix) in .babelrc.
Ensure config is an array with options object: ['replace-import-extension', { extMapping: { '.js': '.mjs' } }].Update to babel-plugin-replace-import-extension v1.1.5+ and ensure Babel 7 is installed.
Only string literals or concatenation of literals are supported. For variable expressions, consider a runtime helper.