From 2f1fbf8e487fdae4db5c144d97f9d20e42bb1603 Mon Sep 17 00:00:00 2001 From: vben Date: Thu, 29 Oct 2020 23:01:11 +0800 Subject: [PATCH] style: add some notes --- .eslintignore | 3 --- build/constant.ts | 3 +++ build/getShortName.ts | 4 +++ build/script/buildConf.ts | 11 ++++---- build/utils.ts | 37 ++++++++++++++++++++++++++ build/vite/hm.ts | 4 +-- build/vite/plugin/gzip/index.ts | 1 + build/vite/proxy.ts | 6 +++++ mock/_createProductionServer.ts | 3 +++ src/App.vue | 6 +++++ src/main.ts | 4 +++ src/useApp.ts | 1 + src/utils/http/axios/const.ts | 1 + vite.config.ts | 46 ++++++++++++++++++++------------- 14 files changed, 101 insertions(+), 29 deletions(-) diff --git a/.eslintignore b/.eslintignore index 63fe48d1..11482780 100644 --- a/.eslintignore +++ b/.eslintignore @@ -20,9 +20,6 @@ lib Dockerfile vue.config.js commit-lint.js -/src/assets/iconfont/ -/types/shims -/src/types/shims postcss.config.js stylelint.config.js commitlint.config.js diff --git a/build/constant.ts b/build/constant.ts index 5e02f35c..b604acfb 100644 --- a/build/constant.ts +++ b/build/constant.ts @@ -1 +1,4 @@ +/** + * The name of the configuration file entered in the production environment + */ export const GLOB_CONFIG_FILE_NAME = '_app.config.js'; diff --git a/build/getShortName.ts b/build/getShortName.ts index 6a52fc34..b46205f0 100644 --- a/build/getShortName.ts +++ b/build/getShortName.ts @@ -1,3 +1,7 @@ +/** + * Get the configuration file variable name + * @param env + */ export const getShortName = (env: any) => { return `__PRODUCTION__${env.VITE_GLOB_APP_SHORT_NAME || '__APP'}__CONF__` .toUpperCase() diff --git a/build/script/buildConf.ts b/build/script/buildConf.ts index b80cafc0..15e54d75 100644 --- a/build/script/buildConf.ts +++ b/build/script/buildConf.ts @@ -1,14 +1,12 @@ +/** + * Generate additional configuration files when used for packaging. The file can be configured with some global variables, so that it can be changed directly externally without repackaging + */ import { GLOB_CONFIG_FILE_NAME } from '../constant'; import fs, { writeFileSync } from 'fs-extra'; import viteConfig from '../../vite.config'; import { errorConsole, successConsole, getCwdPath, getEnvConfig } from '../utils'; - -const getShortName = (env: any) => { - return `__PRODUCTION__${env.VITE_GLOB_APP_SHORT_NAME || '__APP'}__CONF__` - .toUpperCase() - .replace(/\s/g, ''); -}; +import { getShortName } from '../getShortName'; function createConfig( { @@ -20,6 +18,7 @@ function createConfig( try { const windowConf = `window.${configName}`; const outDir = viteConfig.outDir || 'dist'; + // Ensure that the variable will not be modified const configStr = `${windowConf}=${JSON.stringify(config)}; Object.freeze(${windowConf}); diff --git a/build/utils.ts b/build/utils.ts index 87dc1a0d..86afb4b3 100644 --- a/build/utils.ts +++ b/build/utils.ts @@ -7,6 +7,7 @@ import chalk from 'chalk'; export const isFunction = (arg: unknown): arg is (...args: any[]) => any => typeof arg === 'function'; + export const isRegExp = (arg: unknown): arg is RegExp => Object.prototype.toString.call(arg) === '[object RegExp]'; @@ -43,6 +44,9 @@ export function readAllFile(root: string, reg: RegExp) { return resultArr; } +/** + * get client ip address + */ export function getIPAddress() { let interfaces = networkInterfaces(); for (let devName in interfaces) { @@ -67,12 +71,23 @@ export function isProdFn(): boolean { return process.env.NODE_ENV === 'production'; } +/** + * Whether to generate package preview + */ export function isReportMode(): boolean { return process.env.REPORT === 'true'; } + +/** + * Whether to generate gzip for packaging + */ export function isBuildGzip(): boolean { return process.env.VITE_BUILD_GZIP === 'true'; } + +/** + * Whether to generate package site + */ export function isSiteMode(): boolean { return process.env.SITE === 'true'; } @@ -89,6 +104,7 @@ export interface ViteEnv { VITE_BUILD_GZIP: boolean; } +// Read all environment variable configuration files to process.env export function loadEnv(): ViteEnv { const env = process.env.NODE_ENV; const ret: any = {}; @@ -116,6 +132,11 @@ export function loadEnv(): ViteEnv { return ret; } +/** + * Get the environment variables starting with the specified prefix + * @param match prefix + * @param confFiles ext + */ export function getEnvConfig(match = 'VITE_GLOB_', confFiles = ['.env', '.env.production']) { let envConfig = {}; confFiles.forEach((item) => { @@ -142,18 +163,34 @@ function consoleFn(color: string, message: any) { ); } +/** + * warnConsole + * @param message + */ export function successConsole(message: any) { consoleFn('green', '✨ ' + message); } +/** + * warnConsole + * @param message + */ export function errorConsole(message: any) { consoleFn('red', '✨ ' + message); } +/** + * warnConsole + * @param message message + */ export function warnConsole(message: any) { consoleFn('yellow', '✨ ' + message); } +/** + * Get user root directory + * @param dir file path + */ export function getCwdPath(...dir: string[]) { return path.resolve(process.cwd(), ...dir); } diff --git a/build/vite/hm.ts b/build/vite/hm.ts index 2c3f3b6b..14abdd8b 100644 --- a/build/vite/hm.ts +++ b/build/vite/hm.ts @@ -1,5 +1,5 @@ -// 百度统计代码 用于站点部署 -// 只在build:site开启 +// Baidu statistics code for site deployment +// Only open in build:site export const hmScript = `