Skip to content

Commit

Permalink
Expand & rename baseResolverPath (#3421)
Browse files Browse the repository at this point in the history
Co-authored-by: beerose <[email protected]>
  • Loading branch information
dbrxnds and beerose authored Jul 4, 2022
1 parent 1493729 commit bec9512
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 75 deletions.
6 changes: 6 additions & 0 deletions .changeset/slow-walls-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@blitzjs/next": patch
"@blitzjs/rpc": patch
---

Allow resolverPath to be a function which is ran for every file path that is converted to RPC Route
1 change: 0 additions & 1 deletion apps/web/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module.exports = withBlitz(
customServer: {
hotReload: false,
},
resolverBasePath: "root",
},
}),
)
65 changes: 15 additions & 50 deletions packages/blitz-next/src/index-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ import type {
MiddlewareResponse,
} from "blitz"
import {handleRequestWithMiddleware, startWatcher, stopWatcher} from "blitz"
import {dehydrate, getQueryKey, getInfiniteQueryKey, loaderClient, loaderServer} from "@blitzjs/rpc"
import {
dehydrate,
getInfiniteQueryKey,
getQueryKey,
installWebpackConfig,
InstallWebpackConfigOptions,
ResolverPathOptions,
} from "@blitzjs/rpc"
import {DefaultOptions, QueryClient} from "react-query"
import {IncomingMessage, ServerResponse} from "http"
import {withSuperJsonProps} from "./superjson"
import {ResolverBasePath} from "@blitzjs/rpc/src/index-server"
import {ParsedUrlQuery} from "querystring"
import {PreviewData} from "next/types"

