=========================================html
$ npm install -g vue-cli $ yarn global add vue-cli
$ vue init webpack project-name
1)Vue build這個選項選擇Runtime + Compiler
2)安裝vue-router,ESLint、Karma+Mocha、Nightwatch根據需求選擇安裝vue
3)根據提示操做,便可成功啓動項目node
1)項目目錄結構的調整:
在開發路徑src下增長modules和pages文件夾,分別存放模塊和頁面
有關頁面的全部文件都放到同一文件夾下就近管理:index.html(頁面模板)、main.js(頁面入口文件)、App.vue(頁面使用的組件,公用組件放到components文件夾下)、router(頁面的路由配置)、assets(頁面的靜態資源)都移到index文件夾下,並把main.js改成index.js,保證頁面的入口js文件和模板文件的名稱一致webpack
2)在build/utils.js中添加兩個方法:webpack多入口文件和多頁面輸出web
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 }
3)修改build/webpack.base.conf.js的入口配置vue-router
上面紅色線框改爲一下內容: module.exports = { entry: utils.entries(), ...
4)修改build/webpack.dev.conf.js和build/webpack.prod.conf.js的多頁面配置:把原有的頁面模板配置註釋或刪除,並把多頁面配置添加到plugins
webpack.dev.conf.js:vue-cli
plugins: [ ...... // new HtmlWebpackPlugin({ // filename: 'index.html', // template: 'index.html', // inject: true // }), ...... ].concat(utils.htmlPlugin())
修改結果以下:npm
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())
修改結果以下:app
補充說明:在上面多頁面輸出配置中有這樣一行代碼:
chunks: ['manifest', 'vendor', filename],
這是html-webpack-plugin插件對頁面入口文件(即js文件)的限定,若是不設置則會把整個項目下的全部入口文件所有引入
爲何要引入'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模塊作的緩存
關於CommonsChunkPlugin插件的詳細說明請閱讀官方文檔
關於html-webpack-plugin插件的配置還有一行代碼:
chunksSortMode: 'dependency'
插件會按照模塊的依賴關係依次加載,即:manifest,vendor,本頁面入口,其餘頁面入口...至此,多頁面應用已經搭建完畢,只須要在pages文件夾建立相應的頁面文件便可。