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

[legacy-framework] Next.js Fork Migration: Move query/mutation support into nextjs core #2516

Merged
merged 58 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
966b19f
wip
flybayer Jun 15, 2021
5c9a866
more wip
flybayer Jun 15, 2021
0eded4a
Merge branch 'canary' into fork
flybayer Jun 17, 2021
1c9f930
move multi pages folders plus pages+api sibling into nextjs core
flybayer Jun 17, 2021
b9e89b2
fix critical bug for routing index files
flybayer Jun 17, 2021
9d3e3cb
fix for dynamic routes
flybayer Jun 18, 2021
a090394
multiple fixes
flybayer Jun 18, 2021
e251444
more fixes!
flybayer Jun 18, 2021
ff89117
oops
flybayer Jun 18, 2021
15708fa
more fixes
flybayer Jun 18, 2021
d1fb51d
remove blitz server pages stage
flybayer Jun 19, 2021
dd3133c
fix various issues
flybayer Jun 19, 2021
6428925
wip
flybayer Jun 19, 2021
5983fca
Move query/mutation support into nextjs core
flybayer Jun 19, 2021
fc08220
Merge branch 'canary' into fork
flybayer Jun 21, 2021
99f3da3
ignore .test. and .spec. files inside pages and API folders
flybayer Jun 22, 2021
ee5c523
Merge branch 'fork' into fork-rpc
flybayer Jun 22, 2021
aff7d48
few fixes
flybayer Jun 22, 2021
bb95fe4
wip
flybayer Jun 22, 2021
e722853
more wip
flybayer Jun 24, 2021
7f425ea
Merge branch 'canary' into fork-rpc
flybayer Jun 25, 2021
9049f96
rpc client working
flybayer Jun 25, 2021
df1e9ea
move middleware support into core
flybayer Jun 25, 2021
ba79182
more wip
flybayer Jul 3, 2021
b7e58c3
Merge branch 'canary' into fork-rpc
flybayer Jul 5, 2021
2eff848
clean up and fix multi-pages test
flybayer Jul 5, 2021
34f797e
move invokeWithMiddleware to core & add integration test for middleware
flybayer Jul 8, 2021
88f5731
Merge branch 'canary' into fork-rpc
flybayer Jul 9, 2021
02835d8
move all auth code into core
flybayer Jul 9, 2021
4719130
Merge branch 'canary' into fork-rpc
flybayer Jul 22, 2021
6137c84
re-enable auth integration with rpc
flybayer Jul 22, 2021
e9ca335
move react-query stuff into core
flybayer Jul 29, 2021
679f7f4
Merge branch 'canary' into fork-rpc
flybayer Jul 29, 2021
1c6d45b
fix build
flybayer Jul 29, 2021
94e94be
fix blitz auth integration test
flybayer Jul 29, 2021
f753970
fix type
flybayer Jul 29, 2021
4f774ac
fix suspense and route manifest issues
flybayer Aug 11, 2021
57d0f7b
Merge branch 'canary' into fork-rpc
flybayer Aug 11, 2021
1d45454
fix build
flybayer Aug 11, 2021
c8bd556
various fixes
flybayer Aug 12, 2021
7c4d190
multiple more fixes
flybayer Aug 13, 2021
6c22b1e
Merge branch 'canary' into fork-rpc
flybayer Aug 13, 2021
64e81f4
remove debug logs
flybayer Aug 13, 2021
2b17a94
fix missing invoke
flybayer Aug 13, 2021
ddd99ca
Merge branch 'canary' into fork-rpc
flybayer Aug 13, 2021
e53c5f7
more fixes and windows debug log
flybayer Aug 13, 2021
80f4025
more window test
flybayer Aug 13, 2021
3dec6eb
retry log
flybayer Aug 13, 2021
0186475
try fix
flybayer Aug 13, 2021
d7a7a61
restore blitz logging
flybayer Aug 16, 2021
b3ee859
fix logging bug
flybayer Aug 16, 2021
e23a3f5
fix build error
flybayer Aug 16, 2021
15a72dd
fix more config/log stuff
flybayer Aug 16, 2021
d7371fb
working server transform
flybayer Aug 16, 2021
efb2185
couple fixes
flybayer Aug 16, 2021
7f81a5b
export all next types with blitz equivalents
flybayer Aug 16, 2021
ea42dc7
fix some type issues
flybayer Aug 16, 2021
759a464
fix blitz routes
flybayer Aug 16, 2021
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
4 changes: 2 additions & 2 deletions examples/auth/app/auth/mutations/forgotPassword.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ beforeEach(async () => {
})

