場(chǎng)景
在實(shí)際的項(xiàng)目開發(fā)中會(huì)出現(xiàn)這樣的場(chǎng)景,項(xiàng)目中需要多個(gè)模塊(單頁或者多頁應(yīng)用)配合使用的情況,而vue-cli默認(rèn)只提供了單入口打包,所以就想到對(duì)vue-cli進(jìn)行擴(kuò)展
實(shí)現(xiàn)
首先得知道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,可以根據(jù)自己的項(xiàng)目配置更改,此處是以src/modules/文件夾下的目錄作為模塊,每個(gè)模塊中都有一個(gè)main.js作為入口文件
修改build/webpack.base.conf.js中entry
const entryObj = require('./entry') module.exports = { entry: entryObj }
接下來就是如何將打包好的文件注入到html中,這里利用html-webpack-plugin插件來解決這個(gè)問題,首先你需要有一個(gè)html的模板文件,然后在webpack配置中更改默認(rèn)的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 }
實(shí)戰(zhàn)
vue移動(dòng)web通用腳手架
github地址: https://github.com/GavinZhuLei/vue-mobile
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com