@blocklet/pages-kit 0.2.214 → 0.2.215

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.
Files changed (45) hide show
  1. package/lib/cjs/builtin/iconify/react.js +5 -0
  2. package/lib/cjs/builtin/mui/lab.js +5 -0
  3. package/lib/cjs/builtin/mui/material.js +29 -0
  4. package/lib/cjs/builtin/react-hook-form.js +17 -0
  5. package/lib/cjs/builtin/react.js +37 -0
  6. package/lib/cjs/builtin/utils.js +6 -0
  7. package/lib/cjs/builtin/zustand/middleware/immer.js +17 -0
  8. package/lib/cjs/builtin/zustand.js +17 -0
  9. package/lib/cjs/components/CustomComponentRenderer/index.js +51 -29
  10. package/lib/cjs/types/builtin.js +4 -0
  11. package/lib/cjs/utils/inject-global-components.js +53 -0
  12. package/lib/cjs/utils/preload.js +2 -1
  13. package/lib/cjs/utils/property.js +19 -7
  14. package/lib/cjs/utils/typescript/builtin-module-transformer.js +55 -0
  15. package/lib/esm/builtin/iconify/react.js +1 -0
  16. package/lib/esm/builtin/mui/lab.js +1 -0
  17. package/lib/esm/builtin/mui/material.js +13 -0
  18. package/lib/esm/builtin/react-hook-form.js +1 -0
  19. package/lib/esm/builtin/react.js +1 -0
  20. package/lib/esm/builtin/utils.js +1 -0
  21. package/lib/esm/builtin/zustand/middleware/immer.js +1 -0
  22. package/lib/esm/builtin/zustand.js +1 -0
  23. package/lib/esm/components/CustomComponentRenderer/index.js +51 -31
  24. package/lib/esm/types/builtin.js +1 -0
  25. package/lib/esm/utils/inject-global-components.js +28 -0
  26. package/lib/esm/utils/preload.js +2 -1
  27. package/lib/esm/utils/property.js +18 -6
  28. package/lib/esm/utils/typescript/builtin-module-transformer.js +51 -0
  29. package/lib/types/builtin/iconify/react.d.ts +1 -0
  30. package/lib/types/builtin/mui/lab.d.ts +1 -0
  31. package/lib/types/builtin/mui/material.d.ts +13 -0
  32. package/lib/types/builtin/react-hook-form.d.ts +1 -0
  33. package/lib/types/builtin/react.d.ts +1 -0
  34. package/lib/types/builtin/utils.d.ts +1 -0
  35. package/lib/types/builtin/zustand/middleware/immer.d.ts +1 -0
  36. package/lib/types/builtin/zustand.d.ts +1 -0
  37. package/lib/types/components/CustomComponentRenderer/index.d.ts +7 -0
  38. package/lib/types/types/api.d.ts +3 -0
  39. package/lib/types/types/builtin.d.ts +1 -0
  40. package/lib/types/types/core.d.ts +27 -13
  41. package/lib/types/types/preload.d.ts +3 -0
  42. package/lib/types/utils/inject-global-components.d.ts +1 -0
  43. package/lib/types/utils/property.d.ts +5 -1
  44. package/lib/types/utils/typescript/builtin-module-transformer.d.ts +2 -0
  45. package/package.json +15 -4
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Icon = void 0;
4
+ var react_1 = require("@iconify/react");
5
+ Object.defineProperty(exports, "Icon", { enumerable: true, get: function () { return react_1.Icon; } });
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LoadingButton = void 0;
4
+ var lab_1 = require("@mui/lab");
5
+ Object.defineProperty(exports, "LoadingButton", { enumerable: true, get: function () { return lab_1.LoadingButton; } });
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TextField = exports.Switch = exports.Stack = exports.Popper = exports.Paper = exports.DialogTitle = exports.DialogContent = exports.DialogActions = exports.Dialog = exports.ClickAwayListener = exports.Button = exports.Box = exports.Autocomplete = void 0;
4
+ var material_1 = require("@mui/material");
5
+ Object.defineProperty(exports, "Autocomplete", { enumerable: true, get: function () { return material_1.Autocomplete; } });
6
+ var material_2 = require("@mui/material");
7
+ Object.defineProperty(exports, "Box", { enumerable: true, get: function () { return material_2.Box; } });
8
+ var material_3 = require("@mui/material");
9
+ Object.defineProperty(exports, "Button", { enumerable: true, get: function () { return material_3.Button; } });
10
+ var material_4 = require("@mui/material");
11
+ Object.defineProperty(exports, "ClickAwayListener", { enumerable: true, get: function () { return material_4.ClickAwayListener; } });
12
+ var material_5 = require("@mui/material");
13
+ Object.defineProperty(exports, "Dialog", { enumerable: true, get: function () { return material_5.Dialog; } });
14
+ var material_6 = require("@mui/material");
15
+ Object.defineProperty(exports, "DialogActions", { enumerable: true, get: function () { return material_6.DialogActions; } });
16
+ var material_7 = require("@mui/material");
17
+ Object.defineProperty(exports, "DialogContent", { enumerable: true, get: function () { return material_7.DialogContent; } });
18
+ var material_8 = require("@mui/material");
19
+ Object.defineProperty(exports, "DialogTitle", { enumerable: true, get: function () { return material_8.DialogTitle; } });
20
+ var material_9 = require("@mui/material");
21
+ Object.defineProperty(exports, "Paper", { enumerable: true, get: function () { return material_9.Paper; } });
22
+ var material_10 = require("@mui/material");
23
+ Object.defineProperty(exports, "Popper", { enumerable: true, get: function () { return material_10.Popper; } });
24
+ var material_11 = require("@mui/material");
25
+ Object.defineProperty(exports, "Stack", { enumerable: true, get: function () { return material_11.Stack; } });
26
+ var material_12 = require("@mui/material");
27
+ Object.defineProperty(exports, "Switch", { enumerable: true, get: function () { return material_12.Switch; } });
28
+ var material_13 = require("@mui/material");
29
+ Object.defineProperty(exports, "TextField", { enumerable: true, get: function () { return material_13.TextField; } });
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("react-hook-form"), exports);
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.version = exports.useTransition = exports.useSyncExternalStore = exports.useState = exports.useRef = exports.useReducer = exports.useMemo = exports.useLayoutEffect = exports.useInsertionEffect = exports.useImperativeHandle = exports.useId = exports.useEffect = exports.useDeferredValue = exports.useDebugValue = exports.useContext = exports.useCallback = exports.startTransition = exports.memo = exports.lazy = exports.isValidElement = exports.forwardRef = exports.createRef = exports.createFactory = exports.createElement = exports.createContext = exports.cloneElement = exports.Suspense = exports.StrictMode = exports.PureComponent = exports.Profiler = exports.Fragment = exports.Component = exports.Children = void 0;
4
+ var react_1 = require("react");
5
+ Object.defineProperty(exports, "Children", { enumerable: true, get: function () { return react_1.Children; } });
6
+ Object.defineProperty(exports, "Component", { enumerable: true, get: function () { return react_1.Component; } });
7
+ Object.defineProperty(exports, "Fragment", { enumerable: true, get: function () { return react_1.Fragment; } });
8
+ Object.defineProperty(exports, "Profiler", { enumerable: true, get: function () { return react_1.Profiler; } });
9
+ Object.defineProperty(exports, "PureComponent", { enumerable: true, get: function () { return react_1.PureComponent; } });
10
+ Object.defineProperty(exports, "StrictMode", { enumerable: true, get: function () { return react_1.StrictMode; } });
11
+ Object.defineProperty(exports, "Suspense", { enumerable: true, get: function () { return react_1.Suspense; } });
12
+ Object.defineProperty(exports, "cloneElement", { enumerable: true, get: function () { return react_1.cloneElement; } });
13
+ Object.defineProperty(exports, "createContext", { enumerable: true, get: function () { return react_1.createContext; } });
14
+ Object.defineProperty(exports, "createElement", { enumerable: true, get: function () { return react_1.createElement; } });
15
+ Object.defineProperty(exports, "createFactory", { enumerable: true, get: function () { return react_1.createFactory; } });
16
+ Object.defineProperty(exports, "createRef", { enumerable: true, get: function () { return react_1.createRef; } });
17
+ Object.defineProperty(exports, "forwardRef", { enumerable: true, get: function () { return react_1.forwardRef; } });
18
+ Object.defineProperty(exports, "isValidElement", { enumerable: true, get: function () { return react_1.isValidElement; } });
19
+ Object.defineProperty(exports, "lazy", { enumerable: true, get: function () { return react_1.lazy; } });
20
+ Object.defineProperty(exports, "memo", { enumerable: true, get: function () { return react_1.memo; } });
21
+ Object.defineProperty(exports, "startTransition", { enumerable: true, get: function () { return react_1.startTransition; } });
22
+ Object.defineProperty(exports, "useCallback", { enumerable: true, get: function () { return react_1.useCallback; } });
23
+ Object.defineProperty(exports, "useContext", { enumerable: true, get: function () { return react_1.useContext; } });
24
+ Object.defineProperty(exports, "useDebugValue", { enumerable: true, get: function () { return react_1.useDebugValue; } });
25
+ Object.defineProperty(exports, "useDeferredValue", { enumerable: true, get: function () { return react_1.useDeferredValue; } });
26
+ Object.defineProperty(exports, "useEffect", { enumerable: true, get: function () { return react_1.useEffect; } });
27
+ Object.defineProperty(exports, "useId", { enumerable: true, get: function () { return react_1.useId; } });
28
+ Object.defineProperty(exports, "useImperativeHandle", { enumerable: true, get: function () { return react_1.useImperativeHandle; } });
29
+ Object.defineProperty(exports, "useInsertionEffect", { enumerable: true, get: function () { return react_1.useInsertionEffect; } });
30
+ Object.defineProperty(exports, "useLayoutEffect", { enumerable: true, get: function () { return react_1.useLayoutEffect; } });
31
+ Object.defineProperty(exports, "useMemo", { enumerable: true, get: function () { return react_1.useMemo; } });
32
+ Object.defineProperty(exports, "useReducer", { enumerable: true, get: function () { return react_1.useReducer; } });
33
+ Object.defineProperty(exports, "useRef", { enumerable: true, get: function () { return react_1.useRef; } });
34
+ Object.defineProperty(exports, "useState", { enumerable: true, get: function () { return react_1.useState; } });
35
+ Object.defineProperty(exports, "useSyncExternalStore", { enumerable: true, get: function () { return react_1.useSyncExternalStore; } });
36
+ Object.defineProperty(exports, "useTransition", { enumerable: true, get: function () { return react_1.useTransition; } });
37
+ Object.defineProperty(exports, "version", { enumerable: true, get: function () { return react_1.version; } });
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withQuery = exports.joinURL = void 0;
4
+ var ufo_1 = require("ufo");
5
+ Object.defineProperty(exports, "joinURL", { enumerable: true, get: function () { return ufo_1.joinURL; } });
6
+ Object.defineProperty(exports, "withQuery", { enumerable: true, get: function () { return ufo_1.withQuery; } });
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("zustand/middleware/immer"), exports);
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("zustand"), exports);
@@ -43,51 +43,57 @@ var __rest = (this && this.__rest) || function (s, e) {
43
43
  return t;
44
44
  };