const generatedToken = "plain-token"
jest.mock("@blitzjs/core/server", () => ({
...jest.requireActual<object>("@blitzjs/core/server")!,
jest.mock("next/stdlib-server", () => ({
...jest.requireActual<object>("next/stdlib-server")!,
generateToken: () => generatedToken,
}))
jest.mock("preview-email", () => jest.fn())
Expand Down
18 changes: 9 additions & 9 deletions examples/store/app/products/queries/getProducts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ type GetProductsInput = {
take?: number
}

export const middleware: Middleware[] = [
async (req, res, next) => {
await next()
if (req.method !== "HEAD" && Array.isArray(res.blitzResult)) {
console.log("[Middleware] Total product count:", res.blitzResult.length, "\n")
}
},
]

export default async function getProducts(
{where, orderBy, skip = 0, take = 100}: GetProductsInput,
ctx: Record<any, unknown> = {},
Expand All @@ -32,12 +41,3 @@ export default async function getProducts(
count,
}
}

export const middleware: Middleware[] = [
async (req, res, next) => {
await next()
if (req.method !== "HEAD" && Array.isArray(res.blitzResult)) {
console.log("[Middleware] Total product count:", res.blitzResult.length, "\n")
}
},
]
2 changes: 1 addition & 1 deletion nextjs/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
"no-obj-calls": "warn",
"no-octal": "warn",
"no-octal-escape": "warn",
"no-redeclare": ["warn", { "builtinGlobals": false }],
"no-redeclare": ["off"], //blitz
"no-regex-spaces": "warn",
"no-restricted-syntax": ["warn", "WithStatement"],
"no-script-url": "warn",
Expand Down
8 changes: 8 additions & 0 deletions nextjs/db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"users": [
{
"id": 1
}
],
"sessions": []
}
2 changes: 1 addition & 1 deletion nextjs/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
testMatch: ['**/*.test.js'],
testMatch: ['**/*.test.js', '**/*.test.ts', '**/*.test.tsx'],
verbose: true,
rootDir: 'test',
modulePaths: ['<rootDir>/lib'],
Expand Down
9 changes: 2 additions & 7 deletions nextjs/lerna.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
{
"npmClient": "yarn",
"useWorkspaces": true,
"packages": [
"packages/*"
],
"packages": ["packages/*"],
"command": {
"version": {
"exact": true
},
"publish": {
"npmClient": "npm",
"allowBranch": [
"master",
"canary"
],
"allowBranch": ["master", "canary"],
"registry": "https://registry.npmjs.org/"
}
},
Expand Down
29 changes: 25 additions & 4 deletions nextjs/packages/next/build/babel/loader/get-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import loadConfig from 'next/dist/compiled/babel/core-lib-config'

import { NextBabelLoaderOptions, NextJsLoaderContext } from './types'
import { consumeIterator } from './util'
import { isPageFile as isPageFileFn } from '../../utils'
import { getIsPageFile, getIsRpcFile } from '../../utils'

const nextDistPath = /(next[\\/]dist[\\/]next-server[\\/]lib)|(next[\\/]dist[\\/]client)|(next[\\/]dist[\\/]pages)/

Expand All @@ -32,6 +32,7 @@ const nextDistPath = /(next[\\/]dist[\\/]next-server[\\/]lib)|(next[\\/]dist[\\/
interface CharacteristicsGermaneToCaching {
isServer: boolean
isPageFile: boolean
isRpcFile: boolean
isNextDist: boolean
hasModuleExports: boolean
fileExt: string
Expand All @@ -45,13 +46,16 @@ function getCacheCharacteristics(
): CharacteristicsGermaneToCaching {
const { isServer, pagesDir } = loaderOptions
const isNextDist = nextDistPath.test(filename)
const isPageFile = !isNextDist && isPageFileFn(filename.replace(pagesDir, ''))
const relativePathFromRoot = filename.replace(pagesDir, '')
const isPageFile = !isNextDist && getIsPageFile(relativePathFromRoot)
const isRpcFile = !isNextDist && getIsRpcFile(relativePathFromRoot)
const hasModuleExports = source.indexOf('module.exports') !== -1
const fileExt = fileExtensionRegex.exec(filename)?.[1] || 'unknown'

return {
isServer,
isPageFile,
isRpcFile,
isNextDist,
hasModuleExports,
fileExt,
Expand All @@ -69,6 +73,7 @@ function getPlugins(
const {
isServer,
isPageFile,
isRpcFile,
isNextDist,
hasModuleExports,
} = cacheCharacteristics
Expand Down Expand Up @@ -97,6 +102,18 @@ function getPlugins(
type: 'plugin',
})
: null
const rpcClientConfigItem =
!isServer && isRpcFile
? createConfigItem([require('../plugins/blitz-rpc-client')], {
type: 'plugin',
})
: null
const rpcServerTransformConfigItem =
isServer && isRpcFile
? createConfigItem([require('../plugins/blitz-rpc-server-transform')], {
type: 'plugin',
})
: null
const disallowExportAllItem =
!isServer && isPageFile
? createConfigItem(
Expand Down Expand Up @@ -133,6 +150,8 @@ function getPlugins(
noAnonymousDefaultExportItem,
reactRefreshItem,
pageConfigItem,
rpcClientConfigItem,
rpcServerTransformConfigItem,
disallowExportAllItem,
applyCommonJsItem,
transformDefineItem,
Expand All @@ -158,7 +177,7 @@ function getCustomBabelConfig(configFilePath: string) {
return require(configFilePath)
}
throw new Error(
'The Next.js Babel loader does not support .mjs or .cjs config files.'
'The Blitz.js Babel loader does not support .mjs or .cjs config files.'
)
}

Expand Down Expand Up @@ -290,6 +309,7 @@ function getCacheKey(cacheCharacteristics: CharacteristicsGermaneToCaching) {
const {
isServer,
isPageFile,
isRpcFile,
isNextDist,
hasModuleExports,
fileExt,
Expand All @@ -300,7 +320,8 @@ function getCacheKey(cacheCharacteristics: CharacteristicsGermaneToCaching) {
(isServer ? 0b0001 : 0) |
(isPageFile ? 0b0010 : 0) |
(isNextDist ? 0b0100 : 0) |
(hasModuleExports ? 0b1000 : 0)
(hasModuleExports ? 0b1000 : 0) |
(isRpcFile ? 0b10000 : 0)

return fileExt + flags
}
Expand Down
1 change: 1 addition & 0 deletions nextjs/packages/next/build/babel/loader/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface NextJsLoaderContext extends loader.LoaderContext {
export interface NextBabelLoaderOptions {
hasJsxRuntime: boolean
hasReactRefresh: boolean
pageExtensions: string[]
isServer: boolean
development: boolean
pagesDir: string
Expand Down
78 changes: 78 additions & 0 deletions nextjs/packages/next/build/babel/plugins/blitz-rpc-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { PluginObj } from 'next/dist/compiled/babel/core'
import { BabelType } from 'babel-plugin-tester'
import {
convertPageFilePathToRoutePath,
convertPageFilePathToResolverName,
convertPageFilePathToResolverType,
} from '../../utils'

/* This plugin changes the file contents to this:
*
import { buildRpcClient } from "next/data-client";
export default buildRpcClient({
"resolverName": "getUsers",
"resolverType": "query",
"routePath": "/api/rpc/getUsers"
});
*
*/

// https://astexplorer.net/#/gist/02bab3c8f0488923346b607ed578e2f7/latest (may be out of date)

const fileExtensionRegex = /\.([a-z]+)$/

export default function blitzRpcClient(babel: BabelType): PluginObj {
const { types: t } = babel

return {
visitor: {
Program: {
enter(path, state) {
const { filename, cwd } = state
const fileExt = fileExtensionRegex.exec(filename)?.[1] || 'unknown'

const relativePathFromRoot = filename.replace(cwd, '')
const resolverName = convertPageFilePathToResolverName(
relativePathFromRoot
)
const resolverType = convertPageFilePathToResolverType(
relativePathFromRoot
)
const routePath = convertPageFilePathToRoutePath(
relativePathFromRoot,
[fileExt as string]
)

const importDeclaration = t.importDeclaration(
[
t.importSpecifier(
t.identifier('buildRpcClient'),
t.identifier('buildRpcClient')
),
],
t.stringLiteral('next/data-client')
)
const exportDeclaration = t.exportDefaultDeclaration(
t.callExpression(t.identifier('buildRpcClient'), [
t.objectExpression([
t.objectProperty(
t.stringLiteral('resolverName'),
t.stringLiteral(resolverName)
),
t.objectProperty(
t.stringLiteral('resolverType'),
t.stringLiteral(resolverType)
),
t.objectProperty(
t.stringLiteral('routePath'),
t.stringLiteral(routePath)
),
]),
])
)
path.node.body = [importDeclaration, exportDeclaration]
},
},
},
}
}
Loading