Skip to content

Commit 7b7410a

Browse files
authored
fix: handle dynamic import with .then(m => m.a) (#20117)
1 parent 9759c29 commit 7b7410a

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const preloadMarkerRE = new RegExp(preloadMarker, 'g')
4040
const dynamicImportPrefixRE = /import\s*\(/
4141

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

4545
function toRelativePath(filename: string, importer: string) {
4646
const relPath = path.posix.relative(path.posix.dirname(importer), filename)

playground/dynamic-import/__tests__/dynamic-import.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ test('dynamic import treeshaken log', async () => {
184184
expect(log).toContain('treeshaken baz3')
185185
expect(log).toContain('treeshaken baz4')
186186
expect(log).toContain('treeshaken baz5')
187+
expect(log).toContain('treeshaken baz6')
187188
expect(log).toContain('treeshaken default')
188189

189190
expect(log).not.toContain('treeshaken removed')

playground/dynamic-import/nested/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ import(`../nested/nested/${base}.js`).then((mod) => {
174174
const foo2 = await import('./treeshaken/syntax.js').then(
175175
({ foo = {} }) => foo,
176176
)
177+
const foo3 = await import('./treeshaken/syntax.js').then((m) => m.foo)
178+
const e = ('' + window.doesntExist)[3] // to disallow minifier to constant fold
179+
const foo4 = await import('./treeshaken/syntax.js')[`th${e}n`]((m) => m.foo)
177180
await import('./treeshaken/syntax.js').then((mod) => mod.foo({ foo }))
178181
const obj = [
179182
'',
@@ -189,6 +192,8 @@ import(`../nested/nested/${base}.js`).then((mod) => {
189192
other()
190193
foo()
191194
foo2()
195+
foo3()
196+
foo4()
192197
obj[1].lazy()
193198
})()
194199

0 commit comments

Comments
 (0)