How can we compile modern JavaScript into backwards-compatible JavaScript bundles that can be used with Internet Explorer 11 (ie11)? Specifically, how can we do this with the latest versions of Webpack 5 and Babel 7?
Asked
Active
Viewed 4,478 times
5
-
I created this self-answered question after spending days trying to get this working. Hope it helps others avoid the pain too. – anthumchris Nov 27 '20 at 10:13
1 Answers
12
Here's the most minimal configuration I could create, with a test file that's included to test with IE 11. Download on GitHub.
package.json
{
"browserslist": [
"ie 11"
],
"scripts": {
"dev": "webpack -w",
"build": "webpack"
},
"devDependencies": {
"@babel/core": "^7.12.9",
"@babel/preset-env": "^7.12.7",
"babel-loader": "^8.2.2",
"core-js": "^3.8.0",
"webpack": "^5.8.0",
"webpack-cli": "^4.2.0"
}
}
webpack.config.js
module.exports = {
entry: './index.js',
module: {
rules: [{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
['@babel/preset-env', {
useBuiltIns: 'usage',
corejs: 3
}]
]
}
}
}]
}
}
Greeting.js
export default '2020 has been one hell of a year!'
index.js
/*
* Test that uses modern JavaScript and will be compiled to work in IE 11
*/
import greeting from './Greeting'
window.addEventListener('load', async () => {
const o = {
greeting: await Promise.resolve(greeting)
}
console.log(
o,
Object.entries(o),
Object.keys(o),
Object.values(o),
)
})

anthumchris
- 8,245
- 2
- 28
- 53
-
1Thanks for posting the solution to this issue. I suggest you try to mark your own answer to this question. It will surely help other community members in the future in similar kinds of issues. Thanks for your understanding. – Deepak-MSFT Nov 30 '20 at 02:12
-
Configuration is missing "arrowFunction: false" https://webpack.js.org/configuration/output/#outputenvironment to make it work with IE11 and webpack 5 – Thomas May 21 '21 at 11:11
-
-
@AnthumChris not the webpack arrows. Open the output file with your exact code exemple, you will find arrows without `"arrowFunction: false"` – Thomas May 25 '21 at 14:09
-
I'm not seeing arrow functions in the generated bundle. Kindly download the Gist above and run the following: `npm install && npm run build && grep '=>' dist/main.js || echo '✅ No arrow functions exist.'` – anthumchris May 25 '21 at 14:47