首页 > 解决方案 > 为什么 Babel 导入已经支持的 polyfill?

问题描述

我正在使用rollup js和 Babel 来转换我的 javascript,但为什么 babelpolyfill会为“promises”添加一个?当 chrome 支持版本 33 的承诺时。我的目标是chrome version > 61

调试模式:

添加了以下内容core-js polyfill: es.promise { "chrome":"62" }

rollup.config.js

import babel from "rollup-plugin-babel";
import resolve from "rollup-plugin-node-resolve";
import commonjs from "rollup-plugin-commonjs";

export default [
    {
        input: "src/main.js",
        output: {
            file: "./dist/bundle-modern.js",
            format: "iife",
            name: "bundle"
        },
        plugins: [
            babel({
                exclude: "node_modules/**",
                presets: [
                    [
                        "@babel/preset-env",
                        {
                            targets: {
                                browsers: ["Chrome > 61"]
                            },
                            useBuiltIns: "usage",
                            corejs: 3,
                            debug: true
                        }
                    ]
                ]
            }),
            resolve(),
            commonjs()
        ]
    }
];

main.js

function checkStatus(response) {
    if (response.status >= 200 && response.status < 300) {
        return response;
    } else {
        var error = new Error(response.statusText);
        error.response = response;
        throw error;
    }
}

function parseJSON(response) {
    return response.json();
}

fetch("https://randomuser.me/api")
    .then(checkStatus)
    .then(parseJSON)
    .then(function(data) {
        console.log("request succeeded with JSON response", data.results);
    })
    .catch(function(error) {
        console.log("request failed", error);
    });

标签: javascriptbabeljsrollupjs

解决方案


推荐阅读