generated from Daydreamer-riri/starter-lib
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: react18 legacy mode & react17 support (#50)
Co-authored-by: Riri <[email protected]>
- Loading branch information
1 parent
ef8ab7d
commit 12d31df
Showing
8 changed files
with
111 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "vite-react-ssg", | ||
"type": "module", | ||
"version": "0.8.2", | ||
"version": "0.8.3-beta.2", | ||
"packageManager": "[email protected]", | ||
"description": "Static-site generation for React on Vite.", | ||
"author": "Riri <[email protected]>", | ||
|
@@ -85,8 +85,8 @@ | |
"beasties": "^0.1.0", | ||
"critters": "^0.0.24", | ||
"prettier": "*", | ||
"react": "^18.0.0", | ||
"react-dom": "^18.0.0", | ||
"react": "^17.0.2||^18.0.0", | ||
"react-dom": "^17.0.2||^18.0.0", | ||
"react-router-dom": "^6.14.1", | ||
"styled-components": "^6.0.0", | ||
"vite": "^2.0.0 || ^3.0.0 || ^4.0.0 || ^5.0.0" | ||
|
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,72 @@ | ||
import type { ReactElement } from 'react' | ||
import React from 'react' | ||
import * as ReactDOM from 'react-dom' | ||
|
||
export interface RootType { | ||
render: (container: ReactElement) => void | ||
_unmount: () => void | ||
} | ||
export interface RootTypeReact extends RootType { | ||
unmount?: () => void | ||
} | ||
export type CreateRootFnType = (container: Element | DocumentFragment) => RootTypeReact | ||
|
||
export type HydrateRootFnType = (container: Element | DocumentFragment, initialChildren: React.ReactNode) => RootTypeReact | ||
|
||
const CopyReactDOM = { | ||
...ReactDOM, | ||
} as typeof ReactDOM & { | ||
createRoot: CreateRootFnType | ||
hydrateRoot: HydrateRootFnType | ||
} & { | ||
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: { | ||
usingClientEntryPoint: boolean | ||
} | ||
} | ||
|
||
const { version, render: reactRender, hydrate: reactHydrate } = CopyReactDOM | ||
|
||
const isReact18 = Number((version || '').split('.')[0]) > 17 | ||
|
||
interface RenderOptions { | ||
useLegacyRender?: boolean | ||
} | ||
|
||
export function render(app: JSX.Element, container: Element | DocumentFragment, renderOptions: RenderOptions = {}) { | ||
const { useLegacyRender } = renderOptions | ||
|
||
if (useLegacyRender || !isReact18) { | ||
reactRender(app, container) | ||
} | ||
else { | ||
CopyReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.usingClientEntryPoint = true | ||
const { createRoot } = CopyReactDOM | ||
if (!createRoot) { | ||
throw new Error('createRoot not found') | ||
} | ||
const root = createRoot(container) | ||
CopyReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.usingClientEntryPoint = false | ||
React.startTransition(() => { | ||
root.render(app) | ||
}) | ||
} | ||
} | ||
|
||
export function hydrate(app: JSX.Element, container: Element | DocumentFragment, renderOptions: RenderOptions = {}) { | ||
const { useLegacyRender } = renderOptions | ||
|
||
if (useLegacyRender || !isReact18) { | ||
reactHydrate(app, container) | ||
} | ||
else { | ||
CopyReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.usingClientEntryPoint = true | ||
const { hydrateRoot } = CopyReactDOM | ||
if (!hydrateRoot) { | ||
throw new Error('hydrateRoot not found') | ||
} | ||
React.startTransition(() => { | ||
hydrateRoot(container, app) | ||
CopyReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.usingClientEntryPoint = false | ||
}) | ||
} | ||
} |
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