Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Nov 14, 2024
1 parent dcec9af commit e54a2a1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
31 changes: 22 additions & 9 deletions packages/vite-plugin-svelte/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ import { saveSvelteMetadata } from './utils/optimizer.js';
import { VitePluginSvelteCache } from './utils/vite-plugin-svelte-cache.js';
import { loadRaw } from './utils/load-raw.js';
import * as svelteCompiler from 'svelte/compiler';
import {
VITE_CLIENT_RESOLVE_CONDITIONS,
VITE_SERVER_RESOLVE_CONDITIONS
} from './utils/constants.js';

/**
* @param {Partial<import('./public.d.ts').Options>} [inlineOptions]
Expand Down Expand Up @@ -67,14 +63,31 @@ export function svelte(inlineOptions) {
log.debug('additional vite config', extraViteConfig, 'config');
return extraViteConfig;
},
// @ts-ignore Allow exist in vite 6
configEnvironment(name, config) {

// @ts-ignore This hook only works in Vite 6
async configEnvironment(name, config, opts) {
config.resolve ??= {};

// Emulate Vite default fallback for `resolve.mainFields` if not set
if (config.resolve.mainFields == null) {
// These exports only exist in Vite 6
const { defaultClientMainFields, defaultServerMainFields } = await import('vite');
if (name === 'client' || opts.isSsrTargetWebworker) {
config.resolve.mainFields = [...defaultClientMainFields];
} else {
config.resolve.mainFields = [...defaultServerMainFields];
}
}
config.resolve.mainFields.unshift('svelte');

// Emulate Vite default fallback for `resolve.conditions` if not set
if (config.resolve.conditions == null) {
if (name === 'client') {
config.resolve.conditions = [...VITE_CLIENT_RESOLVE_CONDITIONS];
// These exports only exist in Vite 6
const { defaultClientConditions, defaultServerConditions } = await import('vite');
if (name === 'client' || opts.isSsrTargetWebworker) {
config.resolve.conditions = [...defaultClientConditions];
} else {
config.resolve.conditions = [...VITE_SERVER_RESOLVE_CONDITIONS];
config.resolve.conditions = [...defaultServerConditions];
}
}
config.resolve.conditions.push('svelte');
Expand Down
5 changes: 0 additions & 5 deletions packages/vite-plugin-svelte/src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ import { createRequire } from 'node:module';

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

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

export const SVELTE_RESOLVE_MAIN_FIELDS = ['svelte'];

export const SVELTE_IMPORTS = Object.entries(
Expand Down
22 changes: 12 additions & 10 deletions packages/vite-plugin-svelte/src/utils/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,22 +334,24 @@ function resolveViteRoot(viteConfig) {
* @returns {Promise<Partial<import('vite').UserConfig>>}
*/
export async function buildExtraViteConfig(options, config) {
// make sure we only readd vite default mainFields when no other plugin has changed the config already
// see https://github.com/sveltejs/vite-plugin-svelte/issues/581
if (!config.resolve) {
config.resolve = {};
// `resolve.mainFields` override the defaults if set, but we want to extend it, so we directly mutate here.
// We only do so for Vite 5 and below, as in Vite 6, `resolve.mainFields` only apply to the client env,
// so we use the `configEnvironment` hook to set it up instead.
if (!isVite6) {
config.resolve ??= {};
config.resolve.mainFields = [
...SVELTE_RESOLVE_MAIN_FIELDS,
...(config.resolve.mainFields ?? VITE_RESOLVE_MAIN_FIELDS)
];
}
config.resolve.mainFields = [
...SVELTE_RESOLVE_MAIN_FIELDS,
...(config.resolve.mainFields ?? VITE_RESOLVE_MAIN_FIELDS)
];

/** @type {Partial<import('vite').UserConfig>} */
const extraViteConfig = {
resolve: {
dedupe: [...SVELTE_IMPORTS],
// In Vite 6, we need to provide the default conditions too as it now replaces the default,
// instead of extending it. We set undefined here and extend it in the `configEnvironment` hook instead.
// In Vite 6, conditions now override the defaults instead of extending. `resolve.conditions`
// also only apply to the client env, so we use the `configEnvironment` hook to set it up
// instead, so here we set to `undefined` to skip it.
conditions: isVite6 ? undefined : [...SVELTE_EXPORT_CONDITIONS]
}
// this option is still awaiting a PR in vite to be supported
Expand Down

0 comments on commit e54a2a1

Please sign in to comment.