#明確概念node
#使用場景jquery
#實現部分 項目結構 webpack
// a.js
import { common1 } from './common'
import $ from 'jquery';
console.log(common1, 'a')
//b.js
import { common1 } from './common'
import $ from 'jquery';
console.log(common1, 'b')
//common.js
export const common1 = 'common1'
export const common2 = 'common2'
複製代碼
在不使用插件的前提下打包結果以下: git
case 1 把多入口entry抽離爲common.jsgithub
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: "common",
filename: "common.js"
})
]
複製代碼
執行結果以下: web
case 2 從children chunk抽離 common.jsbash
// 單入口文件 main.js
const component1 = function(resolve) {
return require(['./a'], resolve)
}
const component2 = function(resolve) {
return require(['./b'], resolve)
}
console.log(component1, component2, $, 'a')
複製代碼
不使用commonChunk執行結果以下: 異步
//使用commonChunk 配置以下
plugins: [
new webpack.optimize.CommonsChunkPlugin({
children: true,
async: 'children-async',
name: ['main']
})
]
複製代碼
// 執行結果以下 async
case 3 node_modules全部額三方依賴抽離爲vendor.js函數
//webpack 配置
...
entry : {
main: './src/main.js',
vendor: ['jquery']
}
...
...
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor', // 這裏是把入口文件全部公共組件都合併到 vendor模塊當中
filename: '[name].js'
})
...
複製代碼
執行結果若是下:
case 4 case 2和case 3混合使用 vendor.js是三方依賴提取,0.js是children公共部分提取
....
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
filename: '[name].js'
}),
new webpack.optimize.CommonsChunkPlugin({
children: true,
async: 'children-async',
name: ['main']
})
]
....
複製代碼
執行結果以下:
#注意的幾點