Skip to content

Commit

Permalink
chore(esm/cjs): Make dbauth middleware a dual package (#10948)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Actions <>
  • Loading branch information
dac09 committed Jul 17, 2024
1 parent e51cf72 commit 6be62a4
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 55 deletions.
36 changes: 34 additions & 2 deletions packages/auth-providers/dbAuth/middleware/build.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
import { build } from '@redwoodjs/framework-tools'
import {
build,
defaultBuildOptions,
} from '@redwoodjs/framework-tools'

await build()
import { generateCjsTypes} from '@redwoodjs/framework-tools/cjsTypes'
import { writeFileSync } from 'node:fs'

// CJS build
await build({
buildOptions: {
...defaultBuildOptions,
outdir: 'dist/cjs',
packages: 'external',
},
})

// ESM build
await build({
buildOptions: {
...defaultBuildOptions,
format: 'esm',
packages: 'external',
},
})

// Place a package.json file with `type: commonjs` in the dist/cjs folder so that
// all .js files are treated as CommonJS files.
writeFileSync('dist/cjs/package.json', JSON.stringify({ type: 'commonjs' }))

// Place a package.json file with `type: module` in the dist folder so that
// all .js files are treated as ES Module files.
writeFileSync('dist/package.json', JSON.stringify({ type: 'module' }))

await generateCjsTypes()

This file was deleted.

36 changes: 23 additions & 13 deletions packages/auth-providers/dbAuth/middleware/package.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,54 @@
{
"name": "@redwoodjs/auth-dbauth-middleware",
"type": "module",
"version": "7.0.0",
"repository": {
"type": "git",
"url": "git+https://github.com/redwoodjs/redwood.git",
"directory": "packages/auth-providers/dbAuth/middleware"
},
"license": "MIT",
"exports": {
".": {
"default": "./dist/index.js"
},
"./plugin": {
"import": "./dist/vite-plugin-auth-dbauth-middleware.js",
"default": "./cjsWrappers/plugin.js"
}
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist",
"cjsWrappers"
"dist"
],
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/cjs/index.d.ts",
"default": "./dist/cjs/index.js"
}
}
},
"scripts": {
"build": "tsx ./build.mts && yarn build:types",
"build:pack": "yarn pack -o redwoodjs-auth-dbauth-middleware.tgz",
"build:types": "tsc --build --verbose",
"build:types": "tsc --build --verbose ./tsconfig.json",
"prepublishOnly": "NODE_ENV=production yarn build",
"test": "vitest run",
"test": "concurrently npm:test:vitest npm:test:attw npm:test:publint",
"test:attw": "yarn attw -P",
"build:types-cjs": "tsc --build --verbose tsconfig.types-cjs.json",
"test:publint": "yarn publint",
"test:vitest": "vitest run",
"test:watch": "vitest watch"
},
"dependencies": {
"@redwoodjs/auth-dbauth-api": "workspace:*",
"@redwoodjs/web": "workspace:*"
},
"devDependencies": {
"@arethetypeswrong/cli": "0.15.3",
"@redwoodjs/api": "workspace:*",
"@redwoodjs/framework-tools": "workspace:*",
"@redwoodjs/graphql-server": "workspace:*",
"@types/aws-lambda": "8.10.138",
"concurrently": "8.2.2",
"publint": "0.2.8",
"ts-toolbelt": "9.6.0",
"tsx": "4.15.6",
"typescript": "5.4.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,24 @@ beforeAll(() => {
vi.mock('@redwoodjs/auth-dbauth-api', async (importOriginal) => {
const original = (await importOriginal()) as any
return {
...original,
dbAuthSession: vi.fn().mockImplementation((req, cookieName) => {
if (
req.headers
.get('Cookie')
.includes(`${cookieName}=this_is_the_only_correct_session`)
) {
return {
currentUser: {
email: 'user-1@example.com',
id: 'mocked-current-user-1',
},
mockedSession: 'this_is_the_only_correct_session',
default: {
...original,
dbAuthSession: vi.fn().mockImplementation((req, cookieName) => {
if (
req.headers
.get('Cookie')
.includes(`${cookieName}=this_is_the_only_correct_session`)
) {
return {
currentUser: {
email: 'user-1@example.com',
id: 'mocked-current-user-1',
},
mockedSession: 'this_is_the_only_correct_session',
}
}
}
}),
}),
},
}
})
})
Expand Down
14 changes: 5 additions & 9 deletions packages/auth-providers/dbAuth/middleware/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import type { APIGatewayProxyEvent, Context } from 'aws-lambda'

