Babel vs Babel-core vs Babel-loader vs Babel-preset-2015 vs Babel-preset-react vs Babel-polyfill Babel是一个JavaScript编译器,用于将高版本的JavaScript代码转换为向后兼容的代码。Babel-core是Babel的核心模块,提供了编译器的基本功能。Babel-loader是Webpack的一个加载器,用于在Webpack中使用Babel进行编译。Babel-preset-2015和Babel-preset-react是Babel的预设,分别用于编译ES6和React代码。Babel-polyfill是一个用于模拟完整的ES6环境的库,可以在旧版浏览器中使用新版JavaScript特性。

59
我正在为我的React项目设置Webpack,但在babelbabel-corebabel-loaderbabel-preset-2015babel-preset-react之间感到困惑。我知道Babel需要将ES7或ES6代码转换为ES5,但在我的package.json中,除了Babel之外,我已经安装了所有这些依赖项,并且它们也被标记为devDependencies

请问有人能解释一下它们之间的区别以及为什么我的项目需要它们吗?难道没有一种单个依赖项可以代替它们吗?如果它们如此重要,为什么它们包含在devDependencies中?
1个回答

94

babel

Babel doesn't do anything,It basically acts like const babel = code => code; 
by parsing the code and then generating the same code back out again.

You will need to add some plugins for Babel to do anything like transpiling es6,JSX.

babel-core

if you want to use babel in your real project, you need to install babel but 
there's no babel package available.

   babel split it up into two separate packages: babel-cli and babel-core

   **babel-cli** : which can be used to compile files from the command line.

   **babel-core** : if you want to use the Node API you can install babel-
      core, Same as "babel-cli" except you would use it programmatically inside your app.

   use "babel-cli" or "babel-core" to compile your files before production.

在继续之前,

预设与插件:

We can add features(es6,JSX) one at a time with babel plugins(es2015), 
    or 
we can use babel presets to include all the features(es6) of a particular year.

Presets make setup easier.

babel-preset-es2015

babel-preset-env supports es2015 features and replaces es2015, es2016, 
  es2017 and latest.

So use babel-preset-env, it behaves exactly the same as babel-preset-latest 
 (or babel-preset-es2015, babel-preset-es2016, and babel-preset-es2017 together).

babel-preset-react

transform JSX into createElement calls like transforming react pure class to 
   function and transform react remove prop-types.

babel-polyfill

Without babel-polyfill, babel only allows you to use features like arrow 
 functions, destructuring, default arguments, and other syntax-specific 
 features introduced in ES6.

The new ES6 built-ins like Set, Map and Promise must be polyfilled

To include the polyfill you need to require it at the top of the entry point 
  to your application. 

babel-loader

you done with babel-core, babel-cli, and why need preset, plugins and now 
  you are compiling ES6 to ES5 on a file-by-file basis by babel-cli every time.

to get rid-off this, you need to bundle the task/js file. For that you need 
   Webpack.

Loaders are kind of like “tasks”, They gives the ability to leverage 
 webpack's bundling capabilities for all kinds of files by converting them 
 to valid modules that webpack can process.

Webpack has great Babel support through babel-loader

开发依赖

When you deploy your app, modules in dependencies need to be installed or 
your app won't work. Modules in devDependencies don't need to be installed 
on the production server since you're not developing on that machine.

These packages are only needed for development and testing.

有没有一种依赖可以替换所有其他的依赖呢?
as you read the above states, You need some presets and loaders to transpile 
 es2015 or JSX files.

babel -> @babel

Since Babel 7 the Babel team switched to scoped packages, so you now 
have to use @babel/core instead of babel-core.

Your dependencies will need to be modified like so:

babel-cli -> @babel/cli. Ex:  babel- with @babel/.

7
但这仍然没有回答@babel/core和babe-core之间的区别,它们是两个不同的包。此外,Babel现在已经弃用了阶段预设(stage presets):https://babeljs.io/blog/2018/07/27/removing-babels-stage-presets - BugHunterUK
我知道!这有什么区别啊!没人谈论过它! - Captain Hypertext
似乎 babel-core 已经过时了,而 @babel/core 是新的。请参考:v7.x https://babeljs.io/docs/en/babel-core vs v6.x https://babeljs.io/docs/en/6.26.3/babel-core。总的来说,他们似乎已经将所有内容都转移到了 @babel/ 格式下。 - V. Rubinetti
2
well-explained ! :) - koo
"Babel 并不会做任何事情,它基本上就像 const babel = code => code; 一样。" 我想知道你的意思是什么。如果 Babel 没有作用,为什么还要使用它呢 :> - Omkar76
@Omkar76,“Babel 什么也没做”的解释可以在这里找到:https://blog.jakoblind.no/babel-preset-env/#babel-from-scratch - user36339

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接