Skip to content

Commit

Permalink
Move Vite manualChunks SSR workaround into Plugin
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Kingdutch committed Jun 29, 2022
1 parent 4adc80b commit f7bfa9d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
17 changes: 17 additions & 0 deletions RescriptRelayVitePlugin.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ export let rescriptRelayVitePlugin = ({

return {
name: "rescript-relay",
/**
* Workaround until we can upgrade to Vite 3.
*
* Remove manualChunks if this is SSR, since it doesn't work in SSR mode.
* See https://github.com/vitejs/vite/issues/8836
*/
config(userConfig) {
//
if (
Boolean(userConfig.build.ssr) &&
userConfig.build?.rollupOptions?.output?.manualChunks != null
) {
delete userConfig.build.rollupOptions.output.manualChunks;
}

return userConfig;
},
/**
* @param {ResolvedConfig} resolvedConfig
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build:rescript": "rescript build -with-deps",
"build:vite": "run-s build:vite:*",
"build:vite:client": "vite build --outDir dist/client --ssrManifest --manifest",
"build:vite:server": "cross-env IS_VITE_SSR=1 vite build --outDir dist/server --ssr src/EntryServer.mjs",
"build:vite:server": "vite build --outDir dist/server --ssr src/EntryServer.mjs",
"build:vite:server-fix": "perl -i -pe 's/import \\* as ReactRelay/import ReactRelay/' dist/server/EntryServer.js",
"preview": "cross-env ENABLE_FILESERVER=true yarn start",
"start": "cross-env NODE_ENV=production node Server.mjs",
Expand Down
19 changes: 5 additions & 14 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,11 @@ export default defineConfig({
plugins: [visualizer()],
output: {
format: "esm",
// Only enable output chunking for our client bundle.
// At the time of writing Vite does not allow us to know when --ssr
// is passed so we use a custom env variable.
...(
process.env.IS_VITE_SSR === "1"
? {}
: {
manualChunks: {
react: ["react", "react-dom"],
relay: ["react-relay", "relay-runtime"],
vendor: ["react-helmet"],
}
}
),
manualChunks: {
react: ["react", "react-dom"],
relay: ["react-relay", "relay-runtime"],
vendor: ["react-helmet"],
},
},
},
},
Expand Down

0 comments on commit f7bfa9d

Please sign in to comment.