Skip to content

Commit

Permalink
Add support for blitz.config.ts (#2283)
Browse files Browse the repository at this point in the history
(minor)
  • Loading branch information
flybayer authored Apr 29, 2021
1 parent 4bb6ded commit 0086a1f
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {sessionMiddleware, simpleRolesIsAuthorized} = require("blitz")
import {sessionMiddleware, simpleRolesIsAuthorized} from "blitz"
import db from "db"
const withBundleAnalyzer = require("@next/bundle-analyzer")({
enabled: process.env.ANALYZE === "true",
})
Expand All @@ -8,6 +9,7 @@ module.exports = withBundleAnalyzer({
sessionMiddleware({
isAuthorized: simpleRolesIsAuthorized,
// sessionExpiryMinutes: 4,
getSession: (handle) => db.session.findFirst({where: {handle}}),
}),
],
cli: {
Expand Down
11 changes: 7 additions & 4 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ require("v8-compile-cache")
const cacheFile = require("path").join(__dirname, ".blitzjs-cli-cache")
const lazyLoad = require("@salesforce/lazy-require").default.create(cacheFile)
lazyLoad.start()
import {buildConfig} from "@blitzjs/config"
import {run as oclifRun} from "@oclif/command"

// Load the .env environment variable so it's available for all commands
require("dotenv-expand")(require("dotenv-flow").config({silent: true}))

export function run() {
oclifRun()
.then(require("@oclif/command/flush"))
// @ts-ignore (TS complains about using `catch`)
.catch(require("@oclif/errors/handle"))
buildConfig().then(() => {
oclifRun()
.then(require("@oclif/command/flush"))
// @ts-ignore (TS complains about using `catch`)
.catch(require("@oclif/errors/handle"))
})
}
1 change: 1 addition & 0 deletions packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"gitHead": "d3b9fce0bdd251c2b1890793b0aa1cd77c1c0922",
"dependencies": {
"esbuild": "^0.11.12",
"fs-extra": "^9.1.0",
"pkg-dir": "^5.0.0"
}
Expand Down
71 changes: 66 additions & 5 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,67 @@
import * as esbuild from "esbuild"
import {existsSync, readJSONSync} from "fs-extra"
import path, {join} from "path"
import pkgDir from "pkg-dir"
const debug = require("debug")("blitz:config")

export function getProjectRoot() {
return (
path.dirname(path.resolve(process.cwd(), "blitz.config.js")) || pkgDir.sync() || process.cwd()
)
return path.dirname(getConfigSrcPath())
}

export function getConfigSrcPath() {
const tsPath = path.resolve(path.join(process.cwd(), "blitz.config.ts"))
if (existsSync(tsPath)) {
return tsPath
} else {
const jsPath = path.resolve(path.join(process.cwd(), "blitz.config.js"))
return jsPath
}
}
export function getConfigBuildPath() {
return path.join(getProjectRoot(), ".blitz", "blitz.config.js")
}

interface BuildConfigOptions {
watch?: boolean
}

export async function buildConfig({watch}: BuildConfigOptions = {}) {
debug("Starting buildConfig...")
const pkg = readJSONSync(path.join(pkgDir.sync()!, "package.json"))
debug("src", getConfigSrcPath())
debug("build", getConfigBuildPath())

const esbuildOptions: esbuild.BuildOptions = {
entryPoints: [getConfigSrcPath()],
outfile: getConfigBuildPath(),
format: "cjs",
bundle: true,
platform: "node",
external: [
"blitz",
"next",
...Object.keys(require("blitz/package").dependencies),
...Object.keys(pkg?.dependencies ?? {}),
...Object.keys(pkg?.devDependencies ?? {}),
],
}

if (watch) {
esbuildOptions.watch = {
onRebuild(error) {
if (error) {
console.error("Failed to re-build blitz config")
} else {
console.log("\n> Blitz config changed - restart for changes to take effect\n")
}
},
}
}

debug("Building config...")
debug("Src: ", getConfigSrcPath())
debug("Build: ", getConfigBuildPath())
await esbuild.build(esbuildOptions)
}

export interface BlitzConfig extends Record<string, unknown> {
Expand Down Expand Up @@ -59,13 +114,19 @@ export const getConfig = (reload?: boolean): BlitzConfig => {

const projectRoot = getProjectRoot()
const nextConfigPath = path.join(projectRoot, "next.config.js")
const blitzConfigPath = path.join(projectRoot, "blitz.config.js")
let blitzConfigPath
if (existsSync(path.join(projectRoot, ".blitz"))) {
blitzConfigPath = path.join(projectRoot, ".blitz", "blitz.config.js")
} else {
// projectRoot is inside .blitz/build/
blitzConfigPath = path.join(projectRoot, "..", "blitz.config.js")
}

debug("nextConfigPath: " + nextConfigPath)
debug("blitzConfigPath: " + blitzConfigPath)

let loadedNextConfig = {}
let loadedBlitzConfig = {}
let loadedBlitzConfig: any = {}
try {
// --------------------------------
// Load next.config.js if it exists
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/auth/auth-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export type IsAuthorizedArgs = "isAuthorized" extends keyof Session

export interface SessionModel extends Record<any, any> {
handle: string
userId?: PublicData["userId"]
expiresAt?: Date
hashedSessionToken?: string
antiCSRFToken?: string
publicData?: string
privateData?: string
userId?: PublicData["userId"] | null
expiresAt?: Date | null
hashedSessionToken?: string | null
antiCSRFToken?: string | null
publicData?: string | null
privateData?: string | null
}

export type SessionConfig = {
Expand All @@ -41,7 +41,7 @@ export type SessionConfig = {
createSession: (session: SessionModel) => Promise<SessionModel>
updateSession: (handle: string, session: Partial<SessionModel>) => Promise<SessionModel>
deleteSession: (handle: string) => Promise<SessionModel>
isAuthorized: (data: {ctx: Ctx; args: any[]}) => boolean
isAuthorized: (data: {ctx: Ctx; args: any}) => boolean
}

export interface SessionContextBase {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/server/auth/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,8 @@ export async function getSessionKernel(
if (req.method !== "GET") {
// The publicData in the DB could have been updated since this client last made
// a request. If so, then we generate a new access token
const hasPublicDataChanged = hash256(persistedSession.publicData) !== hashedPublicData
const hasPublicDataChanged =
hash256(persistedSession.publicData ?? undefined) !== hashedPublicData
if (hasPublicDataChanged) {
debug("PublicData has changed since the last request")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { sessionMiddleware, simpleRolesIsAuthorized } = require("blitz")
import { sessionMiddleware, simpleRolesIsAuthorized } from "blitz"

module.exports = {
middleware: [
Expand Down
2 changes: 1 addition & 1 deletion packages/installer/src/utils/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const paths = {
return "babel.config.js"
},
blitzConfig() {
return "blitz.config.js"
return `blitz.config${ext()}`
},
packageJson() {
return "package.json"
Expand Down
2 changes: 1 addition & 1 deletion packages/installer/test/utils/paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("path utils", () => {
expect(paths.app()).toBe("app/pages/_app.tsx")
expect(paths.entry()).toBe("app/pages/index.tsx")
// Blitz and Babel configs are always JS, we shouldn't transform this extension
expect(paths.blitzConfig()).toBe("blitz.config.js")
expect(paths.blitzConfig()).toBe("blitz.config.ts")
expect(paths.babelConfig()).toBe("babel.config.js")
})

Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@blitzjs/file-pipeline": "0.35.0-canary.2",
"cross-spawn": "7.0.3",
"detect-port": "1.3.0",
"esbuild": "0.11.12",
"esbuild": "^0.11.12",
"expand-tilde": "2.0.2",
"fast-glob": "3.2.5",
"flush-write-stream": "2.0.0",
Expand Down
12 changes: 11 additions & 1 deletion packages/server/src/next-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {ChildProcess} from "child_process"
import {spawn} from "cross-spawn"
import detect from "detect-port"
import * as esbuild from "esbuild"
import {readJSONSync} from "fs-extra"
import path from "path"
import pkgDir from "pkg-dir"
import {ServerConfig, standardBuildFolderPathRegex} from "./config"
import {Manifest} from "./stages/manifest"
import {resolverBuildFolderReplaceRegex, resolverFullBuildPathRegex} from "./stages/rpc"
Expand Down Expand Up @@ -268,13 +270,21 @@ export function startCustomServer(
})
}

const pkg = readJSONSync(path.join(pkgDir.sync()!, "package.json"))

const esbuildOptions: esbuild.BuildOptions = {
entryPoints: [serverSrcPath],
outfile: getCustomServerBuildPath(),
format: "cjs",
bundle: true,
platform: "node",
external: ["blitz", "next", ...Object.keys(require("blitz/package").dependencies)],
external: [
"blitz",
"next",
...Object.keys(require("blitz/package").dependencies),
...Object.keys(pkg.dependencies),
...Object.keys(pkg.devDependencies),
],
}

if (watch) {
Expand Down
8 changes: 5 additions & 3 deletions packages/server/src/stages/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const isNowBuild = () => process.env.NOW_BUILDER || process.env.VERCEL_BUILDER
export const createStageConfig: Stage = ({config, processNewFile, processNewChildFile}) => {
// Preconditions
const hasNextConfig = pathExistsSync(resolve(config.src, "next.config.js"))
const hasBlitzConfig = pathExistsSync(resolve(config.src, "blitz.config.js"))
const hasBlitzConfig =
pathExistsSync(resolve(config.src, "blitz.config.js")) ||
pathExistsSync(resolve(config.src, "blitz.config.ts"))

if (hasNextConfig && !isNowBuild()) {
// TODO: Pause the stream and ask the user if they wish to have their configuration file renamed
Expand Down Expand Up @@ -41,7 +43,7 @@ export const createStageConfig: Stage = ({config, processNewFile, processNewChil
path: resolve(config.src, "next.config.js"),
contents: Buffer.from(`
const {withBlitz} = require('@blitzjs/core/with-blitz');
const config = require('./blitz.config.js');
const config = require('../blitz.config.js');
module.exports = withBlitz(config);
`),
}),
Expand Down Expand Up @@ -74,7 +76,7 @@ module.exports = withBlitz(config);
file.contents = Buffer.from(`
const {withBlitz} = require('@blitzjs/core/with-blitz');
const vercelConfig = require('./next-vercel.config.js');
const config = require('./blitz.config.js');
const config = require('../blitz.config.js');
module.exports = withBlitz({...config, ...vercelConfig});
`)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {sessionMiddleware, simpleRolesIsAuthorized} = require("blitz")
const db = require("./db")
import {sessionMiddleware, simpleRolesIsAuthorized} from "blitz"
import db from "./db"

module.exports = {
middleware: [
Expand Down
10 changes: 9 additions & 1 deletion test/integration/auth/db.js → test/integration/auth/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ const low = require("lowdb")
const FileSync = require("lowdb/adapters/FileSync")
// const Memory = require("lowdb/adapters/Memory")

declare global {
namespace NodeJS {
interface Global {
db: any
}
}
}

let db = global.db || low(new FileSync("db.json"))
global.db = db

Expand All @@ -10,4 +18,4 @@ db.defaults({
sessions: [],
}).write()

module.exports = db
export default db
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9141,10 +9141,10 @@ es6-symbol@^3.1.1, es6-symbol@~3.1.3:
d "^1.0.1"
ext "^1.1.2"

[email protected]:
version "0.11.12"
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.11.12.tgz#8cbe15bcb44212624c3e77c896a835f74dc71c3c"
integrity sha512-c8cso/1RwVj+fbDvLtUgSG4ZJQ0y9Zdrl6Ot/GAjyy4pdMCHaFnDMts5gqFnWRPLajWtEnI+3hlET4R9fVoZng==
esbuild@^0.11.12:
version "0.11.16"
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.11.16.tgz#435f00474fb21e70f59a545b85e4cfc7ba106485"
integrity sha512-34ZWjo4ouvM5cDe7uRoM9GFuMyEmpH9fnVYmomvS1cq9ED9d/0ZG1r+p4P2VbX7ihjv36zA2SWTmP8Zmt/EANA==

escalade@^3.1.1:
version "3.1.1"
Expand Down

0 comments on commit 0086a1f

Please sign in to comment.