Commit bd7b53f14adc05fd3d4af5027b5fb85015021ac9

Authored by vben
1 parent e6db0d39

fix(build): fix environment variable configuration file failure

.env.development
  1 +VITE_PORT = 3100
  2 +
1 # Whether to open mock 3 # Whether to open mock
2 VITE_USE_MOCK = true 4 VITE_USE_MOCK = true
3 5
.env.production
@@ -8,7 +8,7 @@ VITE_PUBLIC_PATH = ./ @@ -8,7 +8,7 @@ VITE_PUBLIC_PATH = ./
8 VITE_DROP_CONSOLE = true 8 VITE_DROP_CONSOLE = true
9 9
10 # Whether to output gz file for packaging 10 # Whether to output gz file for packaging
11 -VITE_BUILD_GZIP = false 11 +VITE_BUILD_GZIP = true
12 12
13 # Basic interface address SPA 13 # Basic interface address SPA
14 VITE_GLOB_API_URL=/api 14 VITE_GLOB_API_URL=/api
CHANGELOG.zh_CN.md
@@ -5,13 +5,10 @@ @@ -5,13 +5,10 @@
5 - 新增 `v-ripple`水波纹指令 5 - 新增 `v-ripple`水波纹指令
6 - 新增左侧菜单混合模式 6 - 新增左侧菜单混合模式
7 7
8 -### ✨ Refactor  
9 -  
10 -- 移除折叠显示菜单名配置  
11 -  
12 ### 🐛 Bug Fixes 8 ### 🐛 Bug Fixes
13 9
14 - 修复混合模式下滚动条丢失问题 10 - 修复混合模式下滚动条丢失问题
  11 +- 修复环境变量配置失效以及 history 模式下 logo 地址问题