45
45
  Object.defineProperty(exports, "__esModule", { value: true });
46
+ exports.transpileAndLoadScript = void 0;
46
47
  const jsx_runtime_1 = require("react/jsx-runtime");
47
- const react_1 = require("@iconify/react");
48
+ require("../../utils/inject-global-components");
48
49
  const material_1 = require("@mui/material");
49
- const react_2 = __importStar(require("react"));
50
+ const lodash_1 = require("lodash");
51
+ const react_1 = require("react");
50
52
  const react_error_boundary_1 = require("react-error-boundary");
51
- const ufo_1 = require("ufo");
52
53
  const preload_1 = require("../../types/preload");
53
54
  const property_1 = require("../../utils/property");
54
- // inject global variables for web-component renderer
55
- const win = window;
56
- win.joinURL = ufo_1.joinURL;
57
- win.withQuery = ufo_1.withQuery;
58
- win.React = react_2.default;
59
- win.Box = material_1.Box;
60
- win.Stack = material_1.Stack;
61
- win.Button = material_1.Button;
62
- win.IconifyIcon = react_1.Icon;
55
+ const builtin_module_transformer_1 = require("../../utils/typescript/builtin-module-transformer");
56
+ const MAXIMUM_RENDER_STACK_SIZE = 50;
63
57
  function CustomComponentRenderer(_a) {
64
58
  var props = __rest(_a, []);
65
- return ((0, jsx_runtime_1.jsx)(react_error_boundary_1.ErrorBoundary, { fallbackRender: E, children: (0, jsx_runtime_1.jsx)(Renderer, Object.assign({}, props)) }));
59
+ return ((0, jsx_runtime_1.jsx)(react_error_boundary_1.ErrorBoundary, { fallbackRender: E, resetKeys: [Date.now()], children: (0, jsx_runtime_1.jsx)(ComponentRenderer, Object.assign({}, props)) }));
66
60
  }
67
61
  exports.default = CustomComponentRenderer;
62
+ function ComponentRenderer(_a) {
63
+ var { renderCount = 0 } = _a, props = __rest(_a, ["renderCount"]);
64
+ if (renderCount > MAXIMUM_RENDER_STACK_SIZE) {
65
+ throw new Error('Maximum render stack size exceeded');
66
+ }
67
+ return (0, jsx_runtime_1.jsx)(Renderer, Object.assign({ renderCount: renderCount + 1 }, props));
68
+ }
68
69
  function Renderer(props) {
69
- const component = loadComponent(props);
70
+ const component = useComponent(props);
70
71
  if (!component)
71
72
  return null;
72
73
  const { Component, AsyncComponent, properties } = component;
74
+ const componentProps = Object.fromEntries(Object.entries(Object.assign(Object.assign({}, properties), props.props)).map(([key, val]) => [
75
+ key,
76
+ (val === null || val === void 0 ? void 0 : val.type) === property_1.RenderNestedComponent ? ((0, jsx_runtime_1.jsx)(ComponentRenderer, Object.assign({ renderCount: props.renderCount, componentId: val.componentId }, (0, lodash_1.pick)(props, 'components', 'locale', 'defaultLocale'), { properties: val.properties }))) : (val),
77
+ ]));
73
78
  if (Component) {
74
- return (0, jsx_runtime_1.jsx)(Component, Object.assign({}, properties));
79
+ return (0, jsx_runtime_1.jsx)(Component, Object.assign({}, componentProps));
75
80
  }
76
81
  if (AsyncComponent) {
77
- return (0, jsx_runtime_1.jsx)(AsyncComponentRenderer, { AsyncComponent: AsyncComponent, properties: properties });
82
+ return (0, jsx_runtime_1.jsx)(AsyncComponentRenderer, Object.assign({ AsyncComponent: AsyncComponent }, componentProps));
78
83
  }
79
84
  return null;
80
85
  }
81
- function AsyncComponentRenderer({ AsyncComponent, properties, }) {
82
- const [{ Component, error, key }, setState] = (0, react_2.useState)({});
83
- (0, react_2.useEffect)(() => {
84
- AsyncComponent.then((m) => setState({ Component: m.default, key: Date.now() })).catch((error) => setState({ error, key: Date.now() }));
86
+ function AsyncComponentRenderer(_a) {
87
+ var { AsyncComponent } = _a, props = __rest(_a, ["AsyncComponent"]);
88
+ const [{ Component, error }, setState] = (0, react_1.useState)({});
89
+ (0, react_1.useEffect)(() => {
90
+ AsyncComponent.then((m) => setState({ Component: m.default, key: Date.now() })).catch((error) => setState({ error }));
85
91
  }, [AsyncComponent]);
86
92
  if (error) {
87
93
  return (0, jsx_runtime_1.jsx)(material_1.Alert, { severity: "error", children: error.stack });
88
94
  }
89
95
  if (Component) {
90
- return ((0, jsx_runtime_1.jsx)(react_error_boundary_1.ErrorBoundary, { fallbackRender: E, resetKeys: [key], children: (0, jsx_runtime_1.jsx)(Component, Object.assign({}, properties)) }));
96
+ return (0, jsx_runtime_1.jsx)(Component, Object.assign({}, props));
91
97
  }
92
98
  return null;
93
99
  }
@@ -95,8 +101,12 @@ function E({ error }) {
95
101
  return ((0, jsx_runtime_1.jsx)(material_1.Box, { children: (0, jsx_runtime_1.jsx)(material_1.Alert, { severity: "error", children: error.message }) }));
96
102
  }
97
103
  const PRELOAD_COMPONENTS_STATE = window[preload_1.PreloadComponentsStateGlobalVariableName];
98
- function loadComponent({ componentId, components, locale, defaultLocale, properties: parameters, }) {
104
+ function useComponent({ componentId, components, locale, defaultLocale, properties: parameters, }) {
99
105
  var _a, _b, _c, _d, _e;
106
+ const preloadRef = (0, react_1.useRef)();
107
+ const scriptRef = (0, react_1.useRef)({});
108
+ if (preloadRef.current)
109
+ return preloadRef.current;
100
110
  const preload = PRELOAD_COMPONENTS_STATE === null || PRELOAD_COMPONENTS_STATE === void 0 ? void 0 : PRELOAD_COMPONENTS_STATE.components[componentId];
101
111
  if (preload) {
102
112
  const properties = Object.fromEntries(Object.entries(parameters !== null && parameters !== void 0 ? parameters : {}).map(([id, { value }]) => {
@@ -104,12 +114,16 @@ function loadComponent({ componentId, components, locale, defaultLocale, propert
104
114
  const property = (_b = (_a = preload.component.properties) === null || _a === void 0 ? void 0 : _a[id]) === null || _b === void 0 ? void 0 : _b.data;
105
115
  if (!property)
106
116
  return [];
107
- return [property.key, (0, property_1.parsePropertyValue)(property.type, value)];
117
+ return [property.key, (0, property_1.parsePropertyValue)(property, value, { locale, defaultLocale })];
108
118
  }));
109
- return {
110
- Component: window[preload.preloadComponentModuleName],
111
- properties: (0, property_1.assignNullableFields)(properties, (_c = (_b = (_a = preload.locales) === null || _a === void 0 ? void 0 : _a[locale]) === null || _b === void 0 ? void 0 : _b.properties) !== null && _c !== void 0 ? _c : (_e = (_d = preload.locales) === null || _d === void 0 ? void 0 : _d[PRELOAD_COMPONENTS_STATE.config.defaultLocale]) === null || _e === void 0 ? void 0 : _e.properties),
112
- };
119
+ const mod = window[preload.preloadComponentModuleName];
120
+ if (typeof mod === 'function') {
121
+ preloadRef.current = {
122
+ Component: mod(),
123
+ properties: Object.assign(Object.assign({}, (0, property_1.assignNullableFields)(properties, (_c = (_b = (_a = preload.locales) === null || _a === void 0 ? void 0 : _a[locale]) === null || _b === void 0 ? void 0 : _b.properties) !== null && _c !== void 0 ? _c : (_e = (_d = preload.locales) === null || _d === void 0 ? void 0 : _d[PRELOAD_COMPONENTS_STATE.config.defaultLocale]) === null || _e === void 0 ? void 0 : _e.properties)), preload.serverSideProps),
124
+ };
125
+ return preloadRef.current;
126
+ }
113
127
  }
114
128
  const component = (0, property_1.mergeComponent)({
115
129
  componentId,
@@ -120,8 +134,12 @@ function loadComponent({ componentId, components, locale, defaultLocale, propert
120
134
  });
121
135
  if (!component)
122
136
  return null;
137
+ if (scriptRef.current.script !== component.script) {
138
+ scriptRef.current.script = component.script;
139
+ scriptRef.current.module = transpileAndLoadScript(component.script);
140
+ }
123
141
  return {
124
- AsyncComponent: transpileAndLoadScript(component.script),
142
+ AsyncComponent: scriptRef.current.module,
125
143
  properties: component.properties,
126
144
  };
127
145
  }
@@ -129,9 +147,13 @@ function transpileAndLoadScript(script) {
129
147
  return __awaiter(this, void 0, void 0, function* () {
130
148
  const ts = yield Promise.resolve().then(() => __importStar(require('typescript')));
131
149
  const compiled = ts.transpileModule(script, {
132
- compilerOptions: { jsx: ts.JsxEmit.React, module: ts.ModuleKind.ESNext },
150
+ compilerOptions: { jsx: ts.JsxEmit.React, module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2016 },
151
+ transformers: {
152
+ before: [(0, builtin_module_transformer_1.createBuiltinModuleTransformer)(ts)],
153
+ },
133
154
  }).outputText;
134
155
  const url = URL.createObjectURL(new Blob([compiled], { type: 'application/javascript' }));
135
156
  return Promise.resolve(`${url}`).then(s => __importStar(require(s)));
136
157
  });
137
158
  }
159
+ exports.transpileAndLoadScript = transpileAndLoadScript;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BuiltinModulesGlobalVariableName = void 0;
4
+ exports.BuiltinModulesGlobalVariableName = '__PAGES_KIT_BUILTIN_MODULES__';
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ const builtinIconifyReact = __importStar(require("../builtin/iconify/react"));
27
+ const builtinMuiLab = __importStar(require("../builtin/mui/lab"));
28
+ const builtinMuiMaterial = __importStar(require("../builtin/mui/material"));
29
+ const builtinReact = __importStar(require("../builtin/react"));
30
+ const builtinReactHookForm = __importStar(require("../builtin/react-hook-form"));
31
+ const builtinUtils = __importStar(require("../builtin/utils"));
32
+ const builtinZustand = __importStar(require("../builtin/zustand"));
33
+ const builtinZustandMiddlewareImmer = __importStar(require("../builtin/zustand/middleware/immer"));
34
+ const builtin_1 = require("../types/builtin");
35
+ function injectGlobalComponents() {
36
+ const win = window;
37
+ win[builtin_1.BuiltinModulesGlobalVariableName] = {
38
+ modules: {
39
+ '@blocklet/pages-kit/builtin/utils': builtinUtils,
40
+ '@blocklet/pages-kit/builtin/react': builtinReact,
41
+ '@blocklet/pages-kit/builtin/mui/material': builtinMuiMaterial,
42
+ '@blocklet/pages-kit/builtin/mui/lab': builtinMuiLab,
43
+ '@blocklet/pages-kit/builtin/react-hook-form': builtinReactHookForm,
44
+ '@blocklet/pages-kit/builtin/iconify/react': builtinIconifyReact,
45
+ '@blocklet/pages-kit/builtin/zustand': builtinZustand,
46
+ '@blocklet/pages-kit/builtin/zustand/middleware/immer': builtinZustandMiddlewareImmer,
47
+ },
48
+ require(module) {
49
+ return win[builtin_1.BuiltinModulesGlobalVariableName].modules[module];
50
+ },
51
+ };
52
+ }
53
+ injectGlobalComponents();
@@ -5,12 +5,13 @@ const types_1 = require("../types");
5
5
  function injectPreloadComponents({ config, components }) {
6
6
  const injectState = {
7
7
  config,
8
- components: Object.fromEntries(components.map(({ script, component, locales }) => [
8
+ components: Object.fromEntries(components.map(({ script, component, locales, serverSideProps }) => [
9
9
  component.id,
10
10
  {
11
11
  preloadComponentModuleName: script.umd.moduleName,
12
12
  component,
13
13
  locales,
14
+ serverSideProps,
14
15
  },
15
16
  ])),
16
17
  };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.assignNullableFields = exports.parsePropertyValue = exports.mergeComponent = exports.componentUMDName = void 0;
3
+ exports.assignNullableFields = exports.parsePropertyValue = exports.RenderNestedComponent = exports.mergeComponent = exports.componentUMDName = void 0;
4
4
  function componentUMDName({ componentId }) {
5
5
  return `PagesCustomComponent${componentId}`;
6
6
  }
@@ -16,14 +16,14 @@ function mergeComponent({ componentId, getComponent, locale, defaultLocale, prop
16
16
  const property = (_b = (_a = component === null || component === void 0 ? void 0 : component.properties) === null || _a === void 0 ? void 0 : _a[id]) === null || _b === void 0 ? void 0 : _b.data;
17
17
  if (!property)
18
18
  return [];
19
- return [property.key, parsePropertyValue(property.type, value)];
19
+ return [property.key, parsePropertyValue(property, value, { locale, defaultLocale })];
20
20
  }));
21
21
  while (component) {
22
22
  assignNullableFields(properties, Object.fromEntries(Object.values((_a = component.properties) !== null && _a !== void 0 ? _a : {}).map(({ data }) => {
23
23
  var _a, _b, _c, _d, _e;
24
24
  return [
25
25
  data.key,
26
- parsePropertyValue(data.type, (_c = (_b = (_a = data.locales) === null || _a === void 0 ? void 0 : _a[locale]) === null || _b === void 0 ? void 0 : _b.defaultValue) !== null && _c !== void 0 ? _c : (_e = (_d = data.locales) === null || _d === void 0 ? void 0 : _d[defaultLocale]) === null || _e === void 0 ? void 0 : _e.defaultValue),
26
+ parsePropertyValue(data, (_c = (_b = (_a = data.locales) === null || _a === void 0 ? void 0 : _a[locale]) === null || _b === void 0 ? void 0 : _b.defaultValue) !== null && _c !== void 0 ? _c : (_e = (_d = data.locales) === null || _d === void 0 ? void 0 : _d[defaultLocale]) === null || _e === void 0 ? void 0 : _e.defaultValue, { locale, defaultLocale }),
27
27
  ];
28
28
  })));
29
29
  if (((_b = component.renderer) === null || _b === void 0 ? void 0 : _b.type) === 'react-component') {
@@ -34,7 +34,7 @@ function mergeComponent({ componentId, getComponent, locale, defaultLocale, prop
34
34
  script = (_e = component.renderer) === null || _e === void 0 ? void 0 : _e.script;
35
35
  break;
36
36
  }
37
- if (((_f = component.renderer) === null || _f === void 0 ? void 0 : _f.type) === 'custom-component' && component.renderer.componentId) {
37
+ if (((_f = component.renderer) === null || _f === void 0 ? void 0 : _f.type) === 'component' && component.renderer.componentId) {
38
38
  const { componentId } = component.renderer;
39
39
  const next = getComponent(componentId);
40
40
  const nextParameters = component.renderer.properties;
@@ -43,7 +43,7 @@ function mergeComponent({ componentId, getComponent, locale, defaultLocale, prop
43
43
  const locales = (_a = nextParameters === null || nextParameters === void 0 ? void 0 : nextParameters[id]) === null || _a === void 0 ? void 0 : _a.locales;
44
44
  return [
45
45
  data.key,
46
- parsePropertyValue(data.type, (_h = (_e = (_c = (_b = locales === null || locales === void 0 ? void 0 : locales[locale]) === null || _b === void 0 ? void 0 : _b.value) !== null && _c !== void 0 ? _c : (_d = locales === null || locales === void 0 ? void 0 : locales[defaultLocale]) === null || _d === void 0 ? void 0 : _d.value) !== null && _e !== void 0 ? _e : (_g = (_f = data.locales) === null || _f === void 0 ? void 0 : _f[locale]) === null || _g === void 0 ? void 0 : _g.defaultValue) !== null && _h !== void 0 ? _h : (_k = (_j = data.locales) === null || _j === void 0 ? void 0 : _j[defaultLocale]) === null || _k === void 0 ? void 0 : _k.defaultValue),
46
+ parsePropertyValue(data, (_h = (_e = (_c = (_b = locales === null || locales === void 0 ? void 0 : locales[locale]) === null || _b === void 0 ? void 0 : _b.value) !== null && _c !== void 0 ? _c : (_d = locales === null || locales === void 0 ? void 0 : locales[defaultLocale]) === null || _d === void 0 ? void 0 : _d.value) !== null && _e !== void 0 ? _e : (_g = (_f = data.locales) === null || _f === void 0 ? void 0 : _f[locale]) === null || _g === void 0 ? void 0 : _g.defaultValue) !== null && _h !== void 0 ? _h : (_k = (_j = data.locales) === null || _j === void 0 ? void 0 : _j[defaultLocale]) === null || _k === void 0 ? void 0 : _k.defaultValue, { locale, defaultLocale }),
47
47
  ];
48
48
  })));
49
49
  component = next;
@@ -57,8 +57,10 @@ function mergeComponent({ componentId, getComponent, locale, defaultLocale, prop
57
57
  return { script, properties };
58
58
  }
59
59
  exports.mergeComponent = mergeComponent;
60
- function parsePropertyValue(type, value) {
61
- if (type === 'json') {
60
+ exports.RenderNestedComponent = '__RENDER_NESTED_COMPONENT__';
61
+ function parsePropertyValue(property, value, { locale, defaultLocale }) {
62
+ var _a, _b, _c;
63
+ if (property.type === 'json') {
62
64
  if (!value)
63
65
  return undefined;
64
66
  try {
@@ -69,6 +71,16 @@ function parsePropertyValue(type, value) {
69
71
  return undefined;
70
72
  }
71
73
  }
74
+ if (property.type === 'component') {
75
+ const { componentId, locales } = property;
76
+ if (!componentId)
77
+ return undefined;
78
+ return {
79
+ type: exports.RenderNestedComponent,
80
+ componentId,
81
+ properties: (_b = (_a = locales === null || locales === void 0 ? void 0 : locales[locale]) === null || _a === void 0 ? void 0 : _a.defaultValue) !== null && _b !== void 0 ? _b : (_c = locales === null || locales === void 0 ? void 0 : locales[defaultLocale]) === null || _c === void 0 ? void 0 : _c.defaultValue,
82
+ };
83
+ }
72
84
  return value;
73
85
  }
74
86
  exports.parsePropertyValue = parsePropertyValue;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createBuiltinModuleTransformer = void 0;
4
+ const builtin_1 = require("../../types/builtin");
5
+ const createBuiltinModuleTransformer = (ts) => (context) => (file) => {
6
+ const { factory } = context;
7
+ const builtins = [];
8
+ const statements = file.statements.filter((s) => {
9
+ var _a, _b, _c;
10
+ if (ts.isImportDeclaration(s) &&
11
+ ts.isStringLiteral(s.moduleSpecifier) &&
12
+ s.moduleSpecifier.text.startsWith('@blocklet/pages-kit/builtin/')) {
13
+ const builtin = {
14
+ moduleName: s.moduleSpecifier.text,
15
+ name: (_b = (_a = s.importClause) === null || _a === void 0 ? void 0 : _a.name) === null || _b === void 0 ? void 0 : _b.text,
16
+ members: [],
17
+ };
18
+ const namedBindings = (_c = s.importClause) === null || _c === void 0 ? void 0 : _c.namedBindings;
19
+ if (namedBindings) {
20
+ if (ts.isNamespaceImport(namedBindings)) {
21
+ builtin.name = namedBindings.name.text;
22
+ }
23
+ namedBindings.forEachChild((node) => {
24
+ var _a;
25
+ if (ts.isImportSpecifier(node)) {
26
+ builtin.members.push({ name: node.name.text, propertyName: (_a = node.propertyName) === null || _a === void 0 ? void 0 : _a.text });
27
+ }
28
+ });
29
+ }
30
+ if (builtin.name || builtin.members.length) {
31
+ builtins.push(builtin);
32
+ }
33
+ return false;
34
+ }
35
+ return true;
36
+ });
37
+ statements.unshift(...builtins.flatMap((builtin) => {
38
+ const mod = factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(builtin_1.BuiltinModulesGlobalVariableName), 'require'), undefined, [factory.createStringLiteral(builtin.moduleName)]);
39
+ return [
40
+ builtin.name
41
+ ? factory.createVariableStatement([], factory.createVariableDeclarationList([
42
+ factory.createVariableDeclaration(factory.createIdentifier(builtin.name), undefined, undefined, mod),
43
+ ], ts.NodeFlags.Const))
44
+ : undefined,
45
+ ...builtin.members.map((member) => {
46
+ var _a;
47
+ return factory.createVariableStatement([], factory.createVariableDeclarationList([
48
+ factory.createVariableDeclaration(factory.createIdentifier(member.name), undefined, undefined, factory.createPropertyAccessExpression(mod, (_a = member.propertyName) !== null && _a !== void 0 ? _a : member.name)),
49
+ ], ts.NodeFlags.Const));
50
+ }),
51
+ ].filter((i) => !!i);
52
+ }));
53
+ return factory.updateSourceFile(file, statements);
54
+ };
55
+ exports.createBuiltinModuleTransformer = createBuiltinModuleTransformer;
@@ -0,0 +1 @@
1
+ export { Icon } from '@iconify/react';
@@ -0,0 +1 @@
1
+ export { LoadingButton } from '@mui/lab';
@@ -0,0 +1,13 @@
1
+ export { Autocomplete } from '@mui/material';
2
+ export { Box } from '@mui/material';
3
+ export { Button } from '@mui/material';
4
+ export { ClickAwayListener } from '@mui/material';
5
+ export { Dialog } from '@mui/material';
6
+ export { DialogActions } from '@mui/material';
7
+ export { DialogContent } from '@mui/material';
8
+ export { DialogTitle } from '@mui/material';
9
+ export { Paper } from '@mui/material';
10
+ export { Popper } from '@mui/material';
11
+ export { Stack } from '@mui/material';
12
+ export { Switch } from '@mui/material';
13
+ export { TextField } from '@mui/material';
@@ -0,0 +1 @@
1
+ export * from 'react-hook-form';
@@ -0,0 +1 @@
1
+ export { Children, Component, Fragment, Profiler, PureComponent, StrictMode, Suspense, cloneElement, createContext, createElement, createFactory, createRef, forwardRef, isValidElement, lazy, memo, startTransition, useCallback, useContext, useDebugValue, useDeferredValue, useEffect, useId, useImperativeHandle, useInsertionEffect, useLayoutEffect, useMemo, useReducer, useRef, useState, useSyncExternalStore, useTransition, version, } from 'react';
@@ -0,0 +1 @@
1
+ export { joinURL, withQuery } from 'ufo';
@@ -0,0 +1 @@
1
+ export * from 'zustand/middleware/immer';
@@ -0,0 +1 @@
1
+ export * from 'zustand';
@@ -19,49 +19,54 @@ var __rest = (this && this.__rest) || function (s, e) {
19
19
  return t;
20
20
  };
21
21
  import { jsx as _jsx } from "react/jsx-runtime";
22
- import { Icon as IconifyIcon } from '@iconify/react';
23
- import { Alert, Box, Button, Stack } from '@mui/material';
24
- import React, { useEffect, useState } from 'react';
22
+ import '../../utils/inject-global-components';
23
+ import { Alert, Box } from '@mui/material';
24
+ import { pick } from 'lodash';
25
+ import { useEffect, useRef, useState } from 'react';
25
26
  import { ErrorBoundary } from 'react-error-boundary';
26
- import { joinURL, withQuery } from 'ufo';
27
27
  import { PreloadComponentsStateGlobalVariableName } from '../../types/preload';
28
- import { assignNullableFields, mergeComponent, parsePropertyValue } from '../../utils/property';
29
- // inject global variables for web-component renderer
30
- const win = window;
31
- win.joinURL = joinURL;
32
- win.withQuery = withQuery;
33
- win.React = React;
34
- win.Box = Box;
35
- win.Stack = Stack;
36
- win.Button = Button;
37
- win.IconifyIcon = IconifyIcon;
28
+ import { RenderNestedComponent, assignNullableFields, mergeComponent, parsePropertyValue } from '../../utils/property';
29
+ import { createBuiltinModuleTransformer } from '../../utils/typescript/builtin-module-transformer';
30
+ const MAXIMUM_RENDER_STACK_SIZE = 50;
38
31
  export default function CustomComponentRenderer(_a) {
39
32
  var props = __rest(_a, []);
40
- return (_jsx(ErrorBoundary, { fallbackRender: E, children: _jsx(Renderer, Object.assign({}, props)) }));
33
+ return (_jsx(ErrorBoundary, { fallbackRender: E, resetKeys: [Date.now()], children: _jsx(ComponentRenderer, Object.assign({}, props)) }));
34
+ }
35
+ function ComponentRenderer(_a) {
36
+ var { renderCount = 0 } = _a, props = __rest(_a, ["renderCount"]);
37
+ if (renderCount > MAXIMUM_RENDER_STACK_SIZE) {
38
+ throw new Error('Maximum render stack size exceeded');
39
+ }
40
+ return _jsx(Renderer, Object.assign({ renderCount: renderCount + 1 }, props));
41
41
  }
42
42
  function Renderer(props) {
43
- const component = loadComponent(props);
43
+ const component = useComponent(props);
44
44
  if (!component)
45
45
  return null;
46
46
  const { Component, AsyncComponent, properties } = component;
47
+ const componentProps = Object.fromEntries(Object.entries(Object.assign(Object.assign({}, properties), props.props)).map(([key, val]) => [
48
+ key,
49
+ (val === null || val === void 0 ? void 0 : val.type) === RenderNestedComponent ? (_jsx(ComponentRenderer, Object.assign({ renderCount: props.renderCount, componentId: val.componentId }, pick(props, 'components', 'locale', 'defaultLocale'), { properties: val.properties }))) : (val),
50
+ ]));
47
51
  if (Component) {
48
- return _jsx(Component, Object.assign({}, properties));
52
+ return _jsx(Component, Object.assign({}, componentProps));
49
53
  }
50
54
  if (AsyncComponent) {
51
- return _jsx(AsyncComponentRenderer, { AsyncComponent: AsyncComponent, properties: properties });
55
+ return _jsx(AsyncComponentRenderer, Object.assign({ AsyncComponent: AsyncComponent }, componentProps));
52
56
  }
53
57
  return null;
54
58
  }
55
- function AsyncComponentRenderer({ AsyncComponent, properties, }) {
56
- const [{ Component, error, key }, setState] = useState({});
59
+ function AsyncComponentRenderer(_a) {
60
+ var { AsyncComponent } = _a, props = __rest(_a, ["AsyncComponent"]);
61
+ const [{ Component, error }, setState] = useState({});
57
62
  useEffect(() => {
58
- AsyncComponent.then((m) => setState({ Component: m.default, key: Date.now() })).catch((error) => setState({ error, key: Date.now() }));
63
+ AsyncComponent.then((m) => setState({ Component: m.default, key: Date.now() })).catch((error) => setState({ error }));
59
64
  }, [AsyncComponent]);
60
65
  if (error) {
61
66
  return _jsx(Alert, { severity: "error", children: error.stack });
62
67
  }
63
68
  if (Component) {
64
- return (_jsx(ErrorBoundary, { fallbackRender: E, resetKeys: [key], children: _jsx(Component, Object.assign({}, properties)) }));
69
+ return _jsx(Component, Object.assign({}, props));
65
70
  }
66
71
  return null;
67
72
  }
@@ -69,8 +74,12 @@ function E({ error }) {
69
74
  return (_jsx(Box, { children: _jsx(Alert, { severity: "error", children: error.message }) }));
70
75
  }
71
76
  const PRELOAD_COMPONENTS_STATE = window[PreloadComponentsStateGlobalVariableName];
72
- function loadComponent({ componentId, components, locale, defaultLocale, properties: parameters, }) {
77
+ function useComponent({ componentId, components, locale, defaultLocale, properties: parameters, }) {
73
78
  var _a, _b, _c, _d, _e;
79
+ const preloadRef = useRef();
80
+ const scriptRef = useRef({});
81
+ if (preloadRef.current)
82
+ return preloadRef.current;
74
83
  const preload = PRELOAD_COMPONENTS_STATE === null || PRELOAD_COMPONENTS_STATE === void 0 ? void 0 : PRELOAD_COMPONENTS_STATE.components[componentId];
75
84
  if (preload) {
76
85
  const properties = Object.fromEntries(Object.entries(parameters !== null && parameters !== void 0 ? parameters : {}).map(([id, { value }]) => {
@@ -78,12 +87,16 @@ function loadComponent({ componentId, components, locale, defaultLocale, propert
78
87
  const property = (_b = (_a = preload.component.properties) === null || _a === void 0 ? void 0 : _a[id]) === null || _b === void 0 ? void 0 : _b.data;
79
88
  if (!property)
80
89
  return [];
81
- return [property.key, parsePropertyValue(property.type, value)];
90
+ return [property.key, parsePropertyValue(property, value, { locale, defaultLocale })];
82
91
  }));
83
- return {
84
- Component: window[preload.preloadComponentModuleName],
85
- properties: assignNullableFields(properties, (_c = (_b = (_a = preload.locales) === null || _a === void 0 ? void 0 : _a[locale]) === null || _b === void 0 ? void 0 : _b.properties) !== null && _c !== void 0 ? _c : (_e = (_d = preload.locales) === null || _d === void 0 ? void 0 : _d[PRELOAD_COMPONENTS_STATE.config.defaultLocale]) === null || _e === void 0 ? void 0 : _e.properties),
86
- };
92
+ const mod = window[preload.preloadComponentModuleName];
93
+ if (typeof mod === 'function') {
94
+ preloadRef.current = {
95
+ Component: mod(),
96
+ properties: Object.assign(Object.assign({}, assignNullableFields(properties, (_c = (_b = (_a = preload.locales) === null || _a === void 0 ? void 0 : _a[locale]) === null || _b === void 0 ? void 0 : _b.properties) !== null && _c !== void 0 ? _c : (_e = (_d = preload.locales) === null || _d === void 0 ? void 0 : _d[PRELOAD_COMPONENTS_STATE.config.defaultLocale]) === null || _e === void 0 ? void 0 : _e.properties)), preload.serverSideProps),
97
+ };
98
+ return preloadRef.current;
99
+ }
87
100
  }
88
101
  const component = mergeComponent({
89
102
  componentId,
@@ -94,16 +107,23 @@ function loadComponent({ componentId, components, locale, defaultLocale, propert
94
107
  });
95
108
  if (!component)
96
109
  return null;
110
+ if (scriptRef.current.script !== component.script) {
111
+ scriptRef.current.script = component.script;
112
+ scriptRef.current.module = transpileAndLoadScript(component.script);
113
+ }
97
114
  return {
98
- AsyncComponent: transpileAndLoadScript(component.script),
115
+ AsyncComponent: scriptRef.current.module,
99
116
  properties: component.properties,
100
117
  };
101
118
  }
102
- function transpileAndLoadScript(script) {
119
+ export function transpileAndLoadScript(script) {
103
120
  return __awaiter(this, void 0, void 0, function* () {
104
121
  const ts = yield import('typescript');
105
122
  const compiled = ts.transpileModule(script, {
106
- compilerOptions: { jsx: ts.JsxEmit.React, module: ts.ModuleKind.ESNext },
123
+ compilerOptions: { jsx: ts.JsxEmit.React, module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2016 },
124
+ transformers: {
125
+ before: [createBuiltinModuleTransformer(ts)],
126
+ },
107
127
  }).outputText;
108
128
  const url = URL.createObjectURL(new Blob([compiled], { type: 'application/javascript' }));
109
129
  return import(/* @vite-ignore */ url);
@@ -0,0 +1 @@
1
+ export const BuiltinModulesGlobalVariableName = '__PAGES_KIT_BUILTIN_MODULES__';
@@ -0,0 +1,28 @@
1
+ import * as builtinIconifyReact from '../builtin/iconify/react';
2
+ import * as builtinMuiLab from '../builtin/mui/lab';
3
+ import * as builtinMuiMaterial from '../builtin/mui/material';
4
+ import * as builtinReact from '../builtin/react';
5
+ import * as builtinReactHookForm from '../builtin/react-hook-form';
6
+ import * as builtinUtils from '../builtin/utils';
7
+ import * as builtinZustand from '../builtin/zustand';
8
+ import * as builtinZustandMiddlewareImmer from '../builtin/zustand/middleware/immer';
9
+ import { BuiltinModulesGlobalVariableName } from '../types/builtin';
10
+ function injectGlobalComponents() {
11
+ const win = window;
12
+ win[BuiltinModulesGlobalVariableName] = {
13
+ modules: {
14
+ '@blocklet/pages-kit/builtin/utils': builtinUtils,
15
+ '@blocklet/pages-kit/builtin/react': builtinReact,
16
+ '@blocklet/pages-kit/builtin/mui/material': builtinMuiMaterial,
17
+ '@blocklet/pages-kit/builtin/mui/lab': builtinMuiLab,
18
+ '@blocklet/pages-kit/builtin/react-hook-form': builtinReactHookForm,
19
+ '@blocklet/pages-kit/builtin/iconify/react': builtinIconifyReact,
20
+ '@blocklet/pages-kit/builtin/zustand': builtinZustand,
21
+ '@blocklet/pages-kit/builtin/zustand/middleware/immer': builtinZustandMiddlewareImmer,
22
+ },
23
+ require(module) {
24
+ return win[BuiltinModulesGlobalVariableName].modules[module];
25
+ },
26
+ };
27
+ }
28
+ injectGlobalComponents();
@@ -2,12 +2,13 @@ import { PreloadComponentsStateGlobalVariableName } from '../types';
2
2
  export function injectPreloadComponents({ config, components }) {
3
3
  const injectState = {
4
4
  config,
5
- components: Object.fromEntries(components.map(({ script, component, locales }) => [
5
+ components: Object.fromEntries(components.map(({ script, component, locales, serverSideProps }) => [
6
6
  component.id,
7
7
  {
8
8
  preloadComponentModuleName: script.umd.moduleName,
9
9
  component,
10
10
  locales,
11
+ serverSideProps,
11
12
  },
12
13
  ])),
13
14
  };
@@ -12,14 +12,14 @@ export function mergeComponent({ componentId, getComponent, locale, defaultLocal
12
12
  const property = (_b = (_a = component === null || component === void 0 ? void 0 : component.properties) === null || _a === void 0 ? void 0 : _a[id]) === null || _b === void 0 ? void 0 : _b.data;
13
13
  if (!property)
14
14
  return [];
15
- return [property.key, parsePropertyValue(property.type, value)];
15
+ return [property.key, parsePropertyValue(property, value, { locale, defaultLocale })];
16
16
  }));
17
17
  while (component) {
18
18
  assignNullableFields(properties, Object.fromEntries(Object.values((_a = component.properties) !== null && _a !== void 0 ? _a : {}).map(({ data }) => {
19
19
  var _a, _b, _c, _d, _e;
20
20
  return [
21
21
  data.key,
22
- parsePropertyValue(data.type, (_c = (_b = (_a = data.locales) === null || _a === void 0 ? void 0 : _a[locale]) === null || _b === void 0 ? void 0 : _b.defaultValue) !== null && _c !== void 0 ? _c : (_e = (_d = data.locales) === null || _d === void 0 ? void 0 : _d[defaultLocale]) === null || _e === void 0 ? void 0 : _e.defaultValue),
22
+ parsePropertyValue(data, (_c = (_b = (_a = data.locales) === null || _a === void 0 ? void 0 : _a[locale]) === null || _b === void 0 ? void 0 : _b.defaultValue) !== null && _c !== void 0 ? _c : (_e = (_d = data.locales) === null || _d === void 0 ? void 0 : _d[defaultLocale]) === null || _e === void 0 ? void 0 : _e.defaultValue, { locale, defaultLocale }),
23
23
  ];
24
24
  })));
25
25
  if (((_b = component.renderer) === null || _b === void 0 ? void 0 : _b.type) === 'react-component') {
@@ -30,7 +30,7 @@ export function mergeComponent({ componentId, getComponent, locale, defaultLocal
30
30
  script = (_e = component.renderer) === null || _e === void 0 ? void 0 : _e.script;
31
31
  break;
32
32
  }
33
- if (((_f = component.renderer) === null || _f === void 0 ? void 0 : _f.type) === 'custom-component' && component.renderer.componentId) {
33
+ if (((_f = component.renderer) === null || _f === void 0 ? void 0 : _f.type) === 'component' && component.renderer.componentId) {
34
34
  const { componentId } = component.renderer;
35
35
  const next = getComponent(componentId);
36
36
  const nextParameters = component.renderer.properties;
@@ -39,7 +39,7 @@ export function mergeComponent({ componentId, getComponent, locale, defaultLocal
39
39
  const locales = (_a = nextParameters === null || nextParameters === void 0 ? void 0 : nextParameters[id]) === null || _a === void 0 ? void 0 : _a.locales;
40
40
  return [
41
41
  data.key,
42
- parsePropertyValue(data.type, (_h = (_e = (_c = (_b = locales === null || locales === void 0 ? void 0 : locales[locale]) === null || _b === void 0 ? void 0 : _b.value) !== null && _c !== void 0 ? _c : (_d = locales === null || locales === void 0 ? void 0 : locales[defaultLocale]) === null || _d === void 0 ? void 0 : _d.value) !== null && _e !== void 0 ? _e : (_g = (_f = data.locales) === null || _f === void 0 ? void 0 : _f[locale]) === null || _g === void 0 ? void 0 : _g.defaultValue) !== null && _h !== void 0 ? _h : (_k = (_j = data.locales) === null || _j === void 0 ? void 0 : _j[defaultLocale]) === null || _k === void 0 ? void 0 : _k.defaultValue),
42
+ parsePropertyValue(data, (_h = (_e = (_c = (_b = locales === null || locales === void 0 ? void 0 : locales[locale]) === null || _b === void 0 ? void 0 : _b.value) !== null && _c !== void 0 ? _c : (_d = locales === null || locales === void 0 ? void 0 : locales[defaultLocale]) === null || _d === void 0 ? void 0 : _d.value) !== null && _e !== void 0 ? _e : (_g = (_f = data.locales) === null || _f === void 0 ? void 0 : _f[locale]) === null || _g === void 0 ? void 0 : _g.defaultValue) !== null && _h !== void 0 ? _h : (_k = (_j = data.locales) === null || _j === void 0 ? void 0 : _j[defaultLocale]) === null || _k === void 0 ? void 0 : _k.defaultValue, { locale, defaultLocale }),
43
43
  ];
44
44
  })));
45
45
  component = next;
@@ -52,8 +52,10 @@ export function mergeComponent({ componentId, getComponent, locale, defaultLocal
52
52
  return null;
53
53
  return { script, properties };
54
54
  }
55
- export function parsePropertyValue(type, value) {
56
- if (type === 'json') {
55
+ export const RenderNestedComponent = '__RENDER_NESTED_COMPONENT__';
56
+ export function parsePropertyValue(property, value, { locale, defaultLocale }) {
57
+ var _a, _b, _c;
58
+ if (property.type === 'json') {
57
59
  if (!value)
58
60
  return undefined;
59
61
  try {
@@ -64,6 +66,16 @@ export function parsePropertyValue(type, value) {
64
66
  return undefined;
65
67
  }
66
68
  }
69
+ if (property.type === 'component') {
70
+ const { componentId, locales } = property;
71
+ if (!componentId)
72
+ return undefined;
73
+ return {
74
+ type: RenderNestedComponent,
75
+ componentId,
76
+ properties: (_b = (_a = locales === null || locales === void 0 ? void 0 : locales[locale]) === null || _a === void 0 ? void 0 : _a.defaultValue) !== null && _b !== void 0 ? _b : (_c = locales === null || locales === void 0 ? void 0 : locales[defaultLocale]) === null || _c === void 0 ? void 0 : _c.defaultValue,
77
+ };
78
+ }
67
79
  return value;
68
80
  }
69
81
  export function assignNullableFields(properties, newProperties) {
@@ -0,0 +1,51 @@
1
+ import { BuiltinModulesGlobalVariableName } from '../../types/builtin';
2
+ export const createBuiltinModuleTransformer = (ts) => (context) => (file) => {
3
+ const { factory } = context;
4
+ const builtins = [];
5
+ const statements = file.statements.filter((s) => {
6
+ var _a, _b, _c;
7
+ if (ts.isImportDeclaration(s) &&
8
+ ts.isStringLiteral(s.moduleSpecifier) &&
9
+ s.moduleSpecifier.text.startsWith('@blocklet/pages-kit/builtin/')) {
10
+ const builtin = {
11
+ moduleName: s.moduleSpecifier.text,
12
+ name: (_b = (_a = s.importClause) === null || _a === void 0 ? void 0 : _a.name) === null || _b === void 0 ? void 0 : _b.text,
13
+ members: [],
14
+ };
15
+ const namedBindings = (_c = s.importClause) === null || _c === void 0 ? void 0 : _c.namedBindings;
16
+ if (namedBindings) {
17
+ if (ts.isNamespaceImport(namedBindings)) {
18
+ builtin.name = namedBindings.name.text;
19
+ }
20
+ namedBindings.forEachChild((node) => {
21
+ var _a;
22
+ if (ts.isImportSpecifier(node)) {
23
+ builtin.members.push({ name: node.name.text, propertyName: (_a = node.propertyName) === null || _a === void 0 ? void 0 : _a.text });
24
+ }
25
+ });
26
+ }
27
+ if (builtin.name || builtin.members.length) {
28
+ builtins.push(builtin);
29
+ }
30
+ return false;
31
+ }
32
+ return true;
33
+ });
34
+ statements.unshift(...builtins.flatMap((builtin) => {
35
+ const mod = factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(BuiltinModulesGlobalVariableName), 'require'), undefined, [factory.createStringLiteral(builtin.moduleName)]);
36
+ return [
37
+ builtin.name
38
+ ? factory.createVariableStatement([], factory.createVariableDeclarationList([
39
+ factory.createVariableDeclaration(factory.createIdentifier(builtin.name), undefined, undefined, mod),
40
+ ], ts.NodeFlags.Const))
41
+ : undefined,
42
+ ...builtin.members.map((member) => {
43
+ var _a;
44
+ return factory.createVariableStatement([], factory.createVariableDeclarationList([
45
+ factory.createVariableDeclaration(factory.createIdentifier(member.name), undefined, undefined, factory.createPropertyAccessExpression(mod, (_a = member.propertyName) !== null && _a !== void 0 ? _a : member.name)),
46
+ ], ts.NodeFlags.Const));
47
+ }),
48
+ ].filter((i) => !!i);
49
+ }));
50
+ return factory.updateSourceFile(file, statements);
51
+ };
@@ -0,0 +1 @@
1
+ export { Icon } from '@iconify/react';
@@ -0,0 +1 @@
1
+ export { LoadingButton } from '@mui/lab';
@@ -0,0 +1,13 @@
1
+ export { Autocomplete } from '@mui/material';
2
+ export { Box } from '@mui/material';
3
+ export { Button } from '@mui/material';
4
+ export { ClickAwayListener } from '@mui/material';
5
+ export { Dialog } from '@mui/material';
6
+ export { DialogActions } from '@mui/material';
7
+ export { DialogContent } from '@mui/material';
8
+ export { DialogTitle } from '@mui/material';
9
+ export { Paper } from '@mui/material';
10
+ export { Popper } from '@mui/material';
11
+ export { Stack } from '@mui/material';
12
+ export { Switch } from '@mui/material';
13
+ export { TextField } from '@mui/material';
@@ -0,0 +1 @@
1
+ export * from 'react-hook-form';
@@ -0,0 +1 @@
1
+ export { Children, Component, Fragment, Profiler, PureComponent, StrictMode, Suspense, cloneElement, createContext, createElement, createFactory, createRef, forwardRef, isValidElement, lazy, memo, startTransition, useCallback, useContext, useDebugValue, useDeferredValue, useEffect, useId, useImperativeHandle, useInsertionEffect, useLayoutEffect, useMemo, useReducer, useRef, useState, useSyncExternalStore, useTransition, version, } from 'react';
@@ -0,0 +1 @@
1
+ export { joinURL, withQuery } from 'ufo';
@@ -0,0 +1 @@
1
+ export * from 'zustand/middleware/immer';
@@ -0,0 +1 @@
1
+ export * from 'zustand';
@@ -1,3 +1,4 @@
1
+ import '../../utils/inject-global-components';
1
2
  import { CustomComponent } from '../../types/core';
2
3
  export interface CustomComponentRendererProps {
3
4
  componentId: string;
@@ -5,7 +6,13 @@ export interface CustomComponentRendererProps {
5
6
  defaultLocale?: string;
6
7
  components?: CustomComponent[];
7
8
  properties?: {
9
+ [key: string]: {
10
+ value: any;
11
+ };
12
+ };
13
+ props?: {
8
14
  [key: string]: any;
9
15
  };
10
16
  }
11
17
  export default function CustomComponentRenderer({ ...props }: CustomComponentRendererProps): import("react/jsx-runtime").JSX.Element;
18
+ export declare function transpileAndLoadScript(script: string): Promise<any>;
@@ -24,5 +24,8 @@ export interface PreloadComponentsResult {
24
24
  };
25
25
  };
26
26
  };
27
+ serverSideProps?: {
28
+ [key: string]: any;
29
+ };
27
30
  }[];
28
31
  }
@@ -0,0 +1 @@
1
+ export declare const BuiltinModulesGlobalVariableName = "__PAGES_KIT_BUILTIN_MODULES__";
@@ -10,7 +10,7 @@ export type CustomComponent = {
10
10
  type: 'react-component';
11
11
  script?: string;
12
12
  } | {
13
- type: 'custom-component';
13
+ type: 'component';
14
14
  componentId?: string;
15
15
  properties?: {
16
16
  [id: string]: {
@@ -25,18 +25,32 @@ export type CustomComponent = {
25
25
  properties?: {
26
26
  [id: string]: {
27
27
  index: number;
28
- data: {
29
- id: string;
30
- key?: string;
31
- type?: 'string' | 'number' | 'boolean' | 'json' | 'custom-component' | 'url';
32
- multiline?: boolean;
33
- locales?: {
34
- [locale: string]: {
35
- name?: string;
36
- defaultValue?: any;
37
- };
38
- };
39
- };
28
+ data: CustomComponentCommonProperty | CustomComponentCustomProperty | CustomComponentStringProperty | CustomComponentComponentProperty;
40
29
  };
41
30
  };
42
31
  };
32
+ export interface CustomComponentPropertyBase {
33
+ id: string;
34
+ key?: string;
35
+ locales?: {
36
+ [locale: string]: {
37
+ name?: string;
38
+ defaultValue?: any;
39
+ };
40
+ };
41
+ }
42
+ export interface CustomComponentCommonProperty extends CustomComponentPropertyBase {
43
+ type: 'number' | 'boolean' | 'json' | 'url';
44
+ }
45
+ export interface CustomComponentCustomProperty extends CustomComponentPropertyBase {
46
+ type: 'custom';
47
+ componentId?: string;
48
+ }
49
+ export interface CustomComponentStringProperty extends CustomComponentPropertyBase {
50
+ type?: 'string';
51
+ multiline?: boolean;
52
+ }
53
+ export interface CustomComponentComponentProperty extends CustomComponentPropertyBase {
54
+ type: 'component';
55
+ componentId?: string;
56
+ }
@@ -10,6 +10,9 @@ export interface PreloadComponent {
10
10
  };
11
11
  };
12
12
  };
13
+ serverSideProps?: {
14
+ [key: string]: any;
15
+ };
13
16
  }
14
17
  export interface PreloadComponentsState {
15
18
  config: {
@@ -0,0 +1 @@
1
+ export {};
@@ -18,5 +18,9 @@ export declare function mergeComponent({ componentId, getComponent, locale, defa
18
18
  [key: string]: any;
19
19
  };
20
20
  } | null;
21
- export declare function parsePropertyValue(type: NonNullable<CustomComponent['properties']>[string]['data']['type'], value: any): any;
21
+ export declare const RenderNestedComponent = "__RENDER_NESTED_COMPONENT__";
22
+ export declare function parsePropertyValue(property: NonNullable<CustomComponent['properties']>[string]['data'], value: any, { locale, defaultLocale }: {
23
+ locale?: string;
24
+ defaultLocale?: string;
25
+ }): any;
22
26
  export declare function assignNullableFields(properties: any, newProperties: any): any;
@@ -0,0 +1,2 @@
1
+ import { SourceFile, TransformerFactory } from 'typescript';
2
+ export declare const createBuiltinModuleTransformer: (ts: typeof import('typescript')) => TransformerFactory<SourceFile>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/pages-kit",
3
- "version": "0.2.214",
3
+ "version": "0.2.215",
4
4
  "description": "Pages Kit components and utils",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -24,6 +24,10 @@
24
24
  "import": "./lib/esm/api/*.js",
25
25
  "require": "./lib/cjs/api/*.js"
26
26
  },
27
+ "./builtin/*": {
28
+ "import": "./lib/esm/builtin/*.js",
29
+ "require": "./lib/cjs/builtin/*.js"
30
+ },
27
31
  "./components": {
28
32
  "import": "./lib/esm/components/index.js",
29
33
  "require": "./lib/cjs/components/index.js"
@@ -49,6 +53,10 @@
49
53
  "./lib/types/api/*.d.ts",
50
54
  "./lib/types/api/*/index.d.ts"
51
55
  ],
56
+ "builtin/*": [
57
+ "./lib/types/builtin/*.d.ts",
58
+ "./lib/types/builtin/*/index.d.ts"
59
+ ],
52
60
  "components": [
53
61
  "./lib/types/components/index.d.ts"
54
62
  ],
@@ -74,6 +82,7 @@
74
82
  },
75
83
  "scripts": {
76
84
  "lint": "eslint src --ext .mjs,.js,.jsx,.ts,.tsx",
85
+ "lint:fix": "npm run lint -- --fix",
77
86
  "build": "run-p build:*",
78
87
  "build:cjs": "tsc --module commonjs --outDir lib/cjs",
79
88
  "build:esm": "tsc --module es2022 --outDir lib/esm",
@@ -86,11 +95,13 @@
86
95
  "dependencies": {
87
96
  "@blocklet/sdk": "^1.16.22",
88
97
  "@iconify/react": "^4.1.1",
89
- "axios": "^1.6.6",
98
+ "axios": "^1.6.7",
99
+ "immer": "^10.0.3",
90
100
  "lodash": "^4.17.21",
91
101
  "nanoid": "^3.3.7",
92
102
  "react-error-boundary": "^4.0.12",
93
103
  "react-frame-component": "^5.2.6",
104
+ "react-hook-form": "^7.49.3",
94
105
  "typescript": "^5.3.3",
95
106
  "ufo": "^1.3.2",
96
107
  "zustand": "^4.5.0"
@@ -109,7 +120,7 @@
109
120
  "react-router-dom": "^6.16.0"
110
121
  },
111
122
  "devDependencies": {
112
- "@arcblock/ux": "^2.9.17",
123
+ "@arcblock/ux": "^2.9.19",
113
124
  "@emotion/cache": "^11.11.0",
114
125
  "@emotion/css": "^11.11.2",
115
126
  "@emotion/react": "^11.11.3",
@@ -120,7 +131,7 @@
120
131
  "@types/mustache": "^4.2.5",
121
132
  "@types/node-fetch": "^2.6.11",
122
133
  "@types/react": "^18.2.48",
123
- "axios": "^1.6.6",
134
+ "axios": "^1.6.7",
124
135
  "npm-run-all": "^4.1.5",
125
136
  "react": "^18.2.0",
126
137
  "react-dom": "^18.2.0",