有没有一种方法可以设置webpack配置以加载特定的core-js条目 [英] Is there a way to setup webpack config to load specific core-js entries

查看:422
本文介绍了有没有一种方法可以设置webpack配置以加载特定的core-js条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:我对简单安装之外的webpack或babel并不十分熟悉,因此,如果问题不清楚,我深表歉意,并会尽力提供进一步的说明.

DISCLAIMER: I'm not terribly familiar with webpack or babel outside of simple setup, so if the question isn't clear then I apologize and will do my best to offer further clarity.

因此,当前的情况是,一位同事最近更新了一堆软件包,其中包括babel,而babel不再为ie11中的.forEach和spread运算符正确地编译代码(特别是在遍历节点列表时).产生的行为有点令人沮丧;简而言之,在这些浏览器中加载页面时,什么也不会发生,没有控制台错误,什么也没有.

So, the situation currently is that a coworker updated a bunch of packages recently, babel among them, and babel is no longer transpiling the code properly for .forEach and spread operators in ie11 (specifically when iterating over a node list). The resulting behavior is a bit frustrating; simply put, nothing happens when the page is loaded in those browsers, no console errors, just nothing.

在对此进行故障排除时,我已经通过将core-js作为依赖项添加到package.json中并将以下导入添加到main.js文件中来解决了该问题:

While troubleshooting this, I've been able to get it fixed by adding core-js as a dependency in package.json and adding the following imports to the main.js file:

import 'core-js/stable/array/for-each';
import 'core-js/stable/array/from';
import 'core-js/stable/dom-collections';
import 'core-js/stable/object/get-own-property-symbols';

问题是,是否有办法纯粹通过webpack配置获得相同的结果?同样,除了一些基本的常见设置任务之外,我还不太熟悉如何在Webpack中玩游戏,所以我希望我以一种有意义的方式来表述此短语.如果没有,我会尽力根据反馈进行纠正.

The question is, is there a way to get this same result purely through the webpack config? Again, I'm not all that familiar with how to play around in webpack outside of some basic common setup tasks, so I hope I'm phrasing this in a way that makes sense. If not, I'll do my best to correct based on feedback.

推荐答案

您可以通过webpack添加它. 看看文档 https://github.com/zloirock/core-js# babelpreset-env 您需要babel .babelrc

You can add this by webpack. Take a look at the documentation https://github.com/zloirock/core-js#babelpreset-env You need babel .babelrc

{
  "presets": [
    [
     "@babel/preset-env",
     {
       "useBuiltIns": "usage",
       "corejs": 3
     }
    ]
  ]
}

然后您不需要在main.js中添加依赖项,所有依赖项都将由core-js自动添加

Then you do not need to add dependency in main.js all dependencies are added automatically by core-js

我准备了代码 https://github.com/tomik23/webpack-babel-corejs

这篇关于有没有一种方法可以设置webpack配置以加载特定的core-js条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