Skip to content

Commit e54a2a1

Browse files
committed
chore: update
1 parent dcec9af commit e54a2a1

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ 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';
2521

2622
/**
2723
* @param {Partial<import('./public.d.ts').Options>} [inlineOptions]
@@ -67,14 +63,31 @@ export function svelte(inlineOptions) {
6763
log.debug('additional vite config', extraViteConfig, 'config');
6864
return extraViteConfig;
6965
},
70-
// @ts-ignore Allow exist in vite 6
71-
configEnvironment(name, config) {
66+
67+
// @ts-ignore This hook only works in Vite 6
68+
async configEnvironment(name, config, opts) {
7269
config.resolve ??= {};
70+
71+
// Emulate Vite default fallback for `resolve.mainFields` if not set
72+
if (config.resolve.mainFields == null) {
73+
// These exports only exist in Vite 6
74+
const { defaultClientMainFields, defaultServerMainFields } = await import('vite');
75+
if (name === 'client' || opts.isSsrTargetWebworker) {
76+
config.resolve.mainFields = [...defaultClientMainFields];
77+
} else {
78+
config.resolve.mainFields = [...defaultServerMainFields];
79+
}
80+
}
81+
config.resolve.mainFields.unshift('svelte');
82+
83+
// Emulate Vite default fallback for `resolve.conditions` if not set
7384
if (config.resolve.conditions == null) {
74-
if (name === 'client') {
75-
config.resolve.conditions = [...VITE_CLIENT_RESOLVE_CONDITIONS];
85+
// These exports only exist in Vite 6
86+
const { defaultClientConditions, defaultServerConditions } = await import('vite');
87+
if (name === 'client' || opts.isSsrTargetWebworker) {
88+
config.resolve.conditions = [...defaultClientConditions];
7689
} else {
77-
config.resolve.conditions = [...VITE_SERVER_RESOLVE_CONDITIONS];
90+
config.resolve.conditions = [...defaultServerConditions];
7891
}
7992
}
8093
config.resolve.conditions.push('svelte');

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ 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-
105
export const SVELTE_RESOLVE_MAIN_FIELDS = ['svelte'];
116

127
export const SVELTE_IMPORTS = Object.entries(

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -334,22 +334,24 @@ function resolveViteRoot(viteConfig) {
334334
* @returns {Promise<Partial<import('vite').UserConfig>>}
335335
*/
336336
export async function buildExtraViteConfig(options, config) {
337-
// make sure we only readd vite default mainFields when no other plugin has changed the config already
338-
// see https://github.com/sveltejs/vite-plugin-svelte/issues/581
339-
if (!config.resolve) {
340-
config.resolve = {};
337+
// `resolve.mainFields` override the defaults if set, but we want to extend it, so we directly mutate here.
338+
// We only do so for Vite 5 and below, as in Vite 6, `resolve.mainFields` only apply to the client env,
339+
// so we use the `configEnvironment` hook to set it up instead.
340+
if (!isVite6) {
341+
config.resolve ??= {};
342+
config.resolve.mainFields = [
343+
...SVELTE_RESOLVE_MAIN_FIELDS,
344+
...(config.resolve.mainFields ?? VITE_RESOLVE_MAIN_FIELDS)
345+
];
341346
}
342-
config.resolve.mainFields = [
343-
...SVELTE_RESOLVE_MAIN_FIELDS,
344-
...(config.resolve.mainFields ?? VITE_RESOLVE_MAIN_FIELDS)
345-
];
346347

347348
/** @type {Partial<import('vite').UserConfig>} */
348349
const extraViteConfig = {
349350
resolve: {
350351
dedupe: [...SVELTE_IMPORTS],
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.
352+
// In Vite 6, conditions now override the defaults instead of extending. `resolve.conditions`
353+
// also only apply to the client env, so we use the `configEnvironment` hook to set it up
354+
// instead, so here we set to `undefined` to skip it.
353355
conditions: isVite6 ? undefined : [...SVELTE_EXPORT_CONDITIONS]
354356
}
355357
// this option is still awaiting a PR in vite to be supported

0 commit comments

Comments
 (0)