为什么会用多页面

在开发时,对于同一类型的多网站,多页面大大节省开发时间,只需要配置一次就可以实现多次开发变成单次开发,同时一个包就可以展示一整个网站

如何在vue.config.js配置多页面信息

多页面打包会打包多个.html文件,根据.html配置跳转地址就可以了 

目录(四个页面)

配置打包相关

//引入打包组件
const FileManagerPlugin = require('filemanager-webpack-plugin')
//配置打包信息
const fs = require('fs')
const path = require('path')
const FileManagerPlugin = require('filemanager-webpack-plugin')
const productionDistPath = './productionDist'
// 是否打包
const isProduction = process.env.NODE_ENV === 'production'
// 打包环境变量
const envType = process.env.ENV_TYPE || 'prod'
module.exports = {
	// 打包生成压缩包
      const fileManagerPlugin = new FileManagerPlugin({
        //初始化 filemanager-webpack-plugin 插件实例
        events: {
          onEnd: {
            delete: [
              //首先需要删除项目根目录下的dist.zip
              productionDistPath
            ],
            archive: [
              //然后我们选择dist文件夹将之打包成dist.zip并放在根目录
              {
                source: './dist',
                destination: getCompressionName()
              }
            ]
          }
        }
      })
      config.plugins.push(fileManagerPlugin)
}
// 获取打包压缩包路径
function getCompressionName() {
  try {
    const projectName = JSON.parse(fs.readFileSync('package.json')).name
    return `${productionDistPath}/${projectName}-${new Date().getTime()}-${envType}.zip`
  } catch (e) {
    return `${productionDistPath}/dist.zip`
  }
}
function resolve(dir) {
  return path.join(__dirname, dir)
}

配置多页面相关

//定义多页面路径
const pagesArray = [
  {
    pagePath: 'applications',
    pageName: '名称',
    chunks: ['chunk-element-plus']
  },
  { pagePath: 'index', pageName: '名称' },
  {
    pagePath: 'uiLibrary',
    pageName: '名称',
    chunks: ['chunk-element-plus', 'chunk-ant-design-vue']
  },
  {
    pagePath: 'visualizationLibrary',
    pageName: '名称'
  }
]
const pages = {}
pagesArray.forEach(item => {
  const itemChunks = item.chunks
    ? [item.pagePath, ...item.chunks]
    : [item.pagePath]
  pages[item.pagePath] = {
    entry: `src/pages/${item.pagePath}/main.js`,
    template: 'public/index.html',
    filename: `${item.pagePath}.html`,
    title: item.pageName,
    chunks: ['chunk-vendors', 'chunk-common', ...itemChunks]
  }
})
module.exports = {
  publicPath: './',
  // 多页配置
  pages,
  // lintOnSave: false,
  css: {
    loaderOptions: {
      less: {
        lessOptions: {
          javascriptEnabled: true,
          modifyVars: {
            // 'primary-color': 'red'
          }
        }
      }
    }
  },
  // 打包时不生成.map文件
  productionSourceMap: false,
  // 配置webpack
  configureWebpack: config => {
    config.resolve.alias = {
      '@': resolve('src'),
      '@index': resolve('src/pages/index'),
      '@applications': resolve('src/pages/applications'),
      '@uiLibrary': resolve('src/pages/uiLibrary'),
      '@visualizationLibrary': resolve('src/pages/visualizationLibrary')
    }
    if (isProduction) {
      config.optimization.CommonsChunkPlugin
      // 开启分离js
      config.optimization = {
        // runtimeChunk: 'single',
        splitChunks: {
          chunks: 'all',
          cacheGroups: {
            // 抽离所有入口的公用资源为一个chunk
            common: {
              name: 'chunk-common',
              chunks: 'initial',
              minChunks: 2,
              maxInitialRequests: 5,
              minSize: 0,
              priority: 1,
              reuseExistingChunk: true,
              enforce: true
            },
            // 抽离node_modules下的库为一个chunk
            vendors: {
              name: 'chunk-vendors',
              test: /[\\/]node_modules[\\/]/,
              chunks: 'initial',
              priority: 2,
              reuseExistingChunk: true,
              enforce: true
            },
            antd: {
              name: 'chunk-ant-design-vue',
              test: /[\\/]node_modules[\\/]ant-design-vue[\\/]/,
              chunks: 'all',
              priority: 3,
              reuseExistingChunk: true,
              enforce: true
            },
            element: {
              name: 'chunk-element-plus',
              test: /[\\/]node_modules[\\/]element-plus[\\/]/,
              chunks: 'all',
              priority: 3,
              reuseExistingChunk: true,
              enforce: true
            },
            echarts: {
              name: 'chunk-echarts',
              test: /[\\/]node_modules[\\/](vue-)?echarts[\\/]/,
              chunks: 'all',
              priority: 4,
              reuseExistingChunk: true,
              enforce: true
            },
            // 由于echarts使用了zrender库,那么需要将其抽离出来,这样就不会放在公共的chunk中
            zrender: {
              name: 'chunk-zrender',
              test: /[\\/]node_modules[\\/]zrender[\\/]/,
              chunks: 'all',
              priority: 3,
              reuseExistingChunk: true,
              enforce: true
            }
          }
        }
      }
      // 打包生成压缩包
      const fileManagerPlugin = new FileManagerPlugin({
        //初始化 filemanager-webpack-plugin 插件实例
        events: {
          onEnd: {
            delete: [
              //首先需要删除项目根目录下的dist.zip
              productionDistPath
            ],
            archive: [
              //然后我们选择dist文件夹将之打包成dist.zip并放在根目录
              {
                source: './dist',
                destination: getCompressionName()
              }
            ]
          }
        }
      })
      config.plugins.push(fileManagerPlugin)
    }
  }
}