15 12
16 ## 2.0.0-rc.14 (2020-12-15) 13 ## 2.0.0-rc.14 (2020-12-15)
17 14
build/script/buildConf.ts
@@ -4,7 +4,6 @@ @@ -4,7 +4,6 @@
4 import { GLOB_CONFIG_FILE_NAME } from '../constant'; 4 import { GLOB_CONFIG_FILE_NAME } from '../constant';
5 import fs, { writeFileSync } from 'fs-extra'; 5 import fs, { writeFileSync } from 'fs-extra';
6 6
7 -import viteConfig from '../../vite.config';  
8 import { errorConsole, successConsole, getCwdPath, getEnvConfig } from '../utils'; 7 import { errorConsole, successConsole, getCwdPath, getEnvConfig } from '../utils';
9 import { getShortName } from '../getShortName'; 8 import { getShortName } from '../getShortName';
10 9
@@ -17,7 +16,7 @@ function createConfig( @@ -17,7 +16,7 @@ function createConfig(
17 ) { 16 ) {
18 try { 17 try {
19 const windowConf = `window.${configName}`; 18 const windowConf = `window.${configName}`;
20 - const outDir = viteConfig.outDir || 'dist'; 19 + const outDir = 'dist';
21 // Ensure that the variable will not be modified 20 // Ensure that the variable will not be modified
22 const configStr = `${windowConf}=${JSON.stringify(config)}; 21 const configStr = `${windowConf}=${JSON.stringify(config)};
23 22
build/script/postBuild.ts
1 // #!/usr/bin/env node 1 // #!/usr/bin/env node
2 2
3 -import { sh } from 'tasksfile';  
4 -  
5 import { argv } from 'yargs'; 3 import { argv } from 'yargs';
6 import { runBuildConfig } from './buildConf'; 4 import { runBuildConfig } from './buildConf';
7 -// import { runUpdateHtml } from './updateHtml';  
8 import { errorConsole, successConsole } from '../utils'; 5 import { errorConsole, successConsole } from '../utils';
9 import { startGzipStyle } from '../vite/plugin/gzip/compress'; 6 import { startGzipStyle } from '../vite/plugin/gzip/compress';
10 7
11 -export const runBuild = async (preview = false) => { 8 +export const runBuild = async () => {
12 try { 9 try {
13 const argvList = argv._; 10 const argvList = argv._;
14 - if (preview) {  
15 - let cmd = `cross-env NODE_ENV=production vite build`;  
16 - await sh(cmd, {  
17 - async: true,  
18 - nopipe: true,  
19 - });  
20 - }  
21 11
22 // Generate configuration file 12 // Generate configuration file
23 if (!argvList.includes('no-conf')) { 13 if (!argvList.includes('no-conf')) {
24 await runBuildConfig(); 14 await runBuildConfig();
25 } 15 }
26 // await runUpdateHtml(); 16 // await runUpdateHtml();
27 - if (!preview) {  
28 - await startGzipStyle();  
29 - } 17 + await startGzipStyle();
30 successConsole('Vite Build successfully!'); 18 successConsole('Vite Build successfully!');
31 } catch (error) { 19 } catch (error) {
32 errorConsole('Vite Build Error\n' + error); 20 errorConsole('Vite Build Error\n' + error);
build/script/preserve.ts deleted 100644 → 0
1 -// Do you need to update the dependencies to prevent package.json from updating the dependencies, and no install after others get the code  
2 -  
3 -import path from 'path';  
4 -import fs from 'fs-extra';  
5 -import { isEqual } from 'lodash';  
6 -import { sh } from 'tasksfile';  
7 -import { successConsole, errorConsole } from '../utils';  
8 -  
9 -const resolve = (dir: string) => {  
10 - return path.resolve(process.cwd(), dir);  
11 -};  
12 -  
13 -const reg = /[\u4E00-\u9FA5\uF900-\uFA2D]/;  
14 -  
15 -let NEED_INSTALL = false;  
16 -  
17 -export async function runPreserve() {  
18 - // rc.6 fixed  
19 - const cwdPath = process.cwd();  
20 - if (reg.test(cwdPath)) {  
21 - errorConsole(  
22 - 'Do not include Chinese, Japanese or Korean in the full path of the project directory, please modify the directory name and run again!'  
23 - );  
24 - errorConsole('项目目录全路径请勿包含中文、日文、韩文,请修改目录名后再次重新运行!');  
25 - process.exit(1);  
26 - }  
27 -  
28 - await fs.mkdirp(resolve('build/.cache'));  
29 - function checkPkgUpdate() {  
30 - const pkg = require('../../package.json');  
31 - const { dependencies, devDependencies } = pkg;  
32 - const depsFile = resolve('build/.cache/deps.json');  
33 - if (!fs.pathExistsSync(depsFile)) {  
34 - NEED_INSTALL = true;  
35 - return;  
36 - }  
37 - const depsJson = require('../.cache/deps.json');  
38 -  
39 - if (!isEqual(depsJson, { dependencies, devDependencies })) {  
40 - NEED_INSTALL = true;  
41 - }  
42 - }  
43 - checkPkgUpdate();  
44 - if (NEED_INSTALL) {  
45 - // no error  
46 - successConsole(  
47 - 'A dependency change is detected, and the dependency is being installed to ensure that the dependency is consistent! (Tip: The project will be executed for the first time)!'  
48 - );  
49 - try {  
50 - await sh('npm run bootstrap ', {  
51 - async: true,  
52 - nopipe: true,  
53 - });  
54 -  
55 - successConsole('Dependency installation is successful, start running the project!');  
56 -  
57 - const pkg = require('../../package.json');  
58 - const { dependencies, devDependencies } = pkg;  
59 - const depsFile = resolve('build/.cache/deps.json');  
60 - const deps = { dependencies, devDependencies };  
61 - if (!fs.pathExistsSync(depsFile)) {  
62 - fs.writeFileSync(depsFile, JSON.stringify(deps));  
63 - } else {  
64 - const depsFile = resolve('build/.cache/deps.json');  
65 - const depsJson = require('../.cache/deps.json');  
66 - if (!isEqual(depsJson, deps)) {  
67 - fs.writeFileSync(depsFile, JSON.stringify(deps));  
68 - }  
69 - }  
70 - } catch (error) {}  
71 - }  
72 -}  
73 -  
74 -runPreserve();  
build/script/preview.ts
@@ -4,12 +4,7 @@ import Koa from 'koa'; @@ -4,12 +4,7 @@ import Koa from 'koa';
4 import staticServer from 'koa-static'; 4 import staticServer from 'koa-static';
5 import portfinder from 'portfinder'; 5 import portfinder from 'portfinder';
6 import { resolve } from 'path'; 6 import { resolve } from 'path';
7 -import viteConfig from '../../vite.config';  
8 import { getIPAddress } from '../utils'; 7 import { getIPAddress } from '../utils';
9 -// import { runBuild } from './postBuild';  
10 -  
11 -// const BUILD = 1;  
12 -// const NO_BUILD = 2;  
13 8
14 // start server 9 // start server
15 const startApp = () => { 10 const startApp = () => {
@@ -17,7 +12,7 @@ const startApp = () => { @@ -17,7 +12,7 @@ const startApp = () => {
17 portfinder.basePort = port; 12 portfinder.basePort = port;
18 const app = new Koa(); 13 const app = new Koa();
19 14
20 - app.use(staticServer(resolve(process.cwd(), viteConfig.outDir || 'dist'))); 15 + app.use(staticServer(resolve(process.cwd(), 'dist')));
21 16
22 portfinder.getPort(async (err, port) => { 17 portfinder.getPort(async (err, port) => {
23 if (err) { 18 if (err) {
@@ -35,25 +30,4 @@ const startApp = () => { @@ -35,25 +30,4 @@ const startApp = () => {
35 }); 30 });
36 }; 31 };
37 32
38 -// export const runPreview = async () => {  
39 -// // const prompt = inquirer.prompt({  
40 -// // type: 'list',  
41 -// // message: 'Please select a preview method',  
42 -// // name: 'type',  
43 -// // choices: [  
44 -// // {  
45 -// // name: 'Preview after packaging',  
46 -// // value: BUILD,  
47 -// // },  
48 -// // {  
49 -// // name: `No packaging, preview directly (need to have dist file after packaging)`,  
50 -// // value: NO_BUILD,  
51 -// // },  
52 -// // ],  
53 -// // });  
54 -// const { type } = await prompt;  
55 -// if (type === BUILD) {  
56 -// await runBuild(true);  
57 -// }  
58 -// };  
59 startApp(); 33 startApp();
build/utils.ts
@@ -63,12 +63,12 @@ export function getIPAddress() { @@ -63,12 +63,12 @@ export function getIPAddress() {
63 return ''; 63 return '';
64 } 64 }
65 65
66 -export function isDevFn(): boolean {  
67 - return process.env.NODE_ENV === 'development'; 66 +export function isDevFn(mode: 'development' | 'production'): boolean {
  67 + return mode === 'development';
68 } 68 }
69 69
70 -export function isProdFn(): boolean {  
71 - return process.env.NODE_ENV === 'production'; 70 +export function isProdFn(mode: 'development' | 'production'): boolean {
  71 + return mode === 'production';
72 } 72 }
73 73
74 /** 74 /**
@@ -106,18 +106,11 @@ export interface ViteEnv { @@ -106,18 +106,11 @@ export interface ViteEnv {
106 } 106 }
107 107
108 // Read all environment variable configuration files to process.env 108 // Read all environment variable configuration files to process.env
109 -export function loadEnv(): ViteEnv {  
110 - const env = process.env.NODE_ENV; 109 +export function wrapperEnv(envConf: any): ViteEnv {
111 const ret: any = {}; 110 const ret: any = {};
112 - const envList = [`.env.${env}.local`, `.env.${env}`, '.env.local', '.env', ,];  
113 - envList.forEach((e) => {  
114 - dotenv.config({  
115 - path: e,  
116 - });  
117 - });  
118 111
119 - for (const envName of Object.keys(process.env)) {  
120 - let realName = (process.env as any)[envName].replace(/\\n/g, '\n'); 112 + for (const envName of Object.keys(envConf)) {
  113 + let realName = envConf[envName].replace(/\\n/g, '\n');
121 realName = realName === 'true' ? true : realName === 'false' ? false : realName; 114 realName = realName === 'true' ? true : realName === 'false' ? false : realName;
122 if (envName === 'VITE_PORT') { 115 if (envName === 'VITE_PORT') {
123 realName = Number(realName); 116 realName = Number(realName);
build/vite/plugin/gzip/compress.ts
1 import { gzip } from 'zlib'; 1 import { gzip } from 'zlib';
2 import { readFileSync, writeFileSync } from 'fs'; 2 import { readFileSync, writeFileSync } from 'fs';
3 import { GzipPluginOptions } from './types'; 3 import { GzipPluginOptions } from './types';
4 -import viteConfig from '../../../../vite.config';  
5 import { readAllFile, getCwdPath, isBuildGzip, isSiteMode } from '../../../utils'; 4 import { readAllFile, getCwdPath, isBuildGzip, isSiteMode } from '../../../utils';
6 5
7 export function startGzip( 6 export function startGzip(
@@ -22,8 +21,8 @@ export function startGzip( @@ -22,8 +21,8 @@ export function startGzip(
22 // 手动压缩css 21 // 手动压缩css
23 export async function startGzipStyle() { 22 export async function startGzipStyle() {
24 if (isBuildGzip() || isSiteMode()) { 23 if (isBuildGzip() || isSiteMode()) {
25 - const outDir = viteConfig.outDir || 'dist';  
26 - const assets = viteConfig.assetsDir || '_assets'; 24 + const outDir = 'dist';
  25 + const assets = '_assets';
27 const allCssFile = readAllFile(getCwdPath(outDir, assets), /\.(css)$/); 26 const allCssFile = readAllFile(getCwdPath(outDir, assets), /\.(css)$/);
28 for (const path of allCssFile) { 27 for (const path of allCssFile) {
29 const source = readFileSync(path); 28 const source = readFileSync(path);
build/vite/plugin/html.ts
@@ -7,16 +7,21 @@ import { hmScript } from '../hm'; @@ -7,16 +7,21 @@ import { hmScript } from '../hm';
7 import pkg from '../../../package.json'; 7 import pkg from '../../../package.json';
8 import { GLOB_CONFIG_FILE_NAME } from '../../constant'; 8 import { GLOB_CONFIG_FILE_NAME } from '../../constant';
9 9
10 -export function setupHtmlPlugin(plugins: Plugin[], env: ViteEnv) { 10 +export function setupHtmlPlugin(
  11 + plugins: Plugin[],
  12 + env: ViteEnv,
  13 + mode: 'development' | 'production'
  14 +) {
11 const { VITE_GLOB_APP_TITLE, VITE_PUBLIC_PATH } = env; 15 const { VITE_GLOB_APP_TITLE, VITE_PUBLIC_PATH } = env;
12 16
13 const htmlPlugin = ViteHtmlPlugin({ 17 const htmlPlugin = ViteHtmlPlugin({
14 // html title 18 // html title
15 title: VITE_GLOB_APP_TITLE, 19 title: VITE_GLOB_APP_TITLE,
16 - minify: isProdFn(), 20 + minify: isProdFn(mode),
17 options: { 21 options: {
  22 + publicPath: VITE_PUBLIC_PATH,
18 // Package and insert additional configuration files 23 // Package and insert additional configuration files
19 - injectConfig: isProdFn() 24 + injectConfig: isProdFn(mode)
20 ? `<script src='${VITE_PUBLIC_PATH || './'}${GLOB_CONFIG_FILE_NAME}?v=${ 25 ? `<script src='${VITE_PUBLIC_PATH || './'}${GLOB_CONFIG_FILE_NAME}?v=${
21 pkg.version 26 pkg.version
22 }-${new Date().getTime()}'></script>` 27 }-${new Date().getTime()}'></script>`
build/vite/plugin/index.ts
@@ -8,21 +8,21 @@ import gzipPlugin from &#39;./gzip/index&#39;; @@ -8,21 +8,21 @@ import gzipPlugin from &#39;./gzip/index&#39;;
8 8
9 // @ts-ignore 9 // @ts-ignore
10 import pkg from '../../../package.json'; 10 import pkg from '../../../package.json';
11 -import { isProdFn, isSiteMode, ViteEnv, isReportMode, isBuildGzip } from '../../utils'; 11 +import { isSiteMode, ViteEnv, isReportMode, isBuildGzip } from '../../utils';
12 import { setupHtmlPlugin } from './html'; 12 import { setupHtmlPlugin } from './html';
13 import { setupPwaPlugin } from './pwa'; 13 import { setupPwaPlugin } from './pwa';
14 import { setupMockPlugin } from './mock'; 14 import { setupMockPlugin } from './mock';
15 15
16 // gen vite plugins 16 // gen vite plugins
17 -export function createVitePlugins(viteEnv: ViteEnv) { 17 +export function createVitePlugins(viteEnv: ViteEnv, mode: 'development' | 'production') {
18 const vitePlugins: VitePlugin[] = []; 18 const vitePlugins: VitePlugin[] = [];
19 19
20 // vite-plugin-html 20 // vite-plugin-html
21 - setupHtmlPlugin(vitePlugins, viteEnv); 21 + setupHtmlPlugin(vitePlugins, viteEnv, mode);
22 // vite-plugin-pwa 22 // vite-plugin-pwa
23 - setupPwaPlugin(vitePlugins, viteEnv); 23 + setupPwaPlugin(vitePlugins, viteEnv, mode);
24 // vite-plugin-mock 24 // vite-plugin-mock
25 - setupMockPlugin(vitePlugins, viteEnv); 25 + setupMockPlugin(vitePlugins, viteEnv, mode);
26 26
27 // vite-plugin-purge-icons 27 // vite-plugin-purge-icons
28 vitePlugins.push(PurgeIcons()); 28 vitePlugins.push(PurgeIcons());
@@ -34,12 +34,11 @@ export function createVitePlugins(viteEnv: ViteEnv) { @@ -34,12 +34,11 @@ export function createVitePlugins(viteEnv: ViteEnv) {
34 export function createRollupPlugin() { 34 export function createRollupPlugin() {
35 const rollupPlugins: rollupPlugin[] = []; 35 const rollupPlugins: rollupPlugin[] = [];
36 36
37 - if (!isProdFn() && isReportMode()) { 37 + if (isReportMode()) {
38 // rollup-plugin-visualizer 38 // rollup-plugin-visualizer
39 rollupPlugins.push(visualizer({ filename: './build/.cache/stats.html', open: true }) as Plugin); 39 rollupPlugins.push(visualizer({ filename: './build/.cache/stats.html', open: true }) as Plugin);
40 } 40 }
41 -  
42 - if (!isProdFn() && (isBuildGzip() || isSiteMode())) { 41 + if (isBuildGzip() || isSiteMode()) {
43 // rollup-plugin-gizp 42 // rollup-plugin-gizp
44 rollupPlugins.push(gzipPlugin()); 43 rollupPlugins.push(gzipPlugin());
45 } 44 }
build/vite/plugin/mock.ts
@@ -2,14 +2,22 @@ import { createMockServer } from &#39;vite-plugin-mock&#39;; @@ -2,14 +2,22 @@ import { createMockServer } from &#39;vite-plugin-mock&#39;;
2 import type { Plugin } from 'vite'; 2 import type { Plugin } from 'vite';
3 import { isDevFn, ViteEnv } from '../../utils'; 3 import { isDevFn, ViteEnv } from '../../utils';
4 4
5 -export function setupMockPlugin(plugins: Plugin[], env: ViteEnv) { 5 +export function setupMockPlugin(
  6 + plugins: Plugin[],
  7 + env: ViteEnv,
  8 + mode: 'development' | 'production'
  9 +) {
6 const { VITE_USE_MOCK } = env; 10 const { VITE_USE_MOCK } = env;
7 - const mockPlugin = createMockServer({  
8 - ignore: /^\_/,  
9 - mockPath: 'mock',  
10 - showTime: true,  
11 - });  
12 - if (isDevFn() && VITE_USE_MOCK) { 11 +
  12 + const useMock = isDevFn(mode) && VITE_USE_MOCK;
  13 +
  14 + if (useMock) {
  15 + const mockPlugin = createMockServer({
  16 + ignore: /^\_/,
  17 + mockPath: 'mock',
  18 + showTime: true,
  19 + localEnabled: useMock,
  20 + });
13 plugins.push(mockPlugin); 21 plugins.push(mockPlugin);
14 } 22 }
15 return plugins; 23 return plugins;
build/vite/plugin/pwa.ts
1 import { VitePWA } from 'vite-plugin-pwa'; 1 import { VitePWA } from 'vite-plugin-pwa';
2 import type { Plugin } from 'vite'; 2 import type { Plugin } from 'vite';
3 -import { isProdFn, ViteEnv } from '../../utils'; 3 +import { ViteEnv } from '../../utils';
4 4
5 -export function setupPwaPlugin(plugins: Plugin[], env: ViteEnv) { 5 +export function setupPwaPlugin(
  6 + plugins: Plugin[],
  7 + env: ViteEnv,
  8 + // @ts-ignore
  9 + mode: 'development' | 'production'
  10 +) {
6 const { VITE_USE_PWA } = env; 11 const { VITE_USE_PWA } = env;
7 12
8 const pwaPlugin = VitePWA({ 13 const pwaPlugin = VitePWA({
@@ -23,8 +28,7 @@ export function setupPwaPlugin(plugins: Plugin[], env: ViteEnv) { @@ -23,8 +28,7 @@ export function setupPwaPlugin(plugins: Plugin[], env: ViteEnv) {
23 ], 28 ],
24 }, 29 },
25 }); 30 });
26 -  
27 - if (isProdFn() && VITE_USE_PWA) { 31 + if (VITE_USE_PWA) {
28 plugins.push(pwaPlugin); 32 plugins.push(pwaPlugin);
29 } 33 }
30 return plugins; 34 return plugins;
build/vite/proxy.ts
@@ -2,21 +2,31 @@ type ProxyItem = [string, string]; @@ -2,21 +2,31 @@ type ProxyItem = [string, string];
2 2
3 type ProxyList = ProxyItem[]; 3 type ProxyList = ProxyItem[];
4 4
5 -const reg = /^https:\/\//; 5 +type ProxyTargetList = Record<
  6 + string,
  7 + {
  8 + target: string;
  9 + changeOrigin: boolean;
  10 + rewrite: (path: string) => any;
  11 + secure?: boolean;
  12 + }
  13 +>;
  14 +
  15 +const httpsRE = /^https:\/\//;
6 16
7 /** 17 /**
8 * Generate proxy 18 * Generate proxy
9 * @param list 19 * @param list
10 */ 20 */
11 export function createProxy(list: ProxyList = []) { 21 export function createProxy(list: ProxyList = []) {
12 - const ret: any = {}; 22 + const ret: ProxyTargetList = {};
13 for (const [prefix, target] of list) { 23 for (const [prefix, target] of list) {
14 - const isHttps = reg.test(target); 24 + const isHttps = httpsRE.test(target);
15 25
16 ret[prefix] = { 26 ret[prefix] = {
17 target: target, 27 target: target,
18 changeOrigin: true, 28 changeOrigin: true,
19 - rewrite: (path: string) => path.replace(new RegExp(`^${prefix}`), ''), 29 + rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''),
20 // https is require secure=false 30 // https is require secure=false
21 ...(isHttps ? { secure: false } : {}), 31 ...(isHttps ? { secure: false } : {}),
22 }; 32 };
index.html
@@ -137,7 +137,11 @@ @@ -137,7 +137,11 @@
137 </style> 137 </style>
138 <div class="app-loading"> 138 <div class="app-loading">
139 <div class="app-loading-wrap"> 139 <div class="app-loading-wrap">
140 - <img src="./resource/img/logo.png" class="app-loading-logo" alt="Logo" /> 140 + <img
  141 + src="<%= viteHtmlPluginOptions.publicPath %>resource/img/logo.png"
  142 + class="app-loading-logo"
  143 + alt="Logo"
  144 + />
141 <div class="app-loading-dots"> 145 <div class="app-loading-dots">
142 <span class="dot dot-spin"><i></i><i></i><i></i><i></i></span> 146 <span class="dot dot-spin"><i></i><i></i><i></i><i></i></span>
143 </div> 147 </div>
package.json
@@ -3,8 +3,8 @@ @@ -3,8 +3,8 @@
3 "version": "2.0.0-rc.14", 3 "version": "2.0.0-rc.14",
4 "scripts": { 4 "scripts": {
5 "bootstrap": "yarn install", 5 "bootstrap": "yarn install",
6 - "serve": "esno ./build/script/preserve.ts && cross-env NODE_ENV=development vite",  
7 - "build": "cross-env NODE_ENV=production vite build && esno ./build/script/postBuild.ts", 6 + "serve": "cross-env vite --mode=development",
  7 + "build": "cross-env vite build --mode=production && esno ./build/script/postBuild.ts",
8 "build:site": "cross-env SITE=true npm run build ", 8 "build:site": "cross-env SITE=true npm run build ",
9 "build:no-cache": "yarn clean:cache && npm run build", 9 "build:no-cache": "yarn clean:cache && npm run build",
10 "typecheck": "typecheck .", 10 "typecheck": "typecheck .",
src/main.ts
@@ -38,7 +38,7 @@ setupErrorHandle(app); @@ -38,7 +38,7 @@ setupErrorHandle(app);
38 38
39 // Mount when the route is ready 39 // Mount when the route is ready
40 router.isReady().then(() => { 40 router.isReady().then(() => {
41 - app.mount('#app'); 41 + app.mount('#app', true);
42 }); 42 });
43 43
44 // The development environment takes effect 44 // The development environment takes effect
src/router/index.ts
@@ -9,11 +9,9 @@ import { basicRoutes } from &#39;./routes/&#39;; @@ -9,11 +9,9 @@ import { basicRoutes } from &#39;./routes/&#39;;
9 import { scrollBehavior } from './scrollBehavior'; 9 import { scrollBehavior } from './scrollBehavior';
10 import { REDIRECT_NAME } from './constant'; 10 import { REDIRECT_NAME } from './constant';
11 11
12 -export const hashRouter = createWebHashHistory();  
13 -  
14 // app router 12 // app router
15 const router = createRouter({ 13 const router = createRouter({
16 - history: hashRouter, 14 + history: createWebHashHistory(),
17 routes: basicRoutes as RouteRecordRaw[], 15 routes: basicRoutes as RouteRecordRaw[],
18 strict: true, 16 strict: true,
19 scrollBehavior: scrollBehavior, 17 scrollBehavior: scrollBehavior,
vite.config.ts
1 import type { UserConfig, Resolver } from 'vite'; 1 import type { UserConfig, Resolver } from 'vite';
2 import { resolve } from 'path'; 2 import { resolve } from 'path';
3 3
  4 +import { loadEnv } from 'vite';
  5 +
4 import { modifyVars } from './build/config/lessModifyVars'; 6 import { modifyVars } from './build/config/lessModifyVars';
5 import { createProxy } from './build/vite/proxy'; 7 import { createProxy } from './build/vite/proxy';
6 import { configManualChunk } from './build/vite/optimizer'; 8 import { configManualChunk } from './build/vite/optimizer';
@@ -8,16 +10,12 @@ import { configManualChunk } from &#39;./build/vite/optimizer&#39;; @@ -8,16 +10,12 @@ import { configManualChunk } from &#39;./build/vite/optimizer&#39;;
8 import globbyTransform from './build/vite/plugin/transform/globby'; 10 import globbyTransform from './build/vite/plugin/transform/globby';
9 import dynamicImportTransform from './build/vite/plugin/transform/dynamic-import'; 11 import dynamicImportTransform from './build/vite/plugin/transform/dynamic-import';
10 12
11 -import { loadEnv } from './build/utils'; 13 +import { wrapperEnv } from './build/utils';
12 14
13 import { createRollupPlugin, createVitePlugins } from './build/vite/plugin'; 15 import { createRollupPlugin, createVitePlugins } from './build/vite/plugin';
14 16
15 const pkg = require('./package.json'); 17 const pkg = require('./package.json');
16 18
17 -const viteEnv = loadEnv();  
18 -  
19 -const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE, VITE_DYNAMIC_IMPORT } = viteEnv;  
20 -  
21 function pathResolve(dir: string) { 19 function pathResolve(dir: string) {
22 return resolve(__dirname, '.', dir); 20 return resolve(__dirname, '.', dir);
23 } 21 }
@@ -30,84 +28,93 @@ const root: string = process.cwd(); @@ -30,84 +28,93 @@ const root: string = process.cwd();
30 28
31 const resolvers: Resolver[] = []; 29 const resolvers: Resolver[] = [];
32 30
33 -const viteConfig: UserConfig = {  
34 - root,  
35 - alias,  
36 - /**  
37 - * port  
38 - * @default '3000'  
39 - */  
40 - port: VITE_PORT,  
41 -  
42 - /**  
43 - * Base public path when served in production.  
44 - * @default '/'  
45 - */  
46 - base: VITE_PUBLIC_PATH,  
47 -  
48 - /**  
49 - * Transpile target for esbuild.  
50 - * @default 'es2020'  
51 - */  
52 - esbuildTarget: 'es2019',  
53 -  
54 - // terser options  
55 - terserOptions: {  
56 - compress: {  
57 - keep_infinity: true,  
58 - drop_console: VITE_DROP_CONSOLE, 31 +export default (mode: 'development' | 'production'): UserConfig => {
  32 + const env = loadEnv(mode, root);
  33 + const viteEnv = wrapperEnv(env);
  34 + const {
  35 + VITE_PORT,
  36 + VITE_PUBLIC_PATH,
  37 + VITE_PROXY,
  38 + VITE_DROP_CONSOLE,
  39 + VITE_DYNAMIC_IMPORT,
  40 + } = viteEnv;
  41 + return {
  42 + root,
  43 + alias,
  44 + /**
  45 + * port
  46 + * @default '3000'
  47 + */
  48 + port: VITE_PORT,
  49 +
  50 + /**
  51 + * Base public path when served in production.
  52 + * @default '/'
  53 + */
  54 + base: VITE_PUBLIC_PATH,
  55 +
  56 + /**
  57 + * Transpile target for esbuild.
  58 + * @default 'es2020'
  59 + */
  60 + esbuildTarget: 'es2019',
  61 +
  62 + // terser options
  63 + terserOptions: {
  64 + compress: {
  65 + keep_infinity: true,
  66 + drop_console: VITE_DROP_CONSOLE,
  67 + },
  68 + },
  69 +
  70 + define: {
  71 + __VERSION__: pkg.version,
  72 + // setting vue-i18-next
  73 + // Suppress warning
  74 + __VUE_I18N_LEGACY_API__: false,
  75 + __VUE_I18N_FULL_INSTALL__: false,
  76 + __INTLIFY_PROD_DEVTOOLS__: false,
  77 + },
  78 +
  79 + cssPreprocessOptions: {
  80 + less: {
  81 + modifyVars: modifyVars,
  82 + javascriptEnabled: true,
  83 + },
59 }, 84 },
60 - },  
61 -  
62 - define: {  
63 - __VERSION__: pkg.version,  
64 - // setting vue-i18-next  
65 - // Suppress warning  
66 - __VUE_I18N_LEGACY_API__: false,  
67 - __VUE_I18N_FULL_INSTALL__: false,  
68 - __INTLIFY_PROD_DEVTOOLS__: false,  
69 - },  
70 -  
71 - cssPreprocessOptions: {  
72 - less: {  
73 - modifyVars: modifyVars,  
74 - javascriptEnabled: true, 85 +
  86 + // The package will be recompiled using rollup, and the new package compiled into the esm module specification will be put into node_modules/.vite_opt_cache
  87 + optimizeDeps: {
  88 + include: [
  89 + 'qs',
  90 + 'echarts/map/js/china',
  91 + 'ant-design-vue/es/locale/zh_CN',
  92 + 'ant-design-vue/es/locale/en_US',
  93 + '@ant-design/icons-vue',
  94 + ],
75 }, 95 },
76 - },  
77 -  
78 - // The package will be recompiled using rollup, and the new package compiled into the esm module specification will be put into node_modules/.vite_opt_cache  
79 - optimizeDeps: {  
80 - include: [  
81 - 'qs',  
82 - 'echarts/map/js/china',  
83 - 'ant-design-vue/es/locale/zh_CN',  
84 - 'ant-design-vue/es/locale/en_US',  
85 - '@ant-design/icons-vue', 96 +
  97 + transforms: [
  98 + globbyTransform({
  99 + resolvers: resolvers,
  100 + root: root,
  101 + alias: alias,
  102 + includes: [resolve('src/router'), resolve('src/locales')],
  103 + }),
  104 + dynamicImportTransform(VITE_DYNAMIC_IMPORT),
86 ], 105 ],
87 - },  
88 -  
89 - transforms: [  
90 - globbyTransform({  
91 - resolvers: resolvers,  
92 - root: root,  
93 - alias: alias,  
94 - includes: [resolve('src/router'), resolve('src/locales')],  
95 - }),  
96 - dynamicImportTransform(VITE_DYNAMIC_IMPORT),  
97 - ],  
98 -  
99 - proxy: createProxy(VITE_PROXY),  
100 -  
101 - plugins: createVitePlugins(viteEnv),  
102 -  
103 - rollupInputOptions: {  
104 - plugins: createRollupPlugin(),  
105 - },  
106 -  
107 - rollupOutputOptions: {  
108 - compact: true,  
109 - manualChunks: configManualChunk,  
110 - },  
111 -};  
112 106
113 -export default viteConfig; 107 + proxy: createProxy(VITE_PROXY),
  108 +
  109 + plugins: createVitePlugins(viteEnv, mode),
  110 +
  111 + rollupInputOptions: {
  112 + plugins: createRollupPlugin(),
  113 + },
  114 +
  115 + rollupOutputOptions: {
  116 + compact: true,
  117 + manualChunks: configManualChunk,
  118 + },
  119 + };
  120 +};