@taro-minify-pack/react-lazy-enhanced 0.0.5-alpha.1

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/.eslintrc.js ADDED
@@ -0,0 +1,21 @@
1
+ module.exports = {
2
+ env: {
3
+ node: true,
4
+ es6: true
5
+ },
6
+ extends: [
7
+ 'standard',
8
+ 'plugin:@typescript-eslint/recommended'
9
+ ],
10
+ parser: '@typescript-eslint/parser',
11
+ plugins: ['@typescript-eslint'],
12
+ rules: {
13
+ '@typescript-eslint/no-empty-function': 'off',
14
+ '@typescript-eslint/no-explicit-any': 'off',
15
+ '@typescript-eslint/no-var-requires': 'off',
16
+ '@typescript-eslint/no-non-null-assertion': 'off',
17
+ '@typescript-eslint/ban-ts-comment': 'off',
18
+ '@typescript-eslint/no-unused-vars': 'off',
19
+ 'no-use-before-define': 'off'
20
+ }
21
+ }
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 yu.pan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ ## @taro-minify-pack/react-lazy-enhanced
@@ -0,0 +1,7 @@
1
+ import React, { ComponentType } from 'react';
2
+ interface Result<T> {
3
+ default: T;
4
+ }
5
+ type Factory<T extends ComponentType<any>> = () => Promise<Result<T>>;
6
+ export declare const lazy: <T extends ComponentType<any>>(factory: Factory<T>) => React.ForwardRefExoticComponent<React.PropsWithoutRef<React.ComponentPropsWithoutRef<T>> & React.RefAttributes<React.ComponentRef<T>>>;
7
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,44 @@
1
+ import React, { useEffect } from 'react';
2
+ var Status;
3
+ (function (Status) {
4
+ Status["Uninitialized"] = "uninitialized";
5
+ Status["Pending"] = "pending";
6
+ Status["Resolved"] = "resolved";
7
+ Status["Rejected"] = "rejected";
8
+ })(Status || (Status = {}));
9
+ export const lazy = (factory) => {
10
+ const LazyComponent = React.lazy(factory);
11
+ const loadData = { status: Status.Uninitialized };
12
+ const load = () => {
13
+ if (loadData.status !== Status.Uninitialized)
14
+ return;
15
+ const successCallback = (res) => {
16
+ loadData.status = Status.Resolved;
17
+ loadData.result = res.default;
18
+ };
19
+ const errorCallback = (err) => {
20
+ loadData.status = Status.Rejected;
21
+ loadData.result = err;
22
+ };
23
+ loadData.promise = factory().then(successCallback, errorCallback);
24
+ loadData.status = Status.Pending;
25
+ };
26
+ const resetLoadData = () => {
27
+ loadData.status = Status.Uninitialized;
28
+ loadData.result = undefined;
29
+ loadData.promise = undefined;
30
+ };
31
+ return React.forwardRef((props, ref) => {
32
+ if (loadData.status === Status.Uninitialized)
33
+ load();
34
+ if (loadData.status === Status.Pending)
35
+ throw loadData.promise;
36
+ if (loadData.status === Status.Rejected)
37
+ throw loadData.result;
38
+ useEffect(() => {
39
+ return resetLoadData();
40
+ }, []);
41
+ return React.createElement(LazyComponent, Object.assign({}, props, {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MAAM,CAAC,aAAa,CAAA;QACtC,QAAQ,CAAC,MAAM,GAAG,SAAS,CAAA;QAC3B,QAAQ,CAAC,OAAO,GAAG,SAAS,CAAA;IAC9B,CAAC,CAAA;IAED,OAAO,KAAK,CAAC,UAAU,CAA+C,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;QACnF,IAAI,QAAQ,CAAC,MAAM,KAAK,MAAM,CAAC,aAAa;YAAE,IAAI,EAAE,CAAA;QAEpD,IAAI,QAAQ,CAAC,MAAM,KAAK,MAAM,CAAC,OAAO;YAAE,MAAM,QAAQ,CAAC,OAAO,CAAA;QAE9D,IAAI,QAAQ,CAAC,MAAM,KAAK,MAAM,CAAC,QAAQ;YAAE,MAAM,QAAQ,CAAC,MAAM,CAAA;QAE9D,SAAS,CAAC,GAAG,EAAE;YACb,OAAO,aAAa,EAAE,CAAA;QACxB,CAAC,EAAE,EAAE,CAAC,CAAA;QAEN,OAAO,oBAAC,aAAa,oBAAK,KAAK,IAAE,GAAG,EAAE,GAAG,IAAI,CAAA;IAC/C,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA"}
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@taro-minify-pack/react-lazy-enhanced",
3
+ "version": "0.0.5-alpha.1",
4
+ "description": "",
5
+ "module": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "homepage": "https://github.com/panyu97py/taro-minify-pack",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/panyu97py/taro-minify-pack.git"
14
+ },
15
+ "author": "yu.pan",
16
+ "license": "MIT",
17
+ "peerDependencies": {
18
+ "react": ">=16"
19
+ },
20
+ "devDependencies": {
21
+ "@types/node": "^18.19.130",
22
+ "@types/react": "^18.3.27",
23
+ "@types/webpack-sources": "^3.2.3",
24
+ "@typescript-eslint/eslint-plugin": "^8.26.1",
25
+ "@typescript-eslint/parser": "^8.26.1",
26
+ "eslint": "^8.19.0",
27
+ "eslint-config-standard": "^17.0.0",
28
+ "eslint-plugin-import": "^2.26.0",
29
+ "eslint-plugin-n": "^15.2.3",
30
+ "eslint-plugin-promise": "^6.0.0",
31
+ "react": "^18.3.1",
32
+ "typescript": "^5.8.2"
33
+ },
34
+ "scripts": {
35
+ "build": "tsc"
36
+ }
37
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,65 @@
1
+ import React, {
2
+ ComponentPropsWithoutRef,
3
+ ComponentRef,
4
+ ComponentType,
5
+ ForwardRefExoticComponent,
6
+ useEffect
7
+ } from 'react'
8
+
9
+ enum Status {
10
+ Uninitialized = 'uninitialized',
11
+ Pending = 'pending',
12
+ Resolved = 'resolved',
13
+ Rejected = 'rejected',
14
+ }
15
+
16
+ interface Result<T> {
17
+ default: T;
18
+ }
19
+
20
+ interface LoadData<T> {
21
+ status: Status;
22
+ result?: T | Error;
23
+ promise?: Promise<void>;
24
+ }
25
+
26
+ type Factory<T extends ComponentType<any>> = () => Promise<Result<T>>;
27
+
28
+ export const lazy = <T extends ComponentType<any>>(factory: Factory<T>) => {
29
+ const LazyComponent = React.lazy(factory) as ForwardRefExoticComponent<any>
30
+ const loadData: LoadData<T> = { status: Status.Uninitialized }
31
+
32
+ const load = () => {
33
+ if (loadData.status !== Status.Uninitialized) return
34
+ const successCallback = (res: Result<T>) => {
35
+ loadData.status = Status.Resolved
36
+ loadData.result = res.default
37
+ }
38
+ const errorCallback = (err: Error) => {
39
+ loadData.status = Status.Rejected
40
+ loadData.result = err
41
+ }
42
+ loadData.promise = factory().then(successCallback, errorCallback)
43
+ loadData.status = Status.Pending
44
+ }
45
+
46
+ const resetLoadData = () => {
47
+ loadData.status = Status.Uninitialized
48
+ loadData.result = undefined
49
+ loadData.promise = undefined
50
+ }
51
+
52
+ return React.forwardRef<ComponentRef<T>, ComponentPropsWithoutRef<T>>((props, ref) => {
53
+ if (loadData.status === Status.Uninitialized) load()
54
+
55
+ if (loadData.status === Status.Pending) throw loadData.promise
56
+
57
+ if (loadData.status === Status.Rejected) throw loadData.result
58
+
59
+ useEffect(() => {
60
+ return resetLoadData()
61
+ }, [])
62
+
63
+ return <LazyComponent {...props} ref={ref} />
64
+ })
65
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2015",
4
+ "module": "esnext",
5
+ "jsx": "react",
6
+ "strict": true,
7
+ "sourceMap": true,
8
+ "declaration": true,
9
+ "esModuleInterop": true,
10
+ "moduleResolution": "node",
11
+ // 声明文件生成到的文件目录
12
+ "outDir": "./dist/",
13
+ "skipLibCheck": true,
14
+ /* Skip type checking of declaration files. */
15
+ "forceConsistentCasingInFileNames": true,
16
+ "lib": ["esnext", "dom"],
17
+ "types": ["@types/node"]
18
+ },
19
+ "include": ["src"]
20
+ }