umbrella22

升级依赖,添加性能监控,修改渲染进程打包时压缩配置

...@@ -13,7 +13,6 @@ const CopyWebpackPlugin = require('copy-webpack-plugin') ...@@ -13,7 +13,6 @@ const CopyWebpackPlugin = require('copy-webpack-plugin')
13 const MiniCssExtractPlugin = require('mini-css-extract-plugin') 13 const MiniCssExtractPlugin = require('mini-css-extract-plugin')
14 const HtmlWebpackPlugin = require('html-webpack-plugin') 14 const HtmlWebpackPlugin = require('html-webpack-plugin')
15 const TerserPlugin = require('terser-webpack-plugin'); 15 const TerserPlugin = require('terser-webpack-plugin');
16 -const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
17 const { VueLoaderPlugin } = require('vue-loader') 16 const { VueLoaderPlugin } = require('vue-loader')
18 const HappyPack = require('happypack') 17 const HappyPack = require('happypack')
19 const HappyThreadPool = HappyPack.ThreadPool({ size: os.cpus().length }) 18 const HappyThreadPool = HappyPack.ThreadPool({ size: os.cpus().length })
...@@ -82,9 +81,11 @@ let rendererConfig = { ...@@ -82,9 +81,11 @@ let rendererConfig = {
82 }, 81 },
83 { 82 {
84 test: /\.vue$/, 83 test: /\.vue$/,
85 - use: { 84 + use: [ {
86 loader: 'vue-loader', 85 loader: 'vue-loader',
87 options: { 86 options: {
87 + cacheDirectory: 'node_modules/.cache/vue-loader',
88 + cacheIdentifier: '7270960a',
88 extractCSS: process.env.NODE_ENV === 'production', 89 extractCSS: process.env.NODE_ENV === 'production',
89 loaders: { 90 loaders: {
90 sass: 'vue-style-loader!css-loader!sass-loader?indentedSyntax=1', 91 sass: 'vue-style-loader!css-loader!sass-loader?indentedSyntax=1',
...@@ -92,7 +93,7 @@ let rendererConfig = { ...@@ -92,7 +93,7 @@ let rendererConfig = {
92 less: 'vue-style-loader!css-loader!less-loader' 93 less: 'vue-style-loader!css-loader!less-loader'
93 } 94 }
94 } 95 }
95 - } 96 + }]
96 }, 97 },
97 { 98 {
98 test: /\.svg$/, 99 test: /\.svg$/,
...@@ -172,7 +173,6 @@ let rendererConfig = { ...@@ -172,7 +173,6 @@ let rendererConfig = {
172 }), 173 }),
173 new webpack.HotModuleReplacementPlugin(), 174 new webpack.HotModuleReplacementPlugin(),
174 new webpack.NoEmitOnErrorsPlugin(), 175 new webpack.NoEmitOnErrorsPlugin(),
175 - new HardSourceWebpackPlugin(),
176 new HappyPack({ 176 new HappyPack({
177 id: 'HappyRendererBabel', 177 id: 'HappyRendererBabel',
178 loaders: [{ 178 loaders: [{
...@@ -243,6 +243,14 @@ if (process.env.NODE_ENV === 'production') { ...@@ -243,6 +243,14 @@ if (process.env.NODE_ENV === 'production') {
243 terserOptions: { 243 terserOptions: {
244 warnings: false, 244 warnings: false,
245 compress: { 245 compress: {
246 + hoist_funs: false,
247 + hoist_props: false,
248 + hoist_vars: false,
249 + inline: false,
250 + loops: false,
251 + dead_code: true,
252 + booleans: true,
253 + if_return: true,
246 warnings: false, 254 warnings: false,
247 drop_console: true, 255 drop_console: true,
248 drop_debugger: true, 256 drop_debugger: true,
......
...@@ -75,7 +75,7 @@ ...@@ -75,7 +75,7 @@
75 "electron-updater": "^4.3.1", 75 "electron-updater": "^4.3.1",
76 "element-ui": "^2.13.2", 76 "element-ui": "^2.13.2",
77 "express": "^4.17.1", 77 "express": "^4.17.1",
78 - "fs-extra": "^9.0.0", 78 + "fs-extra": "^9.0.1",
79 "js-cookie": "^2.2.1", 79 "js-cookie": "^2.2.1",
80 "nedb": "^1.8.0", 80 "nedb": "^1.8.0",
81 "nprogress": "^0.2.0", 81 "nprogress": "^0.2.0",
...@@ -112,13 +112,14 @@ ...@@ -112,13 +112,14 @@
112 "babel-eslint": "^9.0.0", 112 "babel-eslint": "^9.0.0",
113 "babel-loader": "^8.1.0", 113 "babel-loader": "^8.1.0",
114 "babel-minify-webpack-plugin": "^0.3.1", 114 "babel-minify-webpack-plugin": "^0.3.1",
115 + "cache-loader": "^4.1.0",
115 "cfonts": "^2.8.2", 116 "cfonts": "^2.8.2",
116 "chalk": "^4.0.0", 117 "chalk": "^4.0.0",
117 "copy-webpack-plugin": "^5.1.1", 118 "copy-webpack-plugin": "^5.1.1",
118 "cross-env": "^7.0.2", 119 "cross-env": "^7.0.2",
119 "css-loader": "^3.5.3", 120 "css-loader": "^3.5.3",
120 "del": "^5.1.0", 121 "del": "^5.1.0",
121 - "electron": "^7.3.0", 122 + "electron": "^7.3.1",
122 "electron-builder": "^22.7.0", 123 "electron-builder": "^22.7.0",
123 "electron-devtools-installer": "^3.0.0", 124 "electron-devtools-installer": "^3.0.0",
124 "eslint": "^6.8.0", 125 "eslint": "^6.8.0",
...@@ -143,7 +144,7 @@ ...@@ -143,7 +144,7 @@
143 "split2": "^3.1.1", 144 "split2": "^3.1.1",
144 "style-loader": "^1.2.1", 145 "style-loader": "^1.2.1",
145 "svg-sprite-loader": "^4.3.0", 146 "svg-sprite-loader": "^4.3.0",
146 - "terser-webpack-plugin": "^3.0.2", 147 + "terser-webpack-plugin": "^3.0.3",
147 "through2-filter": "^3.0.0", 148 "through2-filter": "^3.0.0",
148 "url-loader": "^4.1.0", 149 "url-loader": "^4.1.0",
149 "vue-html-loader": "^1.2.4", 150 "vue-html-loader": "^1.2.4",
......
...@@ -2,9 +2,13 @@ ...@@ -2,9 +2,13 @@
2 import router from './router' 2 import router from './router'
3 import store from './store' 3 import store from './store'
4 import { Message } from 'element-ui' 4 import { Message } from 'element-ui'
5 +import Performance from '@/tools/performance'
5 6
6 const whiteList = ['/login'] // 不重定向白名单 7 const whiteList = ['/login'] // 不重定向白名单
8 +var end = null
9 +
7 router.beforeEach((to, from, next) => { 10 router.beforeEach((to, from, next) => {
11 + end = Performance.startExecute(`${from.path} => ${to.path} 路由耗时`) /// 路由性能监控
8 if (store.getters.token) { 12 if (store.getters.token) {
9 if (to.path === '/login') { 13 if (to.path === '/login') {
10 next({ path: '/' }) 14 next({ path: '/' })
...@@ -29,6 +33,9 @@ router.beforeEach((to, from, next) => { ...@@ -29,6 +33,9 @@ router.beforeEach((to, from, next) => {
29 next('/login') 33 next('/login')
30 } 34 }
31 } 35 }
36 + setTimeout(() => {
37 + end()
38 + }, 0)
32 }) 39 })
33 40
34 -router.afterEach(() => {}) 41 +router.afterEach(() => { })
......
1 +/**
2 + * 性能工具
3 + * 1. 计算方法执行时间
4 + * @returns {void}
5 + * @date 2019-11-29
6 + */
7 +import Timer from './timer'
8 +class Performance {
9 + /**
10 + * 计算情况
11 + * @returns {Function} 执行返回值获取时间信息
12 + * @date 2019-11-29
13 + */
14 + startExecute (name = '') {
15 + const timer = Timer.start()
16 + const usedJSHeapSize = this.getMemoryInfo().usedJSHeapSize
17 + return (name2 = '') => {
18 + const executeTime = timer.stop()
19 + const endMemoryInfo = this.getMemoryInfo()
20 + console.log('%cPerformance%c \n1. 执行方法:%c%s%c\n2. 执行耗时: %c%sms%c \n3. 内存波动:%sB \n4. 已分配内存: %sMB \n5. 已使用内存:%sMB \n6. 剩余内存: %sMB',
21 + 'padding: 2px 4px 2px 4px; background-color: #4caf50; color: #fff; border-radius: 4px;', '',
22 + 'color: #ff6f00', `${name} ${name2}`, '',
23 + 'color: #ff6f00', executeTime, '',
24 + endMemoryInfo.usedJSHeapSize - usedJSHeapSize,
25 + this.toMBSize(endMemoryInfo.jsHeapSizeLimit),
26 + this.toMBSize(endMemoryInfo.usedJSHeapSize),
27 + this.toMBSize(endMemoryInfo.totalJSHeapSize)
28 + )
29 + }
30 + }
31 +
32 + /**
33 + * 获取内存信息
34 + * @returns {void}
35 + * @date 2019-11-29
36 + */
37 + getMemoryInfo () {
38 + let memoryInfo = {}
39 + if (window.performance && window.performance.memory) {
40 + memoryInfo = window.performance.memory
41 + }
42 + return memoryInfo
43 + }
44 +
45 + /**
46 + * 转化为MB
47 + * @returns {void}
48 + * @date 2019-11-29
49 + */
50 + toMBSize (byteSize) {
51 + return (byteSize / (1024 * 1024)).toFixed(1)
52 + }
53 +}
54 +
55 +export default new Performance()
1 +/* eslint-disable no-return-assign */
2 +/**
3 + * 计时器
4 + * 支持链式调用
5 + * timeout()
6 + * .then(()=>{
7 + * return inTheEnd();
8 + * })
9 + * .then(()=>{
10 + * return inTheEnd();
11 + * });
12 + *
13 + * @date 2019-11-25
14 + */
15 +class Timer {
16 + /**
17 + * 延时操作
18 + * @returns {void}
19 + * @date 2019-11-25
20 + */
21 + timeout (interval, args) {
22 + return new Promise((resolve) => {
23 + setTimeout(() => {
24 + resolve(args)
25 + }, interval)
26 + })
27 + }
28 +
29 + /**
30 + * 等待代码片段执行完毕后再执行
31 + * @returns {void}
32 + * @date 2019-11-25
33 + */
34 + inTheEnd () {
35 + return this.timeout(0)
36 + }
37 +
38 + /**
39 + * 循环定时, 执行回调后再继续下一轮循环
40 + * @param {Number} interval 执行间隔
41 + * @param {Function} [callback] 回调
42 + * @returns {Object}
43 + * @date 2019-11-25
44 + */
45 + interval (interval, callback) {
46 + this.timeout(interval)
47 + .then(() => {
48 + typeof callback === 'function' &&
49 + callback() !== false &&
50 + this.interval(interval, callback)
51 + })
52 + return { then: c => callback = c }
53 + }
54 +
55 + /**
56 + * 计时,单位毫秒
57 + * @returns {void}
58 + * @date 2019-11-29
59 + */
60 + start () {
61 + const startDate = new Date()
62 + return {
63 + stop () {
64 + const stopDate = new Date()
65 + return stopDate.getTime() - startDate.getTime()
66 + }
67 + }
68 + }
69 +}
70 +
71 +export default new Timer()