From 55b79ce1092973318655d51e9accf624c869c403 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Wed, 24 Apr 2024 20:01:27 +0800 Subject: [PATCH 1/3] chore: dynamic import variable failure error message display pattern --- .../src/node/plugins/dynamicImportVars.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/plugins/dynamicImportVars.ts b/packages/vite/src/node/plugins/dynamicImportVars.ts index a92992800a7473..51efcb95c2ef37 100644 --- a/packages/vite/src/node/plugins/dynamicImportVars.ts +++ b/packages/vite/src/node/plugins/dynamicImportVars.ts @@ -39,14 +39,25 @@ interface DynamicImportPattern { rawPattern: string } -const dynamicImportHelper = (glob: Record, path: string) => { +const dynamicImportHelper = ( + glob: Record, + path: string, + pattern: string, +) => { const v = glob[path] if (v) { return typeof v === 'function' ? v() : Promise.resolve(v) } return new Promise((_, reject) => { ;(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)( - reject.bind(null, new Error('Unknown variable dynamic import: ' + path)), + reject.bind( + null, + new Error( + 'Unknown variable dynamic import: ' + + path + + `. It seems that the corresponding file was not imported by import.meta.global("${pattern}").`, + ), + ), ) }) } @@ -240,13 +251,13 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin { continue } - const { rawPattern, glob } = result + const { rawPattern, glob, pattern } = result needDynamicImportHelper = true s.overwrite( expStart, expEnd, - `__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`)`, + `__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`, \`${pattern}\`)`, ) } From b70a682b7b25c534200490c9e9f044699a5a003c Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Tue, 30 Apr 2024 10:32:56 +0800 Subject: [PATCH 2/3] chore: update --- .../vite/src/node/plugins/dynamicImportVars.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/plugins/dynamicImportVars.ts b/packages/vite/src/node/plugins/dynamicImportVars.ts index 51efcb95c2ef37..b6c8514f59fa4a 100644 --- a/packages/vite/src/node/plugins/dynamicImportVars.ts +++ b/packages/vite/src/node/plugins/dynamicImportVars.ts @@ -42,12 +42,16 @@ interface DynamicImportPattern { const dynamicImportHelper = ( glob: Record, path: string, - pattern: string, + rawPath: string, ) => { const v = glob[path] if (v) { return typeof v === 'function' ? v() : Promise.resolve(v) } + let _path = path + rawPath.split(/\$\{.+\}/).forEach((str) => { + _path = _path.replace(str, '') + }) return new Promise((_, reject) => { ;(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)( reject.bind( @@ -55,7 +59,9 @@ const dynamicImportHelper = ( new Error( 'Unknown variable dynamic import: ' + path + - `. It seems that the corresponding file was not imported by import.meta.global("${pattern}").`, + (_path.includes('/') + ? '. Note that variables only represent file names one level deep.' + : ''), ), ), ) @@ -251,13 +257,13 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin { continue } - const { rawPattern, glob, pattern } = result + const { rawPattern, glob } = result needDynamicImportHelper = true s.overwrite( expStart, expEnd, - `__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`, \`${pattern}\`)`, + `__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`, "${rawPattern}")`, ) } From f7c8109978b368dda75f689f979556e84030fd45 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Tue, 30 Apr 2024 16:24:21 +0800 Subject: [PATCH 3/3] fix: update --- packages/vite/src/node/plugins/dynamicImportVars.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/packages/vite/src/node/plugins/dynamicImportVars.ts b/packages/vite/src/node/plugins/dynamicImportVars.ts index b6c8514f59fa4a..28dc07fe27e393 100644 --- a/packages/vite/src/node/plugins/dynamicImportVars.ts +++ b/packages/vite/src/node/plugins/dynamicImportVars.ts @@ -42,16 +42,12 @@ interface DynamicImportPattern { const dynamicImportHelper = ( glob: Record, path: string, - rawPath: string, + segs: number, ) => { const v = glob[path] if (v) { return typeof v === 'function' ? v() : Promise.resolve(v) } - let _path = path - rawPath.split(/\$\{.+\}/).forEach((str) => { - _path = _path.replace(str, '') - }) return new Promise((_, reject) => { ;(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)( reject.bind( @@ -59,7 +55,7 @@ const dynamicImportHelper = ( new Error( 'Unknown variable dynamic import: ' + path + - (_path.includes('/') + (path.split('/').length !== segs ? '. Note that variables only represent file names one level deep.' : ''), ), @@ -263,7 +259,7 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin { s.overwrite( expStart, expEnd, - `__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`, "${rawPattern}")`, + `__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`, ${rawPattern.split('/').length})`, ) }