I'm working on a browser-based project that needs to be IE11-compatible (sigh). Webpack is choking on async/await
. Here is my console's output:
Based on your code and targets, added regenerator-runtime.
...
Module not found: Error: Can't resolve 'regenerator-runtime/runtime'
I've looked at many SO questions similar to mine, without luck. Many recommend using @babel/polyfill
which I am avoiding since it has been deprecated.
What is causing this issue? I expect it could be fixed by manually importing regenerator-runtime/runtime
, but it seems one of the main selling points of babel-env
is NOT having to manually import polyfills, so I assume I'm missing a step. Thank you!
Here is what I am attempting to run, which is being imported into another file:
class Fetcher {
constructor() {
this.form = document.querySelector('form');
this.form.addEventListener('submit', this.onSubmit.bind(this));
}
async onSubmit(event) {
event.preventDefault();
const apiResponse = await fetch(`${WP_url}/api`);
const apiJson = await apiResponse.json();
console.log(apiJson);
}
}
export default Fetcher;
webpack.config.js:
const path = require('path');
function pathTo(filepath) {
return path.join(__dirname, filepath);
}
module.exports = function(env, argv) {
const isProd = Boolean(argv.mode === 'production');
const config = {
entry: {
index: [
pathTo('index.js'),
],
},
externals: {
jquery: 'jQuery',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
corejs: 3,
debug: true,
targets: {
browsers: [
'IE 11',
],
},
useBuiltIns: 'usage',
},
],
],
},
},
],
},
optimization: {
minimize: isProd,
},
output: {
filename: '[name].js',
path: pathTo('web'),
},
};
return config;
};
package.json
{
"private": true,
"dependencies": {
"core-js": "^3.4.1",
"focus-within-polyfill": "^5.0.5"
},
"devDependencies": {
"@babel/core": "^7.7.2",
"@babel/preset-env": "^7.7.1",
"babel-eslint": "^10.0.3",
"babel-loader": "^8.0.6",
"css-loader": "^3.2.0",
"eslint": "^6.6.0",
"mini-css-extract-plugin": "^0.8.0",
"node-sass": "^4.13.0",
"sass-loader": "^8.0.0",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10"
},
"scripts": {
"dev": "webpack --mode=development --display-modules",
"dev:watch": "npm run dev -- --watch",
"prod": "webpack --mode=production --display-modules",
"prod:watch": "npm run prod -- --watch"
}
}
Simply running npm i regenerator-runtime
fixed it, actually.
With useBuiltIns: 'usage'
, having all the import
statements wasn't necessary I guess.
¯\_(ツ)_/¯
I started getting this error when added transpileDependencies: ['openplayerjs'],
to my vue.config.js
, as Babel with @vue/cli-plugin-babel/preset
won't touch dependencies in node_modules
, go figure why. Installing regenerator-runtime
did not help, and I thought the issue lied in Yarn's pnp modules:
This dependency was not found:
* regenerator-runtime/runtime.js in /Volumes/Backup/home/Documents/.yarn/unplugged/openplayerjs-npm-2.9.3-aa4692035d/node_modules/openplayerjs/dist/esm/media.js, /Volumes/Backup/home/Documents/.yarn/unplugged/openplayerjs-npm-2.9.3-aa4692035d/node_modules/openplayerjs/dist/esm/media/ads.js and 1 other
To install it, you can run: npm install --save regenerator-runtime/runtime.js
so I tried unplugging everything via yarn unplug <module_name>
, still did not work.
Eventually changing useBuiltIns: 'usage'
to useBuiltIns: 'entry'
in babel.config.js
solved it straight away.
Just add import 'regenerator-runtime/runtime'
in the file where you have the async/await.
In my case, your answer wasn't enough and I needed to set babel sourceType to unambiguous
as suggested here to allow a correct compilation of the project. This option was required because the @babel/runtime/regenerator/index.js
file exports its reference using module.exports = require("regenerator-runtime");
that breaks ES6 compilation.
Another useful note resolving a similar but unrelated compilation issue was to use /node_modules\/(css-loader|@babel|core-js|promise-polyfill|webpack|html-webpack-plugin|whatwg-fetch)\//
as exclude
for the babel rule to avoid resolution loops without loosing external libraries compilation (when necessary) as suggested here.
In your specific case the babel rule would become:
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
sourceType: 'unambiguous',
presets: [
['babel/preset-env', {
corejs: 3,
debug: true,
targets: {
browsers: [ 'IE 11', ],
},
useBuiltIns: 'usage',
}],
],
},
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With