Skip to content

Commit d377aae

Browse files
authored
fix: __VITE_PRELOAD__ replacement error (#4163)
Fixes #3051
1 parent 10ab4ba commit d377aae

File tree

9 files changed

+106
-3
lines changed

9 files changed

+106
-3
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { isBuild } from '../../testUtils'
2+
3+
test('should have no 404s', () => {
4+
browserLogs.forEach((msg) => {
5+
expect(msg).not.toMatch('404')
6+
})
7+
})
8+
9+
if (isBuild) {
10+
test('dynamic import', async () => {
11+
let appHtml = await page.content()
12+
expect(appHtml).toMatch('This is <b>home</b> page.')
13+
})
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div id="app"></div>
2+
<script type="module">
3+
import { createApp } from 'vue'
4+
import router from './router.js'
5+
import App from './src/App.vue'
6+
7+
createApp(App).use(router).mount('#app')
8+
</script>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "test-preload",
3+
"private": true,
4+
"version": "0.0.0",
5+
"scripts": {
6+
"dev": "vite",
7+
"build": "vite build",
8+
"debug": "node --inspect-brk ../../vite/bin/vite",
9+
"serve": "vite preview"
10+
},
11+
"dependencies": {
12+
"vue": "^3.0.8",
13+
"vue-router": "^4.0.6"
14+
},
15+
"devDependencies": {
16+
"@vitejs/plugin-vue": "^1.0.0",
17+
"@vue/compiler-sfc": "^3.0.8"
18+
}
19+
}

packages/playground/preload/router.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { createRouter, createWebHashHistory } from 'vue-router'
2+
import Home from './src/components/Home.vue'
3+
4+
const routes = [
5+
{ path: '/', name: 'Home', component: Home },
6+
{
7+
path: '/about',
8+
name: 'About',
9+
component: () => import('./src/components/About.vue')
10+
} // Lazy load route component
11+
]
12+
13+
export default createRouter({
14+
routes,
15+
history: createWebHashHistory()
16+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<template>
2+
<router-view></router-view>
3+
</template>
4+
5+
<script setup>
6+
</script>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<template>
2+
<div>
3+
This is <b>about</b> page.
4+
<br>
5+
Go to <router-link to="/">Home</router-link> page
6+
</div>
7+
</template>
8+
9+
<script setup>
10+
</script>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<template>
2+
<div>
3+
This is <b>home</b> page.
4+
<br>
5+
Go to <router-link to="/about">About</router-link> page
6+
</div>
7+
</template>
8+
9+
<script setup>
10+
</script>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const vuePlugin = require('@vitejs/plugin-vue')
2+
3+
module.exports = {
4+
plugins: [vuePlugin()],
5+
build: {
6+
terserOptions: {
7+
format: {
8+
beautify: true
9+
},
10+
compress: {
11+
passes: 3
12+
}
13+
}
14+
}
15+
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const preloadMarkerRE = new RegExp(`"${preloadMarker}"`, 'g')
2626
*/
2727
function preload(baseModule: () => Promise<{}>, deps?: string[]) {
2828
// @ts-ignore
29-
if (!__VITE_IS_MODERN__ || !deps) {
29+
if (!__VITE_IS_MODERN__ || !deps || deps.length === 0) {
3030
return baseModule()
3131
}
3232

@@ -262,7 +262,12 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
262262
addDeps(normalizedFile)
263263
}
264264

265-
const markPos = code.indexOf(preloadMarker, end)
265+
let markPos = code.indexOf(preloadMarker, end)
266+
// fix issue #3051
267+
if (markPos === -1 && imports.length === 1) {
268+
markPos = code.indexOf(preloadMarker)
269+
}
270+
266271
if (markPos > 0) {
267272
s.overwrite(
268273
markPos - 1,
@@ -271,7 +276,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
271276
// preload when there are actual other deps.
272277
deps.size > 1
273278
? `[${[...deps].map((d) => JSON.stringify(d)).join(',')}]`
274-
: `void 0`
279+
: `[]`
275280
)
276281
}
277282
}

0 commit comments

Comments
 (0)