Skip to content

Commit e7b414e

Browse files
authored
fix(plugin-vue): use server.origin when building base for transformAssetUrls (#8077)
1 parent 3badd82 commit e7b414e

File tree

8 files changed

+63
-0
lines changed

8 files changed

+63
-0
lines changed

Diff for: packages/plugin-vue/src/template.ts

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export function resolveTemplateCompilerOptions(
119119
if (filename.startsWith(options.root)) {
120120
assetUrlOptions = {
121121
base:
122+
(options.devServer.config.server?.origin ?? '') +
122123
options.devServer.config.base +
123124
slash(path.relative(options.root, path.dirname(filename)))
124125
}

Diff for: playground/vue-server-origin/Main.vue

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script setup lang="ts">
2+
import asset from './assets/asset.png'
3+
</script>
4+
5+
<template>
6+
<img alt="Vue logo" src="./assets/asset.png" />
7+
<img alt="Vue logo" :src="asset" />
8+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { isBuild } from 'testUtils'
2+
3+
test('should render', async () => {
4+
const expected = isBuild
5+
? /assets\/asset\.[0-9a-f]+\.png/
6+
: /https:\/\/vue-server-origin\.test\/assets\/asset\.png/
7+
8+
expect(await page.getAttribute('img', 'src')).toMatch(expected)
9+
expect(await page.getAttribute('img:nth-child(2)', 'src')).toMatch(expected)
10+
})

Diff for: playground/vue-server-origin/assets/asset.png

12.5 KB
Loading

Diff for: playground/vue-server-origin/env.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module '*.png'

Diff for: playground/vue-server-origin/index.html

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div id="app"></div>
2+
<script type="module">
3+
import { createApp, defineCustomElement } from 'vue'
4+
import Main from './Main.vue'
5+
6+
createApp(Main).mount('#app')
7+
</script>

Diff for: playground/vue-server-origin/package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "test-vue-server-origin",
3+
"private": true,
4+
"version": "0.0.0",
5+
"scripts": {
6+
"dev": "vite",
7+
"build": "vite build",
8+
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
9+
"preview": "vite preview"
10+
},
11+
"dependencies": {
12+
"vue": "^3.2.25"
13+
},
14+
"devDependencies": {
15+
"@vitejs/plugin-vue": "workspace:*"
16+
}
17+
}

Diff for: playground/vue-server-origin/vite.config.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { defineConfig } from 'vite'
2+
import vuePlugin from '@vitejs/plugin-vue'
3+
4+
export default defineConfig({
5+
base: '',
6+
resolve: {
7+
alias: {
8+
'@': __dirname
9+
}
10+
},
11+
plugins: [vuePlugin()],
12+
server: {
13+
origin: 'https://vue-server-origin.test'
14+
},
15+
build: {
16+
// to make tests faster
17+
minify: false
18+
}
19+
})

0 commit comments

Comments
 (0)