Installations
npm install @aztlan/babel-plugin-replace-import-extension
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
20.12.2
NPM Version
lerna/8.1.2/node@v20.12.2+x64 (linux)
Score
70.5
Supply Chain
98.8
Quality
86.3
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
silane
Download Statistics
Total Downloads
4,398
Last Day
3
Last Week
25
Last Month
95
Last Year
3,754
GitHub Statistics
17 Stars
32 Commits
4 Forks
1 Watching
2 Branches
3 Contributors
Bundle Size
1.23 kB
Minified
662.00 B
Minified + Gzipped
Package Meta Information
Latest Version
3.6.3
Package Id
@aztlan/babel-plugin-replace-import-extension@3.6.3
Unpacked Size
23.21 kB
Size
7.24 kB
File Count
5
NPM Version
lerna/8.1.2/node@v20.12.2+x64 (linux)
Node Version
20.12.2
Publised On
25 Apr 2024
Total Downloads
Cumulative downloads
Total Downloads
4,398
Last day
200%
3
Compared to previous day
Last week
-35.9%
25
Compared to previous week
Last month
-40.6%
95
Compared to previous month
Last year
482.9%
3,754
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
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.
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 More