@shuvi/platform-shared 1.0.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/shared/application.d.ts +82 -0
- package/esm/shared/application.js +87 -0
- package/esm/shared/applicationTypes.d.ts +45 -0
- package/esm/shared/applicationTypes.js +1 -0
- package/esm/shared/helper/getAppData.d.ts +20 -0
- package/esm/shared/helper/getAppData.js +26 -0
- package/esm/shared/helper/getFilesOfRoute.d.ts +10 -0
- package/esm/shared/helper/getFilesOfRoute.js +30 -0
- package/esm/shared/helper/getPageData.d.ts +1 -0
- package/esm/shared/helper/getPageData.js +13 -0
- package/esm/shared/helper/getPublicPath.d.ts +1 -0
- package/esm/shared/helper/getPublicPath.js +6 -0
- package/esm/shared/helper/index.d.ts +5 -0
- package/esm/shared/helper/index.js +5 -0
- package/esm/shared/helper/router.d.ts +4 -0
- package/esm/shared/helper/router.js +60 -0
- package/esm/shared/index.d.ts +9 -0
- package/esm/shared/index.js +10 -0
- package/esm/shared/lifecycle.d.ts +46 -0
- package/esm/shared/lifecycle.js +40 -0
- package/esm/shared/loader/index.d.ts +3 -0
- package/esm/shared/loader/index.js +3 -0
- package/esm/shared/loader/loader.d.ts +9 -0
- package/esm/shared/loader/loader.js +114 -0
- package/esm/shared/loader/loaderManager.d.ts +16 -0
- package/esm/shared/loader/loaderManager.js +49 -0
- package/esm/shared/loader/types.d.ts +73 -0
- package/esm/shared/loader/types.js +1 -0
- package/esm/shared/models/error.d.ts +13 -0
- package/esm/shared/models/error.js +32 -0
- package/esm/shared/response.d.ts +51 -0
- package/esm/shared/response.js +126 -0
- package/esm/shared/router.d.ts +1 -0
- package/esm/shared/router.js +1 -0
- package/esm/shared/routerTypes.d.ts +28 -0
- package/esm/shared/routerTypes.js +1 -0
- package/esm/shared/shuvi-singleton-runtimeConfig.d.ts +12 -0
- package/esm/shared/shuvi-singleton-runtimeConfig.js +19 -0
- package/esm/shuvi-app/application.d.ts +4 -0
- package/esm/shuvi-app/application.js +34 -0
- package/esm/shuvi-app/shared.d.ts +1 -0
- package/esm/shuvi-app/shared.js +1 -0
- package/esm/shuvi-app/shuvi-runtime-app.d.ts +14 -0
- package/esm/shuvi-app/shuvi-runtime-app.js +1 -0
- package/esm/shuvi-app/shuvi-runtime-index.d.ts +10 -0
- package/esm/shuvi-app/shuvi-runtime-index.js +9 -0
- package/lib/node/index.d.ts +2 -0
- package/lib/node/index.js +18 -0
- package/lib/node/paths.d.ts +3 -0
- package/lib/node/paths.js +12 -0
- package/lib/node/platform/index.d.ts +16 -0
- package/lib/node/platform/index.js +23 -0
- package/lib/node/platform/plugins/main/hooks.d.ts +9 -0
- package/lib/node/platform/plugins/main/hooks.js +12 -0
- package/lib/node/platform/plugins/main/index.d.ts +16 -0
- package/lib/node/platform/plugins/main/index.js +32 -0
- package/lib/node/platform/plugins/main/shuvi-app.d.ts +8 -0
- package/lib/node/platform/plugins/main/shuvi-app.js +2 -0
- package/lib/node/platform/runtimeFiles.d.ts +11 -0
- package/lib/node/platform/runtimeFiles.js +47 -0
- package/lib/node/project/file-presets/files/app/core/entry.js.d.ts +3 -0
- package/lib/node/project/file-presets/files/app/core/entry.js.js +52 -0
- package/lib/node/project/file-presets/files/app/core/error.js.d.ts +2 -0
- package/lib/node/project/file-presets/files/app/core/error.js.js +9 -0
- package/lib/node/project/file-presets/files/app/core/platform.js.d.ts +3 -0
- package/lib/node/project/file-presets/files/app/core/platform.js.js +9 -0
- package/lib/node/project/file-presets/files/app/core/polyfill.js.d.ts +3 -0
- package/lib/node/project/file-presets/files/app/core/polyfill.js.js +6 -0
- package/lib/node/project/file-presets/files/app/core/runtimeConfig.js.d.ts +3 -0
- package/lib/node/project/file-presets/files/app/core/runtimeConfig.js.js +12 -0
- package/lib/node/project/file-presets/files/app/core/setPublicRuntimeConfig.js.d.ts +2 -0
- package/lib/node/project/file-presets/files/app/core/setPublicRuntimeConfig.js.js +7 -0
- package/lib/node/project/file-presets/files/app/core/setRuntimeConfig.js.d.ts +2 -0
- package/lib/node/project/file-presets/files/app/core/setRuntimeConfig.js.js +7 -0
- package/lib/node/project/file-presets/files/app/user/app.js.d.ts +3 -0
- package/lib/node/project/file-presets/files/app/user/app.js.js +15 -0
- package/lib/node/project/file-presets/files/app/user/error.js.d.ts +3 -0
- package/lib/node/project/file-presets/files/app/user/error.js.js +15 -0
- package/lib/node/project/file-presets/index.d.ts +4 -0
- package/lib/node/project/file-presets/index.js +58 -0
- package/lib/node/project/index.d.ts +2 -0
- package/lib/node/project/index.js +7 -0
- package/lib/node/project/projectContext.d.ts +13 -0
- package/lib/node/project/projectContext.js +16 -0
- package/lib/node/route/helpers.d.ts +20 -0
- package/lib/node/route/helpers.js +199 -0
- package/lib/node/route/index.d.ts +1 -0
- package/lib/node/route/index.js +7 -0
- package/lib/node/route/route.d.ts +37 -0
- package/lib/node/route/route.js +212 -0
- package/lib/shared/application.d.ts +82 -0
- package/lib/shared/application.js +91 -0
- package/lib/shared/applicationTypes.d.ts +45 -0
- package/lib/shared/applicationTypes.js +2 -0
- package/lib/shared/helper/getAppData.d.ts +20 -0
- package/lib/shared/helper/getAppData.js +30 -0
- package/lib/shared/helper/getFilesOfRoute.d.ts +10 -0
- package/lib/shared/helper/getFilesOfRoute.js +34 -0
- package/lib/shared/helper/getPageData.d.ts +1 -0
- package/lib/shared/helper/getPageData.js +17 -0
- package/lib/shared/helper/getPublicPath.d.ts +1 -0
- package/lib/shared/helper/getPublicPath.js +10 -0
- package/lib/shared/helper/index.d.ts +5 -0
- package/lib/shared/helper/index.js +24 -0
- package/lib/shared/helper/router.d.ts +4 -0
- package/lib/shared/helper/router.js +68 -0
- package/lib/shared/index.d.ts +9 -0
- package/lib/shared/index.js +28 -0
- package/lib/shared/lifecycle.d.ts +46 -0
- package/lib/shared/lifecycle.js +45 -0
- package/lib/shared/loader/index.d.ts +3 -0
- package/lib/shared/loader/index.js +19 -0
- package/lib/shared/loader/loader.d.ts +9 -0
- package/lib/shared/loader/loader.js +122 -0
- package/lib/shared/loader/loaderManager.d.ts +16 -0
- package/lib/shared/loader/loaderManager.js +53 -0
- package/lib/shared/loader/types.d.ts +73 -0
- package/lib/shared/loader/types.js +2 -0
- package/lib/shared/models/error.d.ts +13 -0
- package/lib/shared/models/error.js +35 -0
- package/lib/shared/response.d.ts +51 -0
- package/lib/shared/response.js +134 -0
- package/lib/shared/router.d.ts +1 -0
- package/lib/shared/router.js +11 -0
- package/lib/shared/routerTypes.d.ts +28 -0
- package/lib/shared/routerTypes.js +2 -0
- package/lib/shared/shuvi-singleton-runtimeConfig.d.ts +12 -0
- package/lib/shared/shuvi-singleton-runtimeConfig.js +26 -0
- package/package.json +90 -0
- package/shuvi-app-extensions.d.ts +58 -0
- package/template/document.ejs +10 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { IUserRouteConfig, IApiRouteConfig, IMiddlewareRouteConfig } from '@shuvi/service';
|
|
2
|
+
import { SupportFileType } from './helpers';
|
|
3
|
+
export declare type IPageRouteConfig = IUserRouteConfig;
|
|
4
|
+
export type { IApiRouteConfig, IMiddlewareRouteConfig };
|
|
5
|
+
interface RawFileRoute {
|
|
6
|
+
kind: 'file';
|
|
7
|
+
type: SupportFileType;
|
|
8
|
+
name: string;
|
|
9
|
+
segment: string;
|
|
10
|
+
filepath: string;
|
|
11
|
+
}
|
|
12
|
+
interface RawDirRoute {
|
|
13
|
+
kind: 'dir';
|
|
14
|
+
filepath: string;
|
|
15
|
+
segment: string;
|
|
16
|
+
parentSegment: string;
|
|
17
|
+
children: (RawFileRoute | RawDirRoute)[];
|
|
18
|
+
}
|
|
19
|
+
declare type RawRoute = RawFileRoute | RawDirRoute;
|
|
20
|
+
export interface RouteException {
|
|
21
|
+
type: SupportFileType | 'dir';
|
|
22
|
+
msg: string;
|
|
23
|
+
}
|
|
24
|
+
export interface RouteResult<T> {
|
|
25
|
+
warnings: RouteException[];
|
|
26
|
+
errors: RouteException[];
|
|
27
|
+
routes: T[];
|
|
28
|
+
}
|
|
29
|
+
export declare type RawRoutes = RouteResult<RawRoute>;
|
|
30
|
+
export declare type PageRoutes = RouteResult<IPageRouteConfig>;
|
|
31
|
+
export declare type ApiRoutes = RouteResult<IApiRouteConfig>;
|
|
32
|
+
export declare type MiddlewareRoutes = RouteResult<IMiddlewareRouteConfig>;
|
|
33
|
+
export declare type RouteConfigType = IPageRouteConfig | IApiRouteConfig | IMiddlewareRouteConfig;
|
|
34
|
+
export declare const getRawRoutesFromDir: (dirname: string) => Promise<RawRoutes>;
|
|
35
|
+
export declare const getPageRoutes: (dirname: string) => Promise<PageRoutes>;
|
|
36
|
+
export declare const getApiRoutes: (dir: string) => Promise<ApiRoutes>;
|
|
37
|
+
export declare const getMiddlewareRoutes: (dirname: string) => Promise<MiddlewareRoutes>;
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.getMiddlewareRoutes = exports.getApiRoutes = exports.getPageRoutes = exports.getRawRoutesFromDir = void 0;
|
|
13
|
+
const path_1 = require("path");
|
|
14
|
+
const file_1 = require("@shuvi/utils/lib/file");
|
|
15
|
+
const helpers_1 = require("./helpers");
|
|
16
|
+
const getRawRoutesFromDir = (dirname) => __awaiter(void 0, void 0, void 0, function* () {
|
|
17
|
+
if (!(yield (0, file_1.isDirectory)(dirname))) {
|
|
18
|
+
return {
|
|
19
|
+
routes: [],
|
|
20
|
+
warnings: [],
|
|
21
|
+
errors: []
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
const warnings = [];
|
|
25
|
+
const errors = [];
|
|
26
|
+
const _getRawRoutesFromDir = (dirname, rawRoutes, segment) => __awaiter(void 0, void 0, void 0, function* () {
|
|
27
|
+
const files = yield (0, helpers_1.getAllowFilesAndDirs)(dirname);
|
|
28
|
+
const onlyHasDir = !(0, helpers_1.hasAllowFiles)(files);
|
|
29
|
+
if (!files.length) {
|
|
30
|
+
warnings.push({
|
|
31
|
+
type: 'dir',
|
|
32
|
+
msg: `${dirname} is empty dir!`
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
for (const file of files) {
|
|
36
|
+
const filepath = (0, path_1.join)(dirname, file);
|
|
37
|
+
const isDir = yield (0, file_1.isDirectory)(filepath);
|
|
38
|
+
if (isDir) {
|
|
39
|
+
if (onlyHasDir) {
|
|
40
|
+
// only indent segment,routes was in same level.
|
|
41
|
+
yield _getRawRoutesFromDir(filepath, rawRoutes, `${segment}/${file}`);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const rawRoute = {
|
|
45
|
+
kind: 'dir',
|
|
46
|
+
filepath,
|
|
47
|
+
segment: (0, helpers_1.normalize)(file).replace(/^\//, ''),
|
|
48
|
+
parentSegment: (0, helpers_1.normalize)(segment).replace(/^\//, ''),
|
|
49
|
+
children: []
|
|
50
|
+
};
|
|
51
|
+
yield _getRawRoutesFromDir(filepath, rawRoute.children, file);
|
|
52
|
+
rawRoutes.push(rawRoute);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
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
|
+
}
|
|
65
|
+
return rawRoutes;
|
|
66
|
+
});
|
|
67
|
+
const routes = yield _getRawRoutesFromDir(dirname, [], '');
|
|
68
|
+
return {
|
|
69
|
+
routes,
|
|
70
|
+
warnings,
|
|
71
|
+
errors
|
|
72
|
+
};
|
|
73
|
+
});
|
|
74
|
+
exports.getRawRoutesFromDir = getRawRoutesFromDir;
|
|
75
|
+
const getPageRoutes = (dirname) => __awaiter(void 0, void 0, void 0, function* () {
|
|
76
|
+
const { routes: rawRoutes, warnings, errors } = yield (0, exports.getRawRoutesFromDir)(dirname);
|
|
77
|
+
const _getPageRoutes = (rawRoutes, routes, segment = '') => {
|
|
78
|
+
const layoutRoute = rawRoutes.some(route => route.kind === 'file' && route.type === 'layout');
|
|
79
|
+
const route = {};
|
|
80
|
+
if (layoutRoute) {
|
|
81
|
+
route.children = [];
|
|
82
|
+
}
|
|
83
|
+
for (const rawRoute of rawRoutes) {
|
|
84
|
+
if (rawRoute.kind === 'dir') {
|
|
85
|
+
const workRoutes = layoutRoute ? route.children : routes;
|
|
86
|
+
const preSegment = layoutRoute
|
|
87
|
+
? ''
|
|
88
|
+
: `${segment}${rawRoute.parentSegment}/`;
|
|
89
|
+
_getPageRoutes(rawRoute.children, workRoutes, preSegment);
|
|
90
|
+
}
|
|
91
|
+
else if (rawRoute.type === 'page' || rawRoute.type === 'layout') {
|
|
92
|
+
if (rawRoute.type === 'page' && layoutRoute) {
|
|
93
|
+
route.children.push({
|
|
94
|
+
path: '',
|
|
95
|
+
component: rawRoute.filepath
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
let prefix = segment;
|
|
100
|
+
let suffix = rawRoute.segment;
|
|
101
|
+
route.component = rawRoute.filepath;
|
|
102
|
+
route.path = `${prefix}${suffix}`;
|
|
103
|
+
routes.push(Object.assign({}, route));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return routes;
|
|
108
|
+
};
|
|
109
|
+
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
|
+
return {
|
|
116
|
+
routes,
|
|
117
|
+
warnings,
|
|
118
|
+
errors
|
|
119
|
+
};
|
|
120
|
+
});
|
|
121
|
+
exports.getPageRoutes = getPageRoutes;
|
|
122
|
+
const getApiRoutes = (dir) => __awaiter(void 0, void 0, void 0, function* () {
|
|
123
|
+
const getConflictWaring = (rawRoute, conflictRawRoute) => {
|
|
124
|
+
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
|
+
};
|
|
126
|
+
const { routes: rawRoutes, warnings, errors } = yield (0, exports.getRawRoutesFromDir)(dir);
|
|
127
|
+
const _getApiRoutes = (rawRoutes, routes, prefix = '') => {
|
|
128
|
+
const page = rawRoutes.find(route => route.kind === 'file' && route.type === 'page');
|
|
129
|
+
const layout = rawRoutes.find(route => route.kind === 'file' && route.type === 'layout');
|
|
130
|
+
const allowedRoutes = rawRoutes.filter(route => route.kind === 'dir' || route.type === 'api');
|
|
131
|
+
for (let rawRoute of allowedRoutes) {
|
|
132
|
+
prefix = prefix === '/' ? '' : prefix;
|
|
133
|
+
if (rawRoute.kind === 'dir') {
|
|
134
|
+
_getApiRoutes(rawRoute.children, routes, prefix + '/' + rawRoute.parentSegment);
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (rawRoute.type === 'api') {
|
|
138
|
+
if (layout) {
|
|
139
|
+
warnings.push({
|
|
140
|
+
type: 'api',
|
|
141
|
+
msg: getConflictWaring(rawRoute, layout)
|
|
142
|
+
});
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (page) {
|
|
146
|
+
warnings.push({
|
|
147
|
+
type: 'api',
|
|
148
|
+
msg: getConflictWaring(rawRoute, page)
|
|
149
|
+
});
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
let path = prefix + '/' + rawRoute.segment;
|
|
153
|
+
if (path === '//') {
|
|
154
|
+
path = '/';
|
|
155
|
+
}
|
|
156
|
+
routes.push({
|
|
157
|
+
path,
|
|
158
|
+
api: rawRoute.filepath
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return routes;
|
|
163
|
+
};
|
|
164
|
+
const routes = (0, helpers_1.sortRoutes)(_getApiRoutes(rawRoutes, [], ''));
|
|
165
|
+
const filterException = (e) => e.type === 'api';
|
|
166
|
+
return {
|
|
167
|
+
routes,
|
|
168
|
+
warnings: warnings.filter(filterException),
|
|
169
|
+
errors: errors.filter(filterException)
|
|
170
|
+
};
|
|
171
|
+
});
|
|
172
|
+
exports.getApiRoutes = getApiRoutes;
|
|
173
|
+
const getMiddlewareRoutes = (dirname) => __awaiter(void 0, void 0, void 0, function* () {
|
|
174
|
+
const { routes: rawRoutes, warnings, errors } = yield (0, exports.getRawRoutesFromDir)(dirname);
|
|
175
|
+
const _getMiddlewareRoutes = (rawRoutes, routes, segment) => {
|
|
176
|
+
segment = segment === '/' ? '' : segment;
|
|
177
|
+
for (const rawRoute of rawRoutes) {
|
|
178
|
+
if (rawRoute.kind === 'dir') {
|
|
179
|
+
_getMiddlewareRoutes(rawRoute.children, routes, segment + '/' + rawRoute.parentSegment);
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
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
|
+
}
|
|
191
|
+
routes.push({
|
|
192
|
+
path: path + catchAllOp,
|
|
193
|
+
middleware: rawRoute.filepath
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return routes;
|
|
198
|
+
};
|
|
199
|
+
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
|
+
const exceptionFilter = (e) => e.type === 'middleware';
|
|
206
|
+
return {
|
|
207
|
+
routes,
|
|
208
|
+
warnings: warnings.filter(exceptionFilter),
|
|
209
|
+
errors: errors.filter(exceptionFilter)
|
|
210
|
+
};
|
|
211
|
+
});
|
|
212
|
+
exports.getMiddlewareRoutes = getMiddlewareRoutes;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { IRouter } from './routerTypes';
|
|
2
|
+
import { IStoreManager, IApplication, IAppContext, IApplicationOptions, IRerenderConfig } from './applicationTypes';
|
|
3
|
+
export declare class Application {
|
|
4
|
+
private _error;
|
|
5
|
+
private _router;
|
|
6
|
+
private _appComponent;
|
|
7
|
+
private _pluginManager;
|
|
8
|
+
private _context;
|
|
9
|
+
private _storeManager;
|
|
10
|
+
constructor(options: IApplicationOptions);
|
|
11
|
+
get router(): IRouter<any>;
|
|
12
|
+
get context(): IAppContext;
|
|
13
|
+
get pluginManager(): import("@shuvi/hook").HookManager<import("./lifecycle").BuiltInRuntimePluginHooks, void, import("@shuvi/runtime").CustomRuntimePluginHooks>;
|
|
14
|
+
get appComponent(): any;
|
|
15
|
+
get error(): {
|
|
16
|
+
$state: import("./applicationTypes").IErrorState;
|
|
17
|
+
$set: (state: import("@shuvi/redox").State) => void;
|
|
18
|
+
$modify: (modifier: (state: import("./applicationTypes").IErrorState) => void) => void;
|
|
19
|
+
$patch: (partState: import("@shuvi/redox").ObjectState) => void;
|
|
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
|
+
};
|
|
74
|
+
init(): Promise<void>;
|
|
75
|
+
get storeManager(): IStoreManager;
|
|
76
|
+
updateComponents({ AppComponent }?: IRerenderConfig): Promise<void>;
|
|
77
|
+
dispose(): Promise<void>;
|
|
78
|
+
private _initPlugin;
|
|
79
|
+
private _initAppContext;
|
|
80
|
+
private _initAppComponent;
|
|
81
|
+
getPublicAPI(): IApplication;
|
|
82
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.Application = void 0;
|
|
13
|
+
const lifecycle_1 = require("./lifecycle");
|
|
14
|
+
const lifecycle_2 = require("./lifecycle");
|
|
15
|
+
const redox_1 = require("@shuvi/redox");
|
|
16
|
+
const error_1 = require("./models/error");
|
|
17
|
+
class Application {
|
|
18
|
+
constructor(options) {
|
|
19
|
+
this._router = options.router;
|
|
20
|
+
this._context = {};
|
|
21
|
+
this._storeManager = (0, redox_1.redox)({ initialState: options.initialState });
|
|
22
|
+
this._error = this._storeManager.get(error_1.errorModel);
|
|
23
|
+
this._appComponent = options.AppComponent;
|
|
24
|
+
this._pluginManager = (0, lifecycle_1.getManager)();
|
|
25
|
+
(0, lifecycle_2.initPlugins)(this._pluginManager, options.plugins || []);
|
|
26
|
+
}
|
|
27
|
+
get router() {
|
|
28
|
+
return this._router;
|
|
29
|
+
}
|
|
30
|
+
get context() {
|
|
31
|
+
return this._context;
|
|
32
|
+
}
|
|
33
|
+
get pluginManager() {
|
|
34
|
+
return this._pluginManager;
|
|
35
|
+
}
|
|
36
|
+
get appComponent() {
|
|
37
|
+
return this._appComponent;
|
|
38
|
+
}
|
|
39
|
+
get error() {
|
|
40
|
+
return this._error;
|
|
41
|
+
}
|
|
42
|
+
init() {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
yield this._initPlugin();
|
|
45
|
+
yield this._initAppContext();
|
|
46
|
+
yield this._initAppComponent();
|
|
47
|
+
this._router.init();
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
get storeManager() {
|
|
51
|
+
return this._storeManager;
|
|
52
|
+
}
|
|
53
|
+
updateComponents({ AppComponent } = {}) {
|
|
54
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
if (AppComponent && AppComponent !== this._appComponent) {
|
|
56
|
+
this._appComponent = AppComponent;
|
|
57
|
+
}
|
|
58
|
+
yield this._initAppComponent();
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
dispose() {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63
|
+
yield this._pluginManager.runner.dispose();
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
_initPlugin() {
|
|
67
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
68
|
+
yield this._pluginManager.runner.init();
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
_initAppContext() {
|
|
72
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
this._context = (yield this._pluginManager.runner.appContext(this._context));
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
_initAppComponent() {
|
|
77
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
this._appComponent = yield this._pluginManager.runner.appComponent(this._appComponent, this._context);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
getPublicAPI() {
|
|
82
|
+
return {
|
|
83
|
+
context: this._context,
|
|
84
|
+
router: this._router,
|
|
85
|
+
appComponent: this._appComponent,
|
|
86
|
+
storeManager: this._storeManager,
|
|
87
|
+
error: this._error
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
exports.Application = Application;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { IncomingMessage } from 'http';
|
|
3
|
+
import { IStoreManager } from '@shuvi/redox';
|
|
4
|
+
import { CustomAppContext } from '@shuvi/runtime';
|
|
5
|
+
import { IRouter } from './routerTypes';
|
|
6
|
+
import { IPluginList } from './lifecycle';
|
|
7
|
+
export declare type IRequest = IncomingMessage & {
|
|
8
|
+
[x: string]: any;
|
|
9
|
+
};
|
|
10
|
+
export interface IAppContext extends CustomAppContext {
|
|
11
|
+
[x: string]: unknown;
|
|
12
|
+
}
|
|
13
|
+
export declare type IRerenderConfig = {
|
|
14
|
+
AppComponent?: any;
|
|
15
|
+
};
|
|
16
|
+
export type { IStoreManager };
|
|
17
|
+
export interface IError {
|
|
18
|
+
code?: number;
|
|
19
|
+
message?: string;
|
|
20
|
+
}
|
|
21
|
+
export interface IErrorState {
|
|
22
|
+
error?: IError;
|
|
23
|
+
}
|
|
24
|
+
export declare type IAppState = {
|
|
25
|
+
error?: IErrorState;
|
|
26
|
+
};
|
|
27
|
+
export interface IErrorManager {
|
|
28
|
+
getError: IError | undefined;
|
|
29
|
+
error: (err: IError) => void;
|
|
30
|
+
clear: () => void;
|
|
31
|
+
hasError: boolean;
|
|
32
|
+
}
|
|
33
|
+
export interface IApplication {
|
|
34
|
+
readonly context: IAppContext;
|
|
35
|
+
readonly router: IRouter;
|
|
36
|
+
readonly appComponent: any;
|
|
37
|
+
readonly error: IErrorManager;
|
|
38
|
+
readonly storeManager: IStoreManager;
|
|
39
|
+
}
|
|
40
|
+
export interface IApplicationOptions {
|
|
41
|
+
router: IRouter;
|
|
42
|
+
AppComponent: any;
|
|
43
|
+
initialState?: IAppState;
|
|
44
|
+
plugins?: IPluginList;
|
|
45
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/// <reference lib="dom" />
|
|
2
|
+
export declare type IData = {
|
|
3
|
+
[k: string]: string | number | boolean | undefined | null;
|
|
4
|
+
};
|
|
5
|
+
export declare type IAppData<Data = {}, appState = any> = {
|
|
6
|
+
ssr: boolean;
|
|
7
|
+
runtimeConfig?: Record<string, string>;
|
|
8
|
+
pageData?: {
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
};
|
|
11
|
+
loadersData?: {
|
|
12
|
+
[x: string]: any;
|
|
13
|
+
};
|
|
14
|
+
appState?: appState;
|
|
15
|
+
filesByRoutId: Record<string, string[]>;
|
|
16
|
+
publicPath: string;
|
|
17
|
+
} & {
|
|
18
|
+
[K in keyof Data]: Data[K];
|
|
19
|
+
};
|
|
20
|
+
export declare function getAppData(): IAppData;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAppData = void 0;
|
|
4
|
+
/// <reference lib="dom" />
|
|
5
|
+
const constants_1 = require("@shuvi/shared/lib/constants");
|
|
6
|
+
let appData = null;
|
|
7
|
+
function getAppData() {
|
|
8
|
+
if (appData) {
|
|
9
|
+
return appData;
|
|
10
|
+
}
|
|
11
|
+
if (typeof window === 'undefined') {
|
|
12
|
+
return {
|
|
13
|
+
ssr: false,
|
|
14
|
+
filesByRoutId: {},
|
|
15
|
+
publicPath: ''
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
const el = document.getElementById(constants_1.CLIENT_APPDATA_ID);
|
|
19
|
+
if (!el || !el.textContent) {
|
|
20
|
+
return {
|
|
21
|
+
ssr: false,
|
|
22
|
+
pageData: {},
|
|
23
|
+
loadersData: {},
|
|
24
|
+
filesByRoutId: {},
|
|
25
|
+
publicPath: ''
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
return JSON.parse(el.textContent);
|
|
29
|
+
}
|
|
30
|
+
exports.getAppData = getAppData;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { IRouter, PathRecord } from '../router';
|
|
2
|
+
export declare type RouteFile = {
|
|
3
|
+
id: string;
|
|
4
|
+
url: string;
|
|
5
|
+
};
|
|
6
|
+
export declare type RouteFiles = {
|
|
7
|
+
js: RouteFile[];
|
|
8
|
+
css: RouteFile[];
|
|
9
|
+
};
|
|
10
|
+
export declare function getFilesOfRoute(router: IRouter, to: PathRecord): RouteFiles;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getFilesOfRoute = void 0;
|
|
4
|
+
const getAppData_1 = require("./getAppData");
|
|
5
|
+
const getPublicPath_1 = require("./getPublicPath");
|
|
6
|
+
function getFilesOfRoute(router, to) {
|
|
7
|
+
const { filesByRoutId } = (0, getAppData_1.getAppData)();
|
|
8
|
+
const publicPath = (0, getPublicPath_1.getPublicPath)();
|
|
9
|
+
const js = [];
|
|
10
|
+
const css = [];
|
|
11
|
+
const targetRoute = router.match(to);
|
|
12
|
+
targetRoute.forEach(({ route: { id } }) => {
|
|
13
|
+
if (!filesByRoutId[id]) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
filesByRoutId[id].map(path => {
|
|
17
|
+
const file = {
|
|
18
|
+
url: `${publicPath}${path}`,
|
|
19
|
+
id
|
|
20
|
+
};
|
|
21
|
+
if (path.endsWith('.js')) {
|
|
22
|
+
js.push(file);
|
|
23
|
+
}
|
|
24
|
+
else if (path.endsWith('.css')) {
|
|
25
|
+
css.push(file);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
return {
|
|
30
|
+
js,
|
|
31
|
+
css
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
exports.getFilesOfRoute = getFilesOfRoute;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getPageData<T = unknown>(key: string, defaultValue?: T): any;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getPageData = void 0;
|
|
4
|
+
const getAppData_1 = require("./getAppData");
|
|
5
|
+
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
6
|
+
function getPageData(key, defaultValue) {
|
|
7
|
+
if (typeof window === 'undefined') {
|
|
8
|
+
console.warn('"getPageData" should only be called on client-side');
|
|
9
|
+
return defaultValue;
|
|
10
|
+
}
|
|
11
|
+
const { pageData = {} } = (0, getAppData_1.getAppData)();
|
|
12
|
+
if (!hasOwnProperty.call(pageData, key)) {
|
|
13
|
+
return defaultValue;
|
|
14
|
+
}
|
|
15
|
+
return pageData[key];
|
|
16
|
+
}
|
|
17
|
+
exports.getPageData = getPageData;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getPublicPath(): string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getPublicPath = void 0;
|
|
4
|
+
const constants_1 = require("@shuvi/shared/lib/constants");
|
|
5
|
+
const getAppData_1 = require("./getAppData");
|
|
6
|
+
function getPublicPath() {
|
|
7
|
+
const { publicPath } = (0, getAppData_1.getAppData)();
|
|
8
|
+
return window[constants_1.IDENTITY_RUNTIME_PUBLICPATH] || publicPath;
|
|
9
|
+
}
|
|
10
|
+
exports.getPublicPath = getPublicPath;
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
exports.getPageData = exports.getAppData = void 0;
|
|
18
|
+
var getAppData_1 = require("./getAppData");
|
|
19
|
+
Object.defineProperty(exports, "getAppData", { enumerable: true, get: function () { return getAppData_1.getAppData; } });
|
|
20
|
+
var getPageData_1 = require("./getPageData");
|
|
21
|
+
Object.defineProperty(exports, "getPageData", { enumerable: true, get: function () { return getPageData_1.getPageData; } });
|
|
22
|
+
__exportStar(require("./getPublicPath"), exports);
|
|
23
|
+
__exportStar(require("./getFilesOfRoute"), exports);
|
|
24
|
+
__exportStar(require("./router"), exports);
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { IRoute, IPageRouteRecord, IRouteMatch } from '../routerTypes';
|
|
2
|
+
import { Loader } from '../loader';
|
|
3
|
+
export declare function runPreload(to: IRoute<IPageRouteRecord>): Promise<void>;
|
|
4
|
+
export declare function getRouteMatchesWithInvalidLoader(to: IRoute<IPageRouteRecord>, from: IRoute<IPageRouteRecord>, loaders: Record<string, Loader>): IRouteMatch<IPageRouteRecord>[];
|