简体   繁体   English

如何在没有 Webpack 的情况下使用 Babel?

[英]How to use Babel without Webpack?

I compiled my simple code like below using babel.我使用 babel 编译了如下所示的简单代码。

var aa = new Promise();

to

"use strict";

require("core-js/modules/es.object.to-string");

require("core-js/modules/es.promise");

var aa = new Promise();

However, the old browser like IE9 could not execute that code.但是,像 IE9 这样的旧浏览器无法执行该代码。 Because browser could not resolve the path core-js/modules/es.object.to-string .因为浏览器无法解析路径core-js/modules/es.object.to-string

Must I use webpack to using polyfill?我必须使用 webpack 来使用 polyfill 吗?

I don't want to use webpack.我不想使用 webpack。

My babel setting in package.json is below.我在 package.json 中的 babel 设置如下。

  "babel": {
    "presets": [
      [
        "@babel/preset-env",
        {
          "corejs": 3,
          "useBuiltIns": "usage",
          "targets": ">0.2%, not dead, not ie <= 11, not op_mini all"
        }
      ],
      "@babel/preset-react"
    ]
  }

You can include your polyfills scripts using a <script> tag in your index html file and properly ordering your polyfills and application scripts.您可以在索引 html 文件中使用<script>标签包含您的 polyfill 脚本,并正确排序您的 polyfill 和应用程序脚本。 require is not a recognized command by browsers. require不是浏览器识别的命令。 It is a server-side implementation from node.js.它是来自 node.js 的服务器端实现。

You could probably consider using import instead?您可能会考虑使用import代替?

import 'core-js';

or specific modules as shown here https://github.com/zloirock/core-js或此处显示的特定模块https://github.com/zloirock/core-js

import 'core-js/features/promise'; -> Will modify global namespace -> 将修改全局命名空间

import Promise from 'core-js-pure/features/promise'; -> Will not modify the global namespace -> 不会修改全局命名空间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM