-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add no-op tier import API #9865
Changes from all commits
f84f390
5403fc9
d4e043b
7c76b0c
bfd803b
7a18e5d
4ad1af2
f6a1411
0f1aeca
ca5b78b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "@parcel/config-default" | ||
} |
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 --no-hmr --https --feature-flag tieredImports=true", | ||
"start:prod": "yarn build && npx http-server -p 1234 --ssl --key ../.parcel-cache/private.pem --cert ../.parcel-cache/primary.crt 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" | ||
} | ||
} |
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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. interface DeferredImport<T> {
onReady(callback: (T) => void): void;
}
|
||
|
||
declare function unsafe_importDeferredForDisplay<T extends any | void = void>( | ||
source: T extends void ? ErrorMessage : ModuleRef<T>, | ||
): DeferredImport<T>; | ||
|
||
declare function unsafe_importDeferred<T extends any | void = void>( | ||
source: T extends void ? ErrorMessage : ModuleRef<T>, | ||
): DeferredImport<T>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recommend that these only provide access to the default exported value, not the module itself. It's the bundlers responsibility to ensure that devs can't shoot themselves in the foot by doing things like importing the one file both deferred and non-deferred because they're putting multiple values in the one file. |
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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React, {StrictMode, Suspense, useState} from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
import Tier1 from './tier1'; | ||
const DeferredTier2 = | ||
unsafe_importDeferredForDisplay<typeof import('./tier2')>('./tier2'); | ||
const DeferredTier3 = | ||
unsafe_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'), | ||
); |
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>; | ||
} |
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>; | ||
} |
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'); | ||
} |
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>; | ||
} |
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; | ||
} |
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 | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Call this
Deferred