Skip to content

Commit f3f19d0

Browse files
authored
TypeScript compat changes for CRA and AF (#45)
1 parent 212872a commit f3f19d0

File tree

14 files changed

+80
-79
lines changed

14 files changed

+80
-79
lines changed

src/constants.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Settings } from './types';
2+
13
export const PACKAGE_NAME = 'react-loosely-lazy';
24

35
export const MODE = {
@@ -15,10 +17,6 @@ export const PHASE_LAZY_DELAY = 50;
1517

1618
export const COLLECTED = new Map();
1719

18-
export type Settings = {
19-
CURRENT_MODE: typeof MODE.HYDRATE | typeof MODE.RENDER;
20-
};
21-
2220
export const SETTINGS: Settings = {
2321
CURRENT_MODE: MODE.HYDRATE,
2422
};

src/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export { MODE, SETTINGS } from './constants';
2-
export type { Settings } from './constants';
32

43
export { LooselyLazy as default } from './init';
54

@@ -11,15 +10,17 @@ export {
1110
LoaderError,
1211
} from './lazy';
1312

14-
export type {
13+
export {
1514
ClientLoader,
1615
Loader,
17-
Options as LazyOptions,
16+
LazyOptions,
1817
ServerLoader,
18+
LazyComponent,
1919
} from './lazy';
2020

2121
export { LazySuspense } from './suspense';
22-
export type { Fallback, LazySuspenseProps } from './suspense';
22+
export { Fallback, LazySuspenseProps } from './suspense';
2323

2424
export { LazyWait, useLazyPhase } from './phase';
25-
export type { LazyWaitProps } from './phase';
25+
export { LazyWaitProps } from './phase';
26+
export { Settings } from './types';

src/lazy/__tests__/lazy.typescript.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { ComponentType } from 'react';
22
import { lazyForPaint } from 'react-loosely-lazy';
3-
import type { FooProps } from './__fixtures__/foo';
3+
import { FooProps } from './__fixtures__/foo';
44

55
const UntypedEmptyPropsTestComponent = lazyForPaint(() =>
66
import('./__fixtures__/empty-props')

src/lazy/index.tsx

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,25 @@
1-
import type { ComponentProps, ComponentType, FunctionComponent } from 'react';
1+
import { ComponentProps, ComponentType, FunctionComponent } from 'react';
22

33
import { PHASE } from '../constants';
44
import { hash, displayNameFromId, isNodeEnvironment } from '../utils';
5-
import type { Asset, Manifest } from '../webpack';
5+
import { Asset, Manifest } from '../webpack';
66

77
import { createComponentClient } from './components/client';
88
import { createComponentServer } from './components/server';
99
import { createDeferred } from './deferred';
1010
import { ClientLoader, Loader, ServerLoader } from './loader';
11+
import { LazyOptions, LazyComponent } from './types';
1112

12-
export type { Asset, Manifest };
13-
14-
export type Options = {
15-
/**
16-
* Whenever it should be required and rendered in SSR
17-
* If false it will just render the provided fallback
18-
*/
19-
ssr?: boolean;
20-
/**
21-
* Id of `PHASE` responsible for start loading
22-
*/
23-
defer?: number;
24-
/**
25-
* Id of the module being imported (normally its path).
26-
* It's calculated and provided by babel plugin
27-
*/
28-
moduleId?: string;
29-
};
30-
31-
export type LazyComponent<C extends ComponentType<any>> = FunctionComponent<
32-
ComponentProps<C>
33-
> & {
34-
preload: () => void;
35-
getAssetUrls: (manifest: Manifest) => string[] | undefined;
36-
};
13+
export { Asset, Manifest, LazyOptions, LazyComponent };
3714

3815
function lazyProxy<C extends ComponentType<any>>(
3916
loader: Loader<C>,
40-
{ defer = PHASE.PAINT, moduleId = '', ssr = true }: Options = {}
17+
{ defer = PHASE.PAINT, moduleId = '', ssr = true }: LazyOptions = {}
4118
): LazyComponent<C> {
4219
const isServer = isNodeEnvironment();
4320
const dataLazyId = hash(moduleId);
4421

45-
const LazyComponent: FunctionComponent<ComponentProps<C>> = isServer
22+
const LazyInternal: FunctionComponent<ComponentProps<C>> = isServer
4623
? createComponentServer({
4724
dataLazyId,
4825
loader: loader as ServerLoader<C>,
@@ -57,7 +34,7 @@ function lazyProxy<C extends ComponentType<any>>(
5734
ssr,
5835
});
5936

60-
LazyComponent.displayName = `Lazy(${displayNameFromId(moduleId)})`;
37+
LazyInternal.displayName = `Lazy(${displayNameFromId(moduleId)})`;
6138

6239
const getAssetUrls = (manifest: Manifest) => {
6340
if (!manifest[moduleId]) {
@@ -87,7 +64,7 @@ function lazyProxy<C extends ComponentType<any>>(
8764
head.appendChild(link);
8865
};
8966

90-
return Object.assign(LazyComponent, {
67+
return Object.assign(LazyInternal, {
9168
getAssetUrls,
9269
preload,
9370
});
@@ -103,7 +80,7 @@ export const DEFAULT_OPTIONS: {
10380

10481
export function lazyForPaint<C extends ComponentType<any>>(
10582
loader: Loader<C>,
106-
opts?: Options
83+
opts?: LazyOptions
10784
) {
10885
return lazyProxy<C>(loader, {
10986
...DEFAULT_OPTIONS.lazyForPaint,
@@ -113,7 +90,7 @@ export function lazyForPaint<C extends ComponentType<any>>(
11390

11491
export function lazyAfterPaint<C extends ComponentType<any>>(
11592
loader: Loader<C>,
116-
opts?: Options
93+
opts?: LazyOptions
11794
) {
11895
return lazyProxy<C>(loader, {
11996
...DEFAULT_OPTIONS.lazyAfterPaint,
@@ -123,13 +100,13 @@ export function lazyAfterPaint<C extends ComponentType<any>>(
123100

124101
export function lazy<C extends ComponentType<any>>(
125102
loader: Loader<C>,
126-
opts?: Options
103+
opts?: LazyOptions
127104
) {
128105
return lazyProxy<C>(loader, {
129106
...DEFAULT_OPTIONS.lazy,
130107
...(opts || {}),
131108
});
132109
}
133110

134-
export type { ClientLoader, Loader, ServerLoader };
111+
export { ClientLoader, Loader, ServerLoader };
135112
export { LoaderError, isLoaderError } from './errors/loader-error';

src/lazy/types.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { ComponentProps, ComponentType, FunctionComponent } from 'react';
2+
import { Manifest } from '../webpack';
3+
4+
export type LazyOptions = {
5+
/**
6+
* Whenever it should be required and rendered in SSR
7+
* If false it will just render the provided fallback
8+
*/
9+
ssr?: boolean;
10+
/**
11+
* Id of `PHASE` responsible for start loading
12+
*/
13+
defer?: number;
14+
/**
15+
* Id of the module being imported (normally its path).
16+
* It's calculated and provided by babel plugin
17+
*/
18+
moduleId?: string;
19+
};
20+
21+
export type LazyComponent<C extends ComponentType<any>> = FunctionComponent<
22+
ComponentProps<C>
23+
> & {
24+
preload: () => void;
25+
getAssetUrls: (manifest: Manifest) => string[] | undefined;
26+
};

src/phase/component.tsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
1-
import React, {
2-
ReactNode,
3-
useContext,
4-
useMemo,
5-
useRef,
6-
useEffect,
7-
} from 'react';
1+
import React, { useContext, useMemo, useRef, useEffect } from 'react';
82

93
import { PHASE } from '../constants';
104
import { LazyPhaseContext } from './context';
115
import { Listener } from './listeners';
126
import { createSubscribe } from './utils';
13-
14-
export type LazyWaitProps = {
15-
until: boolean;
16-
children: ReactNode;
17-
};
7+
import { LazyWaitProps } from './types';
188

199
export const LazyWait = ({ until, children }: LazyWaitProps) => {
2010
const { api: ctxApi } = useContext(LazyPhaseContext);

src/phase/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export { LazyWait } from './component';
2-
export type { LazyWaitProps } from './component';
32
export { useLazyPhase, LazyPhaseContext, setCurrent } from './context';
43
export { usePhaseSubscription } from './controller';
54
export { LISTENERS } from './listeners';
5+
export { LazyWaitProps } from './types';

src/phase/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { ReactNode } from 'react';
2+
3+
export type LazyWaitProps = {
4+
until: boolean;
5+
children: ReactNode;
6+
};

src/suspense/component.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,8 @@ import React, {
55
FunctionComponent,
66
} from 'react';
77
import { isNodeEnvironment } from '../utils';
8-
import {
9-
Fallback,
10-
LazySuspenseContext,
11-
LazySuspenseContextType,
12-
} from './context';
13-
14-
export type LazySuspenseProps = {
15-
fallback: Fallback;
16-
};
8+
import { LazySuspenseContext } from './context';
9+
import { Fallback, LazySuspenseContextType, LazySuspenseProps } from './types';
1710

1811
type LazySuspenseState = LazySuspenseContextType;
1912

src/suspense/context.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
import React, { Fragment, createContext, SuspenseProps } from 'react';
2-
3-
export type Fallback = SuspenseProps['fallback'];
4-
5-
export type LazySuspenseContextType = {
6-
fallback: Fallback;
7-
setFallback(fallback: Fallback): void;
8-
};
1+
import React, { Fragment, createContext } from 'react';
2+
import { Fallback, LazySuspenseContextType } from './types';
93

104
export const LazySuspenseContext = createContext<LazySuspenseContextType>({
115
fallback: <Fragment />,

0 commit comments

Comments
 (0)