Gathering detailed insights and metrics for babel-plugin-replace-import-extension
Gathering detailed insights and metrics for babel-plugin-replace-import-extension
Gathering detailed insights and metrics for babel-plugin-replace-import-extension
Gathering detailed insights and metrics for babel-plugin-replace-import-extension
@aztlan/babel-plugin-replace-import-extension
Babel plugin to replace extension of file name written in import statement and dynamic import
babel-plugin-reshow-import-extension
TODO: description
babel-plugin-replace-import-extensions
A babel plugin to replace import extensions
@mo36924/babel-plugin-replace-import-extensions
babel-plugin-replace-import-extensions
Babel plugin to replace extension of file name written in import statement
npm install babel-plugin-replace-import-extension
Typescript
Module System
Node Version
NPM Version
78.1
Supply Chain
99.5
Quality
76.5
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
Apache-2.0 License
17 Stars
35 Commits
4 Forks
2 Branches
3 Contributors
Updated on Feb 28, 2025
Latest Version
1.1.5
Package Id
babel-plugin-replace-import-extension@1.1.5
Unpacked Size
24.76 kB
Size
7.44 kB
File Count
5
NPM Version
10.8.2
Node Version
20.18.3
Published on
Feb 28, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
Babel plugin to replace extension of file name written in import statement and dynamic import.
1npm install --save-dev babel-plugin-replace-import-extension
With the option:
1{ "extMapping": { ".js": ".mjs" }}
1import { foo } from './module1.js'; 2export { bar } from './module2.js'; // Works for re-exporting 3const promise = import('./module3' + '.js'); // Also works for dynamic import!
1import { foo } from './module1.mjs'; 2export { bar } from './module2.mjs'; 3 4// In dynamic import, function to replace extension is inserted. 5// Note the actual code is not exactly the same. 6const promise = import(__transformExtension('./module3' + '.js'));
When you develop a npm package that includes both ESModule and CommonJS version of the code, there is two ways to tell Node which file is which version.
mjs
for ESModule and cjs
for
CommonJS.package.json
with a type
field specified to the directory.If you choose the former and you write your code in ESModule and transpile it to CommonJS, you have to change the extension of the files while transpiling.
In Babel CLI, extension of the output file name can be changed with
--out-file-extension
option. But the file name referenced inside the code
is not changed. In this case, this plugin comes into play.
Note that the conversion is performed only on relative file name
(starts with ./
or ../
), because built-in packages or packages importing
from node_modules
should not be converted.
If project root package.json
has type
field of module
, Babel config of
1{ 2 "plugins": [ 3 ["replace-import-extension", { "extMapping": { ".js": ".cjs" }}], 4 ["@babel/transform-modules-commonjs"] 5 ] 6}
will convert the file extension from .js
to .cjs
and convert ESModule to
CommonJS, allowing both version's code exist together while Node can handle
each versions correctly. (@babel/plugin-transform-modules-commonjs
must be
installed.) Or if you also need other translations, @babel/env
preset can be
used together like,
1{ 2 "presets": [["@babel/env"]], 3 "plugins": [ 4 ["replace-import-extension", { "extMapping": { ".js": ".cjs" }}] 5 ] 6}
If project root package.json
has no type
field or has type
field of
cjs
, ESModule files must be explicitly marked by mjs
extension, which can
be done by Babel config of
1{ 2 "plugins": [ 3 ["replace-import-extension", { "extMapping": { ".js": ".mjs" }}] 4 ] 5}
Once again, --out-file-extension
option must be used together to change the
output file extension.
mjs
and cjs
in the same packageIf you are using .mjs
for your source files, you can use babel to generate .cjs
files for backwards compatibility:
1{ 2 "presets": [["@babel/env"]], 3 "plugins": [ 4 ["replace-import-extension", { "extMapping": { ".mjs": ".cjs" }}] 5 ] 6}
In your package.json
specify the entries accordingly:
1{ 2 "main": "dist/index.cjs", 3 "module": "src/index.mjs", 4 "source": "src/index.mjs", 5 "exports": { 6 ".": { 7 "require": "dist/index.cjs", 8 "import": "src/index.mjs" 9 }, 10 "src/index.mjs": { 11 "import": "src/index.mjs" 12 }, 13 "dist/index.cjs": { 14 "require": "dist/index.cjs", 15 "import": "dist/index.cjs" 16 } 17 }, 18 "scripts": { 19 "build.cjs": "babel -d dist/ src/ --out-file-extension .cjs", 20 "prepare": "npm run build.cjs" 21 } 22}
extMapping
Object
, defaults to {}
.
Mapping of original extension to converted extension.
Leading .
is mandatory.
Both the original and the converted extensions can be empty string ''
, which means
no extension. You can use this feature to add or remove extension.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
1 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
3 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 2
Reason
Found 2/26 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-05-05
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More