@rmc-toolkit/react 0.1.0
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/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/index.test.d.ts +2 -0
- package/dist/index.test.d.ts.map +1 -0
- package/dist/index.test.js +25 -0
- package/package.json +30 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type DynamicImporter, type RuntimeModuleContext } from "@rmc-toolkit/core";
|
|
2
|
+
import React from "react";
|
|
3
|
+
export type DynamicModuleBoundaryProps = {
|
|
4
|
+
specifier: string;
|
|
5
|
+
context?: RuntimeModuleContext;
|
|
6
|
+
fallback?: React.ReactNode;
|
|
7
|
+
errorFallback?: React.ReactNode;
|
|
8
|
+
importer?: DynamicImporter;
|
|
9
|
+
};
|
|
10
|
+
export declare const DynamicModuleBoundary: ({ specifier, context, fallback, errorFallback, importer, }: DynamicModuleBoundaryProps) => React.ReactElement;
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,eAAe,EACpB,KAAK,oBAAoB,EAC1B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,MAAM,OAAO,CAAC;AAM1B,MAAM,MAAM,0BAA0B,GAAG;IACvC,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,oBAAoB,CAAC;IAC/B,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAC3B,aAAa,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAChC,QAAQ,CAAC,EAAE,eAAe,CAAC;CAC5B,CAAC;AA2BF,eAAO,MAAM,qBAAqB,GAAI,4DAMnC,0BAA0B,KAAG,KAAK,CAAC,YAiBrC,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { importModule, } from "@rmc-toolkit/core";
|
|
3
|
+
import React from "react";
|
|
4
|
+
class ErrorBoundary extends React.Component {
|
|
5
|
+
state = { error: null };
|
|
6
|
+
static getDerivedStateFromError(error) {
|
|
7
|
+
return { error };
|
|
8
|
+
}
|
|
9
|
+
render() {
|
|
10
|
+
if (this.state.error) {
|
|
11
|
+
return this.props.fallback;
|
|
12
|
+
}
|
|
13
|
+
return this.props.children;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const defaultImporter = importModule;
|
|
17
|
+
export const DynamicModuleBoundary = ({ specifier, context, fallback = null, errorFallback = null, importer = defaultImporter, }) => {
|
|
18
|
+
const LazyModule = React.lazy(async () => {
|
|
19
|
+
const loadedModule = (await importer(specifier));
|
|
20
|
+
return loadedModule;
|
|
21
|
+
});
|
|
22
|
+
const moduleProps = context === undefined
|
|
23
|
+
? {}
|
|
24
|
+
: { context };
|
|
25
|
+
return (_jsx(React.Suspense, { fallback: fallback, children: _jsx(ErrorBoundary, { fallback: errorFallback, children: _jsx(LazyModule, { ...moduleProps }) }) }));
|
|
26
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../src/index.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
// @vitest-environment happy-dom
|
|
3
|
+
import { describe, expect, test, vi } from "vitest";
|
|
4
|
+
import { act } from "react";
|
|
5
|
+
import { createRoot } from "react-dom/client";
|
|
6
|
+
const FixtureComponent = () => "fixture content";
|
|
7
|
+
const mockImportModule = vi.fn(async () => ({
|
|
8
|
+
default: FixtureComponent,
|
|
9
|
+
}));
|
|
10
|
+
vi.mock("@rmc-toolkit/core", () => ({
|
|
11
|
+
importModule: mockImportModule,
|
|
12
|
+
}));
|
|
13
|
+
const { DynamicModuleBoundary } = await import("./index.js");
|
|
14
|
+
describe("DynamicModuleBoundary", () => {
|
|
15
|
+
test("default importer delegates to core's importModule", async () => {
|
|
16
|
+
const container = document.createElement("div");
|
|
17
|
+
const root = createRoot(container);
|
|
18
|
+
await act(async () => {
|
|
19
|
+
root.render(_jsx(DynamicModuleBoundary, { specifier: "test-specifier" }));
|
|
20
|
+
});
|
|
21
|
+
expect(mockImportModule).toHaveBeenCalledWith("test-specifier");
|
|
22
|
+
expect(container.textContent).toBe("fixture content");
|
|
23
|
+
root.unmount();
|
|
24
|
+
});
|
|
25
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rmc-toolkit/react",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"@rmc-toolkit/core": "0.1.0",
|
|
21
|
+
"react": ">=18"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@rmc-toolkit/core": "0.1.0"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsc -b"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|