Installations
npm install babel-plugin-replace-import-extension
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
18.18.0
NPM Version
10.1.0
Score
82
Supply Chain
99.5
Quality
75.5
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
silane
Download Statistics
Total Downloads
463,858
Last Day
897
Last Week
6,412
Last Month
19,557
Last Year
211,353
GitHub Statistics
17 Stars
32 Commits
4 Forks
1 Watching
2 Branches
3 Contributors
Bundle Size
1.45 kB
Minified
772.00 B
Minified + Gzipped
Package Meta Information
Latest Version
1.1.4
Package Id
babel-plugin-replace-import-extension@1.1.4
Unpacked Size
24.76 kB
Size
7.44 kB
File Count
5
NPM Version
10.1.0
Node Version
18.18.0
Publised On
06 Jan 2024
Total Downloads
Cumulative downloads
Total Downloads
463,858
Last day
-18%
897
Compared to previous day
Last week
43.7%
6,412
Compared to previous week
Last month
-17.6%
19,557
Compared to previous month
Last year
25.4%
211,353
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
3
babel-plugin-replace-import-extension
Babel plugin to replace extension of file name written in import statement and dynamic import.
Installation
1npm install --save-dev babel-plugin-replace-import-extension
Example
With the option:
1{ "extMapping": { ".js": ".mjs" }}
In
1import { foo } from './module1.js'; 2export { bar } from './module2.js'; // Works for re-exporting 3const promise = import('./module3' + '.js'); // Also works for dynamic import!
Out
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'));
Why We Need This Plugin?
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.
- Distinguish files by their extension,
mjs
for ESModule andcjs
for CommonJS. - If two versions are located in separate directories, put a
package.json
with atype
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.
Usage
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.
Supporting both mjs
and cjs
in the same package
If 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}
Options
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
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: Apache License 2.0: LICENSE:0
Reason
3 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
Reason
Found 2/26 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 6 are checked with a SAST tool
Score
2.7
/10
Last Scanned on 2025-01-06
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 MoreOther packages similar to 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