Skip to content

Commit f7bfa9d

Browse files
committed
Move Vite manualChunks SSR workaround into Plugin
We use the plugin to work around a bug in Vite 2 which has been fixed in Vite 3 that breaks the SSR build if the manualChunks Rollup config is used. See vitejs/vite#8836
1 parent 4adc80b commit f7bfa9d

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

RescriptRelayVitePlugin.mjs

+17
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,23 @@ export let rescriptRelayVitePlugin = ({
8585

8686
return {
8787
name: "rescript-relay",
88+
/**
89+
* Workaround until we can upgrade to Vite 3.
90+
*
91+
* Remove manualChunks if this is SSR, since it doesn't work in SSR mode.
92+
* See https://github.com/vitejs/vite/issues/8836
93+
*/
94+
config(userConfig) {
95+
//
96+
if (
97+
Boolean(userConfig.build.ssr) &&
98+
userConfig.build?.rollupOptions?.output?.manualChunks != null
99+
) {
100+
delete userConfig.build.rollupOptions.output.manualChunks;
101+
}
102+
103+
return userConfig;
104+
},
88105
/**
89106
* @param {ResolvedConfig} resolvedConfig
90107
*/

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"build:rescript": "rescript build -with-deps",
1212
"build:vite": "run-s build:vite:*",
1313
"build:vite:client": "vite build --outDir dist/client --ssrManifest --manifest",
14-
"build:vite:server": "cross-env IS_VITE_SSR=1 vite build --outDir dist/server --ssr src/EntryServer.mjs",
14+
"build:vite:server": "vite build --outDir dist/server --ssr src/EntryServer.mjs",
1515
"build:vite:server-fix": "perl -i -pe 's/import \\* as ReactRelay/import ReactRelay/' dist/server/EntryServer.js",
1616
"preview": "cross-env ENABLE_FILESERVER=true yarn start",
1717
"start": "cross-env NODE_ENV=production node Server.mjs",

vite.config.js

+5-14
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,11 @@ export default defineConfig({
2424
plugins: [visualizer()],
2525
output: {
2626
format: "esm",
27-
// Only enable output chunking for our client bundle.
28-
// At the time of writing Vite does not allow us to know when --ssr
29-
// is passed so we use a custom env variable.
30-
...(
31-
process.env.IS_VITE_SSR === "1"
32-
? {}
33-
: {
34-
manualChunks: {
35-
react: ["react", "react-dom"],
36-
relay: ["react-relay", "relay-runtime"],
37-
vendor: ["react-helmet"],
38-
}
39-
}
40-
),
27+
manualChunks: {
28+
react: ["react", "react-dom"],
29+
relay: ["react-relay", "relay-runtime"],
30+
vendor: ["react-helmet"],
31+
},
4132
},
4233
},
4334
},

0 commit comments

Comments
 (0)