diff --git a/.changeset/slow-walls-poke.md b/.changeset/slow-walls-poke.md new file mode 100644 index 0000000000..8746c0d16c --- /dev/null +++ b/.changeset/slow-walls-poke.md @@ -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 diff --git a/apps/web/next.config.js b/apps/web/next.config.js index 64049a51f2..05eae5452e 100644 --- a/apps/web/next.config.js +++ b/apps/web/next.config.js @@ -10,7 +10,6 @@ module.exports = withBlitz( customServer: { hotReload: false, }, - resolverBasePath: "root", }, }), ) diff --git a/packages/blitz-next/src/index-server.ts b/packages/blitz-next/src/index-server.ts index 5e76ffa911..c1023a18da 100644 --- a/packages/blitz-next/src/index-server.ts +++ b/packages/blitz-next/src/index-server.ts @@ -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" @@ -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" && @@ -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) } diff --git a/packages/blitz-rpc/src/index-server.ts b/packages/blitz-rpc/src/index-server.ts index 1b187f9192..b7343f80b5 100644 --- a/packages/blitz-rpc/src/index-server.ts +++ b/packages/blitz-rpc/src/index-server.ts @@ -26,7 +26,7 @@ function getGlobalObject>(key: string, default type Resolver = (...args: unknown[]) => Promise type ResolverFiles = Record 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. @@ -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 diff --git a/packages/blitz-rpc/src/loader-client.ts b/packages/blitz-rpc/src/loader-client.ts index 20f4e853ce..11ad5561d7 100644 --- a/packages/blitz-rpc/src/loader-client.ts +++ b/packages/blitz-rpc/src/loader-client.ts @@ -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) diff --git a/packages/blitz-rpc/src/loader-server.ts b/packages/blitz-rpc/src/loader-server.ts index 3b6e3ea019..7637aab862 100644 --- a/packages/blitz-rpc/src/loader-server.ts +++ b/packages/blitz-rpc/src/loader-server.ts @@ -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" } diff --git a/packages/blitz-rpc/src/loader-utils.ts b/packages/blitz-rpc/src/loader-utils.ts index c660dd7f9b..b4c6509ab3 100644 --- a/packages/blitz-rpc/src/loader-utils.ts +++ b/packages/blitz-rpc/src/loader-utils.ts @@ -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 { @@ -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, "") } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 169ec2fde6..536a42c18d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -50,7 +50,7 @@ importers: "@types/node": 17.0.16 "@types/preview-email": 2.0.1 "@types/react": 18.0.1 - blitz: workspace:2.0.0-alpha.47 + blitz: workspace:2.0.0-alpha.48 eslint: 7.32.0 husky: 7.0.4 jest: 27.5.1 @@ -471,8 +471,8 @@ importers: packages/blitz: specifiers: - "@blitzjs/config": workspace:2.0.0-alpha.47 - "@blitzjs/generator": 2.0.0-alpha.47 + "@blitzjs/config": workspace:2.0.0-alpha.48 + "@blitzjs/generator": 2.0.0-alpha.48 "@types/cookie": 0.4.1 "@types/cross-spawn": 6.0.2 "@types/debug": 4.1.7 @@ -578,7 +578,7 @@ importers: packages/blitz-auth: specifiers: - "@blitzjs/config": workspace:2.0.0-alpha.47 + "@blitzjs/config": workspace:2.0.0-alpha.48 "@testing-library/react": 13.0.0 "@testing-library/react-hooks": 7.0.2 "@types/b64-lite": 1.3.0 @@ -592,7 +592,7 @@ importers: "@types/secure-password": 3.1.1 b64-lite: 1.4.0 bad-behavior: 1.0.1 - blitz: 2.0.0-alpha.47 + blitz: 2.0.0-alpha.48 cookie: 0.4.1 cookie-session: 2.0.0 debug: 4.3.3 @@ -643,8 +643,8 @@ importers: packages/blitz-next: specifiers: - "@blitzjs/config": workspace:2.0.0-alpha.47 - "@blitzjs/rpc": 2.0.0-alpha.47 + "@blitzjs/config": workspace:2.0.0-alpha.48 + "@blitzjs/rpc": 2.0.0-alpha.48 "@testing-library/dom": 8.13.0 "@testing-library/jest-dom": 5.16.3 "@testing-library/react": 13.0.0 @@ -655,7 +655,7 @@ importers: "@types/react": 18.0.1 "@types/react-dom": 17.0.14 "@types/testing-library__react-hooks": 4.0.0 - blitz: 2.0.0-alpha.47 + blitz: 2.0.0-alpha.48 cross-spawn: 7.0.3 debug: 4.3.3 find-up: 4.1.0 @@ -704,14 +704,14 @@ importers: packages/blitz-rpc: specifiers: - "@blitzjs/auth": 2.0.0-alpha.47 - "@blitzjs/config": workspace:2.0.0-alpha.47 + "@blitzjs/auth": 2.0.0-alpha.48 + "@blitzjs/config": workspace:2.0.0-alpha.48 "@types/debug": 4.1.7 "@types/react": 18.0.1 "@types/react-dom": 17.0.14 b64-lite: 1.4.0 bad-behavior: 1.0.1 - blitz: 2.0.0-alpha.47 + blitz: 2.0.0-alpha.48 chalk: ^4.1.0 debug: 4.3.3 next: 12.1.6-canary.17 @@ -753,12 +753,12 @@ importers: "@babel/plugin-syntax-typescript": 7.17.12 "@babel/preset-env": 7.12.10 "@blitzjs/config": workspace:* - "@blitzjs/generator": 2.0.0-alpha.47 + "@blitzjs/generator": 2.0.0-alpha.48 "@types/jscodeshift": 0.11.2 "@types/node": 17.0.16 arg: 5.0.1 ast-types: 0.14.2 - blitz: 2.0.0-alpha.47 + blitz: 2.0.0-alpha.48 chalk: ^4.1.0 cross-spawn: 7.0.3 debug: 4.3.3 @@ -813,7 +813,7 @@ importers: "@babel/plugin-transform-typescript": 7.12.1 "@babel/preset-env": 7.12.10 "@babel/types": 7.12.10 - "@blitzjs/config": 2.0.0-alpha.47 + "@blitzjs/config": 2.0.0-alpha.48 "@juanm04/cpx": 2.0.1 "@mrleebo/prisma-ast": 0.2.6 "@types/babel__core": 7.1.19 @@ -904,7 +904,7 @@ importers: packages/pkg-template: specifiers: - "@blitzjs/config": 2.0.0-alpha.47 + "@blitzjs/config": 2.0.0-alpha.48 "@types/react": 18.0.1 "@types/react-dom": 17.0.14 "@typescript-eslint/eslint-plugin": 5.9.1