Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does the "modules:auto" means in @babel/preset-env field?

the babel docs say the modules default option is auto, what does the auto means? does it transform the "import" to "require" or not? what's the difference between "modules: false" and "modules: auto"? it seems they are the same result; I'm learning tree shaking, when I didn't set any options to my @babel/preset-env, tree shaking success, when I set "modules: false", the tree shaking also succeed;

  "presets": [
    [
      "@babel/preset-react"
    ],
    [
      "@babel/preset-env",{"modules": "false"}
    ]
  ],
like image 550
lastStranger Avatar asked Apr 22 '19 09:04

lastStranger


People also ask

What is preset in Babel?

In Babel, a preset is a set of plugins used to support particular language features. The two presets Babel uses by default: es2015 : Adds support for ES2015 (or ES6) JavaScript. react : Adds support for JSX.

Does Babel use Browserslist?

A Babel preset that compiles ES2015+ down to ES5 by automatically determining the Babel plugins and polyfills you need based on your targeted browser or runtime environments. It uses browserslist to parse this information, so we can use any valid query format supported by browserslist .

How does a babel-loader work?

babel-loader does what ts-loader does for TypeScript; passes off files to the Babel compiler, and returns the result to be used in the bundle in-place of the original source program.


1 Answers

It seems that the documentation for auto is described in this pull request. According to that PR:

The default auto will automatically select false if ES6 module syntax is already supported by the caller, or "commonjs" otherwise.

So, if ES6 is supported (which seems to be your case) setting modules to auto its the same as setting false.

like image 81
mgarcia Avatar answered Sep 23 '22 15:09

mgarcia