記一次基於vue-cli的多頁面應用配置

初始環境

  • vue-cli生成的單頁應用

配置原則

儘可能不修改原有的配置文件和目錄結構,作到增量配置html

目錄結構

├── README.md
├── build                   # build 腳本
├── config                  # prod/dev build config 文件
├── index.html              # 主網頁
├── package.json
├── src                     # Vue.js 核心業務
│   ├── App.vue             # App Root Component
│   ├── api                 # 接入後端服務的基礎 API
│   ├── assets              # 靜態文件
│   ├── components          # 組件
│   ├── main.js             # Vue 主入口文件
│   ├── router              # 路由
│   ├── service             # 服務
│   ├── pages               # 多頁面
│   ├── store               # Vuex 狀態管理
│   ├── util                # 通用 utility
│   └── view                # 各個頁面
├── static                  # 靜態文件
└── test                    # 測試
複製代碼

具體配置

修改文件

├── build                   # build 腳本
    ├── utils.js
    ├── webpack.base.conf.js
    ├── webpack.dev.conf.js
    ├── webpack.prod.conf.js
├── package.json
├── src
    ├── pages               # 多頁面
        ├── scan # 例如增長一個scan的頁面
            ├── scan.html
            ├── scan.js
            ├── scan.vue
複製代碼

配置代碼

  • package.json:glob模塊vue

    "glob": "^7.1.2",
    複製代碼
  • utils.jsnode

    ...
    /* +++++++++++++++ 增長 ++++++++++++++++++++ */
    const glob = require('glob')
    const HtmlWebpackPlugin = require('html-webpack-plugin')
    const merge = require('webpack-merge')
    /* +++++++++++++++++++ 增長 +++++++++++++++ */
    
    ...
    
    /* +++++++++++++++ 增長 ++++++++++++++++++++ */
    const PAGE_PATH = path.resolve(__dirname, '../src/pages')
    exports.entries = function (){
      const entrys = glob.sync(PAGE_PATH + '/*/*.js')
    
      const entrysMap = {}
      entrys.forEach((filePath)=>{
        const fileName = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.js'))
        entrysMap[fileName] = filePath
      })
      return entrysMap
    }
    
    exports.htmlPlugins = function (){
      const entryHtmls = glob.sync(PAGE_PATH + '/*/*.html')
    
      const htmlsArray = []
      entryHtmls.forEach((filePath)=>{
        const fileName = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.html'))
    
        let HtmlWebpackPluginConf =  new HtmlWebpackPlugin({
          filename: fileName + '.html',
          template: filePath,
          chunks: ['manifest', 'vendor', fileName],
          inject: true
        })
    
        if(process.env.NODE_ENV === 'production'){
          HtmlWebpackPluginConf = merge(HtmlWebpackPluginConf, {
            minify: {
              removeComments: true,
              collapseWhitespace: true,
              removeAttributeQuotes: true
            },
            chunksSortMode: 'dependency'
          })
        }
    
        htmlsArray.push(HtmlWebpackPluginConf)
      })
      return htmlsArray
    }
    /* +++++++++++++++ 增長 ++++++++++++++++++++ */
    
    複製代碼
  • webpack.base.conf.jswebpack

    ...
    module.exports = {
      context: path.resolve(__dirname, '../'),
      entry: {
        /* +++++++++++++++ 增長 ++++++++++++++++++++ */
        ...(utils.entries()),
        /* +++++++++++++++ 增長 ++++++++++++++++++++ */
        app: ["babel-polyfill", "./src/main.js"]
      },
    ...
    複製代碼
  • webpack.dev.conf.jsnginx

    ...
      plugins: [
        new webpack.DefinePlugin({
          'process.env': require('../config/dev.env')
        }),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
        new webpack.NoEmitOnErrorsPlugin(),
        // https://github.com/ampedandwired/html-webpack-plugin
        new HtmlWebpackPlugin({
          filename: 'index.html',
          template: 'index.html',
          /* +++++++++++++++ 增長 ++++++++++++++++++++ */
          chunks: ['manifest', 'vendor', 'app'],
          /* +++++++++++++++ 增長 ++++++++++++++++++++ */
          inject: true
        }),
        // copy custom static assets
        new CopyWebpackPlugin([
          {
            from: path.resolve(__dirname, '../static'),
            to: config.dev.assetsSubDirectory,
            ignore: ['.*']
          }
        ])
    /* +++++++++++++++ 增長 ++++++++++++++++++++ */
      ].concat(utils.htmlPlugins())
    /* +++++++++++++++ 增長 ++++++++++++++++++++ */
    })
    ...
    複製代碼
  • webpack.prod.confgit

...
    new HtmlWebpackPlugin({
      filename: process.env.NODE_ENV === 'testing'
        ? 'index.html'
        : config.build.index,
      template: 'index.html',
      /* +++++++++++++++ 增長 ++++++++++++++++++++ */
      chunks: ['manifest', 'vendor', 'app'],
      /* +++++++++++++++ 增長 ++++++++++++++++++++ */
      inject: true,
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
        // more options:
        // https://github.com/kangax/html-minifier#options-quick-reference
      },
      // necessary to consistently work with multiple chunks via CommonsChunkPlugin
      chunksSortMode: 'dependency'
    }),
    ...
  /* +++++++++++++++ 增長 ++++++++++++++++++++ */
  ].concat(utils.htmlPlugins())
  /* +++++++++++++++ 增長 ++++++++++++++++++++ */
})
...

複製代碼

nginx 配置

location = /scan {
    rewrite (\/scan).* /scan.html/ redirct
}
複製代碼

參考

相關文章
相關標籤/搜索