Skip to content

Commit 67c46ad

Browse files
authored
fix: resolve vite root before using it (fixes #113) (#115)
1 parent b959743 commit 67c46ad

File tree

10 files changed

+33
-6
lines changed

10 files changed

+33
-6
lines changed

.changeset/fuzzy-bottles-teach.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
resolve vite.root option correctly (fixes #113)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
it('should load config and work', async () => {
22
expect(await page.textContent('h1')).toMatch('Hello world!');
3+
expect(await page.textContent('#test-child')).toBe('test-child');
4+
expect(await page.textContent('#dependency-import')).toBe('dependency-import');
35
});

packages/e2e-tests/configfile-custom/src/App.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<script>
22
import Dependency from 'e2e-tests-hmr-test-dependency';
3+
import Child from './lib/Child.svelte';
34
</script>
45

56
<h1>Hello world!</h1>
67
<Dependency />
8+
<Child />
79

810
<style>
911
h1 {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h2 id="test-child">test-child</h2>

packages/e2e-tests/configfile-custom/vite.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { defineConfig } = require('vite');
33

44
module.exports = defineConfig(() => {
55
return {
6+
root: './', // ensure custom root works, see https://github.com/sveltejs/vite-plugin-svelte/issues/113
67
plugins: [svelte({ configFile: 'svelte.config.custom.cjs' })],
78
build: {
89
// make build faster by skipping transforms and minification
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// unfortunately this test does not work as jest is not fully compatible with esm
2-
// wait for jest 27 and see https://github.com/facebook/jest/issues/9430
31
it('should load config and work', async () => {
42
expect(await page.textContent('h1')).toMatch('Hello world!');
3+
expect(await page.textContent('#test-child')).toBe('test-child');
4+
expect(await page.textContent('#dependency-import')).toBe('dependency-import');
55
});

packages/e2e-tests/configfile-esm/src/App.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<script>
22
import Dependency from 'e2e-tests-hmr-test-dependency';
3+
import Child from './lib/Child.svelte';
34
</script>
45

56
<h1>Hello world!</h1>
67
<Dependency />
8+
<Child />
79

810
<style>
911
h1 {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h2 id="test-child">test-child</h2>

packages/e2e-tests/configfile-esm/vite.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { defineConfig } from 'vite';
33

44
export default defineConfig(({ command, mode }) => {
55
return {
6+
root: './', // ensure custom root works, see https://github.com/sveltejs/vite-plugin-svelte/issues/113
67
plugins: [svelte()],
78
build: {
89
// make build faster by skipping transforms and minification

packages/vite-plugin-svelte/src/utils/options.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-unused-vars */
2-
import { ConfigEnv, UserConfig, ViteDevServer } from 'vite';
2+
import { ConfigEnv, UserConfig, ViteDevServer, normalizePath } from 'vite';
33
import { log } from './log';
44
import { loadSvelteConfig } from './load-svelte-config';
55
import { 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

1617
const 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+
167179
export function buildExtraViteConfig(
168180
options: ResolvedOptions,
169181
config: UserConfig

0 commit comments

Comments
 (0)