1
1
/* eslint-disable no-unused-vars */
2
- import { ConfigEnv , UserConfig , ViteDevServer } from 'vite' ;
2
+ import { ConfigEnv , UserConfig , ViteDevServer , normalizePath } from 'vite' ;
3
3
import { log } from './log' ;
4
4
import { loadSvelteConfig } from './load-svelte-config' ;
5
5
import { SVELTE_HMR_IMPORTS , SVELTE_IMPORTS , SVELTE_RESOLVE_MAIN_FIELDS } from './constants' ;
@@ -12,6 +12,7 @@ import {
12
12
Processed
13
13
// eslint-disable-next-line node/no-missing-import
14
14
} from 'svelte/types/compiler/preprocess' ;
15
+ import path from 'path' ;
15
16
16
17
const knownOptions = new Set ( [
17
18
'configFile' ,
@@ -131,7 +132,7 @@ function mergeOptions(
131
132
...( svelteConfig ?. experimental || { } ) ,
132
133
...( inlineOptions ?. experimental || { } )
133
134
} ,
134
- root : viteConfig . root || process . cwd ( ) ,
135
+ root : viteConfig . root ! ,
135
136
isProduction : viteEnv . mode === 'production' ,
136
137
isBuild : viteEnv . command === 'build' ,
137
138
isServe : viteEnv . command === 'serve'
@@ -149,13 +150,17 @@ export async function resolveOptions(
149
150
viteConfig : UserConfig ,
150
151
viteEnv : ConfigEnv
151
152
) : Promise < ResolvedOptions > {
153
+ const viteConfigWithResolvedRoot = {
154
+ ...viteConfig ,
155
+ root : resolveViteRoot ( viteConfig )
156
+ } ;
152
157
const defaultOptions = buildDefaultOptions ( viteEnv . mode === 'production' , inlineOptions ) ;
153
- const svelteConfig = ( await loadSvelteConfig ( viteConfig , inlineOptions ) ) || { } ;
158
+ const svelteConfig = ( await loadSvelteConfig ( viteConfigWithResolvedRoot , inlineOptions ) ) || { } ;
154
159
const resolvedOptions = mergeOptions (
155
160
defaultOptions ,
156
161
svelteConfig ,
157
162
inlineOptions ,
158
- viteConfig ,
163
+ viteConfigWithResolvedRoot ,
159
164
viteEnv
160
165
) ;
161
166
@@ -164,6 +169,13 @@ export async function resolveOptions(
164
169
return resolvedOptions ;
165
170
}
166
171
172
+ // vite passes unresolved `root`option to config hook but we need the resolved value, so do it here
173
+ // https://github.com/sveltejs/vite-plugin-svelte/issues/113
174
+ // https://github.com/vitejs/vite/blob/43c957de8a99bb326afd732c962f42127b0a4d1e/packages/vite/src/node/config.ts#L293
175
+ function resolveViteRoot ( viteConfig : UserConfig ) : string | undefined {
176
+ return normalizePath ( viteConfig . root ? path . resolve ( viteConfig . root ) : process . cwd ( ) ) ;
177
+ }
178
+
167
179
export function buildExtraViteConfig (
168
180
options : ResolvedOptions ,
169
181
config : UserConfig
0 commit comments