Skip to content

Commit

Permalink
chore(auth): Convert @redwoodjs/auth to ESM+CJS dual build (#10417)
Browse files Browse the repository at this point in the history
Co-authored-by: Josh GM Walker <56300765+Josh-Walker-GM@users.noreply.github.com>
  • Loading branch information
Tobbe and Josh-Walker-GM committed Apr 11, 2024
1 parent bb7ad2d commit a2a00cf
Show file tree
Hide file tree
Showing 25 changed files with 134 additions and 68 deletions.
1 change: 1 addition & 0 deletions packages/auth-providers/supabase/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"dependencies": {
"@babel/runtime-corejs3": "7.24.1",
"@redwoodjs/auth": "7.3.1",
"core-js": "3.36.1"
},
"devDependencies": {
Expand Down
1 change: 0 additions & 1 deletion packages/auth/.babelrc.js

This file was deleted.

36 changes: 36 additions & 0 deletions packages/auth/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { renameSync, writeFileSync } from 'node:fs'

import { build, defaultBuildOptions } from '@redwoodjs/framework-tools'

// ESM build
await build({
buildOptions: {
...defaultBuildOptions,
tsconfig: 'tsconfig.build-esm.json',
format: 'esm',
outdir: 'dist/esm',
packages: 'external',
},
})

// CJS build
await build({
buildOptions: {
...defaultBuildOptions,
tsconfig: 'tsconfig.build.json',
packages: 'external',
},
})

// Because the package.json files has `type: module` the CJS entry file can't
// be named `index.js` because in that case it would be treated as an ESM file.
// By changing it to .cjs it will be treated as a CommonJS file.
renameSync('dist/index.js', 'dist/index.cjs')

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

// Place a package.json file with `type: module` in the dist/esm folder so that
// all .js files are treated as ES Module files.
writeFileSync('dist/esm/package.json', JSON.stringify({ type: 'module' }))
17 changes: 10 additions & 7 deletions packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,35 @@
"directory": "packages/auth"
},
"license": "MIT",
"main": "./dist/index.js",
"type": "module",
"exports": {
"types": "./dist/esm/index.d.ts",
"import": "./dist/esm/index.js",
"default": "./dist/index.cjs"
},
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "yarn build:js && yarn build:types",
"build:js": "babel src -d dist --extensions \".js,.jsx,.ts,.tsx\"",
"build": "tsx ./build.ts && yarn build:types",
"build:pack": "yarn pack -o redwoodjs-auth.tgz",
"build:types": "tsc --build --verbose tsconfig.build.json",
"build:types": "tsc --build --verbose tsconfig.build.json && tsc --build --verbose tsconfig.build-esm.json",
"build:watch": "nodemon --watch src --ext \"js,jsx,ts,tsx\" --ignore dist --exec \"yarn build\"",
"prepublishOnly": "NODE_ENV=production yarn build",
"test": "vitest run",
"test:watch": "vitest watch"
},
"dependencies": {
"@babel/runtime-corejs3": "7.24.1",
"core-js": "3.36.1",
"react": "18.2.0"
},
"devDependencies": {
"@babel/cli": "7.24.1",
"@babel/core": "^7.22.20",
"@redwoodjs/framework-tools": "7.3.1",
"@testing-library/jest-dom": "6.4.2",
"@testing-library/react": "14.2.2",
"msw": "1.3.3",
"tsx": "4.7.1",
"typescript": "5.4.3",
"vitest": "1.4.0"
},
Expand Down
40 changes: 20 additions & 20 deletions packages/auth/src/AuthProvider/AuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import type { ReactNode } from 'react'
import React, { useEffect, useState } from 'react'

import type { AuthContextInterface, CurrentUser } from '../AuthContext'
import type { AuthImplementation } from '../AuthImplementation'
import type { AuthContextInterface, CurrentUser } from '../AuthContext.js'
import type { AuthImplementation } from '../AuthImplementation.js'

import type { AuthProviderState } from './AuthProviderState'
import { defaultAuthProviderState } from './AuthProviderState'
import { useCurrentUser } from './useCurrentUser'
import { useForgotPassword } from './useForgotPassword'
import { useHasRole } from './useHasRole'
import { useLogIn } from './useLogIn'
import { useLogOut } from './useLogOut'
import { useReauthenticate } from './useReauthenticate'
import { useResetPassword } from './useResetPassword'
import { useSignUp } from './useSignUp'
import { useToken } from './useToken'
import { useValidateResetToken } from './useValidateResetToken'
import type { AuthProviderState } from './AuthProviderState.js'
import { defaultAuthProviderState } from './AuthProviderState.js'
import { useCurrentUser } from './useCurrentUser.js'
import { useForgotPassword } from './useForgotPassword.js'
import { useHasRole } from './useHasRole.js'
import { useLogIn } from './useLogIn.js'
import { useLogOut } from './useLogOut.js'
import { useReauthenticate } from './useReauthenticate.js'
import { useResetPassword } from './useResetPassword.js'
import { useSignUp } from './useSignUp.js'
import { useToken } from './useToken.js'
import { useValidateResetToken } from './useValidateResetToken.js'

export interface AuthProviderProps {
skipFetchCurrentUser?: boolean
Expand All @@ -35,7 +35,7 @@ export function createAuthProvider<
TResetPasswordOptions,
TResetPassword,
TValidateResetToken,
TClient
TClient,
>(
AuthContext: React.Context<
| AuthContextInterface<
Expand Down Expand Up @@ -72,9 +72,9 @@ export function createAuthProvider<
customProviderHooks?: {
useCurrentUser?: () => Promise<CurrentUser>
useHasRole?: (
currentUser: CurrentUser | null
currentUser: CurrentUser | null,
) => (rolesToCheck: string | string[]) => boolean
}
},
) {
const AuthProvider = ({
children,
Expand All @@ -101,7 +101,7 @@ export function createAuthProvider<
authImplementation,
setAuthProviderState,
getCurrentUser,
skipFetchCurrentUser
skipFetchCurrentUser,
)

const hasRole = customProviderHooks?.useHasRole
Expand All @@ -112,13 +112,13 @@ export function createAuthProvider<
authImplementation,
setAuthProviderState,
getCurrentUser,
skipFetchCurrentUser
skipFetchCurrentUser,
)
const logIn = useLogIn(
authImplementation,
setAuthProviderState,
getCurrentUser,
skipFetchCurrentUser
skipFetchCurrentUser,
)
const logOut = useLogOut(authImplementation, setAuthProviderState)
const forgotPassword = useForgotPassword(authImplementation)
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/AuthProvider/AuthProviderState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CurrentUser } from '../AuthContext'
import type { CurrentUser } from '../AuthContext.js'

export type AuthProviderState<TUser, TClient = unknown> = {
loading: boolean
Expand Down
4 changes: 2 additions & 2 deletions packages/auth/src/AuthProvider/useCurrentUser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useCallback } from 'react'

import type { AuthImplementation } from '../AuthImplementation'
import type { AuthImplementation } from '../AuthImplementation.js'

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

export const useCurrentUser = (authImplementation: AuthImplementation) => {
const getToken = useToken(authImplementation)
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/AuthProvider/useForgotPassword.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback } from 'react'

import type { AuthImplementation } from '../AuthImplementation'
import type { AuthImplementation } from '../AuthImplementation.js'

export const useForgotPassword = <
TUser,
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/AuthProvider/useHasRole.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback } from 'react'

import type { CurrentUser } from '../AuthContext'
import type { CurrentUser } from '../AuthContext.js'

export const useHasRole = (currentUser: CurrentUser | null) => {
/**
Expand Down
10 changes: 5 additions & 5 deletions packages/auth/src/AuthProvider/useLogIn.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useCallback } from 'react'

import type { AuthImplementation } from '../AuthImplementation'
import type { AuthImplementation } from '../AuthImplementation.js'

import type { AuthProviderState } from './AuthProviderState'
import { defaultAuthProviderState } from './AuthProviderState'
import type { useCurrentUser } from './useCurrentUser'
import { useReauthenticate } from './useReauthenticate'
import type { AuthProviderState } from './AuthProviderState.js'
import { defaultAuthProviderState } from './AuthProviderState.js'
import type { useCurrentUser } from './useCurrentUser.js'
import { useReauthenticate } from './useReauthenticate.js'

export const useLogIn = <
TUser,
Expand Down
4 changes: 2 additions & 2 deletions packages/auth/src/AuthProvider/useLogOut.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useCallback } from 'react'

import type { AuthImplementation } from '../AuthImplementation'
import type { AuthImplementation } from '../AuthImplementation.js'

import type { AuthProviderState } from './AuthProviderState'
import type { AuthProviderState } from './AuthProviderState.js'

export const useLogOut = <
TUser,
Expand Down
8 changes: 4 additions & 4 deletions packages/auth/src/AuthProvider/useReauthenticate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useCallback } from 'react'

import type { AuthImplementation } from '../AuthImplementation'
import type { AuthImplementation } from '../AuthImplementation.js'

import type { AuthProviderState } from './AuthProviderState'
import type { useCurrentUser } from './useCurrentUser'
import { useToken } from './useToken'
import type { AuthProviderState } from './AuthProviderState.js'
import type { useCurrentUser } from './useCurrentUser.js'
import { useToken } from './useToken.js'

const notAuthenticatedState = {
isAuthenticated: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/AuthProvider/useResetPassword.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback } from 'react'

import type { AuthImplementation } from '../AuthImplementation'
import type { AuthImplementation } from '../AuthImplementation.js'

export const useResetPassword = <
TUser,
Expand Down
8 changes: 4 additions & 4 deletions packages/auth/src/AuthProvider/useSignUp.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useCallback } from 'react'

import type { AuthImplementation } from '../AuthImplementation'
import type { AuthImplementation } from '../AuthImplementation.js'

import type { AuthProviderState } from './AuthProviderState'
import type { useCurrentUser } from './useCurrentUser'
import { useReauthenticate } from './useReauthenticate'
import type { AuthProviderState } from './AuthProviderState.js'
import type { useCurrentUser } from './useCurrentUser.js'
import { useReauthenticate } from './useReauthenticate.js'

export const useSignUp = <
TUser,
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/AuthProvider/useToken.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback } from 'react'

import type { AuthImplementation } from '../AuthImplementation'
import type { AuthImplementation } from '../AuthImplementation.js'

export const useToken = (authImplementation: AuthImplementation) => {
return useCallback(async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/AuthProvider/useValidateResetToken.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback } from 'react'

import type { AuthImplementation } from '../AuthImplementation'
import type { AuthImplementation } from '../AuthImplementation.js'

export const useValidateResetToken = <
TUser,
Expand Down
4 changes: 2 additions & 2 deletions packages/auth/src/__tests__/AuthProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ globalThis.Headers = HeadersPolyfill
globalThis.Request = RequestPolyfill
globalThis.Response = ResponsePolyfill

import type { CustomTestAuthClient } from './fixtures/customTestAuth'
import { createCustomTestAuth } from './fixtures/customTestAuth'
import type { CustomTestAuthClient } from './fixtures/customTestAuth.js'
import { createCustomTestAuth } from './fixtures/customTestAuth.js'

configure({
asyncUtilTimeout: 5_000,
Expand Down
10 changes: 5 additions & 5 deletions packages/auth/src/authFactory.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { CurrentUser } from './AuthContext'
import { createAuthContext } from './AuthContext'
import type { AuthImplementation } from './AuthImplementation'
import { createAuthProvider } from './AuthProvider/AuthProvider'
import { createUseAuth } from './useAuth'
import type { CurrentUser } from './AuthContext.js'
import { createAuthContext } from './AuthContext.js'
import type { AuthImplementation } from './AuthImplementation.js'
import { createAuthProvider } from './AuthProvider/AuthProvider.js'
import { createUseAuth } from './useAuth.js'

export function createAuthentication<
TUser,
Expand Down
9 changes: 5 additions & 4 deletions packages/auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { AuthContextInterface, CurrentUser } from './AuthContext'
export { useNoAuth, UseAuth } from './useAuth'
export { createAuthentication } from './authFactory'
export type { AuthImplementation } from './AuthImplementation'
export type { AuthContextInterface, CurrentUser } from './AuthContext.js'
export { useNoAuth } from './useAuth.js'
export type { UseAuth } from './useAuth.js'
export { createAuthentication } from './authFactory.js'
export type { AuthImplementation } from './AuthImplementation.js'
2 changes: 1 addition & 1 deletion packages/auth/src/useAuth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'

import type { AuthContextInterface } from './AuthContext'
import type { AuthContextInterface } from './AuthContext.js'

export function createUseAuth<
TUser,
Expand Down
14 changes: 14 additions & 0 deletions packages/auth/tsconfig.build-esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../../tsconfig.compilerOption.json",
"compilerOptions": {
"isolatedModules": true,
"moduleResolution": "NodeNext",
"module": "NodeNext",
"rootDir": "src",
"outDir": "dist/esm",
},
"include": ["src", "ambient.d.ts"],
"references": [
{ "path": "../framework-tools" }
]
}
6 changes: 6 additions & 0 deletions packages/auth/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
{
"extends": "../../tsconfig.compilerOption.json",
"compilerOptions": {
"isolatedModules": true,
"moduleResolution": "NodeNext",
"module": "NodeNext",
"rootDir": "src",
"outDir": "dist",
},
"include": ["src", "ambient.d.ts"],
"references": [
{ "path": "../framework-tools" }
]
}
8 changes: 7 additions & 1 deletion packages/auth/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"extends": "../../tsconfig.compilerOption.json",
"compilerOptions": {
"isolatedModules": true,
"moduleResolution": "NodeNext",
"module": "NodeNext",
"outDir": "dist"
},
"include": [
Expand All @@ -10,5 +13,8 @@
"./vitest.setup.ts",
"__tests__"
],
"exclude": ["dist", "node_modules", "**/__mocks__", "**/__fixtures__"]
"exclude": ["dist", "node_modules", "**/__mocks__", "**/__fixtures__"],
"references": [
{ "path": "../framework-tools" }
]
}
2 changes: 1 addition & 1 deletion packages/testing/src/web/mockAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'

// Exporting everything here, but exports further down in this file will
// override exports with the same name
export * from '@redwoodjs/auth/dist/index'
export * from '@redwoodjs/auth'

import { mockedUserMeta } from './mockRequests'

Expand Down
Loading

0 comments on commit a2a00cf

Please sign in to comment.