reactiverse / es4x

🚀 fast JavaScript 4 Eclipse Vert.x
https://reactiverse.io/es4x/
Apache License 2.0
884 stars 75 forks source link

PR: implement correct loaders for cjs mjs and js with script content detection. as also package.json support #382

Closed frank-dspeed closed 4 years ago

frank-dspeed commented 4 years ago

es4x dynamic import broken interop

ESM & CJS can not dynamic import additional CJS modules inside it also accessing module object inside cjs breaks graalvm silent when loaded via dynamic import inside ESM

JS can also not dynamic import CJS same behavior!

Reproduce able example

module.js

module.exports = { str: 'myString'}
// When the following line gets uncommented the secund console log will not get called silent fails when this file gets imported via dynamic import() no matter if from CJS or ESM
//console.log('ctx',module)
console.log('The Import works correct as this sideEffect gets executed')

index.mjs

//then does not even get called sideEffects do execute
import('./module.js').then(function(x) { console.log('t',x)}) // then does not get called { default: { str: 'myString'}}
export const esm = 'Works' 

index.js

// Will not work
//then does not even get called sideEffects do execute
import('./module.js').then(function(x) { console.log('t',x)}) // then does not get called { default: { str: 'myString'}}

//This will work 
console.log(require('module.js'))
import('./index.esm.js').then(function ({esm}) { console.log(esm) })

Conclusion

dynamicImport import() at present only supports ESM no matter if used in ESM context or CJS i think also that the current dynamic import implementation does not hornor .mjs === ESM and .cjs === commonjs(require,module)

frank-dspeed commented 4 years ago

https://github.com/reactiverse/es4x/issues/390 is maybe related as it turns out that the files could get converted by a custom adapt function.