import type { DbAuthResponse } from '@redwoodjs/auth-dbauth-api'
import {
cookieName as cookieNameCreator,
dbAuthSession,
} from '@redwoodjs/auth-dbauth-api'
import dbAuthApi from '@redwoodjs/auth-dbauth-api'
// ^^ above package is still CJS, and named exports aren't supported in import statements
const { dbAuthSession, cookieName: cookieNameCreator } = dbAuthApi
import type { GetCurrentUser } from '@redwoodjs/graphql-server'
import { MiddlewareResponse } from '@redwoodjs/web/middleware'
import type {
Middleware,
MiddlewareRequest,
} from '@redwoodjs/web/middleware' with { 'resolution-mode': 'import' }
import type { Middleware, MiddlewareRequest } from '@redwoodjs/web/middleware'

import { defaultGetRoles } from './defaultGetRoles'
import { defaultGetRoles } from './defaultGetRoles.js'

export interface DbAuthMiddlewareOptions {
cookieName?: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "dist/cjs",
},
"tsBuildInfoFile": "./tsconfig.types-cjs.tsbuildinfo"
}
}
9 changes: 4 additions & 5 deletions packages/framework-tools/src/cjsTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ export const generateCjsTypes = async () => {

try {
await $`yarn build:types-cjs`
} catch (e) {
console.error('Could not build CJS types')
console.error(e)

process.exit(1)
} catch (e: any) {
console.error('---- Error building CJS types ----')
process.exitCode = e.exitCode
throw new Error(e)
} finally {
await $`mv package.json.bak package.json`
}
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/devFeServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ async function createServer() {
plugins: [
cjsInterop({
dependencies: [
// Skip ESM modules: rwjs/auth, rwjs/web
// Skip ESM modules: rwjs/auth, rwjs/web, rwjs/auth-*-middleware
'@redwoodjs/forms',
'@redwoodjs/prerender/*',
'@redwoodjs/router',
'@redwoodjs/auth-*',
'@redwoodjs/auth-*-api',
'@redwoodjs/auth-*-web',
],
}),
rscEnabled && rscRoutesAutoLoader(),
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/rsc/rscBuildForSsr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ export async function rscBuildForSsr({
plugins: [
cjsInterop({
dependencies: [
// Skip ESM modules: rwjs/auth, rwjs/web
// Skip ESM modules: rwjs/auth, rwjs/web, rwjs/auth-*-middleware
'@redwoodjs/forms',
'@redwoodjs/prerender/*',
'@redwoodjs/router',
'@redwoodjs/router/*',
'@redwoodjs/auth-*',
'@redwoodjs/auth-*-api',
'@redwoodjs/auth-*-web',
],
}),
rscRoutesAutoLoader(),
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/streaming/buildForStreamingServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ export async function buildForStreamingServer({
plugins: [
cjsInterop({
dependencies: [
// Skip ESM modules: rwjs/auth, rwjs/web
// Skip ESM modules: rwjs/auth, rwjs/web, rwjs/auth-*-middleware
'@redwoodjs/forms',
'@redwoodjs/prerender/*',
'@redwoodjs/router',
'@redwoodjs/auth-*',
'@redwoodjs/auth-*-api',
'@redwoodjs/auth-*-web',
],
}),
],
Expand Down
3 changes: 3 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7544,12 +7544,15 @@ __metadata:
version: 0.0.0-use.local
resolution: "@redwoodjs/auth-dbauth-middleware@workspace:packages/auth-providers/dbAuth/middleware"
dependencies:
"@arethetypeswrong/cli": "npm:0.15.3"
"@redwoodjs/api": "workspace:*"
"@redwoodjs/auth-dbauth-api": "workspace:*"
"@redwoodjs/framework-tools": "workspace:*"
"@redwoodjs/graphql-server": "workspace:*"
"@redwoodjs/web": "workspace:*"
"@types/aws-lambda": "npm:8.10.138"
concurrently: "npm:8.2.2"
publint: "npm:0.2.8"
ts-toolbelt: "npm:9.6.0"
tsx: "npm:4.15.6"
typescript: "npm:5.4.5"
Expand Down

0 comments on commit 6be62a4

Please sign in to comment.