您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

怎样使用vue-cli扩展多模块打包

2024/3/13 15:29:16发布23次查看
这次给大家带来怎样使用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模块,可以查看文档http://nodejs.cn/api/fs.html,http://nodejs.cn/api/path.html,可以根据自己的项目配置更改,此处是以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 }
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
angular项目中使用scss步骤详解
如何处理webpack打包体积大文件
以上就是怎样使用vue-cli扩展多模块打包的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product