11/* eslint-disable no-unused-vars */
2- import { ConfigEnv , UserConfig , ViteDevServer } from 'vite' ;
2+ import { ConfigEnv , UserConfig , ViteDevServer , normalizePath } from 'vite' ;
33import { log } from './log' ;
44import { loadSvelteConfig } from './load-svelte-config' ;
55import { SVELTE_HMR_IMPORTS , SVELTE_IMPORTS , SVELTE_RESOLVE_MAIN_FIELDS } from './constants' ;
@@ -12,6 +12,7 @@ import {
1212 Processed
1313 // eslint-disable-next-line node/no-missing-import
1414} from 'svelte/types/compiler/preprocess' ;
15+ import path from 'path' ;
1516
1617const knownOptions = new Set ( [
1718 'configFile' ,
@@ -131,7 +132,7 @@ function mergeOptions(
131132 ...( svelteConfig ?. experimental || { } ) ,
132133 ...( inlineOptions ?. experimental || { } )
133134 } ,
134- root : viteConfig . root || process . cwd ( ) ,
135+ root : viteConfig . root ! ,
135136 isProduction : viteEnv . mode === 'production' ,
136137 isBuild : viteEnv . command === 'build' ,
137138 isServe : viteEnv . command === 'serve'
@@ -149,13 +150,17 @@ export async function resolveOptions(
149150 viteConfig : UserConfig ,
150151 viteEnv : ConfigEnv
151152) : Promise < ResolvedOptions > {
153+ const viteConfigWithResolvedRoot = {
154+ ...viteConfig ,
155+ root : resolveViteRoot ( viteConfig )
156+ } ;
152157 const defaultOptions = buildDefaultOptions ( viteEnv . mode === 'production' , inlineOptions ) ;
153- const svelteConfig = ( await loadSvelteConfig ( viteConfig , inlineOptions ) ) || { } ;
158+ const svelteConfig = ( await loadSvelteConfig ( viteConfigWithResolvedRoot , inlineOptions ) ) || { } ;
154159 const resolvedOptions = mergeOptions (
155160 defaultOptions ,
156161 svelteConfig ,
157162 inlineOptions ,
158- viteConfig ,
163+ viteConfigWithResolvedRoot ,
159164 viteEnv
160165 ) ;
161166
@@ -164,6 +169,13 @@ export async function resolveOptions(
164169 return resolvedOptions ;
165170}
166171
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+
167179export function buildExtraViteConfig (
168180 options : ResolvedOptions ,
169181 config : UserConfig
0 commit comments