Skip to content

Commit

Permalink
Support Vite 6 resolve conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Nov 18, 2024
1 parent 873f4ce commit 74c75d0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-comics-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-plugin-solid': patch
---

Support Vite 6's `resolve.conditions` breaking change
32 changes: 26 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { readFileSync } from 'fs';
import { mergeAndConcat } from 'merge-anything';
import { createRequire } from 'module';
import solidRefresh from 'solid-refresh/babel';
import { createFilter } from 'vite';
import { createFilter, version } from 'vite';
import type { Alias, AliasOptions, Plugin, FilterPattern } from 'vite';
import { crawlFrameworkPkgs } from 'vitefu';

Expand All @@ -14,6 +14,8 @@ const runtimePublicPath = '/@solid-refresh';
const runtimeFilePath = require.resolve('solid-refresh/dist/solid-refresh.mjs');
const runtimeCode = readFileSync(runtimeFilePath, 'utf-8');

const isVite6 = version.startsWith('6.');

/** Possible options for the extensions property */
export interface ExtensionOptions {
typescript?: boolean;
Expand Down Expand Up @@ -236,11 +238,13 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
*/
// esbuild: { include: /\.ts$/ },
resolve: {
conditions: [
'solid',
...(replaceDev ? ['development'] : []),
...(userConfig.mode === 'test' && !options.ssr ? ['browser'] : []),
],
conditions: isVite6
? undefined
: [
'solid',
...(replaceDev ? ['development'] : []),
...(userConfig.mode === 'test' && !options.ssr ? ['browser'] : []),
],
dedupe: nestedDeps,
alias: [{ find: /^solid-refresh$/, replacement: runtimePublicPath }],
},
Expand All @@ -253,6 +257,22 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
};
},

// @ts-ignore This hook only works in Vite 6
async configEnvironment(_, config, opts) {
config.resolve ??= {};
// Emulate Vite default fallback for `resolve.conditions` if not set
if (config.resolve.conditions == null) {
// @ts-ignore These exports only exist in Vite 6
const { defaultClientConditions, defaultServerConditions } = await import('vite');
if (config.consumer === 'client' || opts.isSsrTargetWebworker) {
config.resolve.conditions = [...defaultClientConditions];
} else {
config.resolve.conditions = [...defaultServerConditions];
}
}
config.resolve.conditions.push('solid');
},

configResolved(config) {
needHmr = config.command === 'serve' && config.mode !== 'production' && options.hot !== false;
},
Expand Down

0 comments on commit 74c75d0

Please sign in to comment.