@modern-js/runtime 1.4.2 → 1.5.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.
Files changed (83) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/dist/js/modern/cli/index.js +1 -3
  3. package/dist/js/modern/core/app-config.js +10 -0
  4. package/dist/js/modern/core/compatible.js +218 -0
  5. package/dist/js/modern/core/index.js +7 -0
  6. package/dist/js/modern/core/loader/index.js +1 -0
  7. package/dist/js/modern/core/loader/loaderManager.js +189 -0
  8. package/dist/js/modern/core/loader/useLoader.js +105 -0
  9. package/dist/js/modern/core/plugin.js +63 -0
  10. package/dist/js/modern/core/runtime-context.js +2 -0
  11. package/dist/js/modern/exports/styled.js +2 -2
  12. package/dist/js/modern/index.js +1 -1
  13. package/dist/js/modern/router/cli/index.js +1 -3
  14. package/dist/js/modern/router/runtime/plugin.js +1 -1
  15. package/dist/js/modern/router/runtime/utils.js +3 -1
  16. package/dist/js/modern/ssr/cli/index.js +1 -3
  17. package/dist/js/modern/ssr/index.node.js +1 -1
  18. package/dist/js/modern/ssr/prefetch.js +2 -2
  19. package/dist/js/modern/ssr/serverRender/index.js +1 -1
  20. package/dist/js/modern/ssr/serverRender/styledComponent.js +1 -1
  21. package/dist/js/modern/state/cli/index.js +1 -3
  22. package/dist/js/modern/state/runtime/plugin.js +1 -1
  23. package/dist/js/node/cli/index.js +1 -3
  24. package/dist/js/node/core/app-config.js +22 -0
  25. package/dist/js/node/core/compatible.js +247 -0
  26. package/dist/js/node/core/index.js +111 -0
  27. package/dist/js/node/core/loader/index.js +15 -0
  28. package/dist/js/node/core/loader/loaderManager.js +201 -0
  29. package/dist/js/node/core/loader/useLoader.js +118 -0
  30. package/dist/js/node/core/plugin.js +79 -0
  31. package/dist/js/node/core/runtime-context.js +11 -0
  32. package/dist/js/node/exports/styled.js +5 -5
  33. package/dist/js/node/index.js +10 -10
  34. package/dist/js/node/router/cli/index.js +1 -8
  35. package/dist/js/node/router/runtime/plugin.js +3 -3
  36. package/dist/js/node/router/runtime/utils.js +3 -1
  37. package/dist/js/node/ssr/cli/index.js +1 -8
  38. package/dist/js/node/ssr/index.node.js +2 -2
  39. package/dist/js/node/ssr/serverRender/index.js +2 -2
  40. package/dist/js/node/ssr/serverRender/styledComponent.js +2 -2
  41. package/dist/js/node/state/cli/index.js +1 -8
  42. package/dist/js/node/state/runtime/plugin.js +3 -3
  43. package/dist/js/treeshaking/cli/index.js +1 -3
  44. package/dist/js/treeshaking/core/app-config.js +13 -0
  45. package/dist/js/treeshaking/core/compatible.js +255 -0
  46. package/dist/js/treeshaking/core/index.js +7 -0
  47. package/dist/js/treeshaking/core/loader/index.js +1 -0
  48. package/dist/js/treeshaking/core/loader/loaderManager.js +267 -0
  49. package/dist/js/treeshaking/core/loader/useLoader.js +107 -0
  50. package/dist/js/treeshaking/core/plugin.js +105 -0
  51. package/dist/js/treeshaking/core/runtime-context.js +2 -0
  52. package/dist/js/treeshaking/exports/styled.js +2 -2
  53. package/dist/js/treeshaking/index.js +1 -1
  54. package/dist/js/treeshaking/router/cli/index.js +1 -3
  55. package/dist/js/treeshaking/router/runtime/plugin.js +1 -1
  56. package/dist/js/treeshaking/router/runtime/utils.js +3 -1
  57. package/dist/js/treeshaking/ssr/cli/index.js +1 -3
  58. package/dist/js/treeshaking/ssr/index.node.js +1 -1
  59. package/dist/js/treeshaking/ssr/prefetch.js +2 -2
  60. package/dist/js/treeshaking/ssr/serverRender/index.js +1 -1
  61. package/dist/js/treeshaking/ssr/serverRender/styledComponent.js +1 -1
  62. package/dist/js/treeshaking/state/cli/index.js +1 -3
  63. package/dist/js/treeshaking/state/runtime/plugin.js +1 -1
  64. package/dist/types/core/app-config.d.ts +6 -0
  65. package/dist/types/core/compatible.d.ts +13 -0
  66. package/dist/types/core/index.d.ts +9 -0
  67. package/dist/types/core/loader/index.d.ts +2 -0
  68. package/dist/types/core/loader/loaderManager.d.ts +57 -0
  69. package/dist/types/core/loader/useLoader.d.ts +54 -0
  70. package/dist/types/core/plugin.d.ts +208 -0
  71. package/dist/types/core/runtime-context.d.ts +13 -0
  72. package/dist/types/exports/styled.d.ts +2 -2
  73. package/dist/types/index.d.ts +4 -9
  74. package/dist/types/router/runtime/plugin.d.ts +1 -1
  75. package/dist/types/ssr/index.d.ts +2 -2
  76. package/dist/types/ssr/index.node.d.ts +1 -1
  77. package/dist/types/ssr/prefetch.d.ts +2 -2
  78. package/dist/types/ssr/serverRender/entry.d.ts +1 -1
  79. package/dist/types/ssr/serverRender/index.d.ts +1 -1
  80. package/dist/types/ssr/serverRender/type.d.ts +1 -1
  81. package/dist/types/state/runtime/plugin.d.ts +2 -2
  82. package/lib/types.d.ts +10 -0
  83. package/package.json +59 -25
