babel-plugin-transform-commonjs icon indicating copy to clipboard operation
babel-plugin-transform-commonjs copied to clipboard

Does this plugin will benefit from proposal top-level-await?

Open shrinktofit opened this issue 4 years ago • 1 comments

https://github.com/tc39/proposal-top-level-await

Once if babel implements this proposal. This plugin can replace require to await import to support require non-string-literal module ids?

shrinktofit avatar Nov 04 '19 03:11 shrinktofit

Hi @shrinktofit I believe it does. We should remove the synchronousImport option and instead opt to directly transpiling to a top-level await. This also has the potential to solve "top-level" conditional imports such as:

if (process.env.NODE_ENV === 'production') {
  module.exports = require('react/prod');
}
else {
  module.exports = require('react/test');
}

...converting to...

let _a;

if (process.env.NODE_ENV === 'production') {
  _a = await import('react/prod');
}
else {
  _a = await import('react/test');
}

export default _a;

tbranyen avatar Jan 14 '20 22:01 tbranyen