@modern-js/runtime 1.3.5 → 1.4.2
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/CHANGELOG.md +67 -0
- package/dist/js/modern/cli/index.js +4 -4
- package/dist/js/modern/common.js +1 -0
- package/dist/js/modern/exports/model.js +2 -1
- package/dist/js/modern/exports/router.js +2 -1
- package/dist/js/modern/exports/server.js +0 -1
- package/dist/js/modern/exports/ssr.js +2 -1
- package/dist/js/modern/router/cli/index.js +106 -0
- package/dist/js/modern/router/index.js +2 -0
- package/dist/js/modern/router/runtime/DefaultNotFound.js +13 -0
- package/dist/js/modern/router/runtime/index.js +4 -0
- package/dist/js/modern/router/runtime/plugin.js +88 -0
- package/dist/js/modern/router/runtime/utils.js +108 -0
- package/dist/js/modern/ssr/cli/index.js +120 -0
- package/dist/js/modern/ssr/index.js +109 -0
- package/dist/js/modern/ssr/index.node.js +78 -0
- package/dist/js/modern/ssr/prefetch.js +58 -0
- package/dist/js/modern/ssr/react/index.js +2 -0
- package/dist/js/modern/ssr/react/nossr/index.js +13 -0
- package/dist/js/modern/ssr/react/prerender/index.js +104 -0
- package/dist/js/modern/ssr/react/prerender/type.js +0 -0
- package/dist/js/modern/ssr/react/prerender/util.js +99 -0
- package/dist/js/modern/ssr/serverRender/entry.js +179 -0
- package/dist/js/modern/ssr/serverRender/helmet.js +46 -0
- package/dist/js/modern/ssr/serverRender/index.js +29 -0
- package/dist/js/modern/ssr/serverRender/loadable.js +48 -0
- package/dist/js/modern/ssr/serverRender/measure.js +11 -0
- package/dist/js/modern/ssr/serverRender/reduce.js +7 -0
- package/dist/js/modern/ssr/serverRender/styledComponent.js +8 -0
- package/dist/js/modern/ssr/serverRender/template.js +90 -0
- package/dist/js/modern/ssr/serverRender/type.js +8 -0
- package/dist/js/modern/ssr/utils.js +51 -0
- package/dist/js/modern/state/cli/index.js +110 -0
- package/dist/js/modern/state/index.js +2 -0
- package/dist/js/modern/state/plugins.js +7 -0
- package/dist/js/modern/state/runtime/index.js +4 -0
- package/dist/js/modern/state/runtime/plugin.js +71 -0
- package/dist/js/modern/state/types.js +1 -0
- package/dist/js/node/cli/index.js +5 -5
- package/dist/js/node/common.js +10 -0
- package/dist/js/node/exports/model.js +19 -5
- package/dist/js/node/exports/router.js +19 -5
- package/dist/js/node/exports/server.js +0 -17
- package/dist/js/node/exports/ssr.js +19 -5
- package/dist/js/node/router/cli/index.js +122 -0
- package/dist/js/node/router/index.js +30 -0
- package/dist/js/node/router/runtime/DefaultNotFound.js +26 -0
- package/dist/js/node/router/runtime/index.js +39 -0
- package/dist/js/node/router/runtime/plugin.js +111 -0
- package/dist/js/node/router/runtime/utils.js +128 -0
- package/dist/js/node/ssr/cli/index.js +136 -0
- package/dist/js/node/ssr/index.js +138 -0
- package/dist/js/node/ssr/index.node.js +105 -0
- package/dist/js/node/ssr/prefetch.js +75 -0
- package/dist/js/node/ssr/react/index.js +21 -0
- package/dist/js/node/ssr/react/nossr/index.js +28 -0
- package/dist/js/node/ssr/react/prerender/index.js +121 -0
- package/dist/js/node/ssr/react/prerender/type.js +0 -0
- package/dist/js/node/ssr/react/prerender/util.js +119 -0
- package/dist/js/node/ssr/serverRender/entry.js +208 -0
- package/dist/js/node/ssr/serverRender/helmet.js +52 -0
- package/dist/js/node/ssr/serverRender/index.js +46 -0
- package/dist/js/node/ssr/serverRender/loadable.js +60 -0
- package/dist/js/node/ssr/serverRender/measure.js +20 -0
- package/dist/js/node/ssr/serverRender/reduce.js +14 -0
- package/dist/js/node/ssr/serverRender/styledComponent.js +18 -0
- package/dist/js/node/ssr/serverRender/template.js +103 -0
- package/dist/js/node/ssr/serverRender/type.js +15 -0
- package/dist/js/node/ssr/utils.js +65 -0
- package/dist/js/node/state/cli/index.js +127 -0
- package/dist/js/node/state/index.js +30 -0
- package/dist/js/node/state/plugins.js +35 -0
- package/dist/js/node/state/runtime/index.js +61 -0
- package/dist/js/node/state/runtime/plugin.js +101 -0
- package/dist/js/node/state/types.js +5 -0
- package/dist/js/treeshaking/cli/index.js +4 -4
- package/dist/js/treeshaking/common.js +3 -0
- package/dist/js/treeshaking/exports/model.js +2 -1
- package/dist/js/treeshaking/exports/router.js +2 -1
- package/dist/js/treeshaking/exports/server.js +0 -1
- package/dist/js/treeshaking/exports/ssr.js +2 -1
- package/dist/js/treeshaking/router/cli/index.js +100 -0
- package/dist/js/treeshaking/router/index.js +2 -0
- package/dist/js/treeshaking/router/runtime/DefaultNotFound.js +15 -0
- package/dist/js/treeshaking/router/runtime/index.js +4 -0
- package/dist/js/treeshaking/router/runtime/plugin.js +89 -0
- package/dist/js/treeshaking/router/runtime/utils.js +106 -0
- package/dist/js/treeshaking/ssr/cli/index.js +113 -0
- package/dist/js/treeshaking/ssr/index.js +129 -0
- package/dist/js/treeshaking/ssr/index.node.js +100 -0
- package/dist/js/treeshaking/ssr/prefetch.js +97 -0
- package/dist/js/treeshaking/ssr/react/index.js +2 -0
- package/dist/js/treeshaking/ssr/react/nossr/index.js +16 -0
- package/dist/js/treeshaking/ssr/react/prerender/index.js +119 -0
- package/dist/js/treeshaking/ssr/react/prerender/type.js +0 -0
- package/dist/js/treeshaking/ssr/react/prerender/util.js +115 -0
- package/dist/js/treeshaking/ssr/serverRender/entry.js +267 -0
- package/dist/js/treeshaking/ssr/serverRender/helmet.js +37 -0
- package/dist/js/treeshaking/ssr/serverRender/index.js +69 -0
- package/dist/js/treeshaking/ssr/serverRender/loadable.js +59 -0
- package/dist/js/treeshaking/ssr/serverRender/measure.js +17 -0
- package/dist/js/treeshaking/ssr/serverRender/reduce.js +11 -0
- package/dist/js/treeshaking/ssr/serverRender/styledComponent.js +8 -0
- package/dist/js/treeshaking/ssr/serverRender/template.js +111 -0
- package/dist/js/treeshaking/ssr/serverRender/type.js +8 -0
- package/dist/js/treeshaking/ssr/utils.js +53 -0
- package/dist/js/treeshaking/state/cli/index.js +100 -0
- package/dist/js/treeshaking/state/index.js +2 -0
- package/dist/js/treeshaking/state/plugins.js +13 -0
- package/dist/js/treeshaking/state/runtime/index.js +4 -0
- package/dist/js/treeshaking/state/runtime/plugin.js +63 -0
- package/dist/js/treeshaking/state/types.js +1 -0
- package/dist/types/common.d.ts +1 -0
- package/dist/types/exports/model.d.ts +2 -1
- package/dist/types/exports/router.d.ts +2 -1
- package/dist/types/exports/server.d.ts +0 -1
- package/dist/types/exports/ssr.d.ts +2 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/router/cli/index.d.ts +5 -0
- package/dist/types/router/index.d.ts +2 -0
- package/dist/types/router/runtime/DefaultNotFound.d.ts +2 -0
- package/dist/types/router/runtime/index.d.ts +6 -0
- package/dist/types/router/runtime/plugin.d.ts +51 -0
- package/dist/types/router/runtime/utils.d.ts +6 -0
- package/dist/types/ssr/cli/index.d.ts +5 -0
- package/dist/types/ssr/index.d.ts +18 -0
- package/dist/types/ssr/index.node.d.ts +4 -0
- package/dist/types/ssr/prefetch.d.ts +13 -0
- package/dist/types/ssr/react/index.d.ts +2 -0
- package/dist/types/ssr/react/nossr/index.d.ts +2 -0
- package/dist/types/ssr/react/prerender/index.d.ts +1 -0
- package/dist/types/ssr/react/prerender/type.d.ts +29 -0
- package/dist/types/ssr/react/prerender/util.d.ts +6 -0
- package/dist/types/ssr/serverRender/entry.d.ts +20 -0
- package/dist/types/ssr/serverRender/helmet.d.ts +2 -0
- package/dist/types/ssr/serverRender/index.d.ts +8 -0
- package/dist/types/ssr/serverRender/loadable.d.ts +2 -0
- package/dist/types/ssr/serverRender/measure.d.ts +1 -0
- package/dist/types/ssr/serverRender/reduce.d.ts +3 -0
- package/dist/types/ssr/serverRender/styledComponent.d.ts +2 -0
- package/dist/types/ssr/serverRender/template.d.ts +14 -0
- package/dist/types/ssr/serverRender/type.d.ts +32 -0
- package/dist/types/ssr/utils.d.ts +8 -0
- package/dist/types/state/cli/index.d.ts +5 -0
- package/dist/types/state/index.d.ts +2 -0
- package/dist/types/state/plugins.d.ts +4 -0
- package/dist/types/state/runtime/index.d.ts +4 -0
- package/dist/types/state/runtime/plugin.d.ts +17 -0
- package/dist/types/state/types.d.ts +17 -0
- package/package.json +36 -20
- package/types/model.d.ts +4 -1
- package/dist/js/modern/exports/request.js +0 -1
- package/dist/js/node/exports/request.js +0 -13
- package/dist/js/treeshaking/exports/request.js +0 -1
- package/dist/types/exports/request.d.ts +0 -1
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _exportNames = {};
|
|
7
|
+
exports.default = void 0;
|
|
8
|
+
|
|
9
|
+
var _react = require("react");
|
|
10
|
+
|
|
11
|
+
var _runtimeCore = require("@modern-js/runtime-core");
|
|
12
|
+
|
|
13
|
+
var _store = require("@modern-js-reduck/store");
|
|
14
|
+
|
|
15
|
+
var _react2 = require("@modern-js-reduck/react");
|
|
16
|
+
|
|
17
|
+
var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics"));
|
|
18
|
+
|
|
19
|
+
var _common = require("../../common");
|
|
20
|
+
|
|
21
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
22
|
+
|
|
23
|
+
var _plugins = require("../plugins");
|
|
24
|
+
|
|
25
|
+
Object.keys(_plugins).forEach(function (key) {
|
|
26
|
+
if (key === "default" || key === "__esModule") return;
|
|
27
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
28
|
+
if (key in exports && exports[key] === _plugins[key]) return;
|
|
29
|
+
Object.defineProperty(exports, key, {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
get: function () {
|
|
32
|
+
return _plugins[key];
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
38
|
+
|
|
39
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
40
|
+
|
|
41
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
42
|
+
|
|
43
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
44
|
+
|
|
45
|
+
const state = config => ({
|
|
46
|
+
name: '@modern-js/plugin-state',
|
|
47
|
+
setup: () => {
|
|
48
|
+
return {
|
|
49
|
+
hoc({
|
|
50
|
+
App
|
|
51
|
+
}, next) {
|
|
52
|
+
const getStateApp = props => {
|
|
53
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
54
|
+
const context = (0, _react.useContext)(_runtimeCore.RuntimeReactContext);
|
|
55
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_react2.Provider, {
|
|
56
|
+
store: context.store,
|
|
57
|
+
config: config,
|
|
58
|
+
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(App, _objectSpread({}, props))
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
return next({
|
|
63
|
+
App: (0, _hoistNonReactStatics.default)(getStateApp, App)
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
init({
|
|
68
|
+
context
|
|
69
|
+
}, next) {
|
|
70
|
+
const storeConfig = config || {};
|
|
71
|
+
|
|
72
|
+
if ((0, _common.isBrowser)()) {
|
|
73
|
+
var _window, _window$_SSR_DATA, _window$_SSR_DATA$dat;
|
|
74
|
+
|
|
75
|
+
storeConfig.initialState = storeConfig.initialState || ((_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$dat.storeState) || {};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
context.store = (0, _store.createStore)(storeConfig);
|
|
79
|
+
next({
|
|
80
|
+
context
|
|
81
|
+
});
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
pickContext({
|
|
85
|
+
context,
|
|
86
|
+
pickedContext
|
|
87
|
+
}, next) {
|
|
88
|
+
return next({
|
|
89
|
+
context,
|
|
90
|
+
pickedContext: _objectSpread(_objectSpread({}, pickedContext), {}, {
|
|
91
|
+
store: context.store
|
|
92
|
+
})
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
var _default = state;
|
|
101
|
+
exports.default = _default;
|
|
@@ -2,9 +2,9 @@ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
|
|
|
2
2
|
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import { PLUGIN_SCHEMAS, createRuntimeExportsUtils, cleanRequireCache } from '@modern-js/utils';
|
|
5
|
-
import PluginState from
|
|
6
|
-
import
|
|
7
|
-
import
|
|
5
|
+
import PluginState from "../state/cli";
|
|
6
|
+
import PluginSSR from "../ssr/cli";
|
|
7
|
+
import PluginRouter from "../router/cli";
|
|
8
8
|
export default (function () {
|
|
9
9
|
return {
|
|
10
10
|
name: '@modern-js/runtime',
|
|
@@ -48,7 +48,7 @@ export default (function () {
|
|
|
48
48
|
while (1) {
|
|
49
49
|
switch (_context.prev = _context.next) {
|
|
50
50
|
case 0:
|
|
51
|
-
cleanRequireCache([require.resolve(
|
|
51
|
+
cleanRequireCache([require.resolve("../state/cli"), require.resolve("../router/cli"), require.resolve("../ssr/cli")]);
|
|
52
52
|
|
|
53
53
|
case 1:
|
|
54
54
|
case "end":
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { default as state } from "../state";
|
|
2
|
+
export * from "../state";
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { default as router } from "../router";
|
|
2
|
+
export * from "../router";
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { default as ssr } from "../ssr";
|
|
2
|
+
export * from "../ssr";
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { getEntryOptions, createRuntimeExportsUtils, PLUGIN_SCHEMAS } from '@modern-js/utils';
|
|
4
|
+
var PLUGIN_IDENTIFIER = 'router';
|
|
5
|
+
var ROUTES_IDENTIFIER = 'routes';
|
|
6
|
+
export default (function () {
|
|
7
|
+
return {
|
|
8
|
+
name: '@modern-js/plugin-router',
|
|
9
|
+
required: ['@modern-js/runtime'],
|
|
10
|
+
setup: function setup(api) {
|
|
11
|
+
var runtimeConfigMap = new Map();
|
|
12
|
+
var pluginsExportsUtils;
|
|
13
|
+
var runtimeModulePath = path.resolve(__dirname, '../');
|
|
14
|
+
return {
|
|
15
|
+
config: function config() {
|
|
16
|
+
var appContext = api.useAppContext();
|
|
17
|
+
pluginsExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, 'plugins');
|
|
18
|
+
return {
|
|
19
|
+
source: {
|
|
20
|
+
alias: {
|
|
21
|
+
'@modern-js/runtime/plugins': pluginsExportsUtils.getPath()
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
validateSchema: function validateSchema() {
|
|
27
|
+
return PLUGIN_SCHEMAS['@modern-js/plugin-router'];
|
|
28
|
+
},
|
|
29
|
+
modifyEntryImports: function modifyEntryImports(_ref) {
|
|
30
|
+
var entrypoint = _ref.entrypoint,
|
|
31
|
+
imports = _ref.imports;
|
|
32
|
+
var entryName = entrypoint.entryName,
|
|
33
|
+
fileSystemRoutes = entrypoint.fileSystemRoutes;
|
|
34
|
+
var userConfig = api.useResolvedConfigContext();
|
|
35
|
+
|
|
36
|
+
var _api$useAppContext = api.useAppContext(),
|
|
37
|
+
packageName = _api$useAppContext.packageName;
|
|
38
|
+
|
|
39
|
+
var runtimeConfig = getEntryOptions(entryName, userConfig.runtime, userConfig.runtimeByEntries, packageName);
|
|
40
|
+
runtimeConfigMap.set(entryName, runtimeConfig);
|
|
41
|
+
|
|
42
|
+
if (runtimeConfig !== null && runtimeConfig !== void 0 && runtimeConfig.router) {
|
|
43
|
+
imports.push({
|
|
44
|
+
value: '@modern-js/runtime/plugins',
|
|
45
|
+
specifiers: [{
|
|
46
|
+
imported: PLUGIN_IDENTIFIER
|
|
47
|
+
}]
|
|
48
|
+
});
|
|
49
|
+
} else if (fileSystemRoutes) {
|
|
50
|
+
throw new Error("should enable runtime.router for entry ".concat(entryName));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
entrypoint: entrypoint,
|
|
55
|
+
imports: imports
|
|
56
|
+
};
|
|
57
|
+
},
|
|
58
|
+
modifyEntryRuntimePlugins: function modifyEntryRuntimePlugins(_ref2) {
|
|
59
|
+
var entrypoint = _ref2.entrypoint,
|
|
60
|
+
plugins = _ref2.plugins;
|
|
61
|
+
var entryName = entrypoint.entryName,
|
|
62
|
+
fileSystemRoutes = entrypoint.fileSystemRoutes;
|
|
63
|
+
|
|
64
|
+
var _api$useAppContext2 = api.useAppContext(),
|
|
65
|
+
serverRoutes = _api$useAppContext2.serverRoutes;
|
|
66
|
+
|
|
67
|
+
var runtimeConfig = runtimeConfigMap.get(entryName);
|
|
68
|
+
|
|
69
|
+
if (runtimeConfig.router) {
|
|
70
|
+
// Todo: plugin-router best to only handle manage client route.
|
|
71
|
+
// here support base server route usage, part for compatibility
|
|
72
|
+
var serverBase = serverRoutes.filter(function (route) {
|
|
73
|
+
return route.entryName === entryName;
|
|
74
|
+
}).map(function (route) {
|
|
75
|
+
return route.urlPath;
|
|
76
|
+
}).sort(function (a, b) {
|
|
77
|
+
return a.length - b.length > 0 ? -1 : 1;
|
|
78
|
+
});
|
|
79
|
+
plugins.push({
|
|
80
|
+
name: PLUGIN_IDENTIFIER,
|
|
81
|
+
options: JSON.stringify(_objectSpread(_objectSpread({
|
|
82
|
+
serverBase: serverBase
|
|
83
|
+
}, runtimeConfig.router), {}, {
|
|
84
|
+
routesConfig: fileSystemRoutes ? "{ ".concat(ROUTES_IDENTIFIER, ", globalApp: App }") : undefined
|
|
85
|
+
})).replace(/"routesConfig"\s*:\s*"((\S|\s)+)"/g, '"routesConfig": $1,')
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
entrypoint: entrypoint,
|
|
91
|
+
plugins: plugins
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
addRuntimeExports: function addRuntimeExports() {
|
|
95
|
+
pluginsExportsUtils.addExport("export { default as router } from '".concat(runtimeModulePath, "'"));
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
4
|
+
export var DefaultNotFound = function DefaultNotFound() {
|
|
5
|
+
return /*#__PURE__*/_jsx("div", {
|
|
6
|
+
style: {
|
|
7
|
+
margin: '150px auto',
|
|
8
|
+
textAlign: 'center',
|
|
9
|
+
display: 'flex',
|
|
10
|
+
alignItems: 'center',
|
|
11
|
+
justifyContent: 'center'
|
|
12
|
+
},
|
|
13
|
+
children: "404"
|
|
14
|
+
});
|
|
15
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import React, { useContext } from 'react';
|
|
3
|
+
import { createBrowserHistory, createHashHistory } from 'history';
|
|
4
|
+
import { Router, StaticRouter } from 'react-router-dom';
|
|
5
|
+
import { RuntimeReactContext } from '@modern-js/runtime-core';
|
|
6
|
+
import hoistNonReactStatics from 'hoist-non-react-statics';
|
|
7
|
+
import { isBrowser } from "../../common";
|
|
8
|
+
import { renderRoutes, getLocation, urlJoin } from "./utils";
|
|
9
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
10
|
+
export var routerPlugin = function routerPlugin(_ref) {
|
|
11
|
+
var _ref$serverBase = _ref.serverBase,
|
|
12
|
+
serverBase = _ref$serverBase === void 0 ? [] : _ref$serverBase,
|
|
13
|
+
customHistory = _ref.history,
|
|
14
|
+
_ref$supportHtml5Hist = _ref.supportHtml5History,
|
|
15
|
+
supportHtml5History = _ref$supportHtml5Hist === void 0 ? true : _ref$supportHtml5Hist,
|
|
16
|
+
routesConfig = _ref.routesConfig,
|
|
17
|
+
_ref$historyOptions = _ref.historyOptions,
|
|
18
|
+
historyOptions = _ref$historyOptions === void 0 ? {} : _ref$historyOptions;
|
|
19
|
+
var isBrow = isBrowser();
|
|
20
|
+
|
|
21
|
+
var select = function select(pathname) {
|
|
22
|
+
return serverBase.find(function (baseUrl) {
|
|
23
|
+
return pathname.search(baseUrl) === 0;
|
|
24
|
+
}) || '/';
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
name: '@modern-js/plugin-router',
|
|
29
|
+
setup: function setup() {
|
|
30
|
+
return {
|
|
31
|
+
hoc: function hoc(_ref2, next) {
|
|
32
|
+
var App = _ref2.App;
|
|
33
|
+
|
|
34
|
+
var getRouteApp = function getRouteApp() {
|
|
35
|
+
if (isBrow) {
|
|
36
|
+
var _window$_SERVER_DATA;
|
|
37
|
+
|
|
38
|
+
var baseUrl = ((_window$_SERVER_DATA = window._SERVER_DATA) === null || _window$_SERVER_DATA === void 0 ? void 0 : _window$_SERVER_DATA.router.baseUrl) || select(location.pathname);
|
|
39
|
+
historyOptions.basename = baseUrl === '/' ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
|
|
40
|
+
var history = customHistory || (supportHtml5History ? createBrowserHistory(historyOptions) : createHashHistory(historyOptions));
|
|
41
|
+
return function (props) {
|
|
42
|
+
return /*#__PURE__*/_jsx(Router, {
|
|
43
|
+
history: history,
|
|
44
|
+
children: /*#__PURE__*/_jsx(App, _objectSpread(_objectSpread({}, props), {}, {
|
|
45
|
+
children: routesConfig ? renderRoutes(routesConfig, props) : null
|
|
46
|
+
}))
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return function (props) {
|
|
52
|
+
var runtimeContext = useContext(RuntimeReactContext);
|
|
53
|
+
var ssrContext = runtimeContext.ssrContext;
|
|
54
|
+
var location = getLocation(ssrContext);
|
|
55
|
+
var routerContext = (ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.redirection) || {};
|
|
56
|
+
var request = ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.request;
|
|
57
|
+
var baseUrl = request === null || request === void 0 ? void 0 : request.baseUrl;
|
|
58
|
+
var basename = baseUrl === '/' ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
|
|
59
|
+
return /*#__PURE__*/_jsx(StaticRouter, {
|
|
60
|
+
basename: basename === '/' ? '' : basename,
|
|
61
|
+
location: location,
|
|
62
|
+
context: routerContext,
|
|
63
|
+
children: /*#__PURE__*/_jsx(App, _objectSpread(_objectSpread({}, props), {}, {
|
|
64
|
+
children: routesConfig ? renderRoutes(routesConfig, props) : null
|
|
65
|
+
}))
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
var RouteApp = getRouteApp();
|
|
71
|
+
|
|
72
|
+
if (App) {
|
|
73
|
+
RouteApp = hoistNonReactStatics(RouteApp, App);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (routesConfig !== null && routesConfig !== void 0 && routesConfig.globalApp) {
|
|
77
|
+
return next({
|
|
78
|
+
App: hoistNonReactStatics(RouteApp, routesConfig.globalApp)
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return next({
|
|
83
|
+
App: RouteApp
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
};
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
3
|
+
var _excluded = ["Component"];
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
5
|
+
import React from 'react';
|
|
6
|
+
import { Route, matchPath } from 'react-router-dom';
|
|
7
|
+
import { DefaultNotFound } from "./DefaultNotFound";
|
|
8
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
9
|
+
export function renderRoutes(routesConfig) {
|
|
10
|
+
var extraProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
11
|
+
|
|
12
|
+
var Layout = function Layout(_ref) {
|
|
13
|
+
var Component = _ref.Component,
|
|
14
|
+
props = _objectWithoutProperties(_ref, _excluded);
|
|
15
|
+
|
|
16
|
+
var GlobalLayout = routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp;
|
|
17
|
+
|
|
18
|
+
if (!GlobalLayout) {
|
|
19
|
+
return /*#__PURE__*/_jsx(Component, _objectSpread({}, props));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return /*#__PURE__*/_jsx(GlobalLayout, _objectSpread({
|
|
23
|
+
Component: Component
|
|
24
|
+
}, props));
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
var findMatchedRoute = function findMatchedRoute(pathname) {
|
|
28
|
+
var _routesConfig$routes;
|
|
29
|
+
|
|
30
|
+
return routesConfig === null || routesConfig === void 0 ? void 0 : (_routesConfig$routes = routesConfig.routes) === null || _routesConfig$routes === void 0 ? void 0 : _routesConfig$routes.find(function (route) {
|
|
31
|
+
var info = matchPath(pathname, {
|
|
32
|
+
path: route.path,
|
|
33
|
+
exact: route.exact,
|
|
34
|
+
sensitive: route.sensitive
|
|
35
|
+
});
|
|
36
|
+
return Boolean(info);
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
return /*#__PURE__*/_jsx(Route, {
|
|
41
|
+
path: "/",
|
|
42
|
+
render: function render(props) {
|
|
43
|
+
var matchedRoute = findMatchedRoute(props.location.pathname);
|
|
44
|
+
|
|
45
|
+
if (!matchedRoute) {
|
|
46
|
+
return /*#__PURE__*/_jsx(DefaultNotFound, {});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return /*#__PURE__*/_jsx(Route, {
|
|
50
|
+
path: matchedRoute.path,
|
|
51
|
+
exact: matchedRoute.exact,
|
|
52
|
+
sensitive: matchedRoute.sensitive,
|
|
53
|
+
render: function render(routeProps) {
|
|
54
|
+
return /*#__PURE__*/_jsx(Layout, _objectSpread(_objectSpread({
|
|
55
|
+
Component: matchedRoute.component
|
|
56
|
+
}, routeProps), extraProps));
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
export function getLocation(serverContext) {
|
|
63
|
+
var _ref2 = (serverContext === null || serverContext === void 0 ? void 0 : serverContext.request) || {},
|
|
64
|
+
pathname = _ref2.pathname,
|
|
65
|
+
url = _ref2.url;
|
|
66
|
+
|
|
67
|
+
var cleanUrl = url.replace('http://', '').replace('https://', '');
|
|
68
|
+
var index = (cleanUrl || '').indexOf(pathname);
|
|
69
|
+
|
|
70
|
+
if (index === -1) {
|
|
71
|
+
return pathname;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return cleanUrl.substring(index);
|
|
75
|
+
}
|
|
76
|
+
export var urlJoin = function urlJoin() {
|
|
77
|
+
var separator = '/';
|
|
78
|
+
var replace = new RegExp("".concat(separator, "{1,}"), 'g');
|
|
79
|
+
|
|
80
|
+
for (var _len = arguments.length, parts = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
81
|
+
parts[_key] = arguments[_key];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return standardSlash(parts.join(separator).replace(replace, separator));
|
|
85
|
+
};
|
|
86
|
+
export function standardSlash(str) {
|
|
87
|
+
var addr = str;
|
|
88
|
+
|
|
89
|
+
if (!addr || typeof addr !== 'string') {
|
|
90
|
+
return addr;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (addr.startsWith('.')) {
|
|
94
|
+
addr = addr.slice(1);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (!addr.startsWith('/')) {
|
|
98
|
+
addr = "/".concat(addr);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (addr.endsWith('/') && addr !== '/') {
|
|
102
|
+
addr = addr.slice(0, addr.length - 1);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return addr;
|
|
106
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { getEntryOptions, SERVER_RENDER_FUNCTION_NAME, LOADABLE_STATS_FILE, isUseSSRBundle, createRuntimeExportsUtils, isSingleEntry } from '@modern-js/utils';
|
|
3
|
+
var PLUGIN_IDENTIFIER = 'ssr';
|
|
4
|
+
export default (function () {
|
|
5
|
+
return {
|
|
6
|
+
name: '@modern-js/plugin-ssr',
|
|
7
|
+
required: ['@modern-js/runtime'],
|
|
8
|
+
setup: function setup(api) {
|
|
9
|
+
var ssrConfigMap = new Map();
|
|
10
|
+
var pluginsExportsUtils;
|
|
11
|
+
var ssrModulePath = path.resolve(__dirname, '../');
|
|
12
|
+
return {
|
|
13
|
+
config: function config() {
|
|
14
|
+
var appContext = api.useAppContext();
|
|
15
|
+
pluginsExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, 'plugins');
|
|
16
|
+
return {
|
|
17
|
+
source: {
|
|
18
|
+
alias: {
|
|
19
|
+
'@modern-js/runtime/plugins': pluginsExportsUtils.getPath()
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
tools: {
|
|
23
|
+
webpackChain: function webpackChain(chain, _ref) {
|
|
24
|
+
var name = _ref.name,
|
|
25
|
+
CHAIN_ID = _ref.CHAIN_ID;
|
|
26
|
+
var userConfig = api.useResolvedConfigContext();
|
|
27
|
+
|
|
28
|
+
if (isUseSSRBundle(userConfig) && name !== 'server') {
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
|
|
30
|
+
var LoadableWebpackPlugin = require('@modern-js/webpack/@loadable/webpack-plugin');
|
|
31
|
+
|
|
32
|
+
chain.plugin(CHAIN_ID.PLUGIN.LOADABLE).use(LoadableWebpackPlugin, [{
|
|
33
|
+
filename: LOADABLE_STATS_FILE
|
|
34
|
+
}]);
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
babel: function babel(config) {
|
|
38
|
+
var userConfig = api.useResolvedConfigContext();
|
|
39
|
+
|
|
40
|
+
if (isUseSSRBundle(userConfig)) {
|
|
41
|
+
config.plugins.push(require.resolve('@loadable/babel-plugin'));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
},
|
|
47
|
+
modifyEntryImports: function modifyEntryImports(_ref2) {
|
|
48
|
+
var entrypoint = _ref2.entrypoint,
|
|
49
|
+
imports = _ref2.imports;
|
|
50
|
+
var entryName = entrypoint.entryName;
|
|
51
|
+
var userConfig = api.useResolvedConfigContext();
|
|
52
|
+
|
|
53
|
+
var _api$useAppContext = api.useAppContext(),
|
|
54
|
+
packageName = _api$useAppContext.packageName,
|
|
55
|
+
entrypoints = _api$useAppContext.entrypoints;
|
|
56
|
+
|
|
57
|
+
pluginsExportsUtils.addExport("export { default as ssr } from '".concat(ssrModulePath, "'")); // if use ssg then set ssr config to true
|
|
58
|
+
|
|
59
|
+
var ssrConfig = getEntryOptions(entryName, userConfig.server.ssr, userConfig.server.ssrByEntries, packageName);
|
|
60
|
+
var ssgConfig = userConfig.output.ssg;
|
|
61
|
+
var useSSG = isSingleEntry(entrypoints) ? Boolean(ssgConfig) : ssgConfig === true || typeof (ssgConfig === null || ssgConfig === void 0 ? void 0 : ssgConfig[0]) === 'function' || Boolean(ssgConfig === null || ssgConfig === void 0 ? void 0 : ssgConfig[entryName]);
|
|
62
|
+
ssrConfigMap.set(entryName, ssrConfig || useSSG);
|
|
63
|
+
|
|
64
|
+
if (ssrConfig || useSSG) {
|
|
65
|
+
imports.push({
|
|
66
|
+
value: '@modern-js/runtime/plugins',
|
|
67
|
+
specifiers: [{
|
|
68
|
+
imported: PLUGIN_IDENTIFIER
|
|
69
|
+
}]
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
entrypoint: entrypoint,
|
|
75
|
+
imports: imports
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
modifyEntryRuntimePlugins: function modifyEntryRuntimePlugins(_ref3) {
|
|
79
|
+
var entrypoint = _ref3.entrypoint,
|
|
80
|
+
plugins = _ref3.plugins;
|
|
81
|
+
|
|
82
|
+
if (ssrConfigMap.get(entrypoint.entryName)) {
|
|
83
|
+
plugins.push({
|
|
84
|
+
name: PLUGIN_IDENTIFIER,
|
|
85
|
+
options: ssrConfigMap.get(entrypoint.entryName)
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
entrypoint: entrypoint,
|
|
91
|
+
plugins: plugins
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
modifyEntryExport: function modifyEntryExport(_ref4) {
|
|
95
|
+
var entrypoint = _ref4.entrypoint,
|
|
96
|
+
exportStatement = _ref4.exportStatement;
|
|
97
|
+
|
|
98
|
+
if (ssrConfigMap.get(entrypoint.entryName)) {
|
|
99
|
+
return {
|
|
100
|
+
entrypoint: entrypoint,
|
|
101
|
+
exportStatement: ["export function ".concat(SERVER_RENDER_FUNCTION_NAME, "(context) {\n return bootstrap(AppWrapper, context)\n }"), exportStatement].join('\n')
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
entrypoint: entrypoint,
|
|
107
|
+
exportStatement: exportStatement
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
});
|