I want to compile my code to ES6 not ES5. Here is my babelrc.
{
"presets": [
[
"env",
{
"modules": false,
"useBuiltIns": true,
"targets": {
"browsers": ["Chrome >= 60"]
}
}
],
["react"],
["stage-2"]
]}
And with babel-cli, the right ES6 code can be compiled. For example
But when I use webpack, babel-loader in the same babel config, my ES6 code was compiled to ES5.
So how can i compile ES6+ code to ES6+ with Webpack?
Does webpack compile ES6+ code to ES5 ?
Create your scripts in package. json : "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build": "webpack", "start": "webpack-dev-server --open" }, Run npm run build and npm start .
We can now use webpack to bundle our modules and transpile ES6 code down to ES5 with Babel. However, it's a bit of a niggle that, to transpile our ES6 code, we have to run npm run build every time we make a change.
For most browsers, yes, you can accomplish getting all needed code to the browser with just ES6 modules, without Webpack.
There's option target option esmodules
. check it out here.
{
"presets": [
[
"@babel/preset-env",
{
"modules": false,
"useBuiltIns": true,
"targets": {
"browsers": ["Chrome >= 60"],
"esmodules": true
}
}
],
["@babel/preset-react"]
]}
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