获取依赖项
你可以package.json
从babel-polyfill
const path = require('path');
function getDependencies () {
// Read dependencies...
const { dependencies } = require('node_modules/babel-polyfill/package.json');
// Extract module name
return Object.keys(dependencies);
}
只需调用它(应该返回一个带有 的数组dependencies
):
const dependencies = getDependencies(); // ['module', ...]
检测 polyfill
检查模块是否babel-polyfill
或依赖项:
function isPolyfill(module){
// Get module name from path
const name = path.posix.basename(module.context)
// If module has a path
return name &&
// If is main module or dependency
( name === "babel-polyfill" || dependencies.indexOf(name) !== -1 );
}
要删除babel-polyfill
和依赖项,只需检查是否返回false
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: function (module) {
// If has path
return module.context &&
//If is a node-module
module.context.indexOf('node_modules')!== -1 &&
// Remove babel-polyfill and dependencies
isPolyfill(module) === false;
}
})
创建 polyfill 块
仅选择babel-polyfill
和依赖项只需检查是否返回true
new webpack.optimize.CommonsChunkPlugin({
name: 'polyfills',
minChunks: function (module) {
// If has a path
return module.context &&
//If is a node-module
module.context.indexOf('node_modules')!== -1 &&
// Select only babel-polyfill and dependencies
isPolyfill(module) === true;
}
})