-
-
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
29 changed files
with
449 additions
and
108 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
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 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,15 @@ | ||
type ModuleRef<_> = string; | ||
type ErrorMessage = 'You must annotate type with "<typeof import(\'xyz\')>"'; | ||
|
||
interface DeferredImport<T> { | ||
onReady(resource: () => void): () => void; | ||
mod: T | null; | ||
} | ||
|
||
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,28 @@ | ||
import React, {FC, Suspense, useEffect} from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
import ModulePhase1 from './phase1'; | ||
import {deferredLoadComponent} from './utils'; | ||
const Phase2 = deferredLoadComponent( | ||
importDeferredForDisplay<typeof import('./phase2')>('./phase2'), | ||
); | ||
const Phase3 = deferredLoadComponent( | ||
importDeferred<typeof import('./phase3')>('./phase3'), | ||
); | ||
|
||
function App() { | ||
return ( | ||
<> | ||
<div>App</div> | ||
<ModulePhase1 /> | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<Phase2 /> | ||
</Suspense> | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<Phase3 /> | ||
</Suspense> | ||
</> | ||
); | ||
} | ||
|
||
ReactDOM.render(<App />, 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'; | ||
|
||
const Lazy = () => <div>Lazy</div>; | ||
|
||
export default Lazy; |
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'; | ||
|
||
const Phase1 = () => <div>Phase 1</div>; | ||
|
||
export default Phase1; |
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'; | ||
|
||
const ModulePhase2 = () => <div>Phase 2</div>; | ||
|
||
export default ModulePhase2; |
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'; | ||
|
||
const ModulePhase3 = () => <div>Phase 3</div>; | ||
|
||
export default ModulePhase3; |
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,25 @@ | ||
import React, {FC, useEffect} from 'react'; | ||
|
||
export function deferredLoadComponent<T>( | ||
Resource: DeferredImport<T extends {default: any} ? T : never>, | ||
): FC { | ||
let loaded = false; | ||
let cleanUp: undefined | (() => void); | ||
return function WrappedComponent(props) { | ||
useEffect(() => { | ||
return () => { | ||
cleanUp?.(); | ||
}; | ||
}, []); | ||
if (loaded) { | ||
return <Resource.mod.default {...props} />; | ||
} else { | ||
throw new Promise(resolve => { | ||
cleanUp = Resource.onReady(() => { | ||
loaded = true; | ||
resolve(Resource); | ||
}); | ||
}); | ||
} | ||
}; | ||
} |
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.