Webpack's support for es6

webpack configuration function does not configure babel-loader for es6 code conversion, nor does it configure .babelrc, so why does it not report an error when using import directly in index.js?

webpack.config.js

var path = require("path");
module.exports = {

    //
    entry: {
        main: "./src/index.js",
    },
    output: {
        //
        path: path.join(__dirname, "dist"),
        filename: "main.js",
    },
}

index.js

import "./a.js";
console.log("index");
Apr.14,2022

I don't think you understand what babel is for

What is Babel?

Babel is a JavaScript compiler
Babel is a toolchain that is mainly used to convert ECMAScript 2015 + code into a backwards compatible version of JavaScript in current and older browsers or environments. Here are the main things Babel can do for you:

  • Transform syntax
  • Polyfill features that are missing in your target environment (through * @ babel/polyfill)
  • Source code transformations (codemods)
  • And more! (check out these videos for inspiration)

Quick check the latest version of major browsers' support for ES6


my understanding is that webpack itself supports ES6 syntax (depending on your Node version)


babel only translates es6 into es5 to be compatible with browsers that do not support es6. If your browser supports es6, then using es6 in index.js will not report errors


because node itself supports it, ECMAScript Modules can run normally without Babel

hr class= "answer" >

because webpack uses acorn:

webpackimport, export


webpack starts with the entry files configured by the configuration file, that is, the files that webpack has to process the package, and does not have to be executed immediately.

what webpack does is execute webpack's node program, read the wepack configuration file, and use the node program to process the program file to be packaged. You should understand that dealing with files here means dealing with the text contents of files .

that is, uses a js program to process the text contents of other js files .

read the code in your project as text but do not need to execute it. After the webpack program reads the various module syntax of the entry a.js file in your project, it will replace with text and replace it with a module syntax that needs to be recognized by the environment in which your project code needs to be executed.

in short, what webpack does is package the js code text, not execute the code.

Menu