总结

const fs = require('fs')
const path = require('path')
const FileManagerPlugin = require('filemanager-webpack-plugin')
const productionDistPath = './productionDist'
// 是否打包
const isProduction = process.env.NODE_ENV === 'production'
// 打包环境变量
const envType = process.env.ENV_TYPE || 'prod'
const pagesArray = [
  {
    pagePath: 'applications',
    pageName: '名称',
    chunks: ['chunk-element-plus']
  },
  { pagePath: 'index', pageName: '名称' },
  {
    pagePath: 'uiLibrary',
    pageName: '名称',
    chunks: ['chunk-element-plus', 'chunk-ant-design-vue']
  },
  {
    pagePath: 'visualizationLibrary',
    pageName: '名称'
  }
]
const pages = {}
pagesArray.forEach(item => {
  const itemChunks = item.chunks
    ? [item.pagePath, ...item.chunks]
    : [item.pagePath]
  pages[item.pagePath] = {
    entry: `src/pages/${item.pagePath}/main.js`,
    template: 'public/index.html',
    filename: `${item.pagePath}.html`,
    title: item.pageName,
    chunks: ['chunk-vendors', 'chunk-common', ...itemChunks]
  }
})
module.exports = {
  publicPath: './',
  // 多页配置
  pages,
  // lintOnSave: false,
  css: {
    loaderOptions: {
      less: {
        lessOptions: {
          javascriptEnabled: true,
          modifyVars: {
            // 'primary-color': 'red'
          }
        }
      }
    }
  },
  // 打包时不生成.map文件
  productionSourceMap: false,
  // 配置webpack
  configureWebpack: config => {
    config.resolve.alias = {
      '@': resolve('src'),
      '@index': resolve('src/pages/index'),
      '@applications': resolve('src/pages/applications'),
      '@uiLibrary': resolve('src/pages/uiLibrary'),
      '@visualizationLibrary': resolve('src/pages/visualizationLibrary')
    }
    if (isProduction) {
      config.optimization.CommonsChunkPlugin
      // 开启分离js
      config.optimization = {
        // runtimeChunk: 'single',
        splitChunks: {
          chunks: 'all',
          cacheGroups: {
            // 抽离所有入口的公用资源为一个chunk
            common: {
              name: 'chunk-common',
              chunks: 'initial',
              minChunks: 2,
              maxInitialRequests: 5,
              minSize: 0,
              priority: 1,
              reuseExistingChunk: true,
              enforce: true
            },
            // 抽离node_modules下的库为一个chunk
            vendors: {
              name: 'chunk-vendors',
              test: /[\\/]node_modules[\\/]/,
              chunks: 'initial',
              priority: 2,
              reuseExistingChunk: true,
              enforce: true
            },
            antd: {
              name: 'chunk-ant-design-vue',
              test: /[\\/]node_modules[\\/]ant-design-vue[\\/]/,
              chunks: 'all',
              priority: 3,
              reuseExistingChunk: true,
              enforce: true
            },
            element: {
              name: 'chunk-element-plus',
              test: /[\\/]node_modules[\\/]element-plus[\\/]/,
              chunks: 'all',
              priority: 3,
              reuseExistingChunk: true,
              enforce: true
            },
            echarts: {
              name: 'chunk-echarts',
              test: /[\\/]node_modules[\\/](vue-)?echarts[\\/]/,
              chunks: 'all',
              priority: 4,
              reuseExistingChunk: true,
              enforce: true
            },
            // 由于echarts使用了zrender库,那么需要将其抽离出来,这样就不会放在公共的chunk中
            zrender: {
              name: 'chunk-zrender',
              test: /[\\/]node_modules[\\/]zrender[\\/]/,
              chunks: 'all',
              priority: 3,
              reuseExistingChunk: true,
              enforce: true
            }
          }
        }
      }
      // 打包生成压缩包
      const fileManagerPlugin = new FileManagerPlugin({
        //初始化 filemanager-webpack-plugin 插件实例
        events: {
          onEnd: {
            delete: [
              //首先需要删除项目根目录下的dist.zip
              productionDistPath
            ],
            archive: [
              //然后我们选择dist文件夹将之打包成dist.zip并放在根目录
              {
                source: './dist',
                destination: getCompressionName()
              }
            ]
          }
        }
      })
      config.plugins.push(fileManagerPlugin)
    }
  }
}
// 获取打包压缩包路径
function getCompressionName() {
  try {
    const projectName = JSON.parse(fs.readFileSync('package.json')).name
    return `${productionDistPath}/${projectName}-${new Date().getTime()}-${envType}.zip`
  } catch (e) {
    return `${productionDistPath}/dist.zip`
  }
}
function resolve(dir) {
  return path.join(__dirname, dir)
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持Devmax。

Vue如何实现多页面配置以及打包方式的更多相关文章

  1. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  2. vue自定义加载指令v-loading占位图指令v-showimg

    这篇文章主要为大家介绍了vue自定义加载指令和v-loading占位图指令v-showimg的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  3. vue使用动画实现滚动表格效果

    这篇文章主要为大家详细介绍了vue使用动画实现滚动表格效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  4. 关于Vue 监控数组的问题

    这篇文章主要介绍了Vue 监控数组的示例,主要包括Vue 是如何追踪数据发生变化,Vue 如何更新数组以及为什么有些数组的数据变更不能被 Vue 监测到,对vue监控数组知识是面试比较常见的问题,感兴趣的朋友一起看看吧

  5. Vue子组件props从父组件接收数据并存入data

    这篇文章主要介绍了Vue子组件props从父组件接收数据并存入data的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  6. Vue h函数的使用详解

    本文主要介绍了Vue h函数的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  7. 使用pyinstaller打包.exe文件的详细教程

    PyInstaller是一个跨平台的Python应用打包工具,能够把 Python 脚本及其所在的 Python 解释器打包成可执行文件,下面这篇文章主要给大家介绍了关于使用pyinstaller打包.exe文件的相关资料,需要的朋友可以参考下

  8. VUE响应式原理的实现详解

    这篇文章主要为大家详细介绍了VUE响应式原理的实现,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助

  9. vue+Element ui实现照片墙效果

    这篇文章主要为大家详细介绍了vue+Element ui实现照片墙效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. vue+elemet实现表格手动合并行列

    这篇文章主要为大家详细介绍了vue+elemet实现表格手动合并行列,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

随机推荐

  1. js中‘!.’是什么意思

  2. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  3. 基于JavaScript编写一个图片转PDF转换器

    本文为大家介绍了一个简单的 JavaScript 项目,可以将图片转换为 PDF 文件。你可以从本地选择任何一张图片,只需点击一下即可将其转换为 PDF 文件,感兴趣的可以动手尝试一下

  4. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. AngularJs上传前预览图片的实例代码

    使用AngularJs进行开发,在项目中,经常会遇到上传图片后,需在一旁预览图片内容,怎么实现这样的功能呢?今天小编给大家分享AugularJs上传前预览图片的实现代码,需要的朋友参考下吧

  6. JavaScript面向对象编程入门教程

    这篇文章主要介绍了JavaScript面向对象编程的相关概念,例如类、对象、属性、方法等面向对象的术语,并以实例讲解各种术语的使用,非常好的一篇面向对象入门教程,其它语言也可以参考哦

  7. jQuery中的通配符选择器使用总结

    通配符在控制input标签时相当好用,这里简单进行了jQuery中的通配符选择器使用总结,需要的朋友可以参考下

  8. javascript 动态调整图片尺寸实现代码

    在自己的网站上更新文章时一个比较常见的问题是:文章插图太宽,使整个网页都变形了。如果对每个插图都先进行缩放再插入的话,太麻烦了。

  9. jquery ajaxfileupload异步上传插件

    这篇文章主要为大家详细介绍了jquery ajaxfileupload异步上传插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. React学习之受控组件与数据共享实例分析

    这篇文章主要介绍了React学习之受控组件与数据共享,结合实例形式分析了React受控组件与组件间数据共享相关原理与使用技巧,需要的朋友可以参考下

返回
顶部