Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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