babel vs babel-core vs babel-loader vs babel-preset-2015 vs babel-preset-react vs babel-polyfill

2024-01-27

我正在为我的 React 项目设置 Webpack,并对两者感到困惑babel, babel-core, babel-loader, babel-preset-2015 and babel-preset-react。我知道需要 Babel 将 ES7 或 ES6 代码转换为 ES5,但在我的package.json我已经安装了除 Babel 之外的所有这些依赖项,它们也作为devDependencies.

有人可以解释一下所有这些之间有什么区别以及为什么我的项目需要所有这些?没有任何单一的依赖可以替代它们吗?如果它们如此重要,为什么它们被列为devDependencies?


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 核心

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-预设-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 预设反应

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. 

巴别塔装载机

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

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/.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

babel vs babel-core vs babel-loader vs babel-preset-2015 vs babel-preset-react vs babel-polyfill 的相关文章

随机推荐

  • 使用 ADFS IdP 进行单点注销的正确 LogoutRequest

    我成功使用 OneLogin java saml 库进行 SAML SSO 但 Active Directory 联合身份验证服务 ADFS 的 SLO 单点注销 存在问题 该库创建的 LogoutRequest 被 ADFS 拒绝 但被
  • 点覆盖问题

    我最近在测试中遇到了这个问题 给定一组点m 全部在 x 轴上 和一组n具有端点的线 l r 再次在 x 轴上 找到 的最小子集n这样所有的点都被一条线覆盖 证明你的解决方案总是能找到最小子集 我为它编写的算法的效果是 假设线存储为数组 左端
  • SQL-将长整数转换为日期时间

    我有一个数据库 其日期采用以下长整数格式 20100101000000 其中该时间为 2010 年 1 月 1 日 00 00 00 我希望能够将其转换为正常的 SQL 日期时间语法并返回 这可能吗 我只能走到这一步 SELECT CAST
  • 事务之间的竞争条件

    我正在使用 Spring MVC 开发 webapp 并在我的应用程序中有这样的方法 Transactional public void methodA Long id String color Fruit fruit entityMana
  • in_array() 期望参数 2 为数组,Classipress 中给出的字符串

    我在用着Classipress theme对于 wordpress 我正在尝试将我的精选广告粘贴到类别中 我发现一个返回此错误的代码 Warning in array expects parameter 2 to be array stri
  • Android 上 之间的区别

    我现在对两个明显元素之间的区别感到困惑 我认为仅使用 Google Play 会过滤掉不属于支持屏幕列表的设备 该应用程序不会显示在这些设备上的搜索结果中 我以为使用就足够了 但似乎不起作用 我也需要包含吗 有人可以向我解释自然语言的区别吗
  • 如何通过jquery从数据库接收数据? [wordpress.js]

    我正在尝试从 WordPress 主题的 js 文件中的数据库中获取一些数据 我尝试使用 jquery 的 post 但没有任何反应 还请建议我任何替代方案 js 文件中的代码 jq post abc php name kumar accI
  • 磁带平衡 Codility 培训计划

    我在 Codility 中提交了磁带平衡问题的解决方案 礼貌培训 1 问题描述如下 给出一个由 N 个整数组成的非空零索引数组 A 数组 A 代表磁带上的数字 任何整数 P 使得 0 我提交的解决方案是 class Solution pub
  • 我怎样才能避免codeigniter中的URL中的index.php [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我正在使用 apache 服务器 我
  • extern "C" 带有类和 DLL

    我得到了一个 C DLL 源代码 它使用extern C extern C class Something public declspec dllexport Something declspec dllexport virtual Som
  • 读取 Parquet 文件时是什么控制分区数量?

    我的设置 两个 Spark 集群 一份在 EC2 上 一份在 Amazon EMR 上 两者均使用 Spark 1 3 1 EMR集群已安装emr 引导操作 https github com awslabs emr bootstrap ac
  • play框架2.0支持memcached吗?

    我阅读了 Play Framework 2 0 文档 但没有任何关于 memcached 的文字 如何在 Play Framework 2 0 中使用 memcached 为什么在2 0中消失 在1 x中 这很容易 正如评论中已经指出的 默
  • 当 Jenkins 安装在登台服务器上时,如何在本地访问 Jenkins 仪表板?

    我已经通过 putty 在我的项目 网站 的 Ubuntu 临时服务器上安装了 Jenkins 就像我在 WINDOWS7 上一样 我启动了它并检查它是否在那里运行 但是当尝试访问时http staging website com 8080
  • 使用 Javascript 将类添加到每个第三个元素

    我正在尝试使用 javascript 选择父级的每三个元素并向其添加一个 css 类 对我来说听起来很简单 但我无法让它发挥作用 我发现this https stackoverflow com questions 4274673 javas
  • 多实例模式的 C++ 模板类实现

    我使用 C 中的模板类实现了 multiton 模式 ifndef MULTITON H define MULTITON H include
  • 从自动旋转动画中排除 UIView

    TL DR 需要保持自动旋转 但在方向改变时排除一个 UIView 自动旋转 如何 背景故事 我需要在动画过程中保持 UIView 静止并伴随自动旋转 这发生在方向改变时 类似于 iOS 相机应用程序处理旋转的方式 即控件在其位置旋转 我尝
  • 在C#中调用exe程序

    如何从一个 C 文件调用另一个 C 文件生成的 exe using System Diagnostics string command C tmp myExe exe my params ProcessStartInfo procStart
  • 如何启动一个线程来保持GUI刷新?

    我有一个带有按钮的窗口 它会触发冗长的处理 我将处理放在一个单独的线程中 但令我惊讶的是 它无论如何都会使 GUI 冻结 没有控件刷新 我什至无法移动窗口 So 问题是如何启动线程 这样它就不会干扰GUI 即GUI将始终是最新的 在处理时我
  • Laravel 订单按 whereHas

    这个问题与 Laravel 5 3 Eloquent 关系问题 https stackoverflow com questions 41123931 laravel 5 3 eloquent relationship issue 请访问此网
  • 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 and babel preset react 我知道需要 Babel 将 E