1
0
mirror of synced 2026-02-05 06:00:08 -05:00

Compare commits

...

8 Commits

Author SHA1 Message Date
github-actions[bot]
830b51d0dc Version Packages (alpha) (#3492)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-07-04 17:39:14 +02:00
David
bec9512e30 Expand & rename baseResolverPath (#3421)
Co-authored-by: beerose <alexsandra.sikora@gmail.com>
2022-07-04 17:35:36 +02:00
Dillon Raphael
1493729d6b forked changesets action 2022-07-04 16:48:30 +02:00
Dillon Raphael
6bb5980da9 revert changesets action version 2022-07-04 15:44:21 +02:00
Dillon Raphael
7310f7171d use forked changesets github action 2022-07-04 15:32:28 +02:00
github-actions[bot]
d855ee3e1d Version Packages (alpha) (#3487)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-07-04 13:29:42 +02:00
Aleksandra
93851d90ca Handle duplicated imports with Blitz codemod (#3486) 2022-07-04 13:25:44 +02:00
beerose
3ab05b3cda Update pnpm-lock.yaml 2022-07-01 17:07:36 +02:00
33 changed files with 511 additions and 182 deletions

View File

@@ -0,0 +1,6 @@
---
"blitz": patch
"@blitzjs/codemod": patch
---
Handle duplicate imports with Blitz upgrade-legacy codemod

View File

@@ -49,6 +49,7 @@
"moody-squids-cheer",
"nervous-beds-travel",
"nervous-dolls-rule",
"new-coats-turn",
"nice-starfishes-live",
"nine-onions-admire",
"ninety-pets-heal",
@@ -66,6 +67,7 @@
"sharp-falcons-begin",
"shy-olives-hang",
"silent-colts-reply",
"slow-walls-poke",
"small-socks-confess",
"smooth-planets-admire",
"strong-apes-reply",

View File

@@ -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

View File

@@ -1,43 +0,0 @@
name-template: "v$RESOLVED_VERSION"
tag-template: "v$RESOLVED_VERSION"
# categories:
# - title: 'Blitz'
# label: 'blitz'
# - title: '@blitzjs/next'
# label: 'blitz-next'
# - title: '@blitzjs/rpc'
# label: 'blitz-rpc'
# - title: '@blitzjs/auth'
# label: 'blitz-auth'
version-resolver:
major:
labels:
- "major"
minor:
labels:
- "minor"
patch:
labels:
- "patch"
default: patch
# autolabeler:
# - label: 'blitz'
# title:
# - '/blitz/i'
# - label: 'blitz-next'
# title:
# - '/@blitzjs\/next/i'
# - label: 'blitz-rpc'
# title:
# - '/@blitzjs\/rpc/i'
# - label: 'blitz-auth'
# title:
# - '/@blitzjs\/auth/i'
template: |
## Whats Changed
$CHANGES
## Contributors
$CONTRIBUTORS

View File

@@ -42,10 +42,10 @@ jobs:
- run: pnpm build
- name: Create Release Pull Request
uses: changesets/action@v1
uses: blitz-js/changesets-action@main
with:
publish: pnpm release
createGithubReleases: false
createGithubReleases: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

View File

@@ -29,7 +29,7 @@
"@blitzjs/rpc": "workspace:*",
"@hookform/resolvers": "2.8.8",
"@prisma/client": "3.9.0",
"blitz": "workspace:2.0.0-alpha.47",
"blitz": "workspace:2.0.0-alpha.49",
"next": "12.1.6-canary.17",
"prisma": "3.9.0",
"react": "18.0.0",

View File

@@ -10,7 +10,6 @@ module.exports = withBlitz(
customServer: {
hotReload: false,
},
resolverBasePath: "root",
},
}),
)

View File

@@ -1,5 +1,18 @@
# @blitzjs/auth
## 2.0.0-alpha.49
### Patch Changes
- blitz@2.0.0-alpha.49
## 2.0.0-alpha.48
### Patch Changes
- Updated dependencies [93851d90]
- blitz@2.0.0-alpha.48
## 2.0.0-alpha.47
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/auth",
"version": "2.0.0-alpha.47",
"version": "2.0.0-alpha.49",
"scripts": {
"build": "unbuild",
"predev": "wait-on -d 250 ../blitz/dist/index-server.d.ts",
@@ -26,7 +26,7 @@
"@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.49",
"cookie": "0.4.1",
"cookie-session": "2.0.0",
"debug": "4.3.3",
@@ -39,7 +39,7 @@
"url": "0.11.0"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-alpha.47",
"@blitzjs/config": "workspace:2.0.0-alpha.49",
"@testing-library/react": "13.0.0",
"@testing-library/react-hooks": "7.0.2",
"@types/cookie": "0.4.1",

View File

@@ -1,5 +1,19 @@
# @blitzjs/next
## 2.0.0-alpha.49
### Patch Changes
- bec9512e: Allow resolverPath to be a function which is ran for every file path that is converted to RPC Route
- Updated dependencies [bec9512e]
- @blitzjs/rpc@2.0.0-alpha.49
## 2.0.0-alpha.48
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.48
## 2.0.0-alpha.47
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/next",
"version": "2.0.0-alpha.47",
"version": "2.0.0-alpha.49",
"scripts": {
"build": "unbuild",
"dev": "pnpm predev && pnpm watch unbuild src --wait=0.2",
@@ -23,7 +23,7 @@
"eslint.js"
],
"dependencies": {
"@blitzjs/rpc": "2.0.0-alpha.47",
"@blitzjs/rpc": "2.0.0-alpha.49",
"@types/hoist-non-react-statics": "3.3.1",
"debug": "4.3.3",
"fs-extra": "10.0.1",
@@ -32,7 +32,7 @@
"superjson": "1.8.0"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-alpha.47",
"@blitzjs/config": "workspace:2.0.0-alpha.49",
"@testing-library/dom": "8.13.0",
"@testing-library/jest-dom": "5.16.3",
"@testing-library/react": "13.0.0",
@@ -42,7 +42,7 @@
"@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.49",
"cross-spawn": "7.0.3",
"find-up": "4.1.0",
"next": "12.1.6-canary.17",

View File

@@ -220,8 +220,7 @@ test("withErrorBoundary HOC", () => {
expect(cleanStack(onErrorComponentStack)).toMatchInlineSnapshot(`
{
"componentStack": "
at __vite_ssr_import_4__.withErrorBoundary.FallbackComponent
at ErrorBoundary
at ErrorBoundary
at withErrorBoundary",
}
`)

View File

@@ -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)
}

View File

@@ -1,5 +1,21 @@
# @blitzjs/rpc
## 2.0.0-alpha.49
### Patch Changes
- bec9512e: Allow resolverPath to be a function which is ran for every file path that is converted to RPC Route
- @blitzjs/auth@2.0.0-alpha.49
- blitz@2.0.0-alpha.49
## 2.0.0-alpha.48
### Patch Changes
- Updated dependencies [93851d90]
- blitz@2.0.0-alpha.48
- @blitzjs/auth@2.0.0-alpha.48
## 2.0.0-alpha.47
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/rpc",
"version": "2.0.0-alpha.47",
"version": "2.0.0-alpha.49",
"scripts": {
"build": "unbuild",
"predev": "wait-on -d 250 ../blitz/dist/index-server.d.ts && wait-on -d 250 ../blitz-auth/dist/index-browser.d.ts",
@@ -20,7 +20,7 @@
"dist/**"
],
"dependencies": {
"@blitzjs/auth": "2.0.0-alpha.47",
"@blitzjs/auth": "2.0.0-alpha.49",
"b64-lite": "1.4.0",
"bad-behavior": "1.0.1",
"chalk": "^4.1.0",
@@ -30,11 +30,11 @@
"zod": "3.10.1"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-alpha.47",
"@blitzjs/config": "workspace:2.0.0-alpha.49",
"@types/debug": "4.1.7",
"@types/react": "18.0.1",
"@types/react-dom": "17.0.14",
"blitz": "2.0.0-alpha.47",
"blitz": "2.0.0-alpha.49",
"next": "12.1.6-canary.17",
"react": "18.0.0",
"react-dom": "18.0.0",
@@ -43,7 +43,7 @@
"watch": "1.0.2"
},
"peerDependencies": {
"blitz": "2.0.0-alpha.47",
"blitz": "2.0.0-alpha.49",
"next": "*"
},
"publishConfig": {

View File

@@ -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.
@@ -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

View File

@@ -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)

View File

@@ -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"
}

View File

@@ -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, "")
}

