这次给大家带来vue-cli多模块打包使用详解,vue-cli多模块打包的注意事项有哪些,下面就是实战案例,一起来看一下。
场景
在实际的项目开发中会出现这样的场景,项目中需要多个模块(单页或者多页应用)配合使用的情况,而vue-cli默认只提供了单入口打包,所以就想到对vue-cli进行扩展
实现
首先得知道webpack是提供了多入口打包,那就可以从这里开始改造
新建build/entry.js
const path = require('path')
const fs = require('fs')
const moduledir = path.resolve(dirname, '../src/modules')
let entryobj = {}
let moduleitems = fs.readdirsync(moduledir)
moduleitems.foreach(item => {
entryobj[`${item}`] = `./src/modules/${item}/main.js`
})
module.exports = entryobj
这里用到了nodejs的fs和path模块,可以根据自己的项目配置更改,此处是以src/modules/文件夹下的目录作为模块,每个模块中都有一个main.js作为入口文件
修改build/webpack.base.conf.js中entry
const entryobj = require('./entry')
module.exports = {
entry: entryobj
}
接下来就是如何将打包好的文件注入到html中,这里利用html-webpack-plugin插件来解决这个问题,首先你需要有一个html的模板文件,然后在webpack配置中更改默认的html-webpack-plugin插件配置
添加build/plugins.js
const htmlwebpackplugin = require('html-webpack-plugin')
let configplugins = []
object.keys(entryobj).foreach(item => {
configplugins.push(new htmlwebpackplugin(
{
filename: '../dist/' + item + '.html',
template: path.resolve(dirname, '../index.html'),
chunks: [item]
}
))
})
module.exports = configplugins
修改build/webpack.dev.conf.js配置
module.exports = {
plugins: configplugins
}
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
bing map使用步骤详解
node+token实现验证
以上就是vue-cli多模块打包使用详解的详细内容。