This repository has been archived by the owner on Nov 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from fastify/react-ts
feat(react): TypeScript support, starter template
- Loading branch information
Showing
35 changed files
with
891 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// This file serves as a placeholder | ||
// if no context.js file is provided | ||
|
||
export default () => {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Root from '/dx:root.tsx' | ||
|
||
export default function create ({ url, ...serverInit }) { | ||
return ( | ||
<Root url={url} {...serverInit} /> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Head from 'unihead/client' | ||
import { createRoot, hydrateRoot } from 'react-dom/client' | ||
|
||
import create from '/dx:create.tsx' | ||
import routesPromise from '/dx:routes.js' | ||
|
||
mount('main') | ||
|
||
async function mount (target) { | ||
if (typeof target === 'string') { | ||
target = document.querySelector(target) | ||
} | ||
const context = await import('/dx:context.ts') | ||
const ctxHydration = await extendContext(window.route, context) | ||
const head = new Head(window.route.head, window.document) | ||
const resolvedRoutes = await routesPromise | ||
const routeMap = Object.fromEntries( | ||
resolvedRoutes.map((route) => [route.path, route]), | ||
) | ||
|
||
const app = create({ | ||
head, | ||
ctxHydration, | ||
routes: window.routes, | ||
routeMap, | ||
}) | ||
if (ctxHydration.clientOnly) { | ||
createRoot(target).render(app) | ||
} else { | ||
hydrateRoot(target, app) | ||
} | ||
} | ||
|
||
async function extendContext (ctx, { | ||
// The route context initialization function | ||
default: setter, | ||
// We destructure state here just to discard it from extra | ||
state, | ||
// Other named exports from context.js | ||
...extra | ||
}) { | ||
Object.assign(ctx, extra) | ||
if (setter) { | ||
await setter(ctx) | ||
} | ||
return ctx | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
|
||
const fetchMap = new Map() | ||
const resourceMap = new Map() | ||
|
||
export function waitResource (path, id, promise) { | ||
const resourceId = `${path}:${id}` | ||
const loader = resourceMap.get(resourceId) | ||
if (loader) { | ||
if (loader.error) { | ||
throw loader.error | ||
} | ||
if (loader.suspended) { | ||
throw loader.promise | ||
} | ||
resourceMap.delete(resourceId) | ||
|
||
return loader.result | ||
} else { | ||
const loader = { | ||
suspended: true, | ||
error: null, | ||
result: null, | ||
promise: null, | ||
} | ||
loader.promise = promise() | ||
.then((result) => { loader.result = result }) | ||
.catch((loaderError) => { loader.error = loaderError }) | ||
.finally(() => { loader.suspended = false }) | ||
|
||
resourceMap.set(resourceId, loader) | ||
|
||
return waitResource(path, id) | ||
} | ||
} | ||
|
||
export function waitFetch (path) { | ||
const loader = fetchMap.get(path) | ||
if (loader) { | ||
if (loader.error || loader.data?.statusCode === 500) { | ||
if (loader.data?.statusCode === 500) { | ||
throw new Error(loader.data.message) | ||
} | ||
throw loader.error | ||
} | ||
if (loader.suspended) { | ||
throw loader.promise | ||
} | ||
fetchMap.delete(path) | ||
|
||
return loader.data | ||
} else { | ||
const loader = { | ||
suspended: true, | ||
error: null, | ||
data: null, | ||
promise: null, | ||
} | ||
loader.promise = fetch(`/-/data${path}`) | ||
.then((response) => response.json()) | ||
.then((loaderData) => { loader.data = loaderData }) | ||
.catch((loaderError) => { loader.error = loaderError }) | ||
.finally(() => { loader.suspended = false }) | ||
|
||
fetchMap.set(path, loader) | ||
|
||
return waitFetch(path) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,27 @@ | ||
import { Suspense } from 'react' | ||
import { DXApp } from '/dx:core.jsx' | ||
import { Routes, Route } from 'react-router-dom' | ||
import { Router, DXRoute } from '/dx:core.jsx' | ||
|
||
export default function Root ({ url, serverInit }) { | ||
export default function Root ({ url, routes, head, ctxHydration, routeMap }) { | ||
return ( | ||
<Suspense> | ||
<DXApp url={url} {...serverInit} /> | ||
<Router location={url}> | ||
<Routes>{ | ||
routes.map(({ path, component: Component }) => | ||
<Route | ||
key={path} | ||
path={path} | ||
element={ | ||
<DXRoute | ||
head={head} | ||
ctxHydration={ctxHydration} | ||
ctx={routeMap[path]}> | ||
<Component /> | ||
</DXRoute> | ||
} />, | ||
) | ||
}</Routes> | ||
</Router> | ||
</Suspense> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Suspense } from 'react' | ||
import { Routes, Route } from 'react-router-dom' | ||
import { Router, DXRoute } from '/dx:core.jsx' | ||
|
||
export default function Root ({ url, routes, head, ctxHydration, routeMap }) { | ||
return ( | ||
<Suspense> | ||
<Router location={url}> | ||
<Routes>{ | ||
routes.map(({ path, component: Component }) => | ||
<Route | ||
key={path} | ||
path={path} | ||
element={ | ||
<DXRoute | ||
head={head} | ||
ctxHydration={ctxHydration} | ||
ctx={routeMap[path]}> | ||
<Component /> | ||
</DXRoute> | ||
} />, | ||
) | ||
}</Routes> | ||
</Router> | ||
</Suspense> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
parser: '@typescript-eslint/parser', | ||
plugins: ['react'], | ||
extends: [ | ||
'eslint:recommended', | ||
'standard-with-typescript', | ||
'plugin:react/recommended', | ||
], | ||
parserOptions: { | ||
project: './tsconfig.json', | ||
requireConfigFile: false, | ||
ecmaVersion: 2021, | ||
sourceType: 'module', | ||
babelOptions: { | ||
presets: ['@babel/preset-react'], | ||
}, | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
rules: { | ||
'@typescript-eslint/explicit-function-return-type': 'off', | ||
'@typescript-eslint/strict-boolean-expressions': 'off', | ||
'comma-dangle': ['error', 'always-multiline'], | ||
'react/prop-types': 'off', | ||
'import/no-absolute-path': 'off', | ||
'react/react-in-jsx-scope': 'off', | ||
}, | ||
settings: { | ||
react: { | ||
version: '18.0', | ||
}, | ||
}, | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.