Skip to content

Commit d32301f

Browse files
committed
chore: vite 6 ecosystem-ci support
1 parent f508d50 commit d32301f

File tree

6 files changed

+49
-6
lines changed

6 files changed

+49
-6
lines changed

packages/e2e-tests/kit-node/__tests__/kit.spec.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
readFileContent
1515
} from '~utils';
1616

17+
import { version as viteVersion } from 'vite';
1718
import glob from 'tiny-glob';
1819
import path from 'node:path';
1920
import { describe, expect, it } from 'vitest';
@@ -347,11 +348,19 @@ describe('kit-node', () => {
347348
['svelte', 'browser', 'module', 'jsnext:main', 'jsnext'],
348349
`resolve.mainFields in ${filename}`
349350
);
350-
expectArrayEqual(
351-
config.resolve.conditions,
352-
['svelte'],
353-
`resolve.conditions in ${filename}`
354-
);
351+
if (viteVersion.startsWith('6.')) {
352+
expectArrayEqual(
353+
config.resolve.conditions,
354+
['browser', 'development|production', 'module', 'svelte'],
355+
`resolve.conditions in ${filename}`
356+
);
357+
} else {
358+
expectArrayEqual(
359+
config.resolve.conditions,
360+
['svelte'],
361+
`resolve.conditions in ${filename}`
362+
);
363+
}
355364
}
356365
});
357366
});

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ export default defineConfig(({ command, mode }) => {
1414
}
1515
}
1616
},
17+
// TODO: investigate the condition issue. it's the same thing dominik and ben found.
18+
// idk why it's only happening after https://github.com/vitejs/vite/pull/18395
19+
ssr: {
20+
noExternal: ['esm-env']
21+
},
1722
server: {
1823
watch: {
1924
// During tests we edit the files too fast and sometimes chokidar

packages/vite-plugin-svelte/src/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import { saveSvelteMetadata } from './utils/optimizer.js';
1818
import { VitePluginSvelteCache } from './utils/vite-plugin-svelte-cache.js';
1919
import { loadRaw } from './utils/load-raw.js';
2020
import * as svelteCompiler from 'svelte/compiler';
21+
import {
22+
VITE_CLIENT_RESOLVE_CONDITIONS,
23+
VITE_SERVER_RESOLVE_CONDITIONS
24+
} from './utils/constants.js';
2125

2226
/**
2327
* @param {Partial<import('./public.d.ts').Options>} [inlineOptions]
@@ -63,6 +67,17 @@ export function svelte(inlineOptions) {
6367
log.debug('additional vite config', extraViteConfig, 'config');
6468
return extraViteConfig;
6569
},
70+
configEnvironment(name, config) {
71+
config.resolve ??= {};
72+
if (config.resolve.conditions == null) {
73+
if (name === 'client') {
74+
config.resolve.conditions = [...VITE_CLIENT_RESOLVE_CONDITIONS];
75+
} else {
76+
config.resolve.conditions = [...VITE_SERVER_RESOLVE_CONDITIONS];
77+
}
78+
}
79+
config.resolve.conditions.push('svelte');
80+
},
6681

6782
async configResolved(config) {
6883
options = resolveOptions(options, config, cache);

packages/vite-plugin-svelte/src/utils/constants.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import { createRequire } from 'node:module';
22

33
export const VITE_RESOLVE_MAIN_FIELDS = ['browser', 'module', 'jsnext:main', 'jsnext'];
44

5+
// These two are required for Vite 6 only, as specifying conditions will remove the default ones,
6+
// like the `mainFields` option. Vite 6 is working on exposing these which we can use later.
7+
export const VITE_CLIENT_RESOLVE_CONDITIONS = ['module', 'browser', 'development|production'];
8+
export const VITE_SERVER_RESOLVE_CONDITIONS = ['module', 'node', 'development|production'];
9+
510
export const SVELTE_RESOLVE_MAIN_FIELDS = ['svelte'];
611

712
export const SVELTE_IMPORTS = Object.entries(

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030

3131
import { isCommonDepWithoutSvelteField } from './dependencies.js';
3232
import { VitePluginSvelteStats } from './vite-plugin-svelte-stats.js';
33+
import { isVite6 } from './vite-version.js';
3334

3435
const allowedPluginOptions = new Set([
3536
'include',
@@ -347,7 +348,9 @@ export async function buildExtraViteConfig(options, config) {
347348
const extraViteConfig = {
348349
resolve: {
349350
dedupe: [...SVELTE_IMPORTS],
350-
conditions: [...SVELTE_EXPORT_CONDITIONS]
351+
// In Vite 6, we need to provide the default conditions too as it now replaces the default,
352+
// instead of extending it. We set undefined here and extend it in the `configEnvironment` hook instead.
353+
conditions: isVite6 ? undefined : [...SVELTE_EXPORT_CONDITIONS]
351354
}
352355
// this option is still awaiting a PR in vite to be supported
353356
// see https://github.com/sveltejs/vite-plugin-svelte/issues/60
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { version } from 'vite';
2+
3+
/**
4+
* @type {boolean}
5+
*/
6+
export const isVite6 = version.startsWith('6.');

0 commit comments

Comments
 (0)