Skip to content

Commit bb79c9b

Browse files
authored
fix(preload): skip preload for non-static urls (#16556)
1 parent 8c6d4be commit bb79c9b

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
241241

242242
for (let index = 0; index < imports.length; index++) {
243243
const {
244+
s: start,
244245
e: end,
245246
ss: expStart,
246247
se: expEnd,
@@ -255,7 +256,14 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
255256
str().remove(end + 1, expEnd)
256257
}
257258

258-
if (isDynamicImport && insertPreload) {
259+
if (
260+
isDynamicImport &&
261+
insertPreload &&
262+
// Only preload static urls
263+
(source[start] === '"' ||
264+
source[start] === "'" ||
265+
source[start] === '`')
266+
) {
259267
needPreloadHelper = true
260268
str().prependLeft(expStart, `${preloadMethod}(() => `)
261269
str().appendRight(

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { expect, test } from 'vitest'
2-
import { getColor, isBuild, page, serverLogs, untilUpdated } from '~utils'
2+
import {
3+
findAssetFile,
4+
getColor,
5+
isBuild,
6+
page,
7+
serverLogs,
8+
untilUpdated,
9+
} from '~utils'
310

411
test('should load literal dynamic import', async () => {
512
await page.click('.baz')
@@ -170,3 +177,9 @@ test.runIf(isBuild)(
170177
)
171178
},
172179
)
180+
181+
test.runIf(isBuild)('should not preload for non-analyzable urls', () => {
182+
const js = findAssetFile(/index-[-\w]{8}\.js$/)
183+
// should match e.g. await import(e.jss);o(".view",p===i)
184+
expect(js).to.match(/\.jss\);/)
185+
})

0 commit comments

Comments
 (0)