Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC: Avoid bundling middleware using react-server export conditions and vite plugin #10691

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/ogimage-gen/empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This file is intentionally left empty, because we do not want to bundle middleware

export default function empty() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Temporary, so its easy to spot!

console.log(`

Check failure on line 4 in packages/ogimage-gen/empty.js

View workflow job for this annotation

GitHub Actions / 🏗 Build, lint, test / ubuntu-latest / node 20 latest

'console' is not defined
_______ _ _ _______ _______ _______ _______ _______ _______
|______ |_____| |______ |______ |______ |______ |______ |______
______| | | |______ ______| ______| ______| ______| ______|
`)
}
2 changes: 2 additions & 0 deletions packages/ogimage-gen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"types": "./dist/vite-plugin-ogimage-gen.d.ts"
},
"./middleware": {
"react-server": "./empty.js",
"import": "./dist/OgImageMiddleware.js",
"default": "./cjsWrappers/middleware.js",
"types": "./dist/OgImageMiddleware.d.ts"
Expand All @@ -26,6 +27,7 @@
},
"files": [
"dist",
"empty.js",
"cjsWrappers"
],
"scripts": {
Expand Down
9 changes: 1 addition & 8 deletions packages/vite/src/lib/getMergedConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,20 +172,13 @@ function getRollupInput(ssr: boolean): InputOption | undefined {
}

const ssrEnabled = rwConfig.experimental?.streamingSsr?.enabled
const rscEnabled = rwConfig.experimental?.rsc?.enabled

// @NOTE once streaming ssr is out of experimental, this will become the
// default
if (ssrEnabled) {
if (ssr) {
if (rscEnabled) {
return {
Document: rwPaths.web.document,
}
}

if (!rwPaths.web.entryServer) {
throw new Error('entryServer not defined')
throw new Error('entryServer not found')
}

return {
Expand Down
18 changes: 4 additions & 14 deletions packages/vite/src/middleware/register.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fmw from 'find-my-way'
import type Router from 'find-my-way'
import fmw from 'find-my-way'
import type { ViteDevServer } from 'vite'

import { getConfig, getPaths } from '@redwoodjs/project-config'
import { getPaths } from '@redwoodjs/project-config'

import type { EntryServer } from '../types'
import { makeFilePath, ssrLoadEntryServer } from '../utils'
Expand Down Expand Up @@ -104,25 +104,15 @@ export const createMiddlewareRouter = async (
vite?: ViteDevServer,
): Promise<Router.Instance<any>> => {
const rwPaths = getPaths()
const rwConfig = getConfig()
const rscEnabled = rwConfig.experimental?.rsc?.enabled

let entryServerImport: EntryServer

if (vite) {
// For Dev Server
entryServerImport = await ssrLoadEntryServer(vite)
} else {
// This imports from dist!

if (rscEnabled) {
entryServerImport = await import(
makeFilePath(rwPaths.web.distRscEntryServer)
)
} else {
entryServerImport = await import(
makeFilePath(rwPaths.web.distEntryServer)
)
}
entryServerImport = await import(makeFilePath(rwPaths.web.distEntryServer))
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Middleware router still imports from the SSR builds, not the RSC ones (which doesnt include register middleware)


const { registerMiddleware } = entryServerImport
Expand Down
35 changes: 35 additions & 0 deletions packages/vite/src/plugins/vite-plugin-remove-registerMiddlware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as babel from '@babel/core'
import type { NodePath } from '@babel/traverse'
import type * as t from '@babel/types'
import { createFilter } from 'vite'

import { getPaths } from '@redwoodjs/project-config'

const plugin = () => ({
visitor: {
VariableDeclarator(path: NodePath<t.VariableDeclarator>) {
// @ts-expect-error fggg
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to cleanup if we go ahead.

if (path.node.id.name === 'registerMiddleware') {
path.remove()
}
},
},
})

export default function removeRegisterMiddleware() {
const filter = createFilter(getPaths().web.entryServer, 'node_modules/**')

return {
name: 'remove-register-middleware',
transform: async (code: string, id: string) => {
if (filter(id)) {
const result = await babel.transformAsync(code, {
plugins: [plugin],
})
return result?.code
}

return null
},
}
}
2 changes: 2 additions & 0 deletions packages/vite/src/rsc/rscBuildForServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getPaths } from '@redwoodjs/project-config'

import { getEntries } from '../lib/entries.js'
import { onWarn } from '../lib/onWarn.js'
import removeRegisterMiddleware from '../plugins/vite-plugin-remove-registerMiddlware.js'
import { rscRoutesImports } from '../plugins/vite-plugin-rsc-routes-imports.js'
import { rscTransformUseClientPlugin } from '../plugins/vite-plugin-rsc-transform-client.js'
import { rscTransformUseServerPlugin } from '../plugins/vite-plugin-rsc-transform-server.js'
Expand Down Expand Up @@ -68,6 +69,7 @@ export async function rscBuildForServer(
rscTransformUseClientPlugin(clientEntryFiles),
rscTransformUseServerPlugin(),
rscRoutesImports(),
removeRegisterMiddleware(),
],
build: {
// TODO (RSC): Remove `minify: false` when we don't need to debug as often
Expand Down
Loading