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
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,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*(?:$|[^[.]))|(\(\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
1 change: 1 addition & 0 deletions playground/dynamic-import/__tests__/dynamic-import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ test('dynamic import treeshaken log', async () => {
expect(log).toContain('treeshaken baz3')
expect(log).toContain('treeshaken baz4')
expect(log).toContain('treeshaken baz5')
expect(log).toContain('treeshaken baz6')
expect(log).toContain('treeshaken default')

expect(log).not.toContain('treeshaken removed')
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 @@ -174,6 +174,9 @@ import(`../nested/nested/${base}.js`).then((mod) => {
const foo2 = await import('./treeshaken/syntax.js').then(
({ foo = {} }) => foo,
)
const foo3 = await import('./treeshaken/syntax.js').then((m) => m.foo)
const e = ('' + window.doesntExist)[3] // to disallow minifier to constant fold
const foo4 = await import('./treeshaken/syntax.js')[`th${e}n`]((m) => m.foo)
await import('./treeshaken/syntax.js').then((mod) => mod.foo({ foo }))
const obj = [
'',
Expand All @@ -189,6 +192,8 @@ import(`../nested/nested/${base}.js`).then((mod) => {
other()
foo()
foo2()
foo3()
foo4()
obj[1].lazy()
})()

Expand Down