Expand Down Expand Up @@ -163,59 +169,13 @@ export const setupBlitzServer = ({plugins, onError}: SetupBlitzOptions) => {

export interface BlitzConfig extends NextConfig {
blitz?: {
resolverBasePath?: ResolverBasePath
resolverPath?: ResolverPathOptions
customServer?: {
hotReload?: boolean
}
}
}

interface WebpackRuleOptions {
resolverBasePath?: ResolverBasePath
}

interface WebpackRule {
test: RegExp
use: Array<{
loader: string
options: WebpackRuleOptions
}>
}

interface InstallWebpackConfigOptions {
webpackConfig: {
module: {
rules: WebpackRule[]
}
}
nextConfig: BlitzConfig
}

export function installWebpackConfig({webpackConfig, nextConfig}: InstallWebpackConfigOptions) {
const options: WebpackRuleOptions = {
resolverBasePath: nextConfig.blitz?.resolverBasePath,
}

webpackConfig.module.rules.push({
test: /\/\[\[\.\.\.blitz]]\.[jt]s$/,
use: [
{
loader: loaderServer,
options,
},
],
})
webpackConfig.module.rules.push({
test: /[\\/](queries|mutations)[\\/]/,
use: [
{
loader: loaderClient,
options,
},
],
})
}

export function withBlitz(nextConfig: BlitzConfig = {}) {
if (
process.env.NODE_ENV !== "production" &&
Expand All @@ -236,7 +196,12 @@ export function withBlitz(nextConfig: BlitzConfig = {}) {

const config = Object.assign({}, nextConfig, {
webpack: (config: InstallWebpackConfigOptions["webpackConfig"], options: any) => {
installWebpackConfig({webpackConfig: config, nextConfig})
installWebpackConfig({
webpackConfig: config,
webpackRuleOptions: {
resolverPath: nextConfig.blitz?.resolverPath,
},
})
if (typeof nextConfig.webpack === "function") {
return nextConfig.webpack(config, options)
}
Expand Down
51 changes: 48 additions & 3 deletions packages/blitz-rpc/src/index-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function getGlobalObject<T extends Record<string, unknown>>(key: string, default

type Resolver = (...args: unknown[]) => Promise<unknown>
type ResolverFiles = Record<string, () => Promise<{default?: Resolver}>>
export type ResolverBasePath = "queries|mutations" | "root" | undefined
export type ResolverPathOptions = "queries|mutations" | "root" | ((path: string) => string)

// We define `global.__internal_blitzRpcResolverFiles` to ensure we use the same global object.
// Needed for Next.js. I'm guessing that Next.js is including the `node_modules/` files in a seperate bundle than user files.
Expand All @@ -51,8 +51,53 @@ export function __internal_addBlitzRpcResolver(
}

const dir = __dirname + (() => "")() // trick to avoid `@vercel/ncc` to glob import
export const loaderServer = resolve(dir, "./loader-server.cjs")
export const loaderClient = resolve(dir, "./loader-client.cjs")
const loaderServer = resolve(dir, "./loader-server.cjs")
const loaderClient = resolve(dir, "./loader-client.cjs")

interface WebpackRuleOptions {
resolverPath: ResolverPathOptions | undefined
}

interface WebpackRule {
test: RegExp
use: Array<{
loader: string
options: WebpackRuleOptions
}>
}

export interface InstallWebpackConfigOptions {
webpackConfig: {
module: {
rules: WebpackRule[]
}
}
webpackRuleOptions: WebpackRuleOptions
}

export function installWebpackConfig({
webpackConfig,
webpackRuleOptions,
}: InstallWebpackConfigOptions) {
webpackConfig.module.rules.push({
test: /\/\[\[\.\.\.blitz]]\.[jt]s$/,
use: [
{
loader: loaderServer,
options: webpackRuleOptions,
},
],
})
webpackConfig.module.rules.push({
test: /[\\/](queries|mutations)[\\/]/,
use: [
{
loader: loaderClient,
options: webpackRuleOptions,
},
],
})
}

// ----------
// END LOADER
Expand Down
2 changes: 1 addition & 1 deletion packages/blitz-rpc/src/loader-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function transformBlitzRpcResolverClient(

const resolverFilePath = "/" + posix.relative(root, id)
assertPosixPath(resolverFilePath)
const routePath = convertPageFilePathToRoutePath(resolverFilePath, options?.resolverBasePath)
const routePath = convertPageFilePathToRoutePath(resolverFilePath, options?.resolverPath)
const resolverName = convertFilePathToResolverName(resolverFilePath)
const resolverType = convertFilePathToResolverType(resolverFilePath)

Expand Down
2 changes: 1 addition & 1 deletion packages/blitz-rpc/src/loader-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function transformBlitzRpcServer(

for (let resolverFilePath of resolvers) {
const relativeResolverPath = posix.relative(dirname(id), join(root, resolverFilePath))
const routePath = convertPageFilePathToRoutePath(resolverFilePath, options?.resolverBasePath)
const routePath = convertPageFilePathToRoutePath(resolverFilePath, options?.resolverPath)
code += `__internal_addBlitzRpcResolver('${routePath}', () => import('${relativeResolverPath}'));`
code += "\n"
}
Expand Down
12 changes: 8 additions & 4 deletions packages/blitz-rpc/src/loader-utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {assert} from "blitz"
import {posix, sep, win32} from "path"
import {ResolverBasePath} from "./index-server"
import {ResolverPathOptions} from "./index-server"

export interface LoaderOptions {
resolverBasePath?: ResolverBasePath
resolverPath: ResolverPathOptions
}

export interface Loader {
Expand Down Expand Up @@ -51,9 +51,13 @@ const fileExtensionRegex = /\.([a-z]+)$/

export function convertPageFilePathToRoutePath(
filePath: string,
resolverBasePath: ResolverBasePath,
resolverPath?: ResolverPathOptions,
) {
if (resolverBasePath === "root") {
if (typeof resolverPath === "function") {
return resolverPath(filePath)
}

if (resolverPath === "root") {
return filePath.replace(fileExtensionRegex, "")
}

Expand Down
30 changes: 15 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bec9512

Please sign in to comment.