File tree Expand file tree Collapse file tree 3 files changed +19
-1
lines changed Expand file tree Collapse file tree 3 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -35,14 +35,21 @@ module.exports = function (rootDir) {
35
35
}
36
36
37
37
const sourceJsPath = findSource ( rootDir , source )
38
+ const sourceRaw = fs . readFileSync ( sourceJsPath ) . toString ( )
39
+ // eslint-disable-next-line unicorn/prefer-starts-ends-with
40
+ const maybeIsEsm = sourceRaw . match ( / ^ e x p o r t / m)
38
41
const sourceDTsPath = sourceJsPath . replace ( / \. j s $ / , '.d.ts' )
39
42
const destJsPath = path . join ( rootDir , alias + '.js' )
40
43
const destDTsPath = path . join ( rootDir , alias + '.d.ts' )
41
44
if ( fs . existsSync ( sourceDTsPath ) ) {
42
45
fs . writeFileSync ( destDTsPath , `export * from '${ source } '\n` , 'utf8' )
43
46
}
44
47
45
- return fs . writeFileAsync ( destJsPath , `module.exports = require("${ source } ")\n` , 'utf8' )
48
+ const contents = maybeIsEsm ?
49
+ `export * from "${ source } "\n` :
50
+ `module.exports = require("${ source } ")\n`
51
+
52
+ return fs . writeFileAsync ( destJsPath , contents , 'utf8' )
46
53
} ,
47
54
}
48
55
}
Original file line number Diff line number Diff line change @@ -52,6 +52,16 @@ describe('lib/aliases.js', () => {
52
52
} )
53
53
} )
54
54
55
+ it ( 'should create an esm file when source looks like esm' , ( ) => {
56
+ return aliasUtils ( app1Dir ) . createAlias ( 'lib/fileB.esm.js' , 'alias' ) . then ( ( ) => {
57
+ return Promise . try ( ( ) => {
58
+ const contents = fs . readFileSync ( app1Dir + '/alias.js' , 'utf8' )
59
+ contents . should . match ( / ^ e x p o r t \* f r o m / )
60
+ fs . unlinkSync ( app1Dir + '/alias.js' )
61
+ } )
62
+ } )
63
+ } )
64
+
55
65
it ( 'should point the alias to the source file' , ( ) => {
56
66
return aliasUtils ( app3Dir ) . createAlias ( './file.js' , 'alias' ) . then ( ( ) => {
57
67
return Promise . try ( ( ) => {
Original file line number Diff line number Diff line change
1
+ export function helloWorld ( ) { } ;
You can’t perform that action at this time.
0 commit comments