Skip to content

Commit 3e27071

Browse files
authored
fix(build): handle preload treeshaking for commas (#17472)
1 parent 29a260c commit 3e27071

File tree

5 files changed

+50
-18
lines changed

5 files changed

+50
-18
lines changed

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

Lines changed: 13 additions & 14 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-
/(\b(const|let|var)\s+(\{[^}.]+\})\s*=\s*await\s+import\([^)]+\))|(\(\s*await\s+import\([^)]+\)\s*\)(\??\.[^;[\s]+)+)|\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*\{([^}.]+)\})/g
4646

4747
function toRelativePath(filename: string, importer: string) {
4848
const relPath = path.posix.relative(path.posix.dirname(importer), filename)
@@ -252,48 +252,47 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
252252
/* handle `const {foo} = await import('foo')`
253253
*
254254
* match[1]: `const {foo} = await import('foo')`
255-
* match[2]: `const`
256-
* match[3]: `{foo}`
255+
* match[2]: `{foo}`
257256
* import end: `const {foo} = await import('foo')_`
258257
* ^
259258
*/
260259
if (match[1]) {
261260
dynamicImports[dynamicImportTreeshakenRE.lastIndex] = {
262-
declaration: `${match[2]} ${match[3]}`,
263-
names: match[3]?.trim(),
261+
declaration: `const ${match[2]}`,
262+
names: match[2]?.trim(),
264263
}
265264
continue
266265
}
267266

268267
/* handle `(await import('foo')).foo`
269268
*
270-
* match[4]: `(await import('foo')).foo`
271-
* match[5]: `.foo`
269+
* match[3]: `(await import('foo')).foo`
270+
* match[4]: `.foo`
272271
* import end: `(await import('foo'))`
273272
* ^
274273
*/
275-
if (match[4]) {
276-
let names = match[5].match(/\.([^.?]+)/)?.[1] || ''
274+
if (match[3]) {
275+
let names = match[4].match(/\.([^.?]+)/)?.[1] || ''
277276
// avoid `default` keyword error
278277
if (names === 'default') {
279278
names = 'default: __vite_default__'
280279
}
281280
dynamicImports[
282-
dynamicImportTreeshakenRE.lastIndex - match[5]?.length - 1
281+
dynamicImportTreeshakenRE.lastIndex - match[4]?.length - 1
283282
] = { declaration: `const {${names}}`, names: `{ ${names} }` }
284283
continue
285284
}
286285

287286
/* handle `import('foo').then(({foo})=>{})`
288287
*
289-
* match[6]: `.then(({foo}`
290-
* match[7]: `foo`
288+
* match[5]: `.then(({foo}`
289+
* match[6]: `foo`
291290
* import end: `import('foo').`
292291
* ^
293292
*/
294-
const names = match[7]?.trim()
293+
const names = match[6]?.trim()
295294
dynamicImports[
296-
dynamicImportTreeshakenRE.lastIndex - match[6]?.length
295+
dynamicImportTreeshakenRE.lastIndex - match[5]?.length
297296
] = { declaration: `const {${names}}`, names: `{ ${names} }` }
298297
}
299298
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@ test('dynamic import treeshaken log', async () => {
193193
expect(log).not.toContain('treeshaken removed')
194194
})
195195

196+
test('dynamic import syntax parsing', async () => {
197+
const log = browserLogs.join('\n')
198+
expect(log).toContain('treeshaken syntax foo')
199+
expect(log).toContain('treeshaken syntax default')
200+
})
201+
196202
test.runIf(isBuild)('dynamic import treeshaken file', async () => {
197203
expect(findAssetFile(/treeshaken.+\.js$/)).not.toContain('treeshaken removed')
198204
})

playground/dynamic-import/nested/index.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,25 +138,43 @@ import(`../nested/nested/${base}.js`).then((mod) => {
138138
;(async function () {
139139
const { foo } = await import('./treeshaken/treeshaken.js')
140140
const { bar, default: tree } = await import('./treeshaken/treeshaken.js')
141+
const default2 = (await import('./treeshaken/treeshaken.js')).default
141142
const baz1 = (await import('./treeshaken/treeshaken.js')).baz1
142143
const baz2 = (await import('./treeshaken/treeshaken.js')).baz2.log
143144
const baz3 = (await import('./treeshaken/treeshaken.js')).baz3?.log
144145
const baz4 = await import('./treeshaken/treeshaken.js').then(
145146
({ baz4 }) => baz4,
146147
)
147148
const baz5 = await import('./treeshaken/treeshaken.js').then(function ({
148-
baz5,
149-
}) {
150-
return baz5
151-
})
149+
baz5,
150+
}) {
151+
return baz5
152+
}),
153+
{ baz6 } = await import('./treeshaken/treeshaken.js')
152154
foo()
153155
bar()
154156
tree()
157+
;(await import('./treeshaken/treeshaken.js')).default()
158+
default2()
155159
baz1()
156160
baz2()
157161
baz3()
158162
baz4()
159163
baz5()
164+
baz6()
165+
})()
166+
// Test syntax parsing only
167+
;(async function () {
168+
const default1 = await import('./treeshaken/syntax.js').then(
169+
(mod) => mod.default,
170+
)
171+
const default2 = (await import('./treeshaken/syntax.js')).default,
172+
other = () => {}
173+
const foo = await import('./treeshaken/syntax.js').then((mod) => mod.foo)
174+
default1()
175+
default2()
176+
other()
177+
foo()
160178
})()
161179

162180
import(`../nested/static.js`).then((mod) => {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const foo = () => {
2+
console.log('treeshaken syntax foo')
3+
}
4+
export default () => {
5+
console.log('treeshaken syntax default')
6+
}

playground/dynamic-import/nested/treeshaken/treeshaken.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ export const baz4 = () => {
2323
export const baz5 = () => {
2424
console.log('treeshaken baz5')
2525
}
26+
export const baz6 = () => {
27+
console.log('treeshaken baz6')
28+
}
2629
export const removed = () => {
2730
console.log('treeshaken removed')
2831
}

0 commit comments

Comments
 (0)