Browse Source

feat: 加入deadCode检测build时未使用/导出的文件

lanjianrong 4 years ago
parent
commit
0dc55a9512

+ 11 - 31
config/config.ts

@@ -26,11 +26,12 @@ export default defineConfig({
   srcTranspiler: 'esbuild',
   fastRefresh: true,
   unocss: {
-    watch: ['src/pages/**/*.{jsx,tsx,less}', 'src/components/**/*.{jsx,tsx,less}'] // 添加其他包含 unocss 的 classname 的文件目录
+    watch: ['src/{pages,components}/**/*.{jsx,tsx}'] // 添加其他包含 unocss 的 classname 的文件目录
   },
   // Fast Refresh 热更新
   theme: { 'primary-color': '#886ab5' },
   layout: {},
+  deadCode: {}, // 检测未使用的文件和导出,仅在 build 阶段开启
   locale: {
     default: 'zh-CN',
     antd: false,
@@ -67,35 +68,14 @@ export default defineConfig({
     'react-dom': 'ReactDOM'
   },
   chainWebpack(config, { env }) {
-    if (env === 'production') {
-      // config.merge({
-      //   optimization: {
-      //     minimize: true,
-      //     splitChunks: {
-      //       chunks: 'all',
-      //       minSize: 30000,
-      //       minChunks: 2,
-      //       automaticNameDelimiter: '.',
-      //       cacheGroups: {
-      //         vendor: {
-      //           name: 'vendors',
-      //           test: ({ resource }) => /[\\/]node_modules[\\/]/.test(resource),
-      //           chunks: 'all',
-      //           priority: 20
-      //         }
-      //       }
-      //     }
-      //   }
-      // })
-      config.plugin('compression-webpack-plugin').use(
-        new CompressionPlugin({
-          // filename: 文件名称,这里我们不设置,让它保持和未压缩的文件同一个名称
-          algorithm: 'gzip', // 指定生成gzip格式
-          test: new RegExp('\\.(' + prodGzipList.join('|') + ')$'), // 匹配哪些格式文件需要压缩
-          threshold: 10000, //对超过10k的数据进行压缩
-          minRatio: 0.6 // 压缩比例,值为0 ~ 1
-        })
-      )
-    }
+    config.plugin('compression-webpack-plugin').use(
+      new CompressionPlugin({
+        // filename: 文件名称,这里我们不设置,让它保持和未压缩的文件同一个名称
+        algorithm: 'gzip', // 指定生成gzip格式
+        test: new RegExp('\\.(' + prodGzipList.join('|') + ')$'), // 匹配哪些格式文件需要压缩
+        threshold: 10000, //对超过10k的数据进行压缩
+        minRatio: 0.6 // 压缩比例,值为0 ~ 1
+      })
+    )
   }
 })

+ 8 - 8
src/app.tsx

@@ -12,6 +12,7 @@ import ModalStore from './components/ModalStore'
 import MessageDetail from './pages/Hr/Notification/components/MessageDetail'
 import defaultSettings from '../config/defaultSettings'
 import type { RequestConfig, RunTimeLayoutConfig } from '@umijs/max'
+import { isString } from './utils/is'
 
 const loginPath = '/user/login'
 // 错误处理方案: 错误类型
@@ -187,14 +188,13 @@ export const request: RequestConfig = {
         notificationLimit = 0
       }
 
-      const { data, code: errorCode, msg: errorMessage, showType } = response.data
-      if (!errorCode || errorCode !== consts.RET_CODE.SUCCESS) {
-        if (errorCode !== consts.RET_CODE.SUCCESS) {
-          const error: any = new Error(errorMessage)
-          error.name = 'BizError'
-          error.info = { errorCode, errorMessage, showType, data }
-          errorHandler(error)
-        }
+      const { data, code: errorCode, msg, showType } = response.data
+      if (errorCode !== consts.RET_CODE.SUCCESS) {
+        const errorMessage = isString(msg) ? msg : JSON.stringify(msg)
+        const error: any = new Error(errorMessage)
+        error.name = 'BizError'
+        error.info = { errorCode, errorMessage, showType, data }
+        errorHandler(error)
       }
       return response
     }

+ 0 - 11
src/components/PageLoading/index.jsx

@@ -1,11 +0,0 @@
-// import { PageLoading } from '@ant-design/pro-layout' // loading components from code split
-import { Spin } from 'antd'
-// https://umijs.org/plugin/umi-plugin-react.html#dynamicimport
-import './index.less'
-
-const PageLoading = () => (
-  <div className="fixed top-1/2 left-1/2 flex justify-center aspin">
-    <Spin size="large" />
-  </div>
-)
-export default PageLoading

+ 0 - 3
src/components/PageLoading/index.less

@@ -1,3 +0,0 @@
-.aspin {
-  transform: translate(-50%, -50%);
-}

+ 5 - 0
src/global.less

@@ -99,6 +99,11 @@ ol {
 .vditor-reset ol {
   list-style-type: decimal !important;
 }
+
+.aspin {
+  transform: translate(-50%, -50%);
+}
+
 @media (max-width: @screen-xs) {
   .ant-table {
     width: 100%;

+ 8 - 0
src/loading.tsx

@@ -0,0 +1,8 @@
+import { Spin } from 'antd'
+
+const PageLoading = () => (
+  <div className="fixed top-1/2 aspin" style={{ left: '50%' }}>
+    <Spin size="large" />
+  </div>
+)
+export default PageLoading

+ 1 - 1
src/pages/Statistic/components/OptionSelect.tsx

@@ -6,7 +6,7 @@ import { useDebounceFn } from 'ahooks'
 import { Select, TreeSelect } from 'antd'
 import React, { useState, useEffect, useMemo } from 'react'
 
-export enum statisticOptionMap {
+enum statisticOptionMap {
   STAFF = 'staff',
   DEPARTMENT = 'department',
   SINGLE_STAFF = 'singleStaff',

+ 4 - 4
src/services/login.ts

@@ -8,10 +8,10 @@ export async function apiLogin(params) {
   return request('/login', { method: 'POST', data: params })
 }
 
-/** 未读新消息个数 */
-export async function queryUnreadNotice() {
-  return request('/message/read')
-}
+// /** 未读新消息个数 */
+// export async function queryUnreadNotice() {
+//   return request('/message/read')
+// }
 
 /** 获取消息列表 */
 export async function queryNoticeList(params) {

+ 0 - 5
src/services/user.ts

@@ -7,11 +7,6 @@ export async function queryCurrentUser() {
   return request('/login/staff')
 }
 
-/** 获取用户权限 */
-export async function queryRoles() {
-  return request('/login/roles')
-}
-
 /** 获取数据权限下拉菜单 */
 export async function queryPermData(params) {
   return request('/DataPermission/list', {