File tree Expand file tree Collapse file tree 2 files changed +45
-1
lines changed
Expand file tree Collapse file tree 2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change 1919 "presets" : [[" @babel/preset-env" , { "modules" : " commonjs" }]]
2020 },
2121 "mjs" : {
22- "presets" : [[" @babel/preset-env" , { "modules" : false }]]
22+ "presets" : [[" @babel/preset-env" , { "modules" : false }]],
23+ "plugins" : ['./resources/add-extension-to-import-paths' ]
2324 }
2425 }
2526 },
Original file line number Diff line number Diff line change 1+ // @noflow
2+
3+ 'use strict' ;
4+
5+ /**
6+ * Adds extension to all paths imported inside MJS files
7+ *
8+ * Transforms:
9+ *
10+ * import { foo } from './bar';
11+ * export { foo } from './bar';
12+ *
13+ * to:
14+ *
15+ * import { foo } from './bar.mjs';
16+ * export { foo } from './bar.mjs';
17+ *
18+ */
19+ module . exports = function addExtensionToImportPaths ( context ) {
20+ const { types } = context ;
21+
22+ return {
23+ visitor : {
24+ ImportDeclaration : replaceImportPath ,
25+ ExportNamedDeclaration : replaceImportPath ,
26+ } ,
27+ } ;
28+
29+ function replaceImportPath ( path ) {
30+ // bail if the declaration doesn't have a source, e.g. "export { foo };"
31+ if ( ! path . node . source ) {
32+ return ;
33+ }
34+
35+ const source = path . node . source . value ;
36+ if ( source . startsWith ( './' ) || source . startsWith ( '../' ) ) {
37+ if ( ! source . endsWith ( '.mjs' ) ) {
38+ const newSourceNode = types . stringLiteral ( source + '.mjs' ) ;
39+ path . get ( 'source' ) . replaceWith ( newSourceNode ) ;
40+ }
41+ }
42+ }
43+ } ;
You can’t perform that action at this time.
0 commit comments