Skip to content

Commit

Permalink
RSC: Use experimental node loader (#8979)
Browse files Browse the repository at this point in the history
Use experimental node loaders when launching the RSC FE Server

This is all done to try to make loading of 3rd party modules work.
Specifically have been experimenting with the `server-only` package.

In the end it turned out that it's not working because there's a
bug/missing feature in Vite
vitejs/vite#13487
  • Loading branch information
Tobbe authored Jul 27, 2023
1 parent 257919e commit 2add568
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/cli/src/commands/serveBothHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export const bothRscServerHandler = async (argv) => {
const fePromise = execa(
'node',
[
'--experimental-loader @redwoodjs/vite/node-loader',
'--experimental-loader @redwoodjs/vite/react-node-loader',
'--conditions react-server',
'./node_modules/@redwoodjs/vite/dist/runRscFeServer.js',
],
Expand Down
8 changes: 8 additions & 0 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
"./buildFeServer": {
"types": "./dist/buildFeServer.d.ts",
"default": "./dist/buildFeServer.js"
},
"./node-loader": {
"types": "./dist/waku-lib/node-loader.d.ts",
"default": "./dist/waku-lib/node-loader.js"
},
"./react-node-loader": {
"types": "./dist/react-server-dom-webpack/node-loader.d.ts",
"default": "./dist/react-server-dom-webpack/node-loader.js"
}
},
"bin": {
Expand Down
3 changes: 3 additions & 0 deletions packages/vite/src/buildRscFeServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export const buildRscFeServer = async ({
// configFile: viteConfigPath,
root: webSrc,
plugins: [react(), rscIndexPlugin()],
resolve: {
conditions: ['react-server'],
},
build: {
outDir: webDist,
emptyOutDir: true, // Needed because `outDir` is not inside `root`
Expand Down
34 changes: 34 additions & 0 deletions packages/vite/src/waku-lib/node-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This loader is needed to make react-server-dom-webpack/node-loader work.
// Without it we get the following error:
//
// resolve ./dist/node/index.js {
// conditions: [ 'node', 'import', 'node-addons', 'react-server' ],
// importAssertions: [Object: null prototype] {},
// parentURL: 'file:///Users/tobbe/tmp/rw-rsc-esm/node_modules/vite/index.cjs'
// }
// (node:33561) DeprecationWarning: Obsolete loader hook(s) supplied and will be ignored: getSource, transformSource
// /Users/tobbe/tmp/rw-rsc-esm/node_modules/@redwoodjs/vite/dist/react-server-dom-webpack/node-loader.js:357
// throw new Error('Expected source to have been loaded into a string.');
// ^

// Error: Expected source to have been loaded into a string.
// at load (/Users/tobbe/tmp/rw-rsc-esm/node_modules/@redwoodjs/vite/dist/react-server-dom-webpack/node-loader.js:357:13)
// at async nextLoad (node:internal/modules/esm/loader:163:22)

export async function load(url: string, context: any, nextLoad: any) {
// console.log('waku-lib/node-loader: load', context.format, url)

const result = await nextLoad(url, context, nextLoad)

if (result.format === 'module') {
let { source } = result

if (typeof source !== 'string') {
source = source.toString()
}

return { ...result, source }
}

return result
}

0 comments on commit 2add568

Please sign in to comment.