babel vs babel-core vs babel-loader vs babel-preset-2015 vs babel-preset-react vs babel-polyfill
babel vs babel-core vs babel-loader vs babel-preset-2015 vs babel-preset-react vs babel-polyfill
我正在为我的 React 项目设置 Webpack,但在 babel
、babel-core
、babel-loader
、babel-preset-2015
和 babel-preset-react
之间感到困惑。我知道需要 Babel 才能将 ES7 或 ES6 代码转换为 ES5,但在我的 package.json
中,我已经安装了除 Babel 之外的所有这些依赖项,它们也作为 devDependencies
.
有人可以解释一下所有这些之间的区别以及为什么我的项目需要所有这些吗?难道没有任何一个单一的依赖来取代它们吗?如果它们如此重要,为什么将它们列为 devDependencies
?
通天塔
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
devDependencies
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/.
我正在为我的 React 项目设置 Webpack,但在 babel
、babel-core
、babel-loader
、babel-preset-2015
和 babel-preset-react
之间感到困惑。我知道需要 Babel 才能将 ES7 或 ES6 代码转换为 ES5,但在我的 package.json
中,我已经安装了除 Babel 之外的所有这些依赖项,它们也作为 devDependencies
.
有人可以解释一下所有这些之间的区别以及为什么我的项目需要所有这些吗?难道没有任何一个单一的依赖来取代它们吗?如果它们如此重要,为什么将它们列为 devDependencies
?
通天塔
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
devDependencies
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/.