File tree 4 files changed +25
-9
lines changed
4 files changed +25
-9
lines changed Original file line number Diff line number Diff line change @@ -72,6 +72,7 @@ export default function commonjs(options = {}) {
72
72
73
73
const esModulesWithDefaultExport = new Set ( ) ;
74
74
const esModulesWithNamedExports = new Set ( ) ;
75
+ const isCjsPromises = new Map ( ) ;
75
76
76
77
const ignoreRequire =
77
78
typeof options . ignore === 'function'
@@ -208,7 +209,8 @@ export default function commonjs(options = {}) {
208
209
actualId ,
209
210
getRequireReturnsDefault ( actualId ) ,
210
211
esModulesWithDefaultExport ,
211
- esModulesWithNamedExports
212
+ esModulesWithNamedExports ,
213
+ isCjsPromises
212
214
) ;
213
215
}
214
216
@@ -244,11 +246,11 @@ export default function commonjs(options = {}) {
244
246
if ( commonjs ) {
245
247
const isCjs = commonjs . isCommonJS ;
246
248
if ( isCjs != null ) {
247
- setIsCjsPromise ( id , isCjs ) ;
249
+ setIsCjsPromise ( isCjsPromises , id , isCjs ) ;
248
250
return ;
249
251
}
250
252
}
251
- setIsCjsPromise ( id , null ) ;
253
+ setIsCjsPromise ( isCjsPromises , id , null ) ;
252
254
}
253
255
} ;
254
256
}
Original file line number Diff line number Diff line change 1
- const isCjsPromises = new Map ( ) ;
2
-
3
- export function getIsCjsPromise ( id ) {
1
+ export function getIsCjsPromise ( isCjsPromises , id ) {
4
2
let isCjsPromise = isCjsPromises . get ( id ) ;
5
3
if ( isCjsPromise ) return isCjsPromise . promise ;
6
4
@@ -16,7 +14,7 @@ export function getIsCjsPromise(id) {
16
14
return promise ;
17
15
}
18
16
19
- export function setIsCjsPromise ( id , resolution ) {
17
+ export function setIsCjsPromise ( isCjsPromises , id , resolution ) {
20
18
const isCjsPromise = isCjsPromises . get ( id ) ;
21
19
if ( isCjsPromise ) {
22
20
if ( isCjsPromise . resolve ) {
Original file line number Diff line number Diff line change @@ -46,10 +46,11 @@ export async function getStaticRequireProxy(
46
46
id ,
47
47
requireReturnsDefault ,
48
48
esModulesWithDefaultExport ,
49
- esModulesWithNamedExports
49
+ esModulesWithNamedExports ,
50
+ isCjsPromises
50
51
) {
51
52
const name = getName ( id ) ;
52
- const isCjs = await getIsCjsPromise ( id ) ;
53
+ const isCjs = await getIsCjsPromise ( isCjsPromises , id ) ;
53
54
if ( isCjs ) {
54
55
return `import { __moduleExports } from ${ JSON . stringify ( id ) } ; export default __moduleExports;` ;
55
56
} else if ( isCjs === null ) {
Original file line number Diff line number Diff line change @@ -759,3 +759,18 @@ if (Number(/^v(\d+)/.exec(process.version)[1]) >= 12) {
759
759
t . is ( code , await new Promise ( ( done ) => getRollupUpCodeWithCache . on ( 'message' , done ) ) ) ;
760
760
} ) ;
761
761
}
762
+
763
+ test ( 'does not affect subsequently created instances when called with `requireReturnsDefault: "preferred"`' , async ( t ) => {
764
+ const input = 'fixtures/function/import-esm-require-returns-default-preferred/main.js' ;
765
+ const options = { requireReturnsDefault : 'preferred' } ;
766
+
767
+ const instance1 = commonjs ( options ) ;
768
+ const bundle1 = await rollup ( { input, plugins : [ instance1 ] } ) ;
769
+ const code1 = ( await bundle1 . generate ( { } ) ) . output [ 0 ] . code ;
770
+
771
+ const instance2 = commonjs ( options ) ;
772
+ const bundle2 = await rollup ( { input, plugins : [ instance2 ] } ) ;
773
+ const code2 = ( await bundle2 . generate ( { } ) ) . output [ 0 ] . code ;
774
+
775
+ t . is ( code1 , code2 ) ;
776
+ } ) ;
You can’t perform that action at this time.
0 commit comments