我遵循了一些设置CommonsChunkPlugin的指南,但它似乎不起作用。我也搜索和阅读了这里的其他帖子,但没有运气。
我有三个文件(TypeScript),它们正在导入几个相同的库(fancybox、矩和pikaday)。它们是使用ES模块语法导入的:
import * as fancybox from 'fancybox';
import * as moment from 'moment';
import * as pikaday from 'pikaday';我的tsconfig.json被设置为输出ES6语法和模块:
{
"compileOnSave": true,
"compilerOptions": {
"target": "es6",
"module": "es6",
"noEmitOnError": true,
"moduleResolution": "node",
"sourceMap": true,
"diagnostics": false,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"traceResolution": false
},
"exclude": [
"node_modules",
"venue-finder"
]
}我的webpack.config.js
const webpack = require('webpack');
const path = require('path');
const WebpackNotifierPlugin = require('webpack-notifier');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
module.exports = [{
bail: true,
entry: {
global: path.resolve(__dirname, 'js/responsive/global.ts'),
todo: path.resolve(__dirname, 'js/todo/todo.ts'),
budget: path.resolve(__dirname, 'Planner/javascript/Budget/BudgetPlannerUI.ts'),
promotions: path.resolve(__dirname, 'js/promotions-management.ts'),
planner: path.resolve(__dirname, 'Planner/javascript/MyPlanner.ts')
},
output: {
path: path.resolve(__dirname, 'dist/js'),
filename: '[name].js'
},
module: {
rules: [{
test: /\.ts(x?)$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
'presets': [
['env', {
'modules': false
}]
]
}
},
{
loader: 'ts-loader'
}
]
}]
},
plugins: [
new WebpackNotifierPlugin({ alwaysNotify: true }),
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
new webpack.optimize.CommonsChunkPlugin({
name: 'commons',
filename: 'commons-bundle.js',
minchunks: 2
}),
new BundleAnalyzerPlugin()
],
resolve: {
extensions: ['.js', '.ts', '.tsx']
},
devtool: 'none'
}];据我所见,这应该是找到任何被使用两次或更多的块(其中有三个),并将其移动到commons-bundle.js中,但是当我运行webpack并查看输出文件时,我可以看到每个文件中都包含了pikaday、twice和fancybox,而不是移动到commons-bundle.js中。
任何帮助都将不胜感激。
发布于 2017-04-08 08:08:21
您可能希望使用minChunks选项作为函数,而不是整数值。你的fancybox,work,pickaday都来自node_modules,因此,像下面这样修改minChunks应该是有效的-
new webpack.optimize.CommonsChunkPlugin({
name: 'commons',
filename: 'commons-bundle.js',
chunks: [global, todo, budget, promotions, planner], //optional, however I find this as good practice
minChunks: function (module) {
return module.context && module.context.indexOf("node_modules") !== -1;
}
}),https://stackoverflow.com/questions/43277662
复制相似问题