Skip to content

Commit

Permalink
fix(build): handle preload treeshaking for braces (#17479)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Jun 14, 2024
1 parent 1f09344 commit d355568
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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').`
* ^
Expand Down
5 changes: 5 additions & 0 deletions playground/dynamic-import/nested/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit d355568

Please sign in to comment.