1.創(chuàng)建vue模板
vue init webpack project-name
2.安裝依賴。
cd project-name
yarn
3.啟動項目
npm run dev
4.對項目目錄結(jié)構(gòu)進行調(diào)整
(1)將最外面的
index.html
放入src
目錄下(2)創(chuàng)建
modules
饿这、pages
浊伙、index
文件夾。將main.js改名為index.js长捧,這是由于后續(xù)配合的約定嚣鄙。(3)將
App.vue
、index.html
串结、index.js
哑子、router
文件夾、assets
文件夾都放入index
首頁文件夾下肌割。(4)將
index
首頁文件夾放入pages
文件下
5.對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: utils.entries(),
...
(3)修改build/webpack.dev.conf.js和build/webpack.prod.conf.js的多頁面配置:把原有的頁面模板配置注釋或刪除卧蜓,并把多頁面配置添加到plugins.
webpack.dev.conf.js:
plugins: [
......
// new HtmlWebpackPlugin({
// filename: 'index.html',
// template: 'index.html',
// inject: true
// }),
......
].concat(utils.htmlPlugin())
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())
此時配置完成,在pages文件下即可以創(chuàng)建其他頁面把敞。例如本項目所需的購物車頁面弥奸。
首頁
購物車頁面