From 1a6909d9fcf0406a026519db239276710907b58c Mon Sep 17 00:00:00 2001 From: bluwy Date: Mon, 15 Jul 2024 20:44:13 +0800 Subject: [PATCH] fix(build): skip preload treeshaking for nested braces --- packages/vite/src/node/plugins/importAnalysisBuild.ts | 2 +- playground/dynamic-import/nested/index.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index 9af262a70544a9..99407359107a6d 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*(?:function\s*)?\(\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) diff --git a/playground/dynamic-import/nested/index.js b/playground/dynamic-import/nested/index.js index 478f98666c83b6..260f765787bdf3 100644 --- a/playground/dynamic-import/nested/index.js +++ b/playground/dynamic-import/nested/index.js @@ -175,11 +175,21 @@ import(`../nested/nested/${base}.js`).then((mod) => { ({ foo = {} }) => foo, ) await import('./treeshaken/syntax.js').then((mod) => mod.foo({ foo })) + const obj = [ + '', + { + async lazy() { + const { foo } = await import('./treeshaken/treeshaken.js') + return { foo: aaa(foo) } + }, + }, + ] default1() default2() other() foo() foo2() + obj[1].lazy() })() import(`../nested/static.js`).then((mod) => {