Skip to content

Commit

Permalink
Turbopack support for Blitz
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed Mar 16, 2024
1 parent 20309bc commit f2c4f96
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
17 changes: 15 additions & 2 deletions packages/blitz-rpc/src/server/loader/client/loader-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,21 @@ export async function loader(this: Loader, input: string): Promise<string> {
const id = this.resource
const root = this.rootContext

const isSSR = this._compiler.name === "server"
if (!isSSR) {
// Webpack has `_compiler` property. Turbopack does not.
const webpackCompilerName = this._compiler?.name
if (webpackCompilerName) {
const isSSR = webpackCompilerName === "server"
if (!isSSR) {
return await transformBlitzRpcResolverClient(
input,
toPosixPath(id),
toPosixPath(root),
this.query,
)
}
// Handle Turbopack / other bundlers case.
// The decision of which environment to run the loader in is decided by the loader configuration instead.
} else {
return await transformBlitzRpcResolverClient(
input,
toPosixPath(id),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,21 @@ export async function loader(this: Loader, input: string): Promise<string> {
const id = this.resource
const root = this.rootContext

const isSSR = this._compiler.name === "server"
if (isSSR) {
// Webpack has `_compiler` property. Turbopack does not.
const webpackCompilerName = this._compiler?.name
if (webpackCompilerName) {
const isSSR = webpackCompilerName === "server"
if (isSSR) {
return await transformBlitzRpcResolverServer(
input,
toPosixPath(id),
toPosixPath(root),
this.query,
)
}
// Handle Turbopack / other bundlers case.
// The decision of which environment to run the loader in is decided by the loader configuration instead.
} else {
return await transformBlitzRpcResolverServer(
input,
toPosixPath(id),
Expand Down
30 changes: 26 additions & 4 deletions packages/blitz-rpc/src/server/loader/server/loader-server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {join} from "path"
import {join, relative} from "path"
import {promises} from "fs"
import {
assertPosixPath,
Expand All @@ -18,12 +18,33 @@ export async function loader(this: Loader, input: string): Promise<string> {
const root = this.rootContext
const rpcFolders = this.query.includeRPCFolders ? this.query.includeRPCFolders : []

const isSSR = this._compiler.name === "server"
if (isSSR) {
// Webpack has `_compiler` property. Turbopack does not.
const webpackCompilerName = this._compiler?.name
if (webpackCompilerName) {
const isSSR = webpackCompilerName === "server"
if (isSSR) {
this.cacheable(false)

const resolvers = await collectResolvers(root, rpcFolders, ["ts", "js", "tsx", "jsx"])

return await transformBlitzRpcServer(
this.context,
input,
toPosixPath(id),
toPosixPath(root),
resolvers,
this.query,
)
}
// Handle Turbopack / other bundlers case.
// The decision of which environment to run the loader in is decided by the loader configuration instead.
} else {
this.cacheable(false)

const resolvers = await collectResolvers(root, rpcFolders, ["ts", "js", "tsx", "jsx"])

return await transformBlitzRpcServer(
this.context,
input,
toPosixPath(id),
toPosixPath(root),
Expand All @@ -42,6 +63,7 @@ function slash(str: string) {
}

export async function transformBlitzRpcServer(
context: string,
src: string,
id: string,
root: string,
Expand All @@ -67,7 +89,7 @@ export async function transformBlitzRpcServer(

code += `__internal_addBlitzRpcResolver('${routePath}','${slash(
resolverFilePath,
)}',() => ${importStrategy}('${slash(resolverFilePath)}'));`
)}',() => ${importStrategy}('${slash(relative(context, resolverFilePath))}'));`
code += "\n"
}

Expand Down
3 changes: 2 additions & 1 deletion packages/blitz-rpc/src/server/loader/utils/loader-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ export interface LoaderOptions {
}

export interface Loader {
_compiler?: {
_compiler: {
name: string
}
rootContext: string
context: string
resource: string
cacheable: (enabled: boolean) => void
query: LoaderOptions
Expand Down

0 comments on commit f2c4f96

Please sign in to comment.