Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: vite 6 support #1020

Merged
merged 16 commits into from
Nov 21, 2024
19 changes: 14 additions & 5 deletions packages/e2e-tests/kit-node/__tests__/kit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
readFileContent
} from '~utils';

import { version as viteVersion } from 'vite';
import glob from 'tiny-glob';
import path from 'node:path';
import { describe, expect, it } from 'vitest';
Expand Down Expand Up @@ -347,11 +348,19 @@ describe('kit-node', () => {
['svelte', 'browser', 'module', 'jsnext:main', 'jsnext'],
`resolve.mainFields in ${filename}`
);
expectArrayEqual(
config.resolve.conditions,
['svelte'],
`resolve.conditions in ${filename}`
);
if (viteVersion.startsWith('6.')) {
expectArrayEqual(
config.resolve.conditions,
['browser', 'development|production', 'module', 'svelte'],
`resolve.conditions in ${filename}`
);
} else {
expectArrayEqual(
config.resolve.conditions,
['svelte'],
`resolve.conditions in ${filename}`
);
}
}
});
});
Expand Down
5 changes: 5 additions & 0 deletions packages/e2e-tests/vite-ssr-esm/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export default defineConfig(({ command, mode }) => {
}
}
},
// TODO: investigate the condition issue. it's the same thing dominik and ben found.
// idk why it's only happening after https://github.com/vitejs/vite/pull/18395
ssr: {
noExternal: ['esm-env']
bluwy marked this conversation as resolved.
Show resolved Hide resolved
},
server: {
watch: {
// During tests we edit the files too fast and sometimes chokidar
Expand Down
16 changes: 16 additions & 0 deletions packages/vite-plugin-svelte/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ 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 @@ -63,6 +67,18 @@ export function svelte(inlineOptions) {
log.debug('additional vite config', extraViteConfig, 'config');
return extraViteConfig;
},
// @ts-ignore Allow exist in vite 6
configEnvironment(name, config) {
config.resolve ??= {};
if (config.resolve.conditions == null) {
if (name === 'client') {
config.resolve.conditions = [...VITE_CLIENT_RESOLVE_CONDITIONS];
} else {
config.resolve.conditions = [...VITE_SERVER_RESOLVE_CONDITIONS];
}
}
config.resolve.conditions.push('svelte');
},

async configResolved(config) {
options = resolveOptions(options, config, cache);
Expand Down
5 changes: 5 additions & 0 deletions packages/vite-plugin-svelte/src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ 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
5 changes: 4 additions & 1 deletion packages/vite-plugin-svelte/src/utils/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {

import { isCommonDepWithoutSvelteField } from './dependencies.js';
import { VitePluginSvelteStats } from './vite-plugin-svelte-stats.js';
import { isVite6 } from './vite-version.js';

const allowedPluginOptions = new Set([
'include',
Expand Down Expand Up @@ -347,7 +348,9 @@ export async function buildExtraViteConfig(options, config) {
const extraViteConfig = {
resolve: {
dedupe: [...SVELTE_IMPORTS],
conditions: [...SVELTE_EXPORT_CONDITIONS]
// 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.
conditions: isVite6 ? undefined : [...SVELTE_EXPORT_CONDITIONS]
}
// this option is still awaiting a PR in vite to be supported
// see https://github.com/sveltejs/vite-plugin-svelte/issues/60
Expand Down
6 changes: 6 additions & 0 deletions packages/vite-plugin-svelte/src/utils/vite-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { version } from 'vite';

/**
* @type {boolean}
*/
export const isVite6 = version.startsWith('6.');
2 changes: 1 addition & 1 deletion packages/vite-plugin-svelte/types/index.d.ts.map
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
null,
null
],
"mappings": ";;;;aAIYA,OAAOA;;;;;;;;;;;;;kBAaFC,aAAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAgGbC,YAAYA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAkFZC,qBAAqBA;;;;;;;;;;;;;iBC1KtBC,MAAMA;iBCTNC,cAAcA;iBCgBRC,gBAAgBA"
"mappings": ";;;;aAIYA,OAAOA;;;;;;;;;;;;;kBAaFC,aAAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAgGbC,YAAYA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAkFZC,qBAAqBA;;;;;;;;;;;;;iBCtKtBC,MAAMA;iBCbNC,cAAcA;iBCgBRC,gBAAgBA"
}