溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

基于vue cli重構多頁面腳手架過程的示例分析

發(fā)布時間:2021-08-23 14:25:16 來源:億速云 閱讀:116 作者:小新 欄目:web開發(fā)

這篇文章主要介紹基于vue cli重構多頁面腳手架過程的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

準備

使用vue-cli生成一個你需要的單頁面項目腳手架,然后我們就要開始我們的改裝工程了。

重構過程

步驟一 改變目錄結構

  • 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中注釋掉,我沒有使用,因為我的每個頁面不是單頁面的應用,不必要使用路由功能

  • step4 將根目錄下的index.html文件移動到step1中的index文件夾下

步驟二 修改build下的配置文件

在生產環(huán)境下會分頁面打包獨有js文件,并抽取公共js,不會什么都打包成一坨。打包后文件目錄結構也是比較清晰地。一下所有修改都在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文件夾下的對應每個頁面的html后綴文件,然后放入數(shù)組中
//如果想要更深的定制或者修改,建議大家看一下CommonsChunkPlugin
//推薦一個基礎的 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())

配置完成。正常啟動項目即可。

以上是“基于vue cli重構多頁面腳手架過程的示例分析”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI