基于vue cli重構(gòu)多頁面腳手架過程詳解
官方提供的項目生成工具vue-cli沒有對多頁面webApp的支持,但是在實際的項目中,我們需要這樣的腳手架,參考了很多大牛的方法,這里提供了一種我的單頁面腳手架轉(zhuǎn)換為多頁面腳手架的方案,供大家參考。不好的地方也請大家指正。
準(zhǔn)備
使用vue-cli生成一個你需要的單頁面項目腳手架,然后我們就要開始我們的改裝工程了。
重構(gòu)過程
步驟一 改變目錄結(jié)構(gòu)
- step1 在src目錄下面新建views文件夾,然后再views文件夾下新建index文件夾
- step2 將src目錄下的main.js和App.vue移動到step1中的index文件夾下,并將main.js重命名為index.js
- step3 將src目錄下的router文件夾移動到step1中的index文件夾下,如果不使用router可以再index.js中注釋掉,我沒有使用,因為我的每個頁面不是單頁面的應(yīng)用,不必要使用路由功能
- step4 將根目錄下的index.html文件移動到step1中的index文件夾下
步驟二 修改build下的配置文件
在生產(chǎn)環(huán)境下會分頁面打包獨(dú)有js文件,并抽取公共js,不會什么都打包成一坨。打包后文件目錄結(jié)構(gòu)也是比較清晰地。一下所有修改都在build文件夾下
step1 修改utils.js,增加兩個函數(shù),一個用來獲取頁面多入口,一個用來輸入打包后的頁面,并注入js:
var glob = require('glob')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var PAGE_PATH = path.resolve(__dirname, '../src/views')
var merge = require('webpack-merge')
//多入口配置
//獲取views文件夾下,每個頁面下的index.js作為頁面入口,故每個頁面下都必須有index.js
exports.entries = function() {
var entryFiles = glob.sync(PAGE_PATH + '/*/index.js')
var map = {}, tmp = [], pathname = '';
entryFiles.forEach((filePath) => {
var filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.'))
tmp = filePath.split('/').splice(-4)
map[tmp[2] + '/' + filename] = filePath
})
return map
}
//多頁面輸出配置
//讀取views文件夾下的對應(yīng)每個頁面的html后綴文件,然后放入數(shù)組中
//如果想要更深的定制或者修改,建議大家看一下CommonsChunkPlugin
//推薦一個基礎(chǔ)的 https://segmentfault.com/q/1010000009070061
exports.htmlPlugin = function() {
let entryHtml = glob.sync(PAGE_PATH + '/*/*.html')
let arr = []
entryHtml.forEach((filePath) => {
let jsPath = '', tmp = [];
let filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.'))
tmp = filePath.split('/').splice(-4)
jsPath = tmp[2] + '/' + 'index'
let conf = {
template: filePath,
filename: filename + '.html',
chunks: ['manifest', 'vendors', jsPath],
inject: true
}
if (process.env.NODE_ENV === 'production') {
conf = merge(conf, {
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
},
chunksSortMode: 'dependency'
})
}
arr.push(new HtmlWebpackPlugin(conf))
})
return arr
}
step2 修改webpack.base.conf.js文件配置的入口
// entry: {
// app: './src/main.js'
// },
entry: utils.entries(),
step3 修改webpack.dev.conf.js文件的打包方法 找到下面的代碼,將其注釋掉:
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
inject: true
}),
在plugins這個屬性值的后面加上我們上面的方法,下面是代碼片段:
// new HtmlWebpackPlugin({
// filename: 'index.html',
// template: 'index.html',
// inject: true
// }),
new FriendlyErrorsPlugin()
].concat(utils.htmlPlugin())
step4 修改webpack.prod.conf.js 找到下面的代碼,注釋掉:
new HtmlWebpackPlugin({
filename: config.build.index,
template: 'index.html',
inject: true,
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
// more options:
// https://github.com/kangax/html-minifier#options-quick-reference
},
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
chunksSortMode: 'dependency'
}),
在plugins這個屬性值的后面加上我們上面的方法,下面是代碼片段:
new CopyWebpackPlugin([{
from: path.resolve(__dirname, '../static'),
to: config.build.assetsSubDirectory,
ignore: ['.*']
}])
].concat(utils.htmlPlugin())
配置完成。正常啟動項目即可。
總結(jié)
以上所述是小編給大家介紹的基于vue cli重構(gòu)多頁面腳手架過程詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
原生JS實現(xiàn)Vue transition fade過渡動畫效果示例
這篇文章主要為大家介紹了原生JS實現(xiàn)Vue transition fade過渡動畫效果示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
element plus tree拖動節(jié)點(diǎn)交換位置和改變層級問題(解決方案)
圖層list里有各種組件,用element plus的tree來渲染,可以把圖片等組件到面板里,面板是容器,非容器組件,比如圖片、文本等,就不能讓其他組件拖進(jìn)來,這篇文章主要介紹了element plus tree拖動節(jié)點(diǎn)交換位置和改變層級問題(解決方案),需要的朋友可以參考下2024-04-04
vue3結(jié)合hooks開發(fā)可以注冊的二次確認(rèn)彈框
這篇文章主要為大家介紹了vue3結(jié)合hooks開發(fā)可以注冊的二次確認(rèn)彈框,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
antd upload上傳組件如何獲取服務(wù)端返回數(shù)據(jù)
這篇文章主要介紹了antd upload上傳組件如何獲取服務(wù)端返回數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02

