From f06cde88f61f18acb370950ef88edc7cd18781b9 Mon Sep 17 00:00:00 2001 From: Junaid Khan Date: Fri, 27 Nov 2026 19:26:25 +0530 Subject: [PATCH] fix: exclude solid-condition packages from SSR optimizeDeps --- src/index.ts | 17 +++++++++ src/optimize-deps.test.ts | 74 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 18079abb..b79b6139 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2315,6 +2315,23 @@ export default function solidPlugin(options: Partial = {}): Plugin[] { rolldownOptions.plugins = rolldownOptions.plugins ? [rolldownOptions.plugins, tsrxDepScanPlugin] : [tsrxDepScanPlugin]; + // Same seed gap for `exclude`: the client gets vitefu's list of + // packages with a `solid` export condition from the top-level + // `optimizeDeps`, but SSR/other environments do not. With discovery + // on, a package whose `solid` entry is a `.js` barrel is taken into + // the pre-bundle and its `.jsx` files are compiled with the classic + // React runtime above — `React is not defined` at render (#560). A + // `.jsx` solid entry escapes by luck (the optimizer only takes + // `.js`/`.ts` entries). Mirror the framework-package exclude here + // so every environment keeps the README's "has a `solid` key" + // guarantee. + if (solidPkgsConfig?.optimizeDeps.exclude?.length) { + const exclude = new Set(optimizeDeps.exclude ?? []); + for (const pkg of solidPkgsConfig.optimizeDeps.exclude) { + exclude.add(pkg); + } + optimizeDeps.exclude = [...exclude]; + } } // `resolve.conditions` above only governs modules Vite inlines. diff --git a/src/optimize-deps.test.ts b/src/optimize-deps.test.ts index 8fd06499..ecc2414c 100644 --- a/src/optimize-deps.test.ts +++ b/src/optimize-deps.test.ts @@ -1,4 +1,4 @@ -import { mkdtemp, rm, symlink } from 'node:fs/promises'; +import { cp, mkdir, mkdtemp, rm, symlink, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; @@ -83,3 +83,75 @@ describe('optimizeDeps.include under vite dev', () => { expect(include).not.toContain('@solidjs/web/server-functions/client'); }); }); + +describe('optimizeDeps.exclude for solid-condition packages (#560)', () => { + let root: string; + const pkgName = 'solid-js-entry-lib'; + + beforeAll(async () => { + root = await mkdtemp(path.join(tmpdir(), 'solid-vite-plugin-ssr-exclude-')); + // Package whose `solid` entry is a `.js` barrel — the shape the SSR + // optimizer would otherwise take when discovery is on (#560). + const pkgDir = path.join(root, 'node_modules', pkgName); + await mkdir(path.join(pkgDir, 'dist', 'source'), { recursive: true }); + await writeFile( + path.join(pkgDir, 'package.json'), + JSON.stringify({ + name: pkgName, + type: 'module', + exports: { + '.': { + solid: './dist/source/index.js', + import: './dist/index.js', + }, + }, + }), + ); + await writeFile(path.join(pkgDir, 'dist', 'source', 'index.js'), 'export {};\n'); + await writeFile(path.join(pkgDir, 'dist', 'index.js'), 'export {};\n'); + await writeFile( + path.join(root, 'package.json'), + JSON.stringify({ + name: 'ssr-exclude-app', + type: 'module', + dependencies: { [pkgName]: '0.0.0' }, + }), + ); + // Copy (not symlink) the packages the plugin resolves from the project + // root — Windows often denies creating symlinks without elevation. + const shared = fileURLToPath(new URL('../node_modules', import.meta.url)); + for (const name of ['solid-js', 'vite', 'vitefu']) { + await cp(path.join(shared, name), path.join(root, 'node_modules', name), { + recursive: true, + dereference: true, + }); + } + await cp(path.join(shared, '@solidjs'), path.join(root, 'node_modules', '@solidjs'), { + recursive: true, + dereference: true, + }); + }, 120_000); + + afterAll(async () => { + await rm(root, { recursive: true, force: true }); + }); + + it('excludes the package from both client and SSR optimizeDeps', async () => { + const config = await resolveConfig( + { + root, + configFile: false, + logLevel: 'silent', + plugins: [solidPlugin({ ssr: true })], + // Hosts like @cloudflare/vite-plugin turn SSR discovery back on. + environments: { + ssr: { optimizeDeps: { noDiscovery: false } }, + }, + }, + 'serve', + ); + + expect(config.optimizeDeps.exclude ?? []).toContain(pkgName); + expect(config.environments.ssr.optimizeDeps.exclude ?? []).toContain(pkgName); + }); +});