/
vite.config.js
120 lines (116 loc) · 3.18 KB
/
vite.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import path from 'path'
import { defineConfig } from 'vite'
import vuePlugin from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
const virtualFile = '@virtual-file'
const virtualId = '\0' + virtualFile
const nestedVirtualFile = '@nested-virtual-file'
const nestedVirtualId = '\0' + nestedVirtualFile
const base = '/test/'
export default defineConfig(({ command, ssrBuild }) => ({
base,
plugins: [
vuePlugin(),
vueJsx(),
{
name: 'virtual',
resolveId(id) {
if (id === '@foo') {
return id
}
},
load(id, options) {
const ssrFromOptions = options?.ssr ?? false
if (id === '@foo') {
// Force a mismatch error if ssrBuild is different from ssrFromOptions
return `export default { msg: '${
command === 'build' && !!ssrBuild !== ssrFromOptions
? `defineConfig ssrBuild !== ssr from load options`
: 'hi'
}' }`
}
}
},
{
name: 'virtual-module',
resolveId(id) {
if (id === virtualFile) {
return virtualId
} else if (id === nestedVirtualFile) {
return nestedVirtualId
}
},
load(id) {
if (id === virtualId) {
return `export { msg } from "@nested-virtual-file";`
} else if (id === nestedVirtualId) {
return `export const msg = "[success] from conventional virtual file"`
}
}
},
// Example of a plugin that injects a helper from a virtual module that can
// be used in renderBuiltUrl
(function () {
const queryRE = /\?.*$/s
const hashRE = /#.*$/s
const cleanUrl = (url) => url.replace(hashRE, '').replace(queryRE, '')
let config
const virtualId = '\0virtual:ssr-vue-built-url'
return {
name: 'built-url',
enforce: 'post',
configResolved(_config) {
config = _config
},
resolveId(id) {
if (id === virtualId) {
return id
}
},
load(id) {
if (id === virtualId) {
return {
code: `export const __ssr_vue_processAssetPath = (url) => '${base}' + url`,
moduleSideEffects: 'no-treeshake'
}
}
},
transform(code, id) {
if (
config.build.ssr &&
cleanUrl(id).endsWith('.js') &&
!code.includes('__ssr_vue_processAssetPath')
) {
return {
code:
`import { __ssr_vue_processAssetPath } from '${virtualId}';` +
code,
sourcemap: null // no sourcemap support to speed up CI
}
}
}
}
})()
],
experimental: {
renderBuiltUrl(filename, { hostType, type, ssr }) {
if (ssr && type === 'asset' && hostType === 'js') {
return {
runtime: `__ssr_vue_processAssetPath(${JSON.stringify(filename)})`
}
}
}
},
build: {
minify: false
},
ssr: {
noExternal: [
// this package has uncompiled .vue files
'example-external-component'
]
},
optimizeDeps: {
exclude: ['example-external-component']
}
}))