Skip to content

Commit d355568

Browse files
authored
fix(build): handle preload treeshaking for braces (#17479)
1 parent 1f09344 commit d355568

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

packages/vite/src/node/plugins/importAnalysisBuild.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const preloadMarkerRE = new RegExp(preloadMarker, 'g')
4242
const dynamicImportPrefixRE = /import\s*\(/
4343

4444
const dynamicImportTreeshakenRE =
45-
/((?:\bconst\s+|\blet\s+|\bvar\s+|,\s*)(\{[^}.]+\})\s*=\s*await\s+import\([^)]+\))|(\(\s*await\s+import\([^)]+\)\s*\)(\??\.[\w$]+))|\bimport\([^)]+\)(\s*\.then\([^{]*?\(\s*\{([^}.]+)\})/g
45+
/((?:\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
4646

4747
function toRelativePath(filename: string, importer: string) {
4848
const relPath = path.posix.relative(path.posix.dirname(importer), filename)
@@ -285,7 +285,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
285285

286286
/* handle `import('foo').then(({foo})=>{})`
287287
*
288-
* match[5]: `.then(({foo}`
288+
* match[5]: `.then(({foo})`
289289
* match[6]: `foo`
290290
* import end: `import('foo').`
291291
* ^

playground/dynamic-import/nested/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,15 @@ import(`../nested/nested/${base}.js`).then((mod) => {
171171
const default2 = (await import('./treeshaken/syntax.js')).default,
172172
other = () => {}
173173
const foo = await import('./treeshaken/syntax.js').then((mod) => mod.foo)
174+
const foo2 = await import('./treeshaken/syntax.js').then(
175+
({ foo = {} }) => foo,
176+
)
177+
await import('./treeshaken/syntax.js').then((mod) => mod.foo({ foo }))
174178
default1()
175179
default2()
176180
other()
177181
foo()
182+
foo2()
178183
})()
179184

180185
import(`../nested/static.js`).then((mod) => {

0 commit comments

Comments
 (0)