我已经在PHP项目上工作了一段时间,并且客户在最后一刻要求IE11支持。 HTML / CSS问题,我可以处理,但是我的JavaScript是用现代语法编写的。
因此,我安装节点,使用我的JavaScript,在第一次需要它时通过Rollup&Babel运行它,并为以后的请求缓存结果。
现在输出缺少以前让我头疼的箭头功能,但是我遇到了一个更大的问题:polyfills是导入语句,而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
。
将useBuiltIns
从usage
更改为entry
(据我所知,这意味着我希望在其他地方有一个条目来添加polyfill),并且包含https://polyfill.io/v3/并没有”什么都不做(我在Number.parseFloat()
通话中出错)。
出于绝望,我什至在我的应用程序中添加了一个“ core-js”路由,该路由试图提供适当的core-js文件-但没有迹象表明IE11甚至试图遵循require语句。
环顾互联网似乎对其他任何人来说都不是问题-IE11显然对其他任何人都有效?
也许是因为我没有使用节点服务器,而是使用PHP / Apache服务器?
我只希望Babel在我的文件中包含core-js的polyfill,而不是IE11不知道如何解析的require语句。