1.创建vue模板
vue init webpack project-name
2.安装依赖。
cd project-name
yarn
3.启动项目
npm run dev
4.对项目目录结构进行调整
image.png(1)将最外面的
index.html
放入src
目录下(2)创建
modules
、pages
、index
文件夹。将main.js改名为index.js,这是由于后续配合的约定。(3)将
App.vue
、index.html
、index.js
、router
文件夹、assets
文件夹都放入index
首页文件夹下。(4)将
index
首页文件夹放入pages
文件下
5.对webpack配置进行调整
(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文件下即可以创建其他页面。例如本项目所需的购物车页面。
首页
image.png
购物车页面
网友评论