View File

@@ -1,5 +1,18 @@
# blitz
## 2.0.0-alpha.49
### Patch Changes
- @blitzjs/generator@2.0.0-alpha.49
## 2.0.0-alpha.48
### Patch Changes
- 93851d90: Handle duplicate imports with Blitz upgrade-legacy codemod
- @blitzjs/generator@2.0.0-alpha.48
## 2.0.0-alpha.47
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "blitz",
"version": "2.0.0-alpha.47",
"version": "2.0.0-alpha.49",
"scripts": {
"build": "unbuild",
"dev": "watch unbuild src --wait=0.2",
@@ -23,7 +23,7 @@
"blitz": "bin/blitz"
},
"dependencies": {
"@blitzjs/generator": "2.0.0-alpha.47",
"@blitzjs/generator": "2.0.0-alpha.49",
"arg": "5.0.1",
"chalk": "^4.1.0",
"console-table-printer": "2.10.0",
@@ -52,7 +52,7 @@
"watchpack": "2.1.1"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-alpha.47",
"@blitzjs/config": "workspace:2.0.0-alpha.49",
"@types/cookie": "0.4.1",
"@types/cross-spawn": "6.0.2",
"@types/debug": "4.1.7",

View File

@@ -9,6 +9,7 @@ export * from "./types"
export * from "./prisma-utils"
export * from "./middleware"
export * from "./paginate"
export {baseLogger, newLine, log} from "./logging"
export {startWatcher, stopWatcher} from "./cli/utils/routes-manifest"
// const blitzConfig = findBlitzConfigDirectory() as string
// const file = readFileSync(blitzConfig)

View File

@@ -0,0 +1,189 @@
import {ISettingsParam, Logger} from "tslog"
import c from "chalk"
import {Table} from "console-table-printer"
import ora from "ora"
import readline from "readline"
export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal"
declare module globalThis {
let _blitz_baseLogger: Logger
let _blitz_logLevel: LogLevel
}
export const newLine = () => {
const logLevel: LogLevel = globalThis._blitz_logLevel
switch (logLevel) {
case "trace":
case "debug":
case "info":
console.log(" ")
break
case "warn":
case "error":
case "fatal":
default:
//nothing
break
}
}
export const baseLogger = (options?: ISettingsParam): Logger => {
if (globalThis._blitz_baseLogger) return globalThis._blitz_baseLogger
let config
try {
config = {} as any // todo: loadConfigAtRuntime()
} catch {
config = {}
}
globalThis._blitz_baseLogger = new Logger({
minLevel: config?.log?.level || "info",
type: config?.log?.type || "pretty",
dateTimePattern:
process.env.NODE_ENV === "production"
? "year-month-day hour:minute:second.millisecond"
: "hour:minute:second.millisecond",
displayFunctionName: false,
displayFilePath: "hidden",
displayRequestId: false,
dateTimeTimezone:
process.env.NODE_ENV === "production"
? "utc"
: Intl.DateTimeFormat().resolvedOptions().timeZone,
prettyInspectHighlightStyles: {
name: "yellow",
number: "blue",
bigint: "blue",
boolean: "blue",
},
maskValuesOfKeys: ["password", "passwordConfirmation"],
exposeErrorCodeFrame: process.env.NODE_ENV !== "production",
...options,
})
return globalThis._blitz_baseLogger
}
export const table = Table
export const chalk = c
// const blitzTrueBrandColor = '6700AB'
const blitzBrightBrandColor = "8a3df0"
// Using bright brand color so it's better for dark terminals
const brandColor = blitzBrightBrandColor
const withBrand = (str: string) => {
return c.hex(brandColor).bold(str)
}
const withCaret = (str: string) => {
return `${c.gray(">")} ${str}`
}
const withCheck = (str: string) => {
return `${c.green("✔")} ${str}`
}
const withProgress = (str: string) => {
return withCaret(str)
}
/**
* Logs a branded purple message to stdout.
*
* @param {string} msg
*/
const branded = (msg: string) => {
console.log(c.hex(brandColor).bold(msg))
}
/**
* Clears the line and optionally log a message to stdout.
*
* @param {string} msg
*/
const clearLine = (msg?: string) => {
readline.clearLine(process.stdout, 0)
readline.cursorTo(process.stdout, 0)
msg && process.stdout.write(msg)
}
const clearConsole = () => {
if (process.platform === "win32") {
process.stdout.write("\x1B[2J\x1B[0f")
} else {
process.stdout.write("\x1B[2J\x1B[3J\x1B[H")
}
}
/**
* Logs a progress message to stdout.
*
* @param {string} msg
*/
const progress = (msg: string) => {
console.log(withProgress(msg))
}
const spinner = (str: string) => {
return ora({
text: str,
color: "blue",
spinner: {
interval: 120,
frames: ["◢", "◣", "◤", "◥"],
},
})
}
/**
* Logs a green success message to stdout.
*
* @param {string} msg
*/
const success = (msg: string) => {
console.log(withCheck(c.green(msg)))
}
/**
* Logs a red error message to stdout.
*
* @param {string} msg
*/
const error = (msg: string) => {
console.log(`${c.red(msg)}`)
}
/**
* Colorizes a variable for display.
*
* @param {string} val
*/
const variable = (val: any) => {
return c.cyan.bold(`${val}`)
}
/**
* If the DEBUG env var is set this will write to the console
* @param str msg
*/
const debug = require("debug")("blitz")
export const log = {
withBrand,
withCaret,
branded,
clearLine,
clearConsole,
progress,
spinner,
success,
error,
variable,
debug,
Table,
}

View File

@@ -1,5 +1,21 @@
# @blitzjs/codemod
## 2.0.0-alpha.49
### Patch Changes
- @blitzjs/generator@2.0.0-alpha.49
- blitz@2.0.0-alpha.49
## 2.0.0-alpha.48
### Patch Changes
- 93851d90: Handle duplicate imports with Blitz upgrade-legacy codemod
- Updated dependencies [93851d90]
- blitz@2.0.0-alpha.48
- @blitzjs/generator@2.0.0-alpha.48
## 2.0.0-alpha.47
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/codemod",
"version": "2.0.0-alpha.47",
"version": "2.0.0-alpha.49",
"scripts": {
"build": "unbuild",
"dev": "watch unbuild src --wait=0.2",
@@ -25,9 +25,9 @@
"@babel/plugin-proposal-class-properties": "7.17.12",
"@babel/plugin-syntax-jsx": "7.17.12",
"@babel/plugin-syntax-typescript": "7.17.12",
"@blitzjs/generator": "2.0.0-alpha.47",
"@blitzjs/generator": "2.0.0-alpha.49",
"arg": "5.0.1",
"blitz": "2.0.0-alpha.47",
"blitz": "2.0.0-alpha.49",
"chalk": "^4.1.0",
"cross-spawn": "7.0.3",
"debug": "4.3.3",

View File

@@ -12,6 +12,9 @@ import {
getCollectionFromSource,
wrapDeclaration,
findIdentifier,
removeImport,
replaceImport,
replaceIdentifiers,
} from "./utils"
import {log} from "blitz"
@@ -98,6 +101,10 @@ const upgradeLegacy = async () => {
name: "update project's imports",
action: async () => {
const specialImports: Record<string, string> = {
NextApiHandler: "next",
NextApiRequest: "next",
NextApiResponse: "next",
Link: "next/link",
Image: "next/image",
Script: "next/script",
@@ -139,6 +146,7 @@ const upgradeLegacy = async () => {
useSession: "@blitzjs/auth",
useAuthenticatedSession: "@blitzjs/auth",
useRedirectAuthenticated: "@blitzjs/auth",
SessionContext: "@blitzjs/auth",
useAuthorize: "@blitzjs/auth",
useQuery: "@blitzjs/rpc",
useParam: "@blitzjs/next",
@@ -155,6 +163,7 @@ const upgradeLegacy = async () => {
dehydrate: "@blitzjs/rpc",
invoke: "@blitzjs/rpc",
Routes: "@blitzjs/next",
useRouterQuery: "next/router",
useRouter: "next/router",
Router: "next/router",
@@ -182,26 +191,16 @@ const upgradeLegacy = async () => {
parsedProgram.value.program.body.forEach((e: ImportDeclaration) => {
if (e.type === "ImportDeclaration") {
if (e.source.value === "blitz") {
const specifierIndexesToRemove: number[] = []
e.specifiers?.slice().forEach((specifier: any, index) => {
e.specifiers?.slice().forEach((specifier: any) => {
const importedName =
specifier.imported.type === "StringLiteral"
? specifier.imported.value
: specifier.imported.name
if (importedName in specialImports) {
parsedProgram.value.program.body.unshift(
j.importDeclaration(
[specifier],
j.stringLiteral(specialImports[importedName] as string),
),
)
specifierIndexesToRemove.push(index)
addNamedImport(program, importedName, specialImports[importedName]!)
removeImport(program, importedName, "blitz")
}
})
// Remove import from original blitz import deconstruct
specifierIndexesToRemove.reverse().forEach((index) => {
e.specifiers?.splice(index, 1)
})
// Removed left over "import 'blitz';"
if (!e.specifiers?.length) {
const index = parsedProgram.value.program.body.indexOf(e)
@@ -300,30 +299,41 @@ const upgradeLegacy = async () => {
(file) => {
const program = getCollectionFromSource(file)
findIdentifier(program, "BlitzApiRequest")
.paths()
.forEach((path) => {
if (path.parentPath.parentPath.parentPath.value.type === "ImportDeclaration") {
path.parentPath.parentPath.parentPath.value.source.value = "next"
}
path.value.name = "NextApiRequest"
})
findIdentifier(program, "BlitzApiResponse")
.paths()
.forEach((path) => {
if (path.parentPath.parentPath.parentPath.value.type === "ImportDeclaration") {
path.parentPath.parentPath.parentPath.value.source.value = "next"
}
path.value.name = "NextApiResponse"
})
findIdentifier(program, "BlitzApiHandler")
.paths()
.forEach((path) => {
if (path.parentPath.parentPath.parentPath.value.type === "ImportDeclaration") {
path.parentPath.parentPath.parentPath.value.source.value = "next"
}
path.value.name = "NextApiHandler"
})
replaceImport(program, "blitz", "BlitzApiRequest", "next", "NextApiRequest")
replaceIdentifiers(program, "BlitzApiRequest", "NextApiRequest")
fs.writeFileSync(path.join(path.resolve(file)), program.toSource())
},
)
},
})
steps.push({
name: "change BlitzApiResponse to NextApiResponse",
action: async () => {
getAllFiles(path.join(appDir, "api"), [], [], [".ts", ".tsx", ".js", ".jsx"]).forEach(
(file) => {
const program = getCollectionFromSource(file)
replaceImport(program, "blitz", "BlitzApiResponse", "next", "NextApiResponse")
replaceIdentifiers(program, "BlitzApiResponse", "NextApiResponse")
fs.writeFileSync(path.join(path.resolve(file)), program.toSource())
},
)
},
})
steps.push({
name: "change BlitzApiHandler to NextApiHandler",
action: async () => {
getAllFiles(path.join(appDir, "api"), [], [], [".ts", ".tsx", ".js", ".jsx"]).forEach(
(file) => {
const program = getCollectionFromSource(file)
// BlitzApiHandler -> NextApiHandler
replaceImport(program, "blitz", "BlitzApiHandler", "next", "NextApiHandler")
replaceIdentifiers(program, "BlitzApiHandler", "NextApiHandler")
fs.writeFileSync(path.join(path.resolve(file)), program.toSource())
},

View File

@@ -175,6 +175,14 @@ export function addNamedImport(
)
if (existingImport.length) {
// see if existing import has the same specifier
const existingSpecifier = existingImport.find(
j.ImportSpecifier,
(node) => node.imported.name === importStatement,
)
if (existingSpecifier.length) {
return
}
existingImport.get().value.specifiers.push(j.importSpecifier(j.identifier(importStatement)))
} else {
program
@@ -193,6 +201,23 @@ export function addNamedImport(
}
}
export function removeImport(
program: Collection<any>,
importStatement: string,
importFrom: string,
) {
const existingImport = program.find(
j.ImportDeclaration,
(node) => node.source.value === importFrom,
)
if (existingImport.length) {
existingImport.get().value.specifiers = existingImport
.get()
.value.specifiers.filter((specifier: any) => specifier.imported.name !== importStatement)
}
}
export function getAllFiles(
dirPath: string,
accFiles: string[] = [],
@@ -241,3 +266,44 @@ export function getCollectionFromSource(filename: string) {
},
})
}
export function replaceImport(
program: Collection<any>,
importFrom: string,
importStatement: string,
newImportFrom: string,
newImportStatement: string,
) {
const existingImport = findImport(program, importFrom)
existingImport?.forEach((node) => {
const getNode = node.get()
getNode.value.specifiers.slice().forEach((specifier: any, index: number) => {
const importedName =
specifier.imported.type === "StringLiteral"
? specifier.imported.value
: specifier.imported.name
if (importedName === importStatement) {
addNamedImport(program, newImportStatement, newImportFrom)
// make sure we don't have the same import twice
removeImport(program, newImportStatement, importFrom)
getNode.value.specifiers.splice(index, 1)
if (!getNode.value.specifiers?.length) {
const index = program.get().value.program.body.indexOf(getNode.value)
program.get().value.program.body.splice(index, 1)
}
}
})
})
}
export function replaceIdentifiers(
program: Collection<any>,
identifier: string,
newIdentifier: string,
) {
findIdentifier(program, identifier)
.paths()
.forEach((path) => {
path.value.name = "NextApiRequest"
})
}

View File

@@ -1,5 +1,9 @@
# @blitzjs/config
## 2.0.0-alpha.49
## 2.0.0-alpha.48
## 2.0.0-alpha.47
## 2.0.0-alpha.46

View File

@@ -1,7 +1,7 @@
{
"name": "@blitzjs/config",
"private": true,
"version": "2.0.0-alpha.47",
"version": "2.0.0-alpha.49",
"license": "MIT",
"dependencies": {
"@typescript-eslint/eslint-plugin": "5.9.1",

View File

@@ -1,5 +1,9 @@
# @blitzjs/generator
## 2.0.0-alpha.49
## 2.0.0-alpha.48
## 2.0.0-alpha.47
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/generator",
"version": "2.0.0-alpha.47",
"version": "2.0.0-alpha.49",
"scripts": {
"dev": "watch unbuild src --wait=0.2",
"build": "unbuild && pnpm build:templates",
@@ -45,7 +45,7 @@
"vinyl": "2.2.1"
},
"devDependencies": {
"@blitzjs/config": "2.0.0-alpha.47",
"@blitzjs/config": "2.0.0-alpha.49",
"@juanm04/cpx": "2.0.1",
"@types/babel__core": "7.1.19",
"@types/diff": "5.0.2",

View File

@@ -25,7 +25,7 @@
"@typescript-eslint/parser": "5.9.1"
},
"devDependencies": {
"@blitzjs/config": "2.0.0-alpha.47",
"@blitzjs/config": "2.0.0-alpha.49",
"@types/react": "18.0.1",
"@types/react-dom": "17.0.14",
"react": "18.0.0",

30
pnpm-lock.yaml generated
View File

@@ -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.46
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.46
"@blitzjs/generator": 2.0.0-alpha.46
"@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.46
"@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.46
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.46
"@blitzjs/rpc": 2.0.0-alpha.46
"@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.46
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.46
"@blitzjs/config": workspace:2.0.0-alpha.46
"@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.46
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.46
"@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.46
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.46
"@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.46
"@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