webpack5 supports topLevelAwait, just add the following options:
//webpack.config.js
module.exports = {
//...
experiments: {
topLevelAwait: true
},
};
Now we can use top level await happily, like this:
import Test from './Test';
const _Test = await import("./Test");
console.log(_Test);
It works well.
But when I add babel-loader it will not work:
module.exports = {
//...
experiments: {
topLevelAwait: true
},
module:{
rules:[
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
}
]
},
};
It throws an error:
'await' is only allowed within async functions
How can I solve this problem?
I need topLevelAwait and babel-loader.