Skip to content

Commit 35ce381

Browse files
authored
[main] Vite 9 forward-compat (#273)
* feat: update Vite plugin to use per-environment hook APIs and enhance compatibility for vite 9 compat * fix check on .environment
1 parent 9a3f3a6 commit 35ce381

6 files changed

Lines changed: 33 additions & 14 deletions

File tree

.changeset/tidy-lions-smile.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vite-plugin-solid': patch
3+
---
4+
5+
Use per-environment Vite plugin hook APIs while retaining compatibility with Vite 3 through 5.

examples/vite-8/vite.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { defineConfig } from 'vite';
22
import solidPlugin from 'vite-plugin-solid';
33

44
export default defineConfig({
5+
future: {
6+
removePluginHookSsrArgument: 'warn',
7+
},
58
plugins: [
69
solidPlugin(),
710
],
8-
});
11+
});

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vite-plugin-solid",
33
"version": "2.11.12",
4-
"description": "solid-js integration plugin for vite 3/4/5/6",
4+
"description": "solid-js integration plugin for Vite",
55
"type": "module",
66
"files": [
77
"dist"
@@ -73,7 +73,7 @@
7373
"peerDependencies": {
7474
"@testing-library/jest-dom": "^5.16.6 || ^5.17.0 || ^6.*",
7575
"solid-js": "^1.7.2",
76-
"vite": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
76+
"vite": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0 || ^9.0.0"
7777
},
7878
"peerDependenciesMeta": {
7979
"@testing-library/jest-dom": {

pnpm-workspace.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ packages:
22
- '.'
33
- "examples/*"
44
catalog:
5-
"solid-js": ^1.9.4
5+
"solid-js": ^1.9.4

scripts/test-examples.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { spawn, exec, ChildProcess } from 'node:child_process';
2+
import { readdirSync } from 'node:fs';
23
import { promisify } from 'node:util';
34

45
const execAsync = promisify(exec);
5-
const examples = ['vite-3', 'vite-4', 'vite-5', 'vite-6'];
6+
const examples = readdirSync('examples', { withFileTypes: true })
7+
.filter((entry) => entry.isDirectory() && /^vite-\d+$/.test(entry.name))
8+
.map((entry) => entry.name)
9+
.sort((a, b) => Number(a.slice(5)) - Number(b.slice(5)));
10+
const pluginHookSsrDeprecation =
11+
"Plugin hook `options.ssr` is replaced with `this.environment.config.consumer === 'server'`.";
612
const PORT = 4173;
713
const TEST_TIMEOUT = 5 * 60 * 1000; // 5 minutes
814

@@ -28,7 +34,10 @@ async function runExample(example) {
2834
try {
2935
// Install and build
3036
await execAsync('pnpm install', { cwd: examplePath });
31-
await execAsync('pnpm run build', { cwd: examplePath });
37+
const { stdout, stderr } = await execAsync('pnpm run build', { cwd: examplePath });
38+
if (`${stdout}\n${stderr}`.includes(pluginHookSsrDeprecation)) {
39+
throw new Error(`Vite's deprecated plugin hook SSR argument was used in ${example}`);
40+
}
3241

3342
// Start preview server with timeout
3443
const server = spawn('pnpm', ['run', 'preview'], { cwd: examplePath });
@@ -75,4 +84,4 @@ runAll().catch(error => {
7584
console.error('Unexpected error:', error);
7685
cleanup();
7786
process.exit(1);
78-
});
87+
});

src/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const runtimeFilePath = require.resolve('solid-refresh/dist/solid-refresh.mjs');
1515
const runtimeCode = readFileSync(runtimeFilePath, 'utf-8');
1616

1717
const viteVersionMajor = +version.split('.')[0];
18-
const isVite6 = viteVersionMajor >= 6;
19-
const isVite8 = viteVersionMajor >= 8;
18+
const isVite6OrNewer = viteVersionMajor >= 6;
19+
const isVite8OrNewer = viteVersionMajor >= 8;
2020

2121
/** Possible options for the extensions property */
2222
export interface ExtensionOptions {
@@ -266,7 +266,7 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
266266
*/
267267
// esbuild: { include: /\.ts$/ },
268268
resolve: {
269-
conditions: isVite6
269+
conditions: isVite6OrNewer
270270
? undefined
271271
: [
272272
'solid',
@@ -283,11 +283,11 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
283283
// React's automatic JSX runtime for .tsx files, injecting a
284284
// react/jsx-dev-runtime import. Tell it to preserve JSX as-is since
285285
// this plugin handles JSX transformation via babel-preset-solid.
286-
...(isVite8
286+
...(isVite8OrNewer
287287
? { rolldownOptions: { transform: { jsx: 'preserve' as const } } }
288288
: {}),
289289
},
290-
...(!isVite6 ? { ssr: solidPkgsConfig.ssr } : {}),
290+
...(!isVite6OrNewer ? { ssr: solidPkgsConfig.ssr } : {}),
291291
...(test.server ? { test } : {}),
292292
};
293293
},
@@ -314,7 +314,7 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
314314

315315
// Set resolve.noExternal and resolve.external for SSR environment (Vite 6+)
316316
// Only set resolve.external if noExternal is not true (to avoid conflicts with plugins like Cloudflare)
317-
if (isVite6 && name === 'ssr' && solidPkgsConfig) {
317+
if (isVite6OrNewer && name === 'ssr' && solidPkgsConfig) {
318318
if (config.resolve.noExternal !== true) {
319319
config.resolve.noExternal = [
320320
...(Array.isArray(config.resolve.noExternal) ? config.resolve.noExternal : []),
@@ -341,7 +341,9 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
341341
},
342342

343343
async transform(source, id, transformOptions) {
344-
const isSsr = transformOptions && transformOptions.ssr;
344+
const isSsr = this.environment
345+
? this.environment.config.consumer === 'server'
346+
: Boolean(transformOptions?.ssr);
345347
const currentFileExtension = getExtension(id);
346348

347349
const extensionsToWatch = options.extensions || [];

0 commit comments

Comments
 (0)