简体   繁体   中英

How to inline Rollup/Babel polyfills for IE11

I've been working on a PHP project for a while and the client has asked for IE11 support at the last possible minute. HTML/CSS problems I can deal with but my javascript was written modern syntax.

So I install node, take my javascript, run it through Rollup & Babel the first time it's needed and cache the result for future requests.

Now the output lacks the arrow functions that were giving me a headache before but I've got a bigger problem: the polyfills are import statements and IE11 doesn't support import statements .

I feel like I need to emphasise that I'm not running a node server - it's a PHP server, I'm just using node on the backend for rollup & babel. If there's something that node does to make this work I'm not familiar with it.

Here's my 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
    })
  ]
};

And here's 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
         }
      }
    ]
  ]
}

For giggles, here's the shell script I call to run the process:

#!/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}

And it's linked like this:

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

With these settings, my flatfile.js outputs with the following at the top:

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...

Under this setup IE11's console says there's a Syntax error at the first line of every file with the import statements.

Changing useBuiltIns from usage to entry (which I understand means I'm expected to have an entry file elsewhere that adds the polyfills) and including https://polyfill.io/v3/ doesn't do anything (I get errors on Number.parseFloat() calls).

Out of desperation I even added a "core-js" route to my application, which tries to serve up the appropriate core-js file - but there's no indication that IE11 is even trying to follow the require statements.

Looking around the internet it seems like this isn't a problem for anybody else - IE11 apparently works for everybody else?

Maybe it's because I'm not using a node server, but a PHP/Apache one?

I just want the Babel to include the core-js polyfills in my files, not as require statements that IE11 doesn't know how to parse.

I had to disable the babel-minify plugin, but aside from that, copying your configuration seems to work just fine and I get a single bundled JavaScript file with no import statements.

The files are reproduced below, but the full test repo is available at https://github.com/akx/so58712204yarn; yarn build yarn; yarn build and look in 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 };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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