美文网首页前端成神之路我爱编程
仿Nuxt.js的vue-cli多页面配置

仿Nuxt.js的vue-cli多页面配置

作者: 吴佳浩 | 来源:发表于2018-05-17 14:28 被阅读77次

1.全局安装vue-cli:Vue.js官方提供的用于快速创建项目模板的脚手架工具

$ npm install -g vue-cli 

$ yarn global add vue-cli

2.创建项目模板:官方提供了五个模板--webpack、webpack-simple、browserify、browserify-simple、simple,选择webpack模板

$ vue init webpack project-name

3.在安装过程中会有一些提示:
1)Vue build这个选项选择Runtime + Compiler
2)安装vue-router,ESLint、Karma+Mocha、Nightwatch根据需求选择安装

image.png
image.png

3)根据提示操作,即可成功启动项目

4.现在创建的项目模板是单页面应用,与多页面应用还有些差别,需要做一些调整:
1)项目目录结构的调整:


image.png

在开发路径src下增加modules和pages文件夹,分别存放模块和页面
有关页面的所有文件都放到同一文件夹下就近管理:index.html(页面模板)、main.js(页面入口文件)、App.vue(页面使用的组件,公用组件放到components文件夹下)、router(页面的路由配置)、assets(页面的静态资源)都移到index文件夹下,并把main.js改为index.js,保证页面的入口js文件和模板文件的名称一致

2)在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
}

3)修改build/webpack.base.conf.js的入口配置

module.exports = {
  entry: utils.entries(),
...

4)修改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())

补充说明:在上面多页面输出配置中有这样一行代码:

  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文件夹创建相应的页面文件即可。
本文转载自https://github.com/tonyfree/blog/issues/1
如果有什么不理解可以在下方留言
但是我依然觉得 nuxt会更适合上手 如果你是项目需要 建议使用nuxt.js

相关文章

  • 仿Nuxt.js的vue-cli多页面配置

    1.全局安装vue-cli:Vue.js官方提供的用于快速创建项目模板的脚手架工具 2.创建项目模板:官方提供了五...

  • vue-cli多页面配置

    vue-cli多页面配置 对于vue-cli的基础配置可以参考我的这篇blog vue-cli的基础运用 阅读本...

  • Nuxt.js基础

    Nuxt.js工作流 Nuxt.js目录 路由&示例 新建好即等于配置好 页面模版&示例 模版是layouts 异...

  • vue-cli 多页面配置

    参考: https://github.com/bluefox1688/vue-cli-multi-page(参考多...

  • vue-cli 3.0 多页面打包 (四)

    上一篇: vue.config.js 配置 (三) vue2.x要配置一个多页面 挺麻烦的 vue-cli 3.0...

  • vue-cli配置多页面总结

    适用于Vue-cli 2.x 在日常Vue项目开发中,使用vue-cli搭建项目已成为最方便快捷的方式,但是vue...

  • Nuxt.js路由

    路由 Nuxt.js依据pages目录结构自动生成vue-router模块的路由配置。 要在页面之间使用路由,我们...

  • nuxt实现路由跳转的几种方法

    Nuxt.js 依据 pages 目录结构自动生成 vue-router模块的路由配置。官方建议:两个页面间跳转使...

  • 项目踩坑系列(一)——vue-cli

    文章内容大概包括: 选脚手架——vue-cli + 多页面配置 路由去中心化管理 动态标题 打包遇到的问题 一些比...

  • webpack4构建多页应用,了解一下

    用webpack构建多页应用可以有2种思路,多页面单配置 vs. 多页面多配置。本例子采用多页面单配置,即在单页应...

网友评论

    本文标题:仿Nuxt.js的vue-cli多页面配置

    本文链接:https://www.haomeiwen.com/subject/vkubdftx.html