#明确概念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']
})
]
....
复制代码
执行结果以下:
#注意的几点