Vue如何實(shí)現(xiàn)多頁(yè)面配置以及打包方式
為什么會(huì)用多頁(yè)面
在開(kāi)發(fā)時(shí),對(duì)于同一類型的多網(wǎng)站,多頁(yè)面大大節(jié)省開(kāi)發(fā)時(shí)間,只需要配置一次就可以實(shí)現(xiàn)多次開(kāi)發(fā)變成單次開(kāi)發(fā),同時(shí)一個(gè)包就可以展示一整個(gè)網(wǎng)站
如何在vue.config.js配置多頁(yè)面信息
多頁(yè)面打包會(huì)打包多個(gè).html文件,根據(jù).html配置跳轉(zhuǎn)地址就可以了
目錄(四個(gè)頁(yè)面)
配置打包相關(guān)
//引入打包組件 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' // 打包環(huán)境變量 const envType = process.env.ENV_TYPE || 'prod' module.exports = { // 打包生成壓縮包 const fileManagerPlugin = new FileManagerPlugin({ //初始化 filemanager-webpack-plugin 插件實(shí)例 events: { onEnd: { delete: [ //首先需要?jiǎng)h除項(xiàng)目根目錄下的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) }
配置多頁(yè)面相關(guān)
//定義多頁(yè)面路徑 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: './', // 多頁(yè)配置 pages, // lintOnSave: false, css: { loaderOptions: { less: { lessOptions: { javascriptEnabled: true, modifyVars: { // 'primary-color': 'red' } } } } }, // 打包時(shí)不生成.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 // 開(kāi)啟分離js config.optimization = { // runtimeChunk: 'single', splitChunks: { chunks: 'all', cacheGroups: { // 抽離所有入口的公用資源為一個(gè)chunk common: { name: 'chunk-common', chunks: 'initial', minChunks: 2, maxInitialRequests: 5, minSize: 0, priority: 1, reuseExistingChunk: true, enforce: true }, // 抽離node_modules下的庫(kù)為一個(gè)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庫(kù),那么需要將其抽離出來(lái),這樣就不會(huì)放在公共的chunk中 zrender: { name: 'chunk-zrender', test: /[\\/]node_modules[\\/]zrender[\\/]/, chunks: 'all', priority: 3, reuseExistingChunk: true, enforce: true } } } } // 打包生成壓縮包 const fileManagerPlugin = new FileManagerPlugin({ //初始化 filemanager-webpack-plugin 插件實(shí)例 events: { onEnd: { delete: [ //首先需要?jiǎng)h除項(xiàng)目根目錄下的dist.zip productionDistPath ], archive: [ //然后我們選擇dist文件夾將之打包成dist.zip并放在根目錄 { source: './dist', destination: getCompressionName() } ] } } }) config.plugins.push(fileManagerPlugin) } } }
總結(jié)
const fs = require('fs') const path = require('path') const FileManagerPlugin = require('filemanager-webpack-plugin') const productionDistPath = './productionDist' // 是否打包 const isProduction = process.env.NODE_ENV === 'production' // 打包環(huán)境變量 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: './', // 多頁(yè)配置 pages, // lintOnSave: false, css: { loaderOptions: { less: { lessOptions: { javascriptEnabled: true, modifyVars: { // 'primary-color': 'red' } } } } }, // 打包時(shí)不生成.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 // 開(kāi)啟分離js config.optimization = { // runtimeChunk: 'single', splitChunks: { chunks: 'all', cacheGroups: { // 抽離所有入口的公用資源為一個(gè)chunk common: { name: 'chunk-common', chunks: 'initial', minChunks: 2, maxInitialRequests: 5, minSize: 0, priority: 1, reuseExistingChunk: true, enforce: true }, // 抽離node_modules下的庫(kù)為一個(gè)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庫(kù),那么需要將其抽離出來(lái),這樣就不會(huì)放在公共的chunk中 zrender: { name: 'chunk-zrender', test: /[\\/]node_modules[\\/]zrender[\\/]/, chunks: 'all', priority: 3, reuseExistingChunk: true, enforce: true } } } } // 打包生成壓縮包 const fileManagerPlugin = new FileManagerPlugin({ //初始化 filemanager-webpack-plugin 插件實(shí)例 events: { onEnd: { delete: [ //首先需要?jiǎng)h除項(xiàng)目根目錄下的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) }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue2.0如何借用vue-pdf實(shí)現(xiàn)在線預(yù)覽pdf文件
這篇文章主要介紹了vue2.0如何借用vue-pdf實(shí)現(xiàn)在線預(yù)覽pdf文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03vue實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01詳解Vue實(shí)戰(zhàn)指南之依賴注入(provide/inject)
這篇文章主要介紹了詳解Vue實(shí)戰(zhàn)指南之依賴注入(provide/inject),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11vue項(xiàng)目中Toast字體過(guò)小,沒(méi)有邊距的解決方案
這篇文章主要介紹了vue項(xiàng)目中Toast字體過(guò)小,沒(méi)有邊距的解決方案。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04在Vue項(xiàng)目中使用d3.js的實(shí)例代碼
這篇文章主要介紹了在Vue項(xiàng)目中使用d3.js的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值價(jià)值,需要的朋友可以參考下2018-05-05vue、uniapp中動(dòng)態(tài)添加綁定style、class?9種實(shí)現(xiàn)方法
這篇文章主要介紹了vue、uniapp中動(dòng)態(tài)添加綁定style、class?9種方法實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09詳解vite2.0配置學(xué)習(xí)(typescript版本)
這篇文章主要介紹了詳解vite2.0配置學(xué)習(xí)(typescript版本),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02