上篇文章中,我們已經(jīng)成功使用yarn創(chuàng)建了一個vue項目,但是項目模板是單頁面應(yīng)用,與多頁面應(yīng)用還有些差別匠抗,所以現(xiàn)在需要在此基礎(chǔ)上做一些調(diào)整:
關(guān)于多頁面應(yīng)用調(diào)整的思路分為以下兩個部分
一、項目目錄結(jié)構(gòu)調(diào)整
2矢腻、有關(guān)頁面的所有文件都放到同一文件夾下就近管理:
??????index.html(頁面模板)、
??????main.js(頁面入口文件)射赛、
??????App.vue(頁面使用的組件多柑,公用組件放到components文件夾下)、
??????router(頁面的路由配置)楣责、
??????assets(頁面的靜態(tài)資源)
??????把這些列出的文件都移到index文件夾下竣灌,并把main.js改為index.js,保證頁面的入口js文件和模板文件的名稱一致诫隅。
二、webpack配置調(diào)整
項目目錄調(diào)整好后帐偎,我們來進行webpack配置的調(diào)整,步驟如下:
1蛔屹、在build/utils.js中添加兩個方法:webpack多入口文件和多頁面輸出
var path = require('path')
var glob = require('glob')
var HtmlWebpackPlugin = require('html-webpack-plugin') //對每個頁面單獨打包生成一個新頁面的插件
var PAGE_PATH = path.resolve(__dirname, '../src/pages')
var merge = require('webpack-merge')
//多入口配置
exports.entries = function() {
var entryFiles = glob.sync(PAGE_PATH + '/*/*.js')
var map = {}
entryFiles.forEach((filePath) => {
var filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.'))
map[filename] = filePath
})
return map
}
//多頁面輸出配置
exports.htmlPlugin = function() {
let entryHtml = glob.sync(PAGE_PATH + '/*/*.html')
let arr = []
entryHtml.forEach((filePath) => {
let filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.'))
let conf = {
template: filePath,
filename: filename + '.html',
chunks: [filename],
inject: true
}
if (process.env.NODE_ENV === 'production') {
conf = merge(conf, {
chunks: ['manifest', 'vendor', filename],
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
},
chunksSortMode: 'dependency'
})
}
arr.push(new HtmlWebpackPlugin(conf))
})
return arr
}
2削樊、修改build/webpack.base.conf.js的入口配置
原單入口配置
module.exports = {
entry: {
app: './src/main.js'
},
...
修改為多入口配置
module.exports = {
entry: utils.entries(),
...
3、修改build/webpack.dev.conf.js和build/webpack.prod.conf.js的多頁面配置:把原有的頁面模板配置注釋或刪除兔毒,并把多頁面配置添加到plugins
(開發(fā)環(huán)境下)webpack.dev.conf.js:
plugins: [
......
// new HtmlWebpackPlugin({
// filename: 'index.html',
// template: 'index.html',
// inject: true
// }),
......
].concat(utils.htmlPlugin())
(生產(chǎn)環(huán)境下)webpack.prod.conf.js:
plugins: [
......
// new HtmlWebpackPlugin({
// filename: config.build.index,
// template: 'index.html',
// inject: true,
// minify: {
// removeComments: true,
// collapseWhitespace: true,
// removeAttributeQuotes: true
// },
// chunksSortMode: 'dependency'
// }),
......
].concat(utils.htmlPlugin())
補充說明:
- 在上面多頁面輸出配置中有這樣一行代碼:
chunks: ['manifest', 'vendor', filename],
這是html-webpack-plugin插件對頁面入口文件(即js文件)的限定漫贞,如果不設(shè)置則會把整個項目下的所有入口文件全部引入。 - 為什么要引入
'manifest'
和'vendor'
?
因為在build/webpack.prod.conf.js中有如下代碼:
// split vendor js into its own file
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: function (module, count) {
// any required modules inside node_modules are extracted to vendor
return (
module.resource &&
/\.js$/.test(module.resource) &&
module.resource.indexOf(
path.join(__dirname, '../node_modules')
) === 0
)
}
}),
// extract webpack runtime and module manifest to its own file in order to
// prevent vendor hash from being updated whenever app bundle is updated
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
chunks: ['vendor']
}),
vendor模塊是指提取涉及node_modules中的公共模塊
manifest模塊是對vendor模塊做的緩存
關(guān)于CommonsChunkPlugin插件的詳細說明請閱讀官方文檔
- 關(guān)于html-webpack-plugin插件的配置還有一行代碼:
chunksSortMode: 'dependency'
插件會按照模塊的依賴關(guān)系依次加載育叁,即:manifest迅脐,vendor,本頁面入口豪嗽,其他頁面入口...
至此谴蔑,多頁面應(yīng)用已經(jīng)搭建完畢,只需要在pages文件夾創(chuàng)建相應(yīng)的頁面文件即可龟梦。