Skip to content

Commit a08b955

Browse files
committed
fix
1 parent d553fb3 commit a08b955

File tree

2 files changed

+59
-20
lines changed

2 files changed

+59
-20
lines changed

packages/plugin-rsc/examples/basic/vite.config.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,6 @@ export default { fetch: handler };
151151
],
152152
},
153153
},
154-
ssr: {
155-
optimizeDeps: {
156-
include: [
157-
'@vitejs/test-dep-transitive-cjs > use-sync-external-store/shim/index.js',
158-
],
159-
},
160-
},
161154
},
162155
}) as any
163156

packages/plugin-rsc/src/plugin.ts

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -265,18 +265,6 @@ export default function vitePluginRsc(
265265
...result.ssr.noExternal.sort(),
266266
]
267267

268-
// vendor and optimize use-sync-external-store since
269-
// this is a common transitive cjs dep, which tends to cause a cryptic error.
270-
const vendorDeps = [
271-
`${PKG_NAME}/vendor/use-sync-external-store`,
272-
`${PKG_NAME}/vendor/use-sync-external-store/with-selector`,
273-
`${PKG_NAME}/vendor/use-sync-external-store/with-selector.js`,
274-
`${PKG_NAME}/vendor/use-sync-external-store/shim`,
275-
`${PKG_NAME}/vendor/use-sync-external-store/shim/index.js`,
276-
`${PKG_NAME}/vendor/use-sync-external-store/shim/with-selector`,
277-
`${PKG_NAME}/vendor/use-sync-external-store/shim/with-selector.js`,
278-
]
279-
280268
return {
281269
appType: 'custom',
282270
define: {
@@ -323,7 +311,6 @@ export default function vitePluginRsc(
323311
'react/jsx-dev-runtime',
324312
'react-dom/server.edge',
325313
`${REACT_SERVER_DOM_NAME}/client.edge`,
326-
...vendorDeps,
327314
],
328315
exclude: [PKG_NAME],
329316
},
@@ -895,6 +882,7 @@ globalThis.AsyncLocalStorage = __viteRscAyncHooks.AsyncLocalStorage;
895882
...(rscPluginOptions.validateImports !== false
896883
? [validateImportPlugin()]
897884
: []),
885+
...vendorUseSyncExternalStorePlugin(),
898886
scanBuildStripPlugin(),
899887
detectNonOptimizedCjsPlugin(),
900888
]
@@ -2137,6 +2125,64 @@ function validateImportPlugin(): Plugin {
21372125
}
21382126
}
21392127

2128+
function vendorUseSyncExternalStorePlugin(): Plugin[] {
2129+
// vendor and optimize use-sync-external-store out of the box
2130+
// since this is a commonly used cjs dep (e.g. swr, @tanstack/react-store)
2131+
2132+
const vendorDeps = [
2133+
`${PKG_NAME}/vendor/use-sync-external-store/index`,
2134+
`${PKG_NAME}/vendor/use-sync-external-store/with-selector`,
2135+
`${PKG_NAME}/vendor/use-sync-external-store/shim/index`,
2136+
`${PKG_NAME}/vendor/use-sync-external-store/shim/with-selector`,
2137+
]
2138+
2139+
// map exports to vendor deps
2140+
// cf. https://github.com/facebook/react/blob/c499adf8c89bbfd884f4d3a58c4e510001383525/packages/use-sync-external-store/package.json#L5-L20
2141+
const alias: Record<string, string> = {
2142+
'use-sync-external-store': `${PKG_NAME}/vendor/use-sync-external-store/index`,
2143+
'use-sync-external-store/with-selector': `${PKG_NAME}/vendor/use-sync-external-store/with-selector`,
2144+
'use-sync-external-store/with-selector.js': `${PKG_NAME}/vendor/use-sync-external-store/with-selector`,
2145+
'use-sync-external-store/shim': `${PKG_NAME}/vendor/use-sync-external-store/shim/index`,
2146+
'use-sync-external-store/shim/index.js': `${PKG_NAME}/vendor/use-sync-external-store/shim/index`,
2147+
'use-sync-external-store/shim/with-selector': `${PKG_NAME}/vendor/use-sync-external-store/shim/with-selector`,
2148+
'use-sync-external-store/shim/with-selector.js': `${PKG_NAME}/vendor/use-sync-external-store/shim/with-selector`,
2149+
}
2150+
2151+
return [
2152+
{
2153+
name: 'rsc:vendor-use-sync-external-store',
2154+
apply: 'serve',
2155+
config() {
2156+
return {
2157+
environments: {
2158+
ssr: {
2159+
optimizeDeps: {
2160+
include: vendorDeps,
2161+
},
2162+
},
2163+
},
2164+
}
2165+
},
2166+
},
2167+
// TODO: why not alias?
2168+
{
2169+
name: 'rsc:vendor-use-sync-external-store:resolve',
2170+
applyToEnvironment: (e) => e.name === 'ssr',
2171+
enforce: 'pre',
2172+
resolveId: {
2173+
order: 'pre',
2174+
async handler(source) {
2175+
const target = alias[source]
2176+
if (target) {
2177+
const resolved = await this.resolve(target)
2178+
return resolved
2179+
}
2180+
},
2181+
},
2182+
},
2183+
]
2184+
}
2185+
21402186
function sortObject<T extends object>(o: T) {
21412187
return Object.fromEntries(
21422188
Object.entries(o).sort(([a], [b]) => a.localeCompare(b)),

0 commit comments

Comments
 (0)