polyfill 是一个 js 库,用于消除浏览器之间 js 实现的差异。 这里的测试过程是在标签中添加 polyfill 的脚本以支持 fetch 如果浏览器不支持 fetch。如何获取 webpack build 生成的 polyfiles.js带有哈希值的文件名?
webpack.config.js 文件内容:
const path = require('path');
module.exports = {
entry: {
app: './src/index.js',
another: './src/another.js',
polyfills: './src/polyfills.js'
},
output: {
**filename: '[name].[chunkhash].js',**
path: path.resolve(__dirname, 'dist')
}
}
Run Code Online (Sandbox Code Playgroud)
index.js 文件内容:
import 'babel-polyfill';
var modernBrowser = (
'fetch' in window &&
'assign' in Object
);
if (!modernBrowser) {
var scriptElement = document.createElement('script');
scriptElement.async = false;
**scriptElement.src = './polyfills.js';**
document.head.appendChild(scriptElement);
}
fetch('https://jsonplaceholder.typicode.com/users')
.then(response => response.json())
.then(json => …Run Code Online (Sandbox Code Playgroud)