@shuvi/platform-shared 1.0.0-rc.1 → 1.0.0-rc.12
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/esm/shared/application.d.ts +13 -66
- package/esm/shared/application.js +41 -12
- package/esm/shared/applicationTypes.d.ts +15 -18
- package/esm/shared/helper/getAppData.d.ts +1 -4
- package/esm/shared/helper/getAppData.js +0 -1
- package/esm/shared/helper/getPublicPath.js +1 -2
- package/esm/shared/index.d.ts +3 -1
- package/esm/shared/index.js +2 -3
- package/esm/shared/loader/index.d.ts +0 -1
- package/esm/shared/loader/index.js +0 -1
- package/esm/shared/loader/loader.d.ts +1 -1
- package/esm/shared/loader/loader.js +2 -2
- package/esm/shared/loader/types.d.ts +4 -8
- package/esm/shared/models/error.d.ts +4 -4
- package/esm/shared/models/error.js +2 -2
- package/esm/shared/models/loader.d.ts +13 -0
- package/esm/shared/models/loader.js +24 -0
- package/esm/shared/routerTypes.d.ts +28 -15
- package/esm/shared/{lifecycle.d.ts → runtimPlugin.d.ts} +2 -4
- package/esm/shared/{lifecycle.js → runtimPlugin.js} +2 -1
- package/esm/shared/runtimeConfigTypes.d.ts +1 -0
- package/esm/shared/runtimeConfigTypes.js +1 -0
- package/esm/shared/shuvi-singleton-runtimeConfig.d.ts +1 -1
- package/esm/shuvi-app/shuvi-runtime-app.d.ts +1 -1
- package/esm/shuvi-app/shuvi-runtime-index.d.ts +1 -1
- package/esm/shuvi-app/shuvi-runtime-index.js +1 -1
- package/lib/node/platform/index.d.ts +1 -13
- package/lib/node/platform/plugins/main/hooks.d.ts +1 -1
- package/lib/node/platform/plugins/main/index.d.ts +1 -13
- package/lib/node/platform/plugins/main/index.js +1 -2
- package/lib/node/platform/runtimeFiles.d.ts +1 -1
- package/lib/node/platform/runtimeFiles.js +2 -1
- package/lib/node/project/file-presets/files/app/core/entry.js.d.ts +1 -1
- package/lib/node/project/file-presets/files/app/core/error.js.d.ts +1 -1
- package/lib/node/project/file-presets/files/app/core/platform.js.d.ts +1 -1
- package/lib/node/project/file-presets/files/app/core/polyfill.js.d.ts +1 -1
- package/lib/node/project/file-presets/files/app/core/runtimeConfig.js.d.ts +1 -1
- package/lib/node/project/file-presets/files/app/core/setPublicRuntimeConfig.js.d.ts +1 -1
- package/lib/node/project/file-presets/files/app/core/setRuntimeConfig.js.d.ts +1 -1
- package/lib/node/project/file-presets/files/app/user/app.js.d.ts +1 -1
- package/lib/node/project/file-presets/files/app/user/error.js.d.ts +1 -1
- package/lib/node/project/file-presets/files/app/user/server.js.d.ts +3 -0
- package/lib/node/project/file-presets/files/app/user/server.js.js +15 -0
- package/lib/node/project/file-presets/index.d.ts +3 -3
- package/lib/node/project/projectContext.d.ts +2 -1
- package/lib/node/project/projectContext.js +2 -1
- package/lib/node/route/helpers.d.ts +5 -6
- package/lib/node/route/helpers.js +32 -47
- package/lib/node/route/index.d.ts +1 -1
- package/lib/node/route/index.js +2 -1
- package/lib/node/route/matchSpec.d.ts +8 -0
- package/lib/node/route/matchSpec.js +138 -0
- package/lib/node/route/route.d.ts +9 -12
- package/lib/node/route/route.js +88 -78
- package/lib/shared/application.d.ts +13 -66
- package/lib/shared/application.js +43 -14
- package/lib/shared/applicationTypes.d.ts +15 -18
- package/lib/shared/helper/getAppData.d.ts +1 -4
- package/lib/shared/helper/getAppData.js +0 -1
- package/lib/shared/helper/getPublicPath.js +1 -2
- package/lib/shared/index.d.ts +3 -1
- package/lib/shared/index.js +7 -4
- package/lib/shared/loader/index.d.ts +0 -1
- package/lib/shared/loader/index.js +0 -1
- package/lib/shared/loader/loader.d.ts +1 -1
- package/lib/shared/loader/loader.js +2 -2
- package/lib/shared/loader/types.d.ts +4 -8
- package/lib/shared/models/error.d.ts +4 -4
- package/lib/shared/models/error.js +2 -2
- package/lib/shared/models/loader.d.ts +13 -0
- package/lib/shared/models/loader.js +27 -0
- package/lib/shared/routerTypes.d.ts +28 -15
- package/lib/shared/{lifecycle.d.ts → runtimPlugin.d.ts} +2 -4
- package/lib/shared/{lifecycle.js → runtimPlugin.js} +4 -2
- package/lib/shared/runtimeConfigTypes.d.ts +1 -0
- package/lib/{node/platform/plugins/main/shuvi-app.js → shared/runtimeConfigTypes.js} +0 -0
- package/lib/shared/shuvi-singleton-runtimeConfig.d.ts +1 -1
- package/package.json +31 -20
- package/shuvi-type-extensions-node.js +1 -0
- package/{shuvi-app-extensions.d.ts → shuvi-type-extensions-runtime.d.ts} +2 -6
- package/esm/shared/loader/loaderManager.d.ts +0 -16
- package/esm/shared/loader/loaderManager.js +0 -49
- package/lib/node/platform/plugins/main/shuvi-app.d.ts +0 -8
- package/lib/shared/loader/loaderManager.d.ts +0 -16
- package/lib/shared/loader/loaderManager.js +0 -53
package/lib/node/route/route.js
CHANGED
|
@@ -13,8 +13,10 @@ exports.getMiddlewareRoutes = exports.getApiRoutes = exports.getPageRoutes = exp
|
|
|
13
13
|
const path_1 = require("path");
|
|
14
14
|
const file_1 = require("@shuvi/utils/lib/file");
|
|
15
15
|
const helpers_1 = require("./helpers");
|
|
16
|
-
const
|
|
17
|
-
|
|
16
|
+
const matchSpec_1 = require("./matchSpec");
|
|
17
|
+
const getRawRoutesFromDir = (dirname, excludes) => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
|
+
const rootDirname = dirname;
|
|
19
|
+
if (!(yield (0, file_1.isDirectory)(rootDirname))) {
|
|
18
20
|
return {
|
|
19
21
|
routes: [],
|
|
20
22
|
warnings: [],
|
|
@@ -23,7 +25,9 @@ const getRawRoutesFromDir = (dirname) => __awaiter(void 0, void 0, void 0, funct
|
|
|
23
25
|
}
|
|
24
26
|
const warnings = [];
|
|
25
27
|
const errors = [];
|
|
26
|
-
const
|
|
28
|
+
const routes = [];
|
|
29
|
+
const patterns = (0, matchSpec_1.getFileMatcherPatterns)(rootDirname, excludes);
|
|
30
|
+
const visitDirectory = (dirname, routes, parentDir) => __awaiter(void 0, void 0, void 0, function* () {
|
|
27
31
|
const files = yield (0, helpers_1.getAllowFilesAndDirs)(dirname);
|
|
28
32
|
const onlyHasDir = !(0, helpers_1.hasAllowFiles)(files);
|
|
29
33
|
if (!files.length) {
|
|
@@ -33,38 +37,46 @@ const getRawRoutesFromDir = (dirname) => __awaiter(void 0, void 0, void 0, funct
|
|
|
33
37
|
});
|
|
34
38
|
}
|
|
35
39
|
for (const file of files) {
|
|
36
|
-
|
|
40
|
+
let filepath = (0, path_1.join)(dirname, file);
|
|
37
41
|
const isDir = yield (0, file_1.isDirectory)(filepath);
|
|
42
|
+
if (isDir) {
|
|
43
|
+
// transform '/foo' to '/foo/' if `foo` is a dir,
|
|
44
|
+
// this is required for matchFile to wrok properly
|
|
45
|
+
filepath = (0, path_1.join)(filepath, path_1.sep);
|
|
46
|
+
}
|
|
47
|
+
if (!(0, matchSpec_1.matchFile)(filepath, patterns, false)) {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const segment = parentDir === '' ? '/' : (0, helpers_1.normalizeRoutePath)(parentDir);
|
|
38
51
|
if (isDir) {
|
|
39
52
|
if (onlyHasDir) {
|
|
40
53
|
// only indent segment,routes was in same level.
|
|
41
|
-
yield
|
|
42
|
-
continue;
|
|
54
|
+
yield visitDirectory(filepath, routes, `${parentDir}/${file}`);
|
|
43
55
|
}
|
|
44
|
-
|
|
45
|
-
|
|
56
|
+
else {
|
|
57
|
+
const route = {
|
|
58
|
+
kind: 'dir',
|
|
59
|
+
filepath,
|
|
60
|
+
segment: segment,
|
|
61
|
+
children: []
|
|
62
|
+
};
|
|
63
|
+
routes.push(route);
|
|
64
|
+
yield visitDirectory(filepath, route.children, file);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
const ext = (0, path_1.extname)(file);
|
|
69
|
+
const type = (0, path_1.basename)(file, ext);
|
|
70
|
+
routes.push({
|
|
71
|
+
kind: 'file',
|
|
72
|
+
segment: segment,
|
|
46
73
|
filepath,
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
children: []
|
|
50
|
-
};
|
|
51
|
-
yield _getRawRoutesFromDir(filepath, rawRoute.children, file);
|
|
52
|
-
rawRoutes.push(rawRoute);
|
|
53
|
-
continue;
|
|
74
|
+
type
|
|
75
|
+
});
|
|
54
76
|
}
|
|
55
|
-
const ext = (0, path_1.extname)(file);
|
|
56
|
-
const type = (0, path_1.basename)(file, ext);
|
|
57
|
-
rawRoutes.push({
|
|
58
|
-
kind: 'file',
|
|
59
|
-
name: file,
|
|
60
|
-
segment: (0, helpers_1.normalize)(segment),
|
|
61
|
-
filepath,
|
|
62
|
-
type
|
|
63
|
-
});
|
|
64
77
|
}
|
|
65
|
-
return rawRoutes;
|
|
66
78
|
});
|
|
67
|
-
|
|
79
|
+
yield visitDirectory(rootDirname, routes, '');
|
|
68
80
|
return {
|
|
69
81
|
routes,
|
|
70
82
|
warnings,
|
|
@@ -72,9 +84,16 @@ const getRawRoutesFromDir = (dirname) => __awaiter(void 0, void 0, void 0, funct
|
|
|
72
84
|
};
|
|
73
85
|
});
|
|
74
86
|
exports.getRawRoutesFromDir = getRawRoutesFromDir;
|
|
75
|
-
const getPageRoutes = (
|
|
76
|
-
|
|
77
|
-
|
|
87
|
+
const getPageRoutes = (dir, excludes) => __awaiter(void 0, void 0, void 0, function* () {
|
|
88
|
+
let raw;
|
|
89
|
+
if (typeof dir === 'string') {
|
|
90
|
+
raw = yield (0, exports.getRawRoutesFromDir)(dir, excludes);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
raw = dir;
|
|
94
|
+
}
|
|
95
|
+
const { routes: rawRoutes, warnings, errors } = raw;
|
|
96
|
+
const _getPageRoutes = (rawRoutes, routes, parentSegment = '') => {
|
|
78
97
|
const layoutRoute = rawRoutes.some(route => route.kind === 'file' && route.type === 'layout');
|
|
79
98
|
const route = {};
|
|
80
99
|
if (layoutRoute) {
|
|
@@ -83,10 +102,10 @@ const getPageRoutes = (dirname) => __awaiter(void 0, void 0, void 0, function* (
|
|
|
83
102
|
for (const rawRoute of rawRoutes) {
|
|
84
103
|
if (rawRoute.kind === 'dir') {
|
|
85
104
|
const workRoutes = layoutRoute ? route.children : routes;
|
|
86
|
-
const
|
|
105
|
+
const nextSegment = layoutRoute
|
|
87
106
|
? ''
|
|
88
|
-
:
|
|
89
|
-
_getPageRoutes(rawRoute.children, workRoutes,
|
|
107
|
+
: (0, helpers_1.combineComponents)(parentSegment, rawRoute.segment);
|
|
108
|
+
_getPageRoutes(rawRoute.children, workRoutes, nextSegment);
|
|
90
109
|
}
|
|
91
110
|
else if (rawRoute.type === 'page' || rawRoute.type === 'layout') {
|
|
92
111
|
if (rawRoute.type === 'page' && layoutRoute) {
|
|
@@ -96,10 +115,8 @@ const getPageRoutes = (dirname) => __awaiter(void 0, void 0, void 0, function* (
|
|
|
96
115
|
});
|
|
97
116
|
}
|
|
98
117
|
else {
|
|
99
|
-
let prefix = segment;
|
|
100
|
-
let suffix = rawRoute.segment;
|
|
101
118
|
route.component = rawRoute.filepath;
|
|
102
|
-
route.path =
|
|
119
|
+
route.path = (0, helpers_1.combineComponents)(parentSegment, rawRoute.segment);
|
|
103
120
|
routes.push(Object.assign({}, route));
|
|
104
121
|
}
|
|
105
122
|
}
|
|
@@ -107,11 +124,6 @@ const getPageRoutes = (dirname) => __awaiter(void 0, void 0, void 0, function* (
|
|
|
107
124
|
return routes;
|
|
108
125
|
};
|
|
109
126
|
const routes = (0, helpers_1.sortRoutes)(_getPageRoutes(rawRoutes, []));
|
|
110
|
-
routes.forEach(route => {
|
|
111
|
-
if (!route.path.startsWith('/')) {
|
|
112
|
-
route.path = `/${route.path}`;
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
127
|
return {
|
|
116
128
|
routes,
|
|
117
129
|
warnings,
|
|
@@ -119,44 +131,46 @@ const getPageRoutes = (dirname) => __awaiter(void 0, void 0, void 0, function* (
|
|
|
119
131
|
};
|
|
120
132
|
});
|
|
121
133
|
exports.getPageRoutes = getPageRoutes;
|
|
122
|
-
const getApiRoutes = (dir) => __awaiter(void 0, void 0, void 0, function* () {
|
|
134
|
+
const getApiRoutes = (dir, excludes) => __awaiter(void 0, void 0, void 0, function* () {
|
|
135
|
+
let raw;
|
|
136
|
+
if (typeof dir === 'string') {
|
|
137
|
+
raw = yield (0, exports.getRawRoutesFromDir)(dir, excludes);
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
raw = dir;
|
|
141
|
+
}
|
|
142
|
+
const { routes: rawRoutes, warnings, errors } = raw;
|
|
123
143
|
const getConflictWaring = (rawRoute, conflictRawRoute) => {
|
|
124
144
|
return `Find both ${(0, path_1.basename)(conflictRawRoute.filepath)} and ${(0, path_1.basename)(rawRoute.filepath)} in "${(0, path_1.dirname)(rawRoute.filepath)}"!, only "${(0, path_1.basename)(conflictRawRoute.filepath)}" is used.`;
|
|
125
145
|
};
|
|
126
|
-
const
|
|
127
|
-
const _getApiRoutes = (rawRoutes, routes, prefix = '') => {
|
|
146
|
+
const _getApiRoutes = (rawRoutes, routes, parentSegment = '') => {
|
|
128
147
|
const page = rawRoutes.find(route => route.kind === 'file' && route.type === 'page');
|
|
129
148
|
const layout = rawRoutes.find(route => route.kind === 'file' && route.type === 'layout');
|
|
130
149
|
const allowedRoutes = rawRoutes.filter(route => route.kind === 'dir' || route.type === 'api');
|
|
131
150
|
for (let rawRoute of allowedRoutes) {
|
|
132
|
-
|
|
151
|
+
const currentPath = (0, helpers_1.combineComponents)(parentSegment, rawRoute.segment);
|
|
133
152
|
if (rawRoute.kind === 'dir') {
|
|
134
|
-
_getApiRoutes(rawRoute.children, routes,
|
|
135
|
-
continue;
|
|
153
|
+
_getApiRoutes(rawRoute.children, routes, currentPath);
|
|
136
154
|
}
|
|
137
|
-
if (rawRoute.type === 'api') {
|
|
155
|
+
else if (rawRoute.type === 'api') {
|
|
138
156
|
if (layout) {
|
|
139
157
|
warnings.push({
|
|
140
158
|
type: 'api',
|
|
141
159
|
msg: getConflictWaring(rawRoute, layout)
|
|
142
160
|
});
|
|
143
|
-
continue;
|
|
144
161
|
}
|
|
145
|
-
if (page) {
|
|
162
|
+
else if (page) {
|
|
146
163
|
warnings.push({
|
|
147
164
|
type: 'api',
|
|
148
165
|
msg: getConflictWaring(rawRoute, page)
|
|
149
166
|
});
|
|
150
|
-
continue;
|
|
151
167
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
168
|
+
else {
|
|
169
|
+
routes.push({
|
|
170
|
+
path: currentPath,
|
|
171
|
+
api: rawRoute.filepath
|
|
172
|
+
});
|
|
155
173
|
}
|
|
156
|
-
routes.push({
|
|
157
|
-
path,
|
|
158
|
-
api: rawRoute.filepath
|
|
159
|
-
});
|
|
160
174
|
}
|
|
161
175
|
}
|
|
162
176
|
return routes;
|
|
@@ -170,26 +184,27 @@ const getApiRoutes = (dir) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
170
184
|
};
|
|
171
185
|
});
|
|
172
186
|
exports.getApiRoutes = getApiRoutes;
|
|
173
|
-
const getMiddlewareRoutes = (
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
187
|
+
const getMiddlewareRoutes = (dir, excludes) => __awaiter(void 0, void 0, void 0, function* () {
|
|
188
|
+
let raw;
|
|
189
|
+
if (typeof dir === 'string') {
|
|
190
|
+
raw = yield (0, exports.getRawRoutesFromDir)(dir, excludes);
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
raw = dir;
|
|
194
|
+
}
|
|
195
|
+
const { routes: rawRoutes, warnings, errors } = raw;
|
|
196
|
+
const _getMiddlewareRoutes = (rawRoutes, routes, parentSegment) => {
|
|
177
197
|
for (const rawRoute of rawRoutes) {
|
|
198
|
+
const currentPath = (0, helpers_1.combineComponents)(parentSegment, rawRoute.segment);
|
|
178
199
|
if (rawRoute.kind === 'dir') {
|
|
179
|
-
_getMiddlewareRoutes(rawRoute.children, routes,
|
|
180
|
-
continue;
|
|
200
|
+
_getMiddlewareRoutes(rawRoute.children, routes, currentPath);
|
|
181
201
|
}
|
|
182
|
-
if (rawRoute.type === 'middleware') {
|
|
183
|
-
let path = segment + '/' + rawRoute.segment;
|
|
184
|
-
let catchAllOp = '/*';
|
|
185
|
-
if (path === '//') {
|
|
186
|
-
path = '/';
|
|
187
|
-
}
|
|
188
|
-
if (path === '/') {
|
|
189
|
-
catchAllOp = catchAllOp.slice(1);
|
|
190
|
-
}
|
|
202
|
+
else if (rawRoute.type === 'middleware') {
|
|
191
203
|
routes.push({
|
|
192
|
-
|
|
204
|
+
// catch all
|
|
205
|
+
path: currentPath.endsWith('/')
|
|
206
|
+
? `${currentPath}*`
|
|
207
|
+
: `${currentPath}/*`,
|
|
193
208
|
middleware: rawRoute.filepath
|
|
194
209
|
});
|
|
195
210
|
}
|
|
@@ -197,11 +212,6 @@ const getMiddlewareRoutes = (dirname) => __awaiter(void 0, void 0, void 0, funct
|
|
|
197
212
|
return routes;
|
|
198
213
|
};
|
|
199
214
|
const routes = (0, helpers_1.sortRoutes)(_getMiddlewareRoutes(rawRoutes, [], ''));
|
|
200
|
-
routes.forEach(route => {
|
|
201
|
-
if (!route.path.startsWith('/')) {
|
|
202
|
-
route.path = `/${route.path}`;
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
215
|
const exceptionFilter = (e) => e.type === 'middleware';
|
|
206
216
|
return {
|
|
207
217
|
routes,
|
|
@@ -1,78 +1,25 @@
|
|
|
1
|
-
import { IRouter } from './routerTypes';
|
|
2
|
-
import {
|
|
1
|
+
import { IRouter, IPageRouteRecord } from './routerTypes';
|
|
2
|
+
import { RedoxStore, IApplication, IAppContext, IApplicationOptions, IRerenderConfig, IError } from './applicationTypes';
|
|
3
3
|
export declare class Application {
|
|
4
|
-
private _error;
|
|
5
4
|
private _router;
|
|
6
5
|
private _appComponent;
|
|
7
6
|
private _pluginManager;
|
|
8
7
|
private _context;
|
|
9
|
-
private
|
|
8
|
+
private _store;
|
|
9
|
+
private _error;
|
|
10
|
+
private _loader;
|
|
10
11
|
constructor(options: IApplicationOptions);
|
|
11
|
-
get router(): IRouter<
|
|
12
|
+
get router(): IRouter<IPageRouteRecord>;
|
|
12
13
|
get context(): IAppContext;
|
|
13
|
-
get pluginManager(): import("@shuvi/hook").HookManager<import("./
|
|
14
|
+
get pluginManager(): import("@shuvi/hook").HookManager<import("./runtimPlugin").RuntimePluginHooks, void>;
|
|
14
15
|
get appComponent(): any;
|
|
15
|
-
get error():
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
$actions: {
|
|
21
|
-
setError: (payload?: import("./applicationTypes").IError | undefined) => import("@shuvi/redox").Action<import("./applicationTypes").IError | undefined>;
|
|
22
|
-
} & {
|
|
23
|
-
error: (payload?: import("./applicationTypes").IError | undefined) => void;
|
|
24
|
-
clear: () => void;
|
|
25
|
-
} & {
|
|
26
|
-
[X: string]: never;
|
|
27
|
-
[X: number]: never;
|
|
28
|
-
[X: symbol]: never;
|
|
29
|
-
};
|
|
30
|
-
$views: import("@shuvi/redox").RedoxViews<({
|
|
31
|
-
getError(): import("./applicationTypes").IError | undefined;
|
|
32
|
-
hasError(): boolean;
|
|
33
|
-
} & ThisType<import("./applicationTypes").IErrorState & {
|
|
34
|
-
$state: import("./applicationTypes").IErrorState;
|
|
35
|
-
} & import("@shuvi/redox").RedoxViews<{
|
|
36
|
-
getError(): import("./applicationTypes").IError | undefined;
|
|
37
|
-
hasError(): boolean;
|
|
38
|
-
}> & {
|
|
39
|
-
$dep: {} & {} & {};
|
|
40
|
-
}>) | undefined>;
|
|
41
|
-
$createSelector: <TReturn>(selector: import("@shuvi/redox").ISelector<import("@shuvi/redox").Model<"error", import("./applicationTypes").IErrorState, {}, {
|
|
42
|
-
setError(_state: import("./applicationTypes").IErrorState, error?: import("./applicationTypes").IError | undefined): {
|
|
43
|
-
error: import("./applicationTypes").IError | undefined;
|
|
44
|
-
};
|
|
45
|
-
}, {
|
|
46
|
-
error(payload?: import("./applicationTypes").IError | undefined): void;
|
|
47
|
-
clear(): void;
|
|
48
|
-
}, {
|
|
49
|
-
getError(): import("./applicationTypes").IError | undefined;
|
|
50
|
-
hasError(): boolean;
|
|
51
|
-
}>, TReturn>) => (() => TReturn) & {
|
|
52
|
-
clearCache: () => void;
|
|
53
|
-
};
|
|
54
|
-
} & import("@shuvi/redox").RedoxViews<{
|
|
55
|
-
getError(): import("./applicationTypes").IError | undefined;
|
|
56
|
-
hasError(): boolean;
|
|
57
|
-
} & ThisType<import("./applicationTypes").IErrorState & {
|
|
58
|
-
$state: import("./applicationTypes").IErrorState;
|
|
59
|
-
} & import("@shuvi/redox").RedoxViews<{
|
|
60
|
-
getError(): import("./applicationTypes").IError | undefined;
|
|
61
|
-
hasError(): boolean;
|
|
62
|
-
}> & {
|
|
63
|
-
$dep: {} & {} & {};
|
|
64
|
-
}>> & {
|
|
65
|
-
setError: (payload?: import("./applicationTypes").IError | undefined) => import("@shuvi/redox").Action<import("./applicationTypes").IError | undefined>;
|
|
66
|
-
} & {
|
|
67
|
-
error: (payload?: import("./applicationTypes").IError | undefined) => void;
|
|
68
|
-
clear: () => void;
|
|
69
|
-
} & {
|
|
70
|
-
[X: string]: never;
|
|
71
|
-
[X: number]: never;
|
|
72
|
-
[X: symbol]: never;
|
|
73
|
-
};
|
|
16
|
+
get error(): IError | undefined;
|
|
17
|
+
setError(err: IError): void;
|
|
18
|
+
clearError(): void;
|
|
19
|
+
getLoadersData(): Record<string, any>;
|
|
20
|
+
setLoadersData(datas: Record<string, any>): void;
|
|
74
21
|
init(): Promise<void>;
|
|
75
|
-
get
|
|
22
|
+
get store(): RedoxStore;
|
|
76
23
|
updateComponents({ AppComponent }?: IRerenderConfig): Promise<void>;
|
|
77
24
|
dispose(): Promise<void>;
|
|
78
25
|
private _initPlugin;
|
|
@@ -10,19 +10,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.Application = void 0;
|
|
13
|
-
const
|
|
14
|
-
const
|
|
13
|
+
const runtimPlugin_1 = require("./runtimPlugin");
|
|
14
|
+
const runtimPlugin_2 = require("./runtimPlugin");
|
|
15
15
|
const redox_1 = require("@shuvi/redox");
|
|
16
16
|
const error_1 = require("./models/error");
|
|
17
|
+
const loader_1 = require("./models/loader");
|
|
17
18
|
class Application {
|
|
18
19
|
constructor(options) {
|
|
19
20
|
this._router = options.router;
|
|
20
21
|
this._context = {};
|
|
21
|
-
this.
|
|
22
|
-
this._error = this.
|
|
22
|
+
this._store = (0, redox_1.redox)({ initialState: options.initialState });
|
|
23
|
+
this._error = this._store.getModel(error_1.errorModel);
|
|
24
|
+
this._loader = this._store.getModel(loader_1.loaderModel);
|
|
23
25
|
this._appComponent = options.AppComponent;
|
|
24
|
-
this._pluginManager = (0,
|
|
25
|
-
(0,
|
|
26
|
+
this._pluginManager = (0, runtimPlugin_1.getManager)();
|
|
27
|
+
(0, runtimPlugin_2.initPlugins)(this._pluginManager, options.plugins || []);
|
|
26
28
|
}
|
|
27
29
|
get router() {
|
|
28
30
|
return this._router;
|
|
@@ -37,7 +39,19 @@ class Application {
|
|
|
37
39
|
return this._appComponent;
|
|
38
40
|
}
|
|
39
41
|
get error() {
|
|
40
|
-
return this._error;
|
|
42
|
+
return this._error.errorObject;
|
|
43
|
+
}
|
|
44
|
+
setError(err) {
|
|
45
|
+
this._error.set(err);
|
|
46
|
+
}
|
|
47
|
+
clearError() {
|
|
48
|
+
this._error.clear();
|
|
49
|
+
}
|
|
50
|
+
getLoadersData() {
|
|
51
|
+
return this._loader.getAllData;
|
|
52
|
+
}
|
|
53
|
+
setLoadersData(datas) {
|
|
54
|
+
this._loader.setDatas(datas);
|
|
41
55
|
}
|
|
42
56
|
init() {
|
|
43
57
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -47,8 +61,8 @@ class Application {
|
|
|
47
61
|
this._router.init();
|
|
48
62
|
});
|
|
49
63
|
}
|
|
50
|
-
get
|
|
51
|
-
return this.
|
|
64
|
+
get store() {
|
|
65
|
+
return this._store;
|
|
52
66
|
}
|
|
53
67
|
updateComponents({ AppComponent } = {}) {
|
|
54
68
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -79,12 +93,27 @@ class Application {
|
|
|
79
93
|
});
|
|
80
94
|
}
|
|
81
95
|
getPublicAPI() {
|
|
96
|
+
const self = this;
|
|
82
97
|
return {
|
|
83
|
-
context:
|
|
84
|
-
router:
|
|
85
|
-
appComponent:
|
|
86
|
-
|
|
87
|
-
error
|
|
98
|
+
context: self._context,
|
|
99
|
+
router: self._router,
|
|
100
|
+
appComponent: self._appComponent,
|
|
101
|
+
store: self._store,
|
|
102
|
+
get error() {
|
|
103
|
+
return self.error;
|
|
104
|
+
},
|
|
105
|
+
setError(err) {
|
|
106
|
+
self.setError(err);
|
|
107
|
+
},
|
|
108
|
+
clearError() {
|
|
109
|
+
self.clearError();
|
|
110
|
+
},
|
|
111
|
+
getLoadersData() {
|
|
112
|
+
return self.getLoadersData();
|
|
113
|
+
},
|
|
114
|
+
setLoadersData(datas) {
|
|
115
|
+
self.setLoadersData(datas);
|
|
116
|
+
}
|
|
88
117
|
};
|
|
89
118
|
}
|
|
90
119
|
}
|
|
@@ -1,22 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
import type { IncomingMessage } from 'http';
|
|
3
|
-
import { IStoreManager } from '@shuvi/redox';
|
|
1
|
+
import { RedoxStore } from '@shuvi/redox';
|
|
4
2
|
import { CustomAppContext } from '@shuvi/runtime';
|
|
5
|
-
import { IRouter } from './routerTypes';
|
|
6
|
-
import { IPluginList } from './
|
|
7
|
-
export declare type IRequest = IncomingMessage & {
|
|
8
|
-
[x: string]: any;
|
|
9
|
-
};
|
|
3
|
+
import { IRouter, IPageRouteRecord } from './routerTypes';
|
|
4
|
+
import { IPluginList } from './runtimPlugin';
|
|
10
5
|
export interface IAppContext extends CustomAppContext {
|
|
11
6
|
[x: string]: unknown;
|
|
12
7
|
}
|
|
13
8
|
export declare type IRerenderConfig = {
|
|
14
9
|
AppComponent?: any;
|
|
15
10
|
};
|
|
16
|
-
export type {
|
|
11
|
+
export type { RedoxStore };
|
|
12
|
+
export declare type ErrorSource = 'server';
|
|
17
13
|
export interface IError {
|
|
18
14
|
code?: number;
|
|
19
15
|
message?: string;
|
|
16
|
+
stack?: string;
|
|
17
|
+
name?: string;
|
|
18
|
+
source?: ErrorSource;
|
|
20
19
|
}
|
|
21
20
|
export interface IErrorState {
|
|
22
21
|
error?: IError;
|
|
@@ -24,18 +23,16 @@ export interface IErrorState {
|
|
|
24
23
|
export declare type IAppState = {
|
|
25
24
|
error?: IErrorState;
|
|
26
25
|
};
|
|
27
|
-
export interface IErrorManager {
|
|
28
|
-
getError: IError | undefined;
|
|
29
|
-
error: (err: IError) => void;
|
|
30
|
-
clear: () => void;
|
|
31
|
-
hasError: boolean;
|
|
32
|
-
}
|
|
33
26
|
export interface IApplication {
|
|
34
27
|
readonly context: IAppContext;
|
|
35
|
-
readonly router: IRouter
|
|
28
|
+
readonly router: IRouter<IPageRouteRecord>;
|
|
36
29
|
readonly appComponent: any;
|
|
37
|
-
readonly
|
|
38
|
-
readonly
|
|
30
|
+
readonly store: RedoxStore;
|
|
31
|
+
readonly error: IError | undefined;
|
|
32
|
+
setError(err: IError): void;
|
|
33
|
+
clearError(): void;
|
|
34
|
+
getLoadersData(): Record<string, any>;
|
|
35
|
+
setLoadersData(datas: Record<string, any>): void;
|
|
39
36
|
}
|
|
40
37
|
export interface IApplicationOptions {
|
|
41
38
|
router: IRouter;
|
|
@@ -5,13 +5,10 @@ export declare type IData = {
|
|
|
5
5
|
export declare type IAppData<Data = {}, appState = any> = {
|
|
6
6
|
ssr: boolean;
|
|
7
7
|
runtimeConfig?: Record<string, string>;
|
|
8
|
+
appState?: appState;
|
|
8
9
|
pageData?: {
|
|
9
10
|
[key: string]: any;
|
|
10
11
|
};
|
|
11
|
-
loadersData?: {
|
|
12
|
-
[x: string]: any;
|
|
13
|
-
};
|
|
14
|
-
appState?: appState;
|
|
15
12
|
filesByRoutId: Record<string, string[]>;
|
|
16
13
|
publicPath: string;
|
|
17
14
|
} & {
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getPublicPath = void 0;
|
|
4
|
-
const constants_1 = require("@shuvi/shared/lib/constants");
|
|
5
4
|
const getAppData_1 = require("./getAppData");
|
|
6
5
|
function getPublicPath() {
|
|
7
6
|
const { publicPath } = (0, getAppData_1.getAppData)();
|
|
8
|
-
return
|
|
7
|
+
return publicPath;
|
|
9
8
|
}
|
|
10
9
|
exports.getPublicPath = getPublicPath;
|
package/lib/shared/index.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ export * from './router';
|
|
|
4
4
|
export * from './response';
|
|
5
5
|
export * from './loader';
|
|
6
6
|
export { errorModel } from './models/error';
|
|
7
|
+
export { loaderModel } from './models/loader';
|
|
7
8
|
export * from './applicationTypes';
|
|
8
9
|
export type { Application } from './application';
|
|
9
|
-
export {
|
|
10
|
+
export type { IRuntimeConfig } from './runtimeConfigTypes';
|
|
11
|
+
export { IAppModule, IPluginInstance, BuiltInRuntimePluginHooks, CustomRuntimePluginHooks, RuntimePluginHooks, createRuntimePlugin, createRuntimePluginBefore, createRuntimePluginAfter, RuntimePluginInstance } from './runtimPlugin';
|
package/lib/shared/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.createRuntimePlugin = exports.errorModel = void 0;
|
|
17
|
+
exports.createRuntimePluginAfter = exports.createRuntimePluginBefore = exports.createRuntimePlugin = exports.loaderModel = exports.errorModel = void 0;
|
|
18
18
|
__exportStar(require("./helper"), exports);
|
|
19
19
|
__exportStar(require("./routerTypes"), exports);
|
|
20
20
|
__exportStar(require("./router"), exports);
|
|
@@ -22,7 +22,10 @@ __exportStar(require("./response"), exports);
|
|
|
22
22
|
__exportStar(require("./loader"), exports);
|
|
23
23
|
var error_1 = require("./models/error");
|
|
24
24
|
Object.defineProperty(exports, "errorModel", { enumerable: true, get: function () { return error_1.errorModel; } });
|
|
25
|
+
var loader_1 = require("./models/loader");
|
|
26
|
+
Object.defineProperty(exports, "loaderModel", { enumerable: true, get: function () { return loader_1.loaderModel; } });
|
|
25
27
|
__exportStar(require("./applicationTypes"), exports);
|
|
26
|
-
var
|
|
27
|
-
|
|
28
|
-
Object.defineProperty(exports, "
|
|
28
|
+
var runtimPlugin_1 = require("./runtimPlugin");
|
|
29
|
+
Object.defineProperty(exports, "createRuntimePlugin", { enumerable: true, get: function () { return runtimPlugin_1.createRuntimePlugin; } });
|
|
30
|
+
Object.defineProperty(exports, "createRuntimePluginBefore", { enumerable: true, get: function () { return runtimPlugin_1.createRuntimePluginBefore; } });
|
|
31
|
+
Object.defineProperty(exports, "createRuntimePluginAfter", { enumerable: true, get: function () { return runtimPlugin_1.createRuntimePluginAfter; } });
|
|
@@ -15,5 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./loader"), exports);
|
|
18
|
-
__exportStar(require("./loaderManager"), exports);
|
|
19
18
|
__exportStar(require("./types"), exports);
|
|
@@ -5,5 +5,5 @@ interface Result<T> {
|
|
|
5
5
|
result?: T;
|
|
6
6
|
}
|
|
7
7
|
export declare function runInParallerAndBail<T>(fns: Array<() => Promise<T> | T>): Promise<Result<T>[]>;
|
|
8
|
-
export declare function runLoaders(matches: IRouteMatch<IPageRouteRecord>[], loadersByRouteId: Record<string, Loader>, {
|
|
8
|
+
export declare function runLoaders(matches: IRouteMatch<IPageRouteRecord>[], loadersByRouteId: Record<string, Loader>, { query, req, getAppContext }: LoaderContextOptions): Promise<LoaderDataRecord>;
|
|
9
9
|
export {};
|
|
@@ -75,7 +75,7 @@ function errorHelper(msg, statusCode = 500) {
|
|
|
75
75
|
(0, invariant_1.default)(statusCode >= 400 && statusCode < 600, 'status code should be 4xx and 5xx');
|
|
76
76
|
throw (0, response_1.response)(msg, { status: statusCode });
|
|
77
77
|
}
|
|
78
|
-
function runLoaders(matches, loadersByRouteId, {
|
|
78
|
+
function runLoaders(matches, loadersByRouteId, { query, req, getAppContext }) {
|
|
79
79
|
return __awaiter(this, void 0, void 0, function* () {
|
|
80
80
|
if (!matches.length) {
|
|
81
81
|
return [];
|
|
@@ -85,7 +85,7 @@ function runLoaders(matches, loadersByRouteId, { isServer, query, req, getAppCon
|
|
|
85
85
|
const loaderFn = loadersByRouteId[match.route.id];
|
|
86
86
|
let res;
|
|
87
87
|
try {
|
|
88
|
-
const value = yield loaderFn(Object.assign({
|
|
88
|
+
const value = yield loaderFn(Object.assign({ pathname: match.pathname, params: match.params, query: query, redirect: redirectHelper, error: errorHelper, appContext }, (req ? { req } : {})));
|
|
89
89
|
if (value) {
|
|
90
90
|
res = (0, response_1.json)(value);
|
|
91
91
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
import type { ShuviRequest } from '@shuvi/service';
|
|
1
2
|
import { IURLQuery, IURLParams } from '../routerTypes';
|
|
2
|
-
import { IAppContext
|
|
3
|
+
import { IAppContext } from '../applicationTypes';
|
|
3
4
|
import { Response } from '../response';
|
|
4
5
|
export interface LoaderContextOptions {
|
|
5
|
-
|
|
6
|
-
req?: IRequest;
|
|
6
|
+
req?: ShuviRequest;
|
|
7
7
|
query: IURLQuery;
|
|
8
8
|
getAppContext: () => IAppContext;
|
|
9
9
|
}
|
|
@@ -17,10 +17,6 @@ export interface ErrorFunction {
|
|
|
17
17
|
* route component getInitialProps params `context`
|
|
18
18
|
*/
|
|
19
19
|
export interface IRouteLoaderContext {
|
|
20
|
-
/**
|
|
21
|
-
* is running on server, if server is true, client will be false
|
|
22
|
-
*/
|
|
23
|
-
isServer: boolean;
|
|
24
20
|
/**
|
|
25
21
|
* current url path
|
|
26
22
|
*/
|
|
@@ -62,7 +58,7 @@ export interface IRouteLoaderContext {
|
|
|
62
58
|
/**
|
|
63
59
|
* server only
|
|
64
60
|
*/
|
|
65
|
-
req?:
|
|
61
|
+
req?: ShuviRequest;
|
|
66
62
|
/**
|
|
67
63
|
* Application context object, which is accessiable during the entire lifecycle of application
|
|
68
64
|
*/
|