【问题标题】:How to inline Rollup/Babel polyfills for IE11如何为 IE11 内联 Rollup/Babel polyfill
【发布时间】:2019-11-05 13:19:29
【问题描述】:

我从事 PHP 项目已经有一段时间了,客户在最后一刻要求 IE11 支持。我可以处理 HTML/CSS 问题,但我的 javascript 是用现代语法编写的。

所以我安装节点,获取我的 javascript,在第一次需要它时通过 Rollup & Babel 运行它,并为将来的请求缓存结果。

现在输出缺少以前让我头疼的箭头函数,但我遇到了一个更大的问题:polyfill 是导入语句而 IE11 不支持导入语句

我觉得我需要强调我不是在运行节点服务器——它是一个 PHP 服务器,我只是在后端使用节点来进行汇总和 babel。如果节点做了什么来完成这项工作,我不熟悉它。

这是我的 rollup.config.js:

import commonjs from 'rollup-plugin-commonjs';
import resolve from 'rollup-plugin-node-resolve';
import babel from 'rollup-plugin-babel';
import minify from 'rollup-plugin-babel-minify';

export default {
  input: '_dud_input.js', // These are set in the exec() call
  output: {
    file: '_dud_output.js', // These are set in the exec() call
    format: 'iife',
    strict : false
  },
  plugins: [
    resolve({
      browser: true
    }),
    commonjs({
      sourceMap: false
    }),
    babel({
      exclude: 'node_modules/**' // only transpile our source code
    }),
    minify({
      "comments": false
    })
  ]
};

这里是babel.config.js

module.exports = {
  "presets" : [
    [
      "@babel/preset-env",
      {
         "targets": {
           "browsers": "> 0.5%, ie >= 11"
         },
        "modules": false,
        "spec": true,
         "useBuiltIns": "usage",
         "forceAllTransforms": true,
         "corejs": {
           "version": 3,
           "proposals": false
         }
      }
    ]
  ]
}

为了咯咯笑,这是我调用来运行该进程的 shell 脚本:

#!/bin/bash

set -e

# Expected argument values:
# $1 - Path of the root directory
# $2 - Path of the node binary
# $3 - Path of the rollup binary
# $4 - Source file path
# $5 - Destination file path

if [ $# -ne 5 ]
  then
    exit 99
fi

ROOT_DIR=$1
NODE_BIN=$2
ROLLUP_BIN=$3
SRC_PATH=$4
DEST_PATH=$5

cd ${ROOT_DIR}

${NODE_BIN} ${ROLLUP_BIN} -c ${ROOT_DIR}/rollup.config.js -i ${SRC_PATH} -o ${DEST_PATH}

它是这样链接的:

<script defer="" type="text/javascript" src="http://example.com/site-asset/flatfile.js"></script>

通过这些设置,我的 flatfile.js 在顶部输出以下内容:

import"core-js/modules/es.symbol";
import"core-js/modules/es.symbol.description";
import"core-js/modules/es.symbol.iterator";
import"core-js/modules/es.array.concat";
import"core-js/modules/es.array.filter";
import"core-js/modules/es.array.find";
import"core-js/modules/es.array.for-each";
// ...etc...

在此设置下,IE11 的控制台显示每个带有导入语句的文件的第一行都有一个Syntax error

useBuiltInsusage 更改为entry(我理解这意味着我应该在其他地方有一个添加polyfills 的入口文件)并且包括https://polyfill.io/v3/ 没有做任何事情(我得到错误Number.parseFloat() 来电)。

出于绝望,我什至在我的应用程序中添加了一个“core-js”路由,它试图提供适当的 core-js 文件 - 但没有迹象表明 IE11 甚至试图遵循 require 语句。

环顾互联网,这似乎对其他人来说不是问题 - IE11 显然适用于其他所有人?

也许是因为我使用的不是节点服务器,而是 PHP/Apache 的?

我只希望 Babel 在我的文件中包含 core-js 填充,而不是 IE11 不知道如何解析的 require 语句。

【问题讨论】:

    标签: javascript php internet-explorer-11 babeljs rollupjs


    【解决方案1】:

    我不得不禁用 babel-minify 插件,但除此之外,复制您的配置似乎工作得很好,而且我得到了一个没有导入语句的捆绑 JavaScript 文件。

    文件在下面复制,但完整的测试 repo 可在 https://github.com/akx/so58712204 - yarn; yarn build 并查看 dist/...

    babel.config.js

    module.exports = {
      presets: [
        [
          "@babel/preset-env",
          {
            targets: {
              browsers: "> 0.5%, ie >= 11"
            },
            modules: false,
            spec: true,
            useBuiltIns: "usage",
            forceAllTransforms: true,
            corejs: {
              version: 3,
              proposals: false
            }
          }
        ]
      ]
    };
    
    

    package.json

    {
      "scripts": {
        "build": "rollup -c ./rollup.config.js -i ./src/entry.js -o ./dist/output.js"
      },
      "dependencies": {
        "@babel/core": "^7.7.0",
        "@babel/preset-env": "^7.7.0",
        "core-js": "^3.3.6",
        "rollup": "^1.26.3",
        "rollup-plugin-babel": "^4.3.3",
        "rollup-plugin-babel-minify": "^9.1.0",
        "rollup-plugin-commonjs": "^10.1.0",
        "rollup-plugin-node-resolve": "^5.2.0"
      }
    }
    

    rollup.config.js

    import commonjs from "rollup-plugin-commonjs";
    import resolve from "rollup-plugin-node-resolve";
    import babel from "rollup-plugin-babel";
    
    export default {
      input: "_dud_input.js", // These are set in the exec() call
      output: {
        file: "_dud_output.js", // These are set in the exec() call
        format: "iife",
        strict: false
      },
      plugins: [
        resolve({
          browser: true
        }),
        commonjs({
          sourceMap: false
        }),
        babel({
          exclude: "node_modules/**" // only transpile our source code
        })
      ]
    };
    

    src/entry.js

    import { magicNumber } from "./magic";
    console.log(new Set([Number.parseFloat(magicNumber)]));
    

    src/magic.js

    const magicNumber = "8.82";
    export { magicNumber };
    

    【讨论】:

    • 所以一直是babel-minify。谢谢吨。 (现在我只能解决常规的 IE11 废话了!)
    • 是的,我只需要禁用 babel-minify,因为它根本无法使用它构建 - 可能是版本问题。
    • 好吧,至少我们偶然发现了一个答案:D
    • 添加 forceAllTransforms: true 对我有用,谢谢!
    猜你喜欢
    • 2019-07-07
    • 2021-03-12
    • 2020-04-09
    • 2019-07-02
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多