piral-lazy 1.0.0-pre.2296 → 1.0.1-beta.5640
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +5 -5
- package/esm/create.js +21 -15
- package/esm/create.js.map +1 -1
- package/esm/types.d.ts +6 -4
- package/lib/create.js +22 -16
- package/lib/create.js.map +1 -1
- package/lib/index.js +1 -1
- package/lib/types.d.ts +6 -4
- package/package.json +27 -8
- package/piral-lazy.min.js +1 -0
- package/src/create.test.tsx +68 -0
- package/src/create.ts +15 -6
- package/src/types.ts +7 -4
- package/src/create.test.ts +0 -34
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
[](https://piral.io)
|
|
2
2
|
|
|
3
|
-
# [Piral Lazy](https://piral.io) · [](https://github.com/smapiot/piral/blob/
|
|
3
|
+
# [Piral Lazy](https://piral.io) · [](https://github.com/smapiot/piral/blob/main/LICENSE) [](https://www.npmjs.com/package/piral-lazy) [](https://jestjs.io) [](https://gitter.im/piral-io/community)
|
|
4
4
|
|
|
5
5
|
This is a plugin that only has a peer dependency to `piral-core`. What `piral-lazy` brings to the table is a set of Pilet API extensions that can be used with `piral` or `piral-core`.
|
|
6
6
|
|
|
@@ -12,14 +12,14 @@ By default, these API extensions are not integrated in `piral`, so you'd need to
|
|
|
12
12
|
|
|
13
13
|
Lazy loading of data or other dependencies can be crucial in more complex scenarios. It should not be necessary to include everything in one load to bring a pilet or components of it to live.
|
|
14
14
|
|
|
15
|
-
`piral-lazy` allows defining generic dependencies that will be loaded *before* loading
|
|
15
|
+
`piral-lazy` allows defining generic dependencies that will be loaded *before* loading/using a component. The provided helpers which are exposed via the pilet API give you the power to
|
|
16
16
|
|
|
17
17
|
1. define new dependencies (given by a unique name and a loader function) and
|
|
18
|
-
2. rely on defined dependencies before actually loading
|
|
18
|
+
2. rely on defined dependencies before actually loading/trying to render a component.
|
|
19
19
|
|
|
20
20
|
If your pilets use lazy loading in some way then `piral-lazy` may be the right choice. Also, if your pilets are too large and could benefit from further resource sharing and lazy loading then this plugin could be helpful. `piral-lazy` remains framework agnostic and thus works beyond React.
|
|
21
21
|
|
|
22
|
-
Alternative: Just define
|
|
22
|
+
Alternative: Just define/use what is there out of the box. By using `React.lazy` together with bundle splitting most of the things may be already properly transported. Remember that you could also lazy load context that provide your dependencies - thus making `React.lazy` a single solution (if your framework is "React").
|
|
23
23
|
|
|
24
24
|
## Documentation
|
|
25
25
|
|
package/esm/create.js
CHANGED
|
@@ -1,30 +1,36 @@
|
|
|
1
|
-
import { lazy } from 'react';
|
|
1
|
+
import { lazy, createElement } from 'react';
|
|
2
2
|
import { withApi } from 'piral-core';
|
|
3
3
|
export function createLazyApi() {
|
|
4
|
-
return
|
|
5
|
-
return
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
return (context) => {
|
|
5
|
+
return (api) => {
|
|
6
|
+
const cache = {};
|
|
7
|
+
const getDependency = (name) => {
|
|
8
8
|
var _a;
|
|
9
|
-
|
|
9
|
+
const dep = cache[name];
|
|
10
10
|
if (!dep) {
|
|
11
|
-
throw new Error(
|
|
11
|
+
throw new Error(`The given dependency "${name}" cannot be found. Please add it first using "defineDependency"`);
|
|
12
12
|
}
|
|
13
13
|
return (_a = dep.result) !== null && _a !== void 0 ? _a : (dep.result = dep.loader());
|
|
14
14
|
};
|
|
15
|
-
var wrapComponent = function (comp) { return ({
|
|
16
|
-
default: withApi(context, comp, api, 'unknown'),
|
|
17
|
-
}); };
|
|
18
15
|
return {
|
|
19
|
-
defineDependency
|
|
16
|
+
defineDependency(name, loader) {
|
|
20
17
|
cache[name] = {
|
|
21
|
-
loader
|
|
18
|
+
loader,
|
|
22
19
|
result: undefined,
|
|
23
20
|
};
|
|
24
21
|
},
|
|
25
|
-
fromLazy
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
fromLazy(load, deps = []) {
|
|
23
|
+
return lazy(() => Promise.all(deps.map(getDependency)).then((values) => {
|
|
24
|
+
const depMap = deps.reduce((obj, name, index) => {
|
|
25
|
+
obj[name] = values[index];
|
|
26
|
+
return obj;
|
|
27
|
+
}, {});
|
|
28
|
+
return load()
|
|
29
|
+
.then((comp) => withApi(context, comp, api, 'unknown'))
|
|
30
|
+
.then((compWithApi) => ({
|
|
31
|
+
default: (props) => createElement(compWithApi, Object.assign({ deps: depMap }, props)),
|
|
32
|
+
}));
|
|
33
|
+
}));
|
|
28
34
|
},
|
|
29
35
|
};
|
|
30
36
|
};
|
package/esm/create.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,aAAa,EAAiB,MAAM,OAAO,CAAC;AAC3D,OAAO,EAAe,OAAO,EAAE,MAAM,YAAY,CAAC;AAUlD,MAAM,UAAU,aAAa;IAC3B,OAAO,CAAC,OAAO,EAAE,EAAE;QACjB,OAAO,CAAC,GAAG,EAAE,EAAE;YACb,MAAM,KAAK,GAAoB,EAAE,CAAC;YAElC,MAAM,aAAa,GAAG,CAAC,IAAY,EAAE,EAAE;;gBACrC,MAAM,GAAG,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC;gBAExB,IAAI,CAAC,GAAG,EAAE;oBACR,MAAM,IAAI,KAAK,CACb,yBAAyB,IAAI,iEAAiE,CAC/F,CAAC;iBACH;gBAED,OAAO,MAAA,GAAG,CAAC,MAAM,mCAAI,CAAC,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;YACnD,CAAC,CAAC;YAEF,OAAO;gBACL,gBAAgB,CAAC,IAAI,EAAE,MAAM;oBAC3B,KAAK,CAAC,IAAI,CAAC,GAAG;wBACZ,MAAM;wBACN,MAAM,EAAE,SAAS;qBAClB,CAAC;gBACJ,CAAC;gBACD,QAAQ,CAAC,IAAI,EAAE,IAAI,GAAG,EAAE;oBACtB,OAAO,IAAI,CAAC,GAAG,EAAE,CACf,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE;wBACnD,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE;4BAC9C,GAAG,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;4BAC1B,OAAO,GAAG,CAAC;wBACb,CAAC,EAAE,EAAE,CAAC,CAAC;wBACP,OAAO,IAAI,EAAE;6BACV,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC,CAAC;6BACtD,IAAI,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC;4BACtB,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,aAAa,CAAC,WAA4B,kBAAI,IAAI,EAAE,MAAM,IAAK,KAAK,EAAG;yBAC5F,CAAC,CAAC,CAAC;oBACR,CAAC,CAAC,CACH,CAAC;gBACJ,CAAC;aACF,CAAC;QACJ,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
|
package/esm/types.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { ComponentType
|
|
2
|
-
import {
|
|
1
|
+
import type { ComponentType } from 'react';
|
|
2
|
+
import type { AnyComponent, BaseComponentProps } from 'piral-core';
|
|
3
3
|
declare module 'piral-core/lib/types/custom' {
|
|
4
4
|
interface PiletCustomApi extends PiletLazyApi {
|
|
5
5
|
}
|
|
6
6
|
}
|
|
7
7
|
export interface LazyComponentLoader<TProps> {
|
|
8
|
-
(): Promise<
|
|
8
|
+
(): Promise<AnyComponent<TProps>>;
|
|
9
9
|
}
|
|
10
10
|
export interface LazyDependencyLoader {
|
|
11
11
|
(): Promise<any>;
|
|
@@ -26,5 +26,7 @@ export interface PiletLazyApi {
|
|
|
26
26
|
* @param deps The optional names of the dependencies to load beforehand.
|
|
27
27
|
* @returns The lazy loading component.
|
|
28
28
|
*/
|
|
29
|
-
fromLazy<T>(cb: LazyComponentLoader<T>, deps?: Array<string>):
|
|
29
|
+
fromLazy<T extends BaseComponentProps>(cb: LazyComponentLoader<T>, deps?: Array<string>): ComponentType<T & {
|
|
30
|
+
deps?: Record<string, any>;
|
|
31
|
+
}>;
|
|
30
32
|
}
|
package/lib/create.js
CHANGED
|
@@ -1,33 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createLazyApi = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const piral_core_1 = require("piral-core");
|
|
6
6
|
function createLazyApi() {
|
|
7
|
-
return
|
|
8
|
-
return
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
return (context) => {
|
|
8
|
+
return (api) => {
|
|
9
|
+
const cache = {};
|
|
10
|
+
const getDependency = (name) => {
|
|
11
11
|
var _a;
|
|
12
|
-
|
|
12
|
+
const dep = cache[name];
|
|
13
13
|
if (!dep) {
|
|
14
|
-
throw new Error(
|
|
14
|
+
throw new Error(`The given dependency "${name}" cannot be found. Please add it first using "defineDependency"`);
|
|
15
15
|
}
|
|
16
16
|
return (_a = dep.result) !== null && _a !== void 0 ? _a : (dep.result = dep.loader());
|
|
17
17
|
};
|
|
18
|
-
var wrapComponent = function (comp) { return ({
|
|
19
|
-
default: piral_core_1.withApi(context, comp, api, 'unknown'),
|
|
20
|
-
}); };
|
|
21
18
|
return {
|
|
22
|
-
defineDependency
|
|
19
|
+
defineDependency(name, loader) {
|
|
23
20
|
cache[name] = {
|
|
24
|
-
loader
|
|
21
|
+
loader,
|
|
25
22
|
result: undefined,
|
|
26
23
|
};
|
|
27
24
|
},
|
|
28
|
-
fromLazy
|
|
29
|
-
|
|
30
|
-
|
|
25
|
+
fromLazy(load, deps = []) {
|
|
26
|
+
return (0, react_1.lazy)(() => Promise.all(deps.map(getDependency)).then((values) => {
|
|
27
|
+
const depMap = deps.reduce((obj, name, index) => {
|
|
28
|
+
obj[name] = values[index];
|
|
29
|
+
return obj;
|
|
30
|
+
}, {});
|
|
31
|
+
return load()
|
|
32
|
+
.then((comp) => (0, piral_core_1.withApi)(context, comp, api, 'unknown'))
|
|
33
|
+
.then((compWithApi) => ({
|
|
34
|
+
default: (props) => (0, react_1.createElement)(compWithApi, Object.assign({ deps: depMap }, props)),
|
|
35
|
+
}));
|
|
36
|
+
}));
|
|
31
37
|
},
|
|
32
38
|
};
|
|
33
39
|
};
|
package/lib/create.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":";;;AAAA
|
|
1
|
+
{"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":";;;AAAA,iCAA2D;AAC3D,2CAAkD;AAUlD,SAAgB,aAAa;IAC3B,OAAO,CAAC,OAAO,EAAE,EAAE;QACjB,OAAO,CAAC,GAAG,EAAE,EAAE;YACb,MAAM,KAAK,GAAoB,EAAE,CAAC;YAElC,MAAM,aAAa,GAAG,CAAC,IAAY,EAAE,EAAE;;gBACrC,MAAM,GAAG,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC;gBAExB,IAAI,CAAC,GAAG,EAAE;oBACR,MAAM,IAAI,KAAK,CACb,yBAAyB,IAAI,iEAAiE,CAC/F,CAAC;iBACH;gBAED,OAAO,MAAA,GAAG,CAAC,MAAM,mCAAI,CAAC,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;YACnD,CAAC,CAAC;YAEF,OAAO;gBACL,gBAAgB,CAAC,IAAI,EAAE,MAAM;oBAC3B,KAAK,CAAC,IAAI,CAAC,GAAG;wBACZ,MAAM;wBACN,MAAM,EAAE,SAAS;qBAClB,CAAC;gBACJ,CAAC;gBACD,QAAQ,CAAC,IAAI,EAAE,IAAI,GAAG,EAAE;oBACtB,OAAO,IAAA,YAAI,EAAC,GAAG,EAAE,CACf,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE;wBACnD,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE;4BAC9C,GAAG,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;4BAC1B,OAAO,GAAG,CAAC;wBACb,CAAC,EAAE,EAAE,CAAC,CAAC;wBACP,OAAO,IAAI,EAAE;6BACV,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAA,oBAAO,EAAC,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC,CAAC;6BACtD,IAAI,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC;4BACtB,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAA,qBAAa,EAAC,WAA4B,kBAAI,IAAI,EAAE,MAAM,IAAK,KAAK,EAAG;yBAC5F,CAAC,CAAC,CAAC;oBACR,CAAC,CAAC,CACH,CAAC;gBACJ,CAAC;aACF,CAAC;QACJ,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AA1CD,sCA0CC"}
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
4
|
tslib_1.__exportStar(require("./create"), exports);
|
|
5
5
|
tslib_1.__exportStar(require("./types"), exports);
|
|
6
6
|
//# sourceMappingURL=index.js.map
|
package/lib/types.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { ComponentType
|
|
2
|
-
import {
|
|
1
|
+
import type { ComponentType } from 'react';
|
|
2
|
+
import type { AnyComponent, BaseComponentProps } from 'piral-core';
|
|
3
3
|
declare module 'piral-core/lib/types/custom' {
|
|
4
4
|
interface PiletCustomApi extends PiletLazyApi {
|
|
5
5
|
}
|
|
6
6
|
}
|
|
7
7
|
export interface LazyComponentLoader<TProps> {
|
|
8
|
-
(): Promise<
|
|
8
|
+
(): Promise<AnyComponent<TProps>>;
|
|
9
9
|
}
|
|
10
10
|
export interface LazyDependencyLoader {
|
|
11
11
|
(): Promise<any>;
|
|
@@ -26,5 +26,7 @@ export interface PiletLazyApi {
|
|
|
26
26
|
* @param deps The optional names of the dependencies to load beforehand.
|
|
27
27
|
* @returns The lazy loading component.
|
|
28
28
|
*/
|
|
29
|
-
fromLazy<T>(cb: LazyComponentLoader<T>, deps?: Array<string>):
|
|
29
|
+
fromLazy<T extends BaseComponentProps>(cb: LazyComponentLoader<T>, deps?: Array<string>): ComponentType<T & {
|
|
30
|
+
deps?: Record<string, any>;
|
|
31
|
+
}>;
|
|
30
32
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "piral-lazy",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1-beta.5640",
|
|
4
4
|
"description": "Plugin for lazy loading third-party framework components in Piral.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"piral",
|
|
@@ -19,11 +19,29 @@
|
|
|
19
19
|
"module": "esm/index.js",
|
|
20
20
|
"main": "lib/index.js",
|
|
21
21
|
"typings": "lib/index.d.ts",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"import": "./esm/index.js",
|
|
25
|
+
"require": "./lib/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./esm/*": {
|
|
28
|
+
"import": "./esm/*"
|
|
29
|
+
},
|
|
30
|
+
"./lib/*": {
|
|
31
|
+
"require": "./lib/*"
|
|
32
|
+
},
|
|
33
|
+
"./_/*": {
|
|
34
|
+
"import": "./esm/*.js",
|
|
35
|
+
"require": "./lib/*.js"
|
|
36
|
+
},
|
|
37
|
+
"./package.json": "./package.json"
|
|
38
|
+
},
|
|
22
39
|
"sideEffects": false,
|
|
23
40
|
"files": [
|
|
24
41
|
"esm",
|
|
25
42
|
"lib",
|
|
26
|
-
"src"
|
|
43
|
+
"src",
|
|
44
|
+
"piral-lazy.min.js"
|
|
27
45
|
],
|
|
28
46
|
"repository": {
|
|
29
47
|
"type": "git",
|
|
@@ -33,17 +51,18 @@
|
|
|
33
51
|
"url": "https://github.com/smapiot/piral/issues"
|
|
34
52
|
},
|
|
35
53
|
"scripts": {
|
|
36
|
-
"
|
|
54
|
+
"cleanup": "rimraf esm lib piral-lazy.min.js",
|
|
55
|
+
"build": "yarn build:bundle && yarn build:commonjs && yarn build:esnext",
|
|
56
|
+
"build:bundle": "esbuild src/index.ts --outfile=piral-lazy.min.js --bundle --external:piral-core --external:react --minify --global-name=piralLazy",
|
|
37
57
|
"build:commonjs": "tsc --project tsconfig.json --outDir lib --module commonjs",
|
|
38
58
|
"build:esnext": "tsc --project tsconfig.json --outDir esm --module esnext",
|
|
39
59
|
"typedoc": "typedoc --json ../../../docs/types/piral-lazy.json src --exclude \"src/**/*.test.*\"",
|
|
40
60
|
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
41
61
|
},
|
|
42
62
|
"devDependencies": {
|
|
43
|
-
"
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
"piral-core": "0.13.x"
|
|
63
|
+
"@types/react": "^18.0.0",
|
|
64
|
+
"piral-core": "1.0.1-beta.5640",
|
|
65
|
+
"react": "^18.0.0"
|
|
47
66
|
},
|
|
48
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "fa0a72b28fd0a20afec7ef491ec19e93c090fc72"
|
|
49
68
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var piralLazy=(()=>{var D=Object.create;var p=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames,y=Object.getOwnPropertySymbols,C=Object.getPrototypeOf,l=Object.prototype.hasOwnProperty,x=Object.prototype.propertyIsEnumerable;var s=(e,n,r)=>n in e?p(e,n,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[n]=r,f=(e,n)=>{for(var r in n||(n={}))l.call(n,r)&&s(e,r,n[r]);if(y)for(var r of y(n))x.call(n,r)&&s(e,r,n[r]);return e};var m=e=>p(e,"__esModule",{value:!0});var h=(e=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(e,{get:(n,r)=>(typeof require!="undefined"?require:n)[r]}):e)(function(e){if(typeof require!="undefined")return require.apply(this,arguments);throw new Error('Dynamic require of "'+e+'" is not supported')});var T=(e,n)=>{m(e);for(var r in n)p(e,r,{get:n[r],enumerable:!0})},E=(e,n,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of A(n))!l.call(e,o)&&o!=="default"&&p(e,o,{get:()=>n[o],enumerable:!(r=w(n,o))||r.enumerable});return e},P=e=>E(m(p(e!=null?D(C(e)):{},"default",e&&e.__esModule&&"default"in e?{get:()=>e.default,enumerable:!0}:{value:e,enumerable:!0})),e);var v={};T(v,{createLazyApi:()=>k});var d=P(h("react")),L=P(h("piral-core"));function k(){return e=>n=>{let r={},o=a=>{var c;let t=r[a];if(!t)throw new Error(`The given dependency "${a}" cannot be found. Please add it first using "defineDependency"`);return(c=t.result)!=null?c:t.result=t.loader()};return{defineDependency(a,t){r[a]={loader:t,result:void 0}},fromLazy(a,t=[]){return(0,d.lazy)(()=>Promise.all(t.map(o)).then(c=>{let g=t.reduce((i,u,z)=>(i[u]=c[z],i),{});return a().then(i=>(0,L.withApi)(e,i,n,"unknown")).then(i=>({default:u=>(0,d.createElement)(i,f({deps:g},u))}))}))}}}}return v;})();
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import create from 'zustand';
|
|
3
|
+
import { render, act } from '@testing-library/react';
|
|
4
|
+
import { StateContext } from 'piral-core';
|
|
5
|
+
import { createLazyApi } from './create';
|
|
6
|
+
|
|
7
|
+
function createMockContainer() {
|
|
8
|
+
const state = create(() => ({
|
|
9
|
+
app: {
|
|
10
|
+
publicPath: '/',
|
|
11
|
+
},
|
|
12
|
+
components: {},
|
|
13
|
+
errorComponents: {},
|
|
14
|
+
portals: {},
|
|
15
|
+
registry: {
|
|
16
|
+
wrappers: {},
|
|
17
|
+
},
|
|
18
|
+
}));
|
|
19
|
+
return {
|
|
20
|
+
context: {
|
|
21
|
+
on: jest.fn(),
|
|
22
|
+
off: jest.fn(),
|
|
23
|
+
emit: jest.fn(),
|
|
24
|
+
defineActions() {},
|
|
25
|
+
state,
|
|
26
|
+
converters: {
|
|
27
|
+
html: ({ component }) => component,
|
|
28
|
+
},
|
|
29
|
+
navigation: {
|
|
30
|
+
router: undefined,
|
|
31
|
+
},
|
|
32
|
+
readState(read) {
|
|
33
|
+
return read(state.getState());
|
|
34
|
+
},
|
|
35
|
+
destroyPortal() {},
|
|
36
|
+
dispatch(update) {
|
|
37
|
+
state.setState(update(state.getState()));
|
|
38
|
+
},
|
|
39
|
+
} as any,
|
|
40
|
+
api: {
|
|
41
|
+
meta: {
|
|
42
|
+
name: 'sample-pilet',
|
|
43
|
+
},
|
|
44
|
+
} as any,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
describe('Piral-Lazy create module', () => {
|
|
49
|
+
it('appends lazy loading for a DOM component', async () => {
|
|
50
|
+
const mount = jest.fn();
|
|
51
|
+
const MyComponent = { component: { mount }, type: 'html' };
|
|
52
|
+
const load = async () => await Promise.resolve(MyComponent);
|
|
53
|
+
const { context, api } = createMockContainer();
|
|
54
|
+
const apiCreator: any = createLazyApi()(context);
|
|
55
|
+
const { fromLazy, defineDependency } = apiCreator(api);
|
|
56
|
+
defineDependency('testName', () => {});
|
|
57
|
+
const LazyComponent = fromLazy(load, ['testName']);
|
|
58
|
+
render(
|
|
59
|
+
<StateContext.Provider value={context}>
|
|
60
|
+
<React.Suspense fallback="anything">
|
|
61
|
+
<LazyComponent />
|
|
62
|
+
</React.Suspense>
|
|
63
|
+
</StateContext.Provider>,
|
|
64
|
+
);
|
|
65
|
+
await act(() => Promise.resolve());
|
|
66
|
+
expect(LazyComponent).not.toBeUndefined();
|
|
67
|
+
});
|
|
68
|
+
});
|
package/src/create.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { lazy } from 'react';
|
|
2
|
-
import { PiralPlugin, withApi
|
|
1
|
+
import { lazy, createElement, ComponentType } from 'react';
|
|
2
|
+
import { PiralPlugin, withApi } from 'piral-core';
|
|
3
3
|
import { PiletLazyApi, LazyDependencyLoader } from './types';
|
|
4
4
|
|
|
5
5
|
interface DependencyCache {
|
|
@@ -25,9 +25,6 @@ export function createLazyApi(): PiralPlugin<PiletLazyApi> {
|
|
|
25
25
|
|
|
26
26
|
return dep.result ?? (dep.result = dep.loader());
|
|
27
27
|
};
|
|
28
|
-
const wrapComponent = <T>(comp: HtmlComponent<T>) => ({
|
|
29
|
-
default: withApi(context, comp, api, 'unknown'),
|
|
30
|
-
});
|
|
31
28
|
|
|
32
29
|
return {
|
|
33
30
|
defineDependency(name, loader) {
|
|
@@ -37,7 +34,19 @@ export function createLazyApi(): PiralPlugin<PiletLazyApi> {
|
|
|
37
34
|
};
|
|
38
35
|
},
|
|
39
36
|
fromLazy(load, deps = []) {
|
|
40
|
-
return lazy(() =>
|
|
37
|
+
return lazy(() =>
|
|
38
|
+
Promise.all(deps.map(getDependency)).then((values) => {
|
|
39
|
+
const depMap = deps.reduce((obj, name, index) => {
|
|
40
|
+
obj[name] = values[index];
|
|
41
|
+
return obj;
|
|
42
|
+
}, {});
|
|
43
|
+
return load()
|
|
44
|
+
.then((comp) => withApi(context, comp, api, 'unknown'))
|
|
45
|
+
.then((compWithApi) => ({
|
|
46
|
+
default: (props) => createElement(compWithApi as ComponentType, { deps: depMap, ...props }),
|
|
47
|
+
}));
|
|
48
|
+
}),
|
|
49
|
+
);
|
|
41
50
|
},
|
|
42
51
|
};
|
|
43
52
|
};
|
package/src/types.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { ComponentType
|
|
2
|
-
import {
|
|
1
|
+
import type { ComponentType } from 'react';
|
|
2
|
+
import type { AnyComponent, BaseComponentProps } from 'piral-core';
|
|
3
3
|
|
|
4
4
|
declare module 'piral-core/lib/types/custom' {
|
|
5
5
|
interface PiletCustomApi extends PiletLazyApi {}
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
export interface LazyComponentLoader<TProps> {
|
|
9
|
-
(): Promise<
|
|
9
|
+
(): Promise<AnyComponent<TProps>>;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
export interface LazyDependencyLoader {
|
|
@@ -29,5 +29,8 @@ export interface PiletLazyApi {
|
|
|
29
29
|
* @param deps The optional names of the dependencies to load beforehand.
|
|
30
30
|
* @returns The lazy loading component.
|
|
31
31
|
*/
|
|
32
|
-
fromLazy<T
|
|
32
|
+
fromLazy<T extends BaseComponentProps>(
|
|
33
|
+
cb: LazyComponentLoader<T>,
|
|
34
|
+
deps?: Array<string>,
|
|
35
|
+
): ComponentType<T & { deps?: Record<string, any> }>;
|
|
33
36
|
}
|
package/src/create.test.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { Atom, swap } from '@dbeining/react-atom';
|
|
2
|
-
import { createLazyApi } from './create';
|
|
3
|
-
|
|
4
|
-
function createMockContainer() {
|
|
5
|
-
const state = Atom.of({});
|
|
6
|
-
return {
|
|
7
|
-
context: {
|
|
8
|
-
converters: {},
|
|
9
|
-
on: jest.fn(),
|
|
10
|
-
off: jest.fn(),
|
|
11
|
-
emit: jest.fn(),
|
|
12
|
-
defineActions() {},
|
|
13
|
-
state,
|
|
14
|
-
readState(read) {
|
|
15
|
-
return read(state);
|
|
16
|
-
},
|
|
17
|
-
dispatch(update) {
|
|
18
|
-
swap(state, update);
|
|
19
|
-
},
|
|
20
|
-
} as any,
|
|
21
|
-
api: {} as any,
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
describe('Piral-Lazy create module', () => {
|
|
26
|
-
it('appends lazy loading for a DOM component', () => {
|
|
27
|
-
const { context } = createMockContainer();
|
|
28
|
-
const load = () => Promise.resolve({});
|
|
29
|
-
const apiCreator: any = createLazyApi()(context);
|
|
30
|
-
const { fromLazy } = apiCreator();
|
|
31
|
-
const lazyComponent = fromLazy(load);
|
|
32
|
-
expect(lazyComponent).not.toBeUndefined();
|
|
33
|
-
});
|
|
34
|
-
});
|