From 24bedc5e56e76f8560e727718773284f633475dc Mon Sep 17 00:00:00 2001 From: Michael Brevard Date: Fri, 15 Dec 2023 11:38:19 +0200 Subject: [PATCH] perf(schema): avoid duplicate `get` operations (#24734) --- packages/schema/src/config/common.ts | 58 +++++++++++++++++----------- packages/schema/src/config/vite.ts | 17 ++++---- 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/packages/schema/src/config/common.ts b/packages/schema/src/config/common.ts index 18c60764a0..3f6d39a0c5 100644 --- a/packages/schema/src/config/common.ts +++ b/packages/schema/src/config/common.ts @@ -50,7 +50,10 @@ export default defineUntypedSchema({ * It is normally not needed to configure this option. */ workspaceDir: { - $resolve: async (val, get) => val ? resolve(await get('rootDir'), val) : await findWorkspaceDir(await get('rootDir')).catch(() => get('rootDir')) + $resolve: async (val, get) => { + const rootDir = await get('rootDir') + return val ? resolve(rootDir, val) : await findWorkspaceDir(rootDir).catch(() => rootDir) + } }, /** @@ -305,15 +308,21 @@ export default defineUntypedSchema({ * @type {Record} */ alias: { - $resolve: async (val, get) => ({ - '~': await get('srcDir'), - '@': await get('srcDir'), - '~~': await get('rootDir'), - '@@': await get('rootDir'), - [await get('dir.assets')]: join(await get('srcDir'), await get('dir.assets')), - [await get('dir.public')]: join(await get('srcDir'), await get('dir.public')), - ...val - }) + $resolve: async (val, get) => { + const srcDir = await get('srcDir') + const rootDir = await get('rootDir') + const dirAssets = await get('dir.assets') + const dirPublic = await get('dir.public') + return { + '~': srcDir, + '@': srcDir, + '~~': rootDir, + '@@': rootDir, + [dirAssets]: join(srcDir, dirAssets), + [dirPublic]: join(srcDir, dirPublic), + ...val + } + } }, /** @@ -342,15 +351,19 @@ export default defineUntypedSchema({ * inside the `ignore` array will be ignored in building. */ ignore: { - $resolve: async (val, get) => [ - '**/*.stories.{js,cts,mts,ts,jsx,tsx}', // ignore storybook files - '**/*.{spec,test}.{js,cts,mts,ts,jsx,tsx}', // ignore tests - '**/*.d.{cts,mts,ts}', // ignore type declarations - '**/.{pnpm-store,vercel,netlify,output,git,cache,data}', - relative(await get('rootDir'), await get('analyzeDir')), - relative(await get('rootDir'), await get('buildDir')), - await get('ignorePrefix') && `**/${await get('ignorePrefix')}*.*` - ].concat(val).filter(Boolean) + $resolve: async (val, get) => { + const rootDir = await get('rootDir') + const ignorePrefix = await get('ignorePrefix') + return [ + '**/*.stories.{js,cts,mts,ts,jsx,tsx}', // ignore storybook files + '**/*.{spec,test}.{js,cts,mts,ts,jsx,tsx}', // ignore tests + '**/*.d.{cts,mts,ts}', // ignore type declarations + '**/.{pnpm-store,vercel,netlify,output,git,cache,data}', + relative(rootDir, await get('analyzeDir')), + relative(rootDir, await get('buildDir')), + ignorePrefix && `**/${ignorePrefix}*.*` + ].concat(val).filter(Boolean) + } }, /** @@ -444,13 +457,14 @@ export default defineUntypedSchema({ */ runtimeConfig: { $resolve: async (val: RuntimeConfig, get) => { + const app = await get('app') provideFallbackValues(val) return defu(val, { public: {}, app: { - baseURL: (await get('app')).baseURL, - buildAssetsDir: (await get('app')).buildAssetsDir, - cdnURL: (await get('app')).cdnURL + baseURL: app.baseURL, + buildAssetsDir: app.buildAssetsDir, + cdnURL: app.cdnURL } }) } diff --git a/packages/schema/src/config/vite.ts b/packages/schema/src/config/vite.ts index 9b46437e84..e42fdce755 100644 --- a/packages/schema/src/config/vite.ts +++ b/packages/schema/src/config/vite.ts @@ -20,13 +20,16 @@ export default defineUntypedSchema({ $resolve: async (val, get) => val ?? (await get('dev') ? 'development' : 'production') }, define: { - $resolve: async (val, get) => ({ - 'process.dev': await get('dev'), - 'import.meta.dev': await get('dev'), - 'process.test': isTest, - 'import.meta.test': isTest, - ...val - }) + $resolve: async (val, get) => { + const dev = await get('dev') + return { + 'process.dev': dev, + 'import.meta.dev': dev, + 'process.test': isTest, + 'import.meta.test': isTest, + ...val + } + } }, resolve: { extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']