From 31e2715e674fc7d5144f39d8d5e595d904585af8 Mon Sep 17 00:00:00 2001 From: vben Date: Wed, 14 Oct 2020 21:08:07 +0800 Subject: [PATCH] chore: detail optimization --- build/script/updateHtml.ts | 8 +- build/transform/require-context/index.ts | 95 ++++++++++++++++++++++++ build/utils.ts | 20 ++--- src/components/CountTo/src/index.vue | 5 +- src/components/Menu/src/BasicMenu.tsx | 8 +- src/components/Menu/src/index.less | 34 ++++++++- src/design/var/index.less | 2 +- src/hooks/web/useTabs.ts | 15 ++-- src/layouts/default/index.tsx | 5 ++ src/layouts/default/multitabs/index.tsx | 9 +-- src/layouts/page/index.tsx | 3 +- src/router/guard/index.ts | 6 +- src/router/guard/pageLoadingGuard.ts | 12 ++- src/router/menus/index.ts | 3 +- src/router/menus/modules/demo/feat.ts | 14 ++++ src/router/routes/index.ts | 11 ++- src/router/routes/modules/demo/feat.ts | 8 ++ src/router/routes/modules/sys.ts | 12 --- src/router/types.d.ts | 1 + src/store/modules/tab.ts | 41 ++++++---- src/types/source.d.ts | 4 +- src/utils/eventHub.ts | 3 - src/utils/helper/menuHelper.ts | 4 +- src/utils/helper/routeHelper.ts | 27 ++++++- src/views/demo/feat/tab-params/index.vue | 17 +++++ src/views/sys/redirect/index.vue | 28 ++++--- vite.config.ts | 2 +- 27 files changed, 304 insertions(+), 93 deletions(-) create mode 100644 build/transform/require-context/index.ts delete mode 100644 src/router/routes/modules/sys.ts create mode 100644 src/views/demo/feat/tab-params/index.vue diff --git a/build/script/updateHtml.ts b/build/script/updateHtml.ts index f5de56a29..e13756763 100644 --- a/build/script/updateHtml.ts +++ b/build/script/updateHtml.ts @@ -74,9 +74,7 @@ function injectCdnjs(html: string) { export async function runUpdateHtml() { const outDir = viteConfig.outDir || 'dist'; const indexPath = getCwdPath(outDir, 'index.html'); - if (!existsSync(`${indexPath}`)) { - return; - } + if (!existsSync(indexPath)) return; try { let processedHtml = ''; const rawHtml = readFileSync(indexPath, 'utf-8'); @@ -92,7 +90,9 @@ export async function runUpdateHtml() { } if (minify) { const { enable, ...miniOpt } = minify; - processedHtml = HtmlMinifier.minify(processedHtml, miniOpt); + if (enable) { + processedHtml = HtmlMinifier.minify(processedHtml, miniOpt); + } } writeFileSync(indexPath, processedHtml); diff --git a/build/transform/require-context/index.ts b/build/transform/require-context/index.ts new file mode 100644 index 000000000..0800a3bac --- /dev/null +++ b/build/transform/require-context/index.ts @@ -0,0 +1,95 @@ +// https://github.com/luxueyan/vite-transform-globby-import/blob/master/src/index.ts + +// TODO 目前还不能监听文件新增及删除 内容已经改变,缓存问题? +// 可以使用,先不打算集成 +import { join } from 'path'; +import { lstatSync } from 'fs'; +import glob from 'glob'; +import { createResolver, Resolver } from 'vite/dist/node/resolver.js'; +import { Transform } from 'vite/dist/node/transform.js'; + +const modulesDir: string = join(process.cwd(), '/node_modules/'); + +interface SharedConfig { + root?: string; + alias?: Record; + resolvers?: Resolver[]; +} + +function template(template: string) { + return (data: { [x: string]: any }) => { + return template.replace(/#([^#]+)#/g, (_, g1) => data[g1] || g1); + }; +} + +const globbyTransform = function (config: SharedConfig): Transform { + const resolver = createResolver( + config.root || process.cwd(), + config.resolvers || [], + config.alias || {} + ); + const cache = new Map(); + + const urlMap = new Map(); + return { + test({ path }) { + const filePath = path.replace('\u0000', ''); // why some path startsWith '\u0000'? + try { + return ( + !filePath.startsWith(modulesDir) && + /\.(vue|js|jsx|ts|tsx)$/.test(filePath) && + lstatSync(filePath).isFile() + ); + } catch { + return false; + } + }, + transform({ code, path, isBuild }) { + let result = cache.get(path); + if (!result) { + const reg = /import\s+([\w\s{}*]+)\s+from\s+(['"])globby(\?path)?!([^'"]+)\2/g; + const lastImport = urlMap.get(path); + const match = code.match(reg); + if (lastImport && match) { + code = code.replace(lastImport, match[0]); + } + result = code.replace(reg, (_, g1, g2, g3, g4) => { + const filePath = path.replace('\u0000', ''); // why some path startsWith '\u0000'? + // resolve path + const resolvedFilePath = g4.startsWith('.') + ? resolver.resolveRelativeRequest(filePath, g4) + : { pathname: resolver.requestToFile(g4) }; + const files = glob.sync(resolvedFilePath.pathname, { dot: true }); + let templateStr = 'import #name# from #file#'; // import default + let name = g1; + const m = g1.match(/\{\s*(\w+)(\s+as\s+(\w+))?\s*\}/); // import module + const m2 = g1.match(/\*\s+as\s+(\w+)/); // import * as all module + if (m) { + templateStr = `import { ${m[1]} as #name# } from #file#`; + name = m[3] || m[1]; + } else if (m2) { + templateStr = 'import * as #name# from #file#'; + name = m2[1]; + } + const temRender = template(templateStr); + + const groups: Array[] = []; + const replaceFiles = files.map((f, i) => { + const file = g2 + resolver.fileToRequest(f) + g2; + groups.push([name + i, file]); + return temRender({ name: name + i, file }); + }); + urlMap.set(path, replaceFiles.join('\n')); + return ( + replaceFiles.join('\n') + + (g3 ? '\n' + groups.map((v) => `${v[0]}._path = ${v[1]}`).join('\n') : '') + + `\nconst ${name} = { ${groups.map((v) => v[0]).join(',')} }\n` + ); + }); + if (isBuild) cache.set(path, result); + } + return result; + }, + }; +}; +export default globbyTransform; diff --git a/build/utils.ts b/build/utils.ts index 9c44a0e4a..11d164417 100644 --- a/build/utils.ts +++ b/build/utils.ts @@ -124,28 +124,24 @@ export function getEnvConfig(match = 'VITE_GLOB_', confFiles = ['.env', '.env.pr return envConfig; } -export function successConsole(message: any) { +function consoleFn(color: string, message: any) { console.log( chalk.blue.bold('**************** ') + - chalk.green.bold('✨ ' + message) + + (chalk as any)[color].bold(message) + chalk.blue.bold(' ****************') ); } +export function successConsole(message: any) { + consoleFn('green', '✨ ' + message); +} + export function errorConsole(message: any) { - console.log( - chalk.blue.bold('**************** ') + - chalk.red.bold('✨ ' + message) + - chalk.blue.bold(' ****************') - ); + consoleFn('red', '✨ ' + message); } export function warnConsole(message: any) { - console.log( - chalk.blue.bold('**************** ') + - chalk.yellow.bold('✨ ' + message) + - chalk.blue.bold(' ****************') - ); + consoleFn('yellow', '✨ ' + message); } export function getCwdPath(...dir: string[]) { diff --git a/src/components/CountTo/src/index.vue b/src/components/CountTo/src/index.vue index 55f8a1c31..bd95b9a94 100644 --- a/src/components/CountTo/src/index.vue +++ b/src/components/CountTo/src/index.vue @@ -5,7 +5,6 @@ diff --git a/src/views/sys/redirect/index.vue b/src/views/sys/redirect/index.vue index 099df59e6..58e316dc4 100644 --- a/src/views/sys/redirect/index.vue +++ b/src/views/sys/redirect/index.vue @@ -2,7 +2,7 @@