diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index fcf92bebd8b10f..71f9b535e9061e 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -42,7 +42,7 @@ const preloadMarkerRE = new RegExp(preloadMarker, 'g') const dynamicImportPrefixRE = /import\s*\(/ const dynamicImportTreeshakenRE = - /((?:\bconst\s+|\blet\s+|\bvar\s+|,\s*)(\{[^}.]+\})\s*=\s*await\s+import\([^)]+\))|(\(\s*await\s+import\([^)]+\)\s*\)(\??\.[\w$]+))|\bimport\([^)]+\)(\s*\.then\([^{]*?\(\s*\{([^}.]+)\})/g + /((?:\bconst\s+|\blet\s+|\bvar\s+|,\s*)(\{[^}.]+\})\s*=\s*await\s+import\([^)]+\))|(\(\s*await\s+import\([^)]+\)\s*\)(\??\.[\w$]+))|\bimport\([^)]+\)(\s*\.then\(\s*(?:function\s*)?\(\s*\{([^}.]+)\}\))/g function toRelativePath(filename: string, importer: string) { const relPath = path.posix.relative(path.posix.dirname(importer), filename) @@ -285,7 +285,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { /* handle `import('foo').then(({foo})=>{})` * - * match[5]: `.then(({foo}` + * match[5]: `.then(({foo})` * match[6]: `foo` * import end: `import('foo').` * ^ diff --git a/playground/dynamic-import/nested/index.js b/playground/dynamic-import/nested/index.js index 8357cbc7afca42..478f98666c83b6 100644 --- a/playground/dynamic-import/nested/index.js +++ b/playground/dynamic-import/nested/index.js @@ -171,10 +171,15 @@ import(`../nested/nested/${base}.js`).then((mod) => { const default2 = (await import('./treeshaken/syntax.js')).default, other = () => {} const foo = await import('./treeshaken/syntax.js').then((mod) => mod.foo) + const foo2 = await import('./treeshaken/syntax.js').then( + ({ foo = {} }) => foo, + ) + await import('./treeshaken/syntax.js').then((mod) => mod.foo({ foo })) default1() default2() other() foo() + foo2() })() import(`../nested/static.js`).then((mod) => {