@@ -0,0 +1,107 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
3
+ import { useContext, useRef, useMemo, useState, useCallback, useEffect } from 'react';
4
+ import invariant from 'invariant';
5
+ import { RuntimeReactContext } from "../runtime-context";
6
+ import { LoaderStatus } from "./loaderManager";
7
+
8
+ var useLoader = function useLoader(loaderFn) {
9
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
10
+ params: undefined
11
+ };
12
+ var context = useContext(RuntimeReactContext);
13
+ var isSSRRender = Boolean(context.ssr);
14
+ var loaderManager = context.loaderManager;
15
+ var loaderRef = useRef();
16
+ var unlistenLoaderChangeRef = useRef(null); // SSR render should ignore `_cache` prop
17
+
18
+ if (isSSRRender && Object.prototype.hasOwnProperty.call(options, '_cache')) {
19
+ delete options._cache;
20
+ }
21
+
22
+ var load = useCallback(function (params) {
23
+ var _unlistenLoaderChange, _window, _window$_SSR_DATA, _window$_SSR_DATA$dat, _window$_SSR_DATA$dat2, _loaderRef$current2;
24
+
25
+ if (typeof params === 'undefined') {
26
+ var _loaderRef$current;
27
+
28
+ return (_loaderRef$current = loaderRef.current) === null || _loaderRef$current === void 0 ? void 0 : _loaderRef$current.load();
29
+ }
30
+
31
+ var id = loaderManager.add(function () {
32
+ try {
33
+ var _res = loaderFn(context, params);
34
+
35
+ if (_res instanceof Promise) {
36
+ return _res;
37
+ }
38
+
39
+ return Promise.resolve(_res);
40
+ } catch (e) {
41
+ return Promise.reject(e);
42
+ }
43
+ }, _objectSpread(_objectSpread({}, options), {}, {
44
+ params: params
45
+ }));
46
+ loaderRef.current = loaderManager.get(id); // unsubscribe old loader onChange event
47
+
48
+ (_unlistenLoaderChange = unlistenLoaderChangeRef.current) === null || _unlistenLoaderChange === void 0 ? void 0 : _unlistenLoaderChange.call(unlistenLoaderChangeRef);
49
+
50
+ if (isSSRRender) {
51
+ return undefined;
52
+ } // skip this loader, then try to unlisten loader change
53
+
54
+
55
+ if (options.skip) {
56
+ return undefined;
57
+ } // do not load data again in CSR hydrate stage if SSR data exists
58
+
59
+
60
+ if (context._hydration && ((_window = window) === null || _window === void 0 ? void 0 : (_window$_SSR_DATA = _window._SSR_DATA) === null || _window$_SSR_DATA === void 0 ? void 0 : (_window$_SSR_DATA$dat = _window$_SSR_DATA.data) === null || _window$_SSR_DATA$dat === void 0 ? void 0 : (_window$_SSR_DATA$dat2 = _window$_SSR_DATA$dat.loadersData[id]) === null || _window$_SSR_DATA$dat2 === void 0 ? void 0 : _window$_SSR_DATA$dat2.error) === null) {
61
+ return undefined;
62
+ }
63
+
64
+ var res = loaderRef.current.load();
65
+ unlistenLoaderChangeRef.current = (_loaderRef$current2 = loaderRef.current) === null || _loaderRef$current2 === void 0 ? void 0 : _loaderRef$current2.onChange(function (_status, _result) {
66
+ setResult(_result);
67
+
68
+ if (_status === LoaderStatus.fulfilled) {
69
+ var _options$onSuccess;
70
+
71
+ options === null || options === void 0 ? void 0 : (_options$onSuccess = options.onSuccess) === null || _options$onSuccess === void 0 ? void 0 : _options$onSuccess.call(options, _result.data);
72
+ }
73
+
74
+ if (_status === LoaderStatus.rejected) {
75
+ var _options$onError;
76
+
77
+ options === null || options === void 0 ? void 0 : (_options$onError = options.onError) === null || _options$onError === void 0 ? void 0 : _options$onError.call(options, _result.error);
78
+ }
79
+ });
80
+ return res;
81
+ }, [options.skip]);
82
+ useEffect(function () {
83
+ return function () {
84
+ var _unlistenLoaderChange2;
85
+
86
+ (_unlistenLoaderChange2 = unlistenLoaderChangeRef.current) === null || _unlistenLoaderChange2 === void 0 ? void 0 : _unlistenLoaderChange2.call(unlistenLoaderChangeRef);
87
+ };
88
+ }, []);
89
+ useMemo(function () {
90
+ var _options$params;
91
+
92
+ var p = (_options$params = options.params) !== null && _options$params !== void 0 ? _options$params : loaderFn.id;
93
+ invariant(typeof p !== 'undefined' && p !== null, 'Params is required in useLoader');
94
+ load(p);
95
+ }, [options.params]);
96
+
97
+ var _useState = useState(loaderRef.current.result),
98
+ _useState2 = _slicedToArray(_useState, 2),
99
+ result = _useState2[0],
100
+ setResult = _useState2[1];
101
+
102
+ return _objectSpread(_objectSpread({}, result), {}, {
103
+ reload: load
104
+ });
105
+ };
106
+
107
+ export default useLoader;
@@ -0,0 +1,105 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
3
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
4
+ import { createManager, createPipeline, createAsyncPipeline } from '@modern-js/plugin';
5
+ import { createLoaderManager } from "./loader/loaderManager";
6
+ var hoc = createPipeline(); // eslint-disable-next-line @typescript-eslint/no-empty-interface
7
+
8
+ var provide = createPipeline();
9
+ var client = createAsyncPipeline();
10
+ var server = createAsyncPipeline();
11
+ var init = createAsyncPipeline();
12
+ var pickContext = createPipeline();
13
+ var runtimeHooks = {
14
+ hoc: hoc,
15
+ provide: provide,
16
+ client: client,
17
+ server: server,
18
+ init: init,
19
+ pickContext: pickContext
20
+ };
21
+ /** All hooks of runtime plugin. */
22
+
23
+ export var createRuntime = function createRuntime() {
24
+ return createManager(runtimeHooks);
25
+ };
26
+ /**
27
+ * register init hook. It would be revoked both ssr and csr.
28
+ */
29
+
30
+ var registerInit = function registerInit(App, _init) {
31
+ var originalInit = App.init;
32
+
33
+ App.init = /*#__PURE__*/function () {
34
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(context) {
35
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
36
+ while (1) {
37
+ switch (_context.prev = _context.next) {
38
+ case 0:
39
+ if (!context.loaderManager) {
40
+ context.loaderManager = createLoaderManager({});
41
+ }
42
+
43
+ _context.next = 3;
44
+ return Promise.all([originalInit === null || originalInit === void 0 ? void 0 : originalInit(context), _init === null || _init === void 0 ? void 0 : _init(context)]);
45
+
46
+ case 3:
47
+ case "end":
48
+ return _context.stop();
49
+ }
50
+ }
51
+ }, _callee);
52
+ }));
53
+
54
+ return function (_x) {
55
+ return _ref.apply(this, arguments);
56
+ };
57
+ }();
58
+ };
59
+ /**
60
+ * register prefetch hook. It would be revoked both ssr and csr.
61
+ * But if ssr success, It wont exec in csr again.
62
+ * If ssr prefetch failed, It will fallback to exec in csr.
63
+ */
64
+
65
+
66
+ var registerPrefetch = function registerPrefetch(App, prefetch) {
67
+ var originalPrefetch = App.prefetch;
68
+
69
+ App.prefetch = /*#__PURE__*/function () {
70
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(context) {
71
+ var originResult, result;
72
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
73
+ while (1) {
74
+ switch (_context2.prev = _context2.next) {
75
+ case 0:
76
+ _context2.next = 2;
77
+ return originalPrefetch === null || originalPrefetch === void 0 ? void 0 : originalPrefetch(context);
78
+
79
+ case 2:
80
+ originResult = _context2.sent;
81
+ _context2.next = 5;
82
+ return prefetch === null || prefetch === void 0 ? void 0 : prefetch(context);
83
+
84
+ case 5:
85
+ result = _context2.sent;
86
+ return _context2.abrupt("return", _objectSpread(_objectSpread({}, originResult), result));
87
+
88
+ case 7:
89
+ case "end":
90
+ return _context2.stop();
91
+ }
92
+ }
93
+ }, _callee2);
94
+ }));
95
+
96
+ return function (_x2) {
97
+ return _ref2.apply(this, arguments);
98
+ };
99
+ }();
100
+ };
101
+
102
+ export var runtime = createRuntime();
103
+ var createPlugin = runtime.createPlugin;
104
+ export { createPlugin };
105
+ export { registerInit, registerPrefetch };
@@ -0,0 +1,2 @@
1
+ import { createContext } from 'react';
2
+ export var RuntimeReactContext = /*#__PURE__*/createContext({});
@@ -1,3 +1,3 @@
1
- import styled from '@modern-js/runtime-core/styled';
1
+ import styled from 'styled-components';
2
2
  export default styled;
3
- export * from '@modern-js/runtime-core/styled';
3
+ export * from 'styled-components';
@@ -1 +1 @@
1
- export { createApp, createPlugin, useLoader, bootstrap, RuntimeReactContext, registerPrefetch, defineConfig, registerInit, useRuntimeContext } from '@modern-js/runtime-core';
1
+ export { createApp, createPlugin, useLoader, bootstrap, RuntimeReactContext, registerPrefetch, defineConfig, registerInit, useRuntimeContext } from "./core";
@@ -1,5 +1,4 @@
1
1
  import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
- import path from 'path';
3
2
  import { getEntryOptions, createRuntimeExportsUtils, PLUGIN_SCHEMAS } from '@modern-js/utils';
4
3
  var PLUGIN_IDENTIFIER = 'router';
5
4
  var ROUTES_IDENTIFIER = 'routes';
@@ -10,7 +9,6 @@ export default (function () {
10
9
  setup: function setup(api) {
11
10
  var runtimeConfigMap = new Map();
12
11
  var pluginsExportsUtils;
13
- var runtimeModulePath = path.resolve(__dirname, '../');
14
12
  return {
15
13
  config: function config() {
16
14
  var appContext = api.useAppContext();
@@ -92,7 +90,7 @@ export default (function () {
92
90
  };
93
91
  },
94
92
  addRuntimeExports: function addRuntimeExports() {
95
- pluginsExportsUtils.addExport("export { default as router } from '".concat(runtimeModulePath, "'"));
93
+ pluginsExportsUtils.addExport("export { default as router } from '@modern-js/runtime/runtime-router'");
96
94
  }
97
95
  };
98
96
  }
@@ -2,8 +2,8 @@ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
2
  import React, { useContext } from 'react';
3
3
  import { createBrowserHistory, createHashHistory } from 'history';
4
4
  import { Router, StaticRouter } from 'react-router-dom';
5
- import { RuntimeReactContext } from '@modern-js/runtime-core';
6
5
  import hoistNonReactStatics from 'hoist-non-react-statics';
6
+ import { RuntimeReactContext } from "../../core";
7
7
  import { isBrowser } from "../../common";
8
8
  import { renderRoutes, getLocation, urlJoin } from "./utils";
9
9
  import { jsx as _jsx } from "react/jsx-runtime";
@@ -60,11 +60,13 @@ export function renderRoutes(routesConfig) {
60
60
  });
61
61
  }
62
62
  export function getLocation(serverContext) {
63
+ var _url$replace;
64
+
63
65
  var _ref2 = (serverContext === null || serverContext === void 0 ? void 0 : serverContext.request) || {},
64
66
  pathname = _ref2.pathname,
65
67
  url = _ref2.url;
66
68
 
67
- var cleanUrl = url.replace('http://', '').replace('https://', '');
69
+ var cleanUrl = url === null || url === void 0 ? void 0 : (_url$replace = url.replace('http://', '')) === null || _url$replace === void 0 ? void 0 : _url$replace.replace('https://', '');
68
70
  var index = (cleanUrl || '').indexOf(pathname);
69
71
 
70
72
  if (index === -1) {
@@ -1,4 +1,3 @@
1
- import path from 'path';
2
1
  import { getEntryOptions, SERVER_RENDER_FUNCTION_NAME, LOADABLE_STATS_FILE, isUseSSRBundle, createRuntimeExportsUtils, isSingleEntry } from '@modern-js/utils';
3
2
  var PLUGIN_IDENTIFIER = 'ssr';
4
3
  export default (function () {
@@ -8,7 +7,6 @@ export default (function () {
8
7
  setup: function setup(api) {
9
8
  var ssrConfigMap = new Map();
10
9
  var pluginsExportsUtils;
11
- var ssrModulePath = path.resolve(__dirname, '../');
12
10
  return {
13
11
  config: function config() {
14
12
  var appContext = api.useAppContext();
@@ -54,7 +52,7 @@ export default (function () {
54
52
  packageName = _api$useAppContext.packageName,
55
53
  entrypoints = _api$useAppContext.entrypoints;
56
54
 
57
- pluginsExportsUtils.addExport("export { default as ssr } from '".concat(ssrModulePath, "'")); // if use ssg then set ssr config to true
55
+ pluginsExportsUtils.addExport("export { default as ssr } from '@modern-js/runtime/runtime-ssr'"); // if use ssg then set ssr config to true
58
56
 
59
57
  var ssrConfig = getEntryOptions(entryName, userConfig.server.ssr, userConfig.server.ssrByEntries, packageName);
60
58
  var ssgConfig = userConfig.output.ssg;
@@ -6,7 +6,7 @@ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
6
6
 
7
7
  /* eslint-disable @typescript-eslint/no-require-imports */
8
8
  import path from 'path';
9
- import { registerPrefetch } from '@modern-js/runtime-core';
9
+ import { registerPrefetch } from "../core";
10
10
  import { isBrowser } from "../common";
11
11
  import prefetch from "./prefetch";
12
12
  import { formatServer } from "./utils";
@@ -5,10 +5,10 @@ import fs from 'fs';
5
5
  import { renderToStaticMarkup } from 'react-dom/server';
6
6
  import { run } from '@modern-js/utils/ssr';
7
7
  import { LOADABLE_STATS_FILE } from '@modern-js/utils/constants';
8
- import { ChunkExtractor } from '@loadable/server'; // todo: SSRContext
9
-
8
+ import { ChunkExtractor } from '@loadable/server';
10
9
  import { jsx as _jsx } from "react/jsx-runtime";
11
10
 
11
+ // todo: SSRContext
12
12
  var prefetch = /*#__PURE__*/function () {
13
13
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(App, context) {
14
14
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
@@ -1,7 +1,7 @@
1
1
  import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
2
  import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
- import { RuntimeContext } from '@modern-js/runtime-core';
4
3
  import { run } from '@modern-js/utils/ssr';
4
+ import { RuntimeContext } from "../../core";
5
5
  import { PreRender } from "../react/prerender";
6
6
  import SSREntry from "./entry";
7
7
  import { time } from "./measure";
@@ -1,4 +1,4 @@
1
- import { ServerStyleSheet } from '@modern-js/runtime-core/styled';
1
+ import { ServerStyleSheet } from 'styled-components';
2
2
  export var toHtml = function toHtml(jsx, renderer, next) {
3
3
  var sheet = new ServerStyleSheet();
4
4
  var html = next(sheet.collectStyles(jsx));
@@ -1,4 +1,3 @@
1
- import path from 'path';
2
1
  import { getEntryOptions, createRuntimeExportsUtils, PLUGIN_SCHEMAS } from '@modern-js/utils';
3
2
  import "../types";
4
3
  var PLUGIN_IDENTIFIER = 'state';
@@ -9,7 +8,6 @@ export default (function () {
9
8
  setup: function setup(api) {
10
9
  var stateConfigMap = new Map();
11
10
  var pluginsExportsUtils;
12
- var stateModulePath = path.resolve(__dirname, '../');
13
11
  return {
14
12
  config: function config() {
15
13
  var appContext = api.useAppContext();
@@ -92,7 +90,7 @@ export default (function () {
92
90
  return PLUGIN_SCHEMAS['@modern-js/plugin-state'];
93
91
  },
94
92
  addRuntimeExports: function addRuntimeExports() {
95
- pluginsExportsUtils.addExport("export { default as state } from '".concat(stateModulePath, "'"));
93
+ pluginsExportsUtils.addExport("export { default as state } from '@modern-js/runtime/runtime-state'");
96
94
  }
97
95
  };
98
96
  }
@@ -1,9 +1,9 @@
1
1
  import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
2
  import { useContext } from 'react';
3
- import { RuntimeReactContext } from '@modern-js/runtime-core';
4
3
  import { createStore } from '@modern-js-reduck/store';
5
4
  import { Provider } from '@modern-js-reduck/react';
6
5
  import hoistNonReactStatics from 'hoist-non-react-statics';
6
+ import { RuntimeReactContext } from "../../core";
7
7
  import { isBrowser } from "../../common";
8
8
  import { jsx as _jsx } from "react/jsx-runtime";
9
9
 
@@ -0,0 +1,6 @@
1
+ import type React from 'react';
2
+ export interface AppConfig {
3
+ [key: string]: any;
4
+ }
5
+ export declare const getConfig: (Component: React.ComponentType<any>) => AppConfig | undefined;
6
+ export declare const defineConfig: (Component: React.ComponentType<any>, config: AppConfig) => React.ComponentType<any>;
@@ -0,0 +1,13 @@
1
+ import React from 'react';
2
+ import { Plugin } from './plugin';
3
+ import { TRuntimeContext } from './runtime-context';
4
+ export declare type CreateAppOptions = {
5
+ plugins: Plugin[];
6
+ };
7
+ export declare const createApp: ({
8
+ plugins
9
+ }: CreateAppOptions) => (App?: React.ComponentType<any>) => React.ComponentType<any>;
10
+ declare type BootStrap<T = unknown> = (App: React.ComponentType, id?: string | Record<string, any> | HTMLElement) => Promise<T>;
11
+ export declare const bootstrap: BootStrap;
12
+ export declare const useRuntimeContext: () => TRuntimeContext;
13
+ export {};
@@ -0,0 +1,9 @@
1
+ export { createPlugin, createRuntime, runtime, registerInit, registerPrefetch } from './plugin';
2
+ export type { Plugin } from './plugin';
3
+ export { defineConfig, getConfig } from './app-config';
4
+ export type { AppConfig } from './app-config';
5
+ export * from './compatible';
6
+ export type { TRuntimeContext, RuntimeContext } from './runtime-context';
7
+ export { RuntimeReactContext } from './runtime-context';
8
+ export * from './loader';
9
+ export * from '@modern-js/plugin';
@@ -0,0 +1,2 @@
1
+ export { default as useLoader } from './useLoader';
2
+ export type { SSRData, SSRContainer } from './useLoader';
@@ -0,0 +1,57 @@
1
+ import { LoaderOptions } from './useLoader';
2
+ export declare enum LoaderStatus {
3
+ idle = 0,
4
+ loading = 1,
5
+ fulfilled = 2,
6
+ rejected = 3,
7
+ }
8
+ export declare type LoaderResult = {
9
+ loading: boolean;
10
+ reloading: boolean;
11
+ data: any;
12
+ error: any;
13
+ _error?: any;
14
+ };
15
+ declare const createLoader: (id: string, initialData: Partial<LoaderResult> | undefined, loaderFn: () => Promise<any>, skip?: boolean) => {
16
+ readonly result: {
17
+ loading: boolean;
18
+ reloading: boolean;
19
+ data: any;
20
+ error: any;
21
+ _error: any;
22
+ };
23
+ readonly promise: Promise<any> | null;
24
+ onChange: (handler: (status: LoaderStatus, result: LoaderResult) => void) => () => void;
25
+ load: () => Promise<any>;
26
+ };
27
+ declare type ManagerOption = {
28
+ /**
29
+ * whether current manage only exec static loader
30
+ */
31
+ skipStatic?: boolean;
32
+ skipNonStatic?: boolean;
33
+ };
34
+ /**
35
+ * Create loaders manager. It's returned instance will add to context
36
+ * @param initialDataMap used to initialing loader data
37
+ */
38
+
39
+ export declare const createLoaderManager: (initialDataMap: Record<string, LoaderResult>, managerOptions?: ManagerOption) => {
40
+ hasPendingLoaders: () => boolean;
41
+ awaitPendingLoaders: () => Promise<Record<string, LoaderResult>>;
42
+ add: (loaderFn: () => Promise<any>, loaderOptions: LoaderOptions) => any;
43
+ get: (id: string) => {
44
+ readonly result: {
45
+ loading: boolean;
46
+ reloading: boolean;
47
+ data: any;
48
+ error: any;
49
+ _error: any;
50
+ };
51
+ readonly promise: Promise<any> | null;
52
+ onChange: (handler: (status: LoaderStatus, result: LoaderResult) => void) => () => void;
53
+ load: () => Promise<any>;
54
+ } | undefined;
55
+ };
56
+ export declare type Loader = ReturnType<typeof createLoader>;
57
+ export {};
@@ -0,0 +1,54 @@
1
+ import { LoaderResult } from './loaderManager';
2
+ export interface SSRData {
3
+ loadersData: Record<string, LoaderResult | undefined>;
4
+ initialData?: Record<string, unknown>;
5
+ }
6
+ export interface SSRContainer {
7
+ data?: SSRData;
8
+ }
9
+ declare global {
10
+ interface Window {
11
+ _SSR_DATA?: SSRContainer;
12
+ }
13
+ }
14
+ export interface LoaderOptions<Params = any, TData = any, TError extends Error = any> {
15
+ /**
16
+ * Revoke when loader excuted successfully.
17
+ */
18
+ onSuccess?: (data: TData) => void;
19
+ /**
20
+ * Revoke when loader ended with error
21
+ */
22
+
23
+ onError?: (error: TError) => void;
24
+ /**
25
+ * initialData to display once loader is ready.
26
+ */
27
+
28
+ initialData?: TData;
29
+ /**
30
+ * whether skip loader
31
+ * if true, the loader will not exec.
32
+ */
33
+
34
+ skip?: boolean;
35
+ /**
36
+ * User params, it will pass to loader's second parameter.
37
+ */
38
+
39
+ params?: Params;
40
+ /**
41
+ * whether loader can exec on build phase.
42
+ */
43
+
44
+ static?: boolean;
45
+ }
46
+ declare type LoaderFn<P = any, T = any> = (context: any, params: P) => Promise<T>;
47
+ declare const useLoader: <TData = any, Params = any, E = any>(loaderFn: LoaderFn<Params, TData>, options?: LoaderOptions<Params, any, any>) => {
48
+ reload: (params?: Params | undefined) => Promise<any> | undefined;
49
+ loading: boolean;
50
+ reloading: boolean;
51
+ data: TData;
52
+ error: E;
53
+ };
54
+ export default useLoader;