-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
606 additions
and
115 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
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
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,3 @@ | ||
{ | ||
"extends": "@parcel/config-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "@parcel/phases-example", | ||
"version": "2.12.0", | ||
"license": "MIT", | ||
"private": true, | ||
"scripts": { | ||
"start": "parcel serve src/index.html --no-cache --https --feature-flag tieredImports=true", | ||
"start:prod": "yarn build && npx http-server -p 1234 dist/", | ||
"build": "PARCEL_WORKERS=0 parcel build src/index.html --no-cache --feature-flag tieredImports=true", | ||
"debug": "PARCEL_WORKERS=0 node --inspect-brk $(yarn bin parcel) serve src/index.html --no-cache --https --feature-flag tieredImports=true --no-hmr", | ||
"debug:prod": "PARCEL_WORKERS=0 node --inspect-brk $(yarn bin parcel) build src/index.html --no-cache --feature-flag tieredImports=true" | ||
}, | ||
"devDependencies": { | ||
"parcel": "2.12.0" | ||
}, | ||
"dependencies": { | ||
"react": "^17.0.2", | ||
"react-dom": "^17.0.2" | ||
} | ||
} |
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,14 @@ | ||
type ModuleRef<_> = string; | ||
type ErrorMessage = 'You must annotate type with "<typeof import(\'xyz\')>"'; | ||
|
||
interface DeferredImport<T extends {default: any}> { | ||
onReady(resource: (mod: T['default']) => void): void; | ||
} | ||
|
||
declare function importDeferredForDisplay<T extends any | void = void>( | ||
source: T extends void ? ErrorMessage : ModuleRef<T>, | ||
): DeferredImport<T>; | ||
|
||
declare function importDeferred<T extends any | void = void>( | ||
source: T extends void ? ErrorMessage : ModuleRef<T>, | ||
): DeferredImport<T>; |
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,14 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge" /> | ||
<title>Parcel | React Phases</title> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
|
||
<script src="./index.tsx" type="module"></script> | ||
</body> | ||
</html> |
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,51 @@ | ||
import React, {StrictMode, Suspense, useState} from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
import Tier1 from './tier1'; | ||
const DeferredTier2 = | ||
importDeferredForDisplay<typeof import('./tier2')>('./tier2'); | ||
const DeferredTier3 = importDeferred<typeof import('./tier3')>('./tier3'); | ||
|
||
import {deferredLoadComponent} from './utils'; | ||
|
||
const Tier2 = deferredLoadComponent(DeferredTier2); | ||
const Tier3Instance1 = deferredLoadComponent(DeferredTier3); | ||
const Tier3Instance2 = deferredLoadComponent(DeferredTier3); | ||
|
||
function App() { | ||
const [count, setCount] = useState(0); | ||
|
||
return ( | ||
<> | ||
<button | ||
onClick={() => { | ||
setCount(count + 1); | ||
}} | ||
> | ||
{count} | ||
</button> | ||
<div>App</div> | ||
<Tier1 /> | ||
<Suspense fallback={<div>Loading Tier 2...</div>}> | ||
<Tier2 enabled /> | ||
</Suspense> | ||
<Suspense fallback={<div>Loading Tier 3 instance 1...</div>}> | ||
<Tier3Instance1 /> | ||
</Suspense> | ||
<Suspense fallback={<div>Loading Tier 3 instance 2...</div>}> | ||
<Tier3Instance2 /> | ||
</Suspense> | ||
<div> | ||
Tier3Instance1 and Tier3Instance2 are{' '} | ||
{Tier3Instance1 === Tier3Instance2 ? 'the same' : 'different'} | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
ReactDOM.render( | ||
<StrictMode> | ||
<App /> | ||
</StrictMode>, | ||
document.getElementById('app'), | ||
); |
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,5 @@ | ||
import React from 'react'; | ||
|
||
export default function Lazy() { | ||
return <div>Lazy</div>; | ||
} |
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,5 @@ | ||
import React from 'react'; | ||
|
||
export default function Tier1() { | ||
return <div>Tier 1</div>; | ||
} |
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,6 @@ | ||
import React from 'react'; | ||
|
||
export default function Tier2({enabled}: {enabled: boolean}) { | ||
if (enabled) return <div>Tier 2</div>; | ||
throw new Error('Enabled prop missing'); | ||
} |
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,5 @@ | ||
import React from 'react'; | ||
|
||
export default function Tier3() { | ||
return <div>Tier 3</div>; | ||
} |
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,57 @@ | ||
import React, { | ||
ComponentType, | ||
ForwardRefExoticComponent, | ||
ForwardedRef, | ||
MemoExoticComponent, | ||
PropsWithChildren, | ||
PropsWithoutRef, | ||
RefAttributes, | ||
forwardRef, | ||
memo, | ||
} from 'react'; | ||
|
||
export function deferredLoadComponent<P extends {[k: string]: any} | undefined>( | ||
resource: DeferredImport<{default: ComponentType<P>}>, | ||
): MemoExoticComponent< | ||
ForwardRefExoticComponent< | ||
PropsWithoutRef<P> & RefAttributes<ComponentType<P>> | ||
> | ||
> { | ||
// Create a deferred component map in the global context, so we can reuse the components everywhere | ||
if (!globalThis.deferredComponentMap) { | ||
globalThis.deferredComponentMap = new WeakMap<DeferredImport<any>, any>(); | ||
} | ||
|
||
if (globalThis.deferredComponentMap.has(resource)) { | ||
return globalThis.deferredComponentMap.get(resource); | ||
} | ||
|
||
let Component: ComponentType | undefined; | ||
let loader = new Promise(resolve => { | ||
resource.onReady(loaded => { | ||
Component = loaded; | ||
resolve(loaded); | ||
}); | ||
}); | ||
|
||
const wrapper = function DeferredComponent( | ||
props: PropsWithChildren<P>, | ||
ref: ForwardedRef<ComponentType<P>>, | ||
) { | ||
if (Component) { | ||
return <Component {...props} ref={ref} />; | ||
} | ||
|
||
throw loader; | ||
}; | ||
|
||
// Support refs in the deferred component | ||
const forwardedRef = forwardRef(wrapper); | ||
|
||
// Memoise so we avoid re-renders | ||
const memoised = memo(forwardedRef); | ||
|
||
// Store in weak map so we only have one instance | ||
globalThis.deferredComponentMap.set(resource, memoised); | ||
return memoised; | ||
} |
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,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"typeRoots": ["phases.d.ts"], | ||
"jsx": "react", | ||
"lib": ["ESNext", "DOM"], | ||
"esModuleInterop": true | ||
} | ||
} |
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
Oops, something went wrong.