@shuvi/platform-web 1.0.0-rc.3 → 1.0.0-rc.6
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/appTypes.d.ts +5 -3
- package/esm/shared/renderTypes.d.ts +6 -5
- package/esm/shared/routeTypes.d.ts +4 -9
- package/esm/shuvi-app/app/client.d.ts +2 -0
- package/esm/shuvi-app/app/client.js +13 -17
- package/esm/shuvi-app/app/server.js +10 -9
- package/esm/shuvi-app/dev/eventsource.d.ts +1 -0
- package/esm/shuvi-app/dev/eventsource.js +60 -0
- package/esm/shuvi-app/dev/hotDevClient.d.ts +32 -0
- package/esm/shuvi-app/dev/hotDevClient.js +327 -0
- package/esm/shuvi-app/dev/index.d.ts +3 -0
- package/esm/shuvi-app/dev/index.js +27 -0
- package/esm/shuvi-app/dev/websocket.d.ts +16 -0
- package/esm/shuvi-app/dev/websocket.js +61 -0
- package/esm/shuvi-app/entry/client/app.d.ts +2 -1
- package/esm/shuvi-app/entry/client/app.js +2 -2
- package/esm/shuvi-app/entry/client/run.dev.js +4 -4
- package/esm/shuvi-app/entry/server/index.d.ts +5 -4
- package/esm/shuvi-app/entry/server/index.js +5 -4
- package/esm/shuvi-app/helper/serializeServerError.d.ts +2 -0
- package/esm/shuvi-app/helper/serializeServerError.js +21 -0
- package/esm/shuvi-app/react/AppContainer.d.ts +2 -3
- package/esm/shuvi-app/react/AppContainer.jsx +3 -4
- package/esm/shuvi-app/react/getRoutes.d.ts +2 -2
- package/esm/shuvi-app/react/getRoutes.js +9 -8
- package/esm/shuvi-app/react/{redox-react → model}/RedoxWrapper.d.ts +3 -3
- package/esm/shuvi-app/react/{redox-react → model}/RedoxWrapper.jsx +3 -3
- package/esm/shuvi-app/react/model/runtime.d.ts +8 -0
- package/esm/shuvi-app/react/{redox-react → model}/runtime.js +8 -3
- package/esm/shuvi-app/react/store.d.ts +5 -0
- package/esm/shuvi-app/react/store.js +3 -0
- package/esm/shuvi-app/react/types.d.ts +0 -7
- package/esm/shuvi-app/react/useLoaderData.js +9 -20
- package/esm/shuvi-app/react/view/ReactView.client.jsx +25 -4
- package/esm/shuvi-app/react/view/ReactView.server.jsx +17 -14
- package/esm/shuvi-app/shuvi-runtime-index.d.ts +3 -3
- package/esm/shuvi-app/shuvi-runtime-index.js +1 -1
- package/lib/node/features/custom-server/index.d.ts +1 -1
- package/lib/node/features/custom-server/server.d.ts +1 -1
- package/lib/node/features/filesystem-routes/api/apiRouteHandler.d.ts +7 -7
- package/lib/node/features/filesystem-routes/api/apiRouteHandler.js +0 -4
- package/lib/node/features/filesystem-routes/api/middleware.d.ts +2 -2
- package/lib/node/features/filesystem-routes/index.d.ts +1 -13
- package/lib/node/features/filesystem-routes/index.js +20 -48
- package/lib/node/features/filesystem-routes/middleware/middleware.d.ts +2 -2
- package/lib/node/features/filesystem-routes/page/routes.d.ts +2 -2
- package/lib/node/features/filesystem-routes/page/routes.js +29 -11
- package/lib/node/features/html-render/index.d.ts +3 -18
- package/lib/node/features/html-render/index.js +87 -16
- package/lib/node/features/{main → html-render/lib}/buildHtml.d.ts +0 -0
- package/lib/node/features/{main → html-render/lib}/buildHtml.js +0 -0
- package/lib/node/features/html-render/lib/generateFilesByRoutId.d.ts +2 -2
- package/lib/node/features/html-render/lib/generateFilesByRoutId.js +3 -3
- package/lib/node/features/{main → html-render/lib}/generateResource.d.ts +0 -0
- package/lib/node/features/{main → html-render/lib}/generateResource.js +0 -0
- package/lib/node/features/html-render/lib/getPageMiddleware.d.ts +2 -2
- package/lib/node/features/html-render/lib/index.d.ts +0 -1
- package/lib/node/features/html-render/lib/index.js +1 -3
- package/lib/node/features/html-render/lib/renderToHTML.d.ts +2 -2
- package/lib/node/features/html-render/lib/renderToHTML.js +7 -48
- package/lib/node/features/html-render/lib/renderer/base.d.ts +6 -6
- package/lib/node/features/html-render/lib/renderer/base.js +5 -5
- package/lib/node/features/html-render/lib/renderer/index.d.ts +5 -4
- package/lib/node/features/html-render/lib/renderer/index.js +69 -6
- package/lib/node/features/html-render/lib/renderer/spa.d.ts +2 -2
- package/lib/node/features/html-render/lib/renderer/spa.js +4 -6
- package/lib/node/features/html-render/lib/renderer/ssr.d.ts +2 -2
- package/lib/node/features/html-render/lib/renderer/ssr.js +5 -6
- package/lib/node/features/html-render/lib/renderer/types.d.ts +8 -5
- package/lib/node/features/html-render/server.d.ts +1 -1
- package/lib/node/features/index.d.ts +3 -31
- package/lib/node/features/index.js +6 -7
- package/lib/node/features/model/index.d.ts +1 -13
- package/lib/node/features/model/runtime.d.ts +3 -8
- package/lib/node/features/model/runtime.js +13 -17
- package/lib/node/features/model/server.js +2 -3
- package/lib/node/features/model/shuvi-app.d.ts +2 -2
- package/lib/node/features/on-demand-compile-page/index.d.ts +3 -13
- package/lib/node/features/on-demand-compile-page/index.js +4 -1
- package/lib/node/features/on-demand-compile-page/onDemandRouteManager.d.ts +3 -3
- package/lib/node/features/on-demand-compile-page/onDemandRouteManager.js +2 -4
- package/lib/node/index.js +1 -3
- package/lib/node/paths.js +0 -2
- package/lib/node/shuvi-runtime-server.d.ts +18 -0
- package/lib/node/shuvi-runtime-server.js +2 -0
- package/lib/node/shuvi-type-extensions-node.d.ts +2 -6
- package/lib/node/targets/react/bundler/index.d.ts +1 -13
- package/lib/node/targets/react/bundler/index.js +5 -0
- package/lib/node/targets/react/index.d.ts +2 -26
- package/lib/node/targets/react/index.js +2 -2
- package/lib/node/targets/react/model/index.d.ts +6 -0
- package/lib/node/targets/react/{redox-react → model}/index.js +7 -6
- package/lib/shared/appTypes.d.ts +5 -3
- package/lib/shared/renderTypes.d.ts +6 -5
- package/lib/shared/routeTypes.d.ts +4 -9
- package/package.json +20 -15
- package/shuvi-env.d.ts +10 -0
- package/shuvi-image.d.ts +54 -0
- package/shuvi-type-extensions-node.js +1 -0
- package/shuvi-type-extensions-runtime.d.ts +2 -2
- package/esm/shuvi-app/dev/webpackHotDevClient.d.ts +0 -5
- package/esm/shuvi-app/dev/webpackHotDevClient.js +0 -34
- package/esm/shuvi-app/react/redox-react/runtime.d.ts +0 -2
- package/esm/shuvi-app/shuvi-runtime-server.d.ts +0 -6
- package/esm/shuvi-app/shuvi-runtime-server.js +0 -1
- package/lib/node/features/main/index.d.ts +0 -3
- package/lib/node/features/main/index.js +0 -82
- package/lib/node/targets/react/redox-react/index.d.ts +0 -18
package/esm/shared/appTypes.d.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IPageRouteRecord, IAppData, IAppState } from '@shuvi/platform-shared/shared';
|
|
2
2
|
import { Application } from '@shuvi/platform-shared/shuvi-app/application';
|
|
3
|
+
import type { ShuviRequest } from '@shuvi/service';
|
|
3
4
|
export interface CreateAppServer {
|
|
4
5
|
(options: {
|
|
5
|
-
req:
|
|
6
|
+
req: ShuviRequest;
|
|
6
7
|
ssr: boolean;
|
|
8
|
+
isDev: boolean;
|
|
7
9
|
}): Application;
|
|
8
10
|
}
|
|
9
11
|
export interface CreateAppClient {
|
|
10
12
|
(options: {
|
|
11
|
-
routes:
|
|
13
|
+
routes: IPageRouteRecord[];
|
|
12
14
|
appComponent: any;
|
|
13
15
|
appData: IAppData<any, IAppState>;
|
|
14
16
|
}): Application;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { IManifest } from '@shuvi/toolpack/lib/webpack/types';
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import type { ShuviRequest } from '@shuvi/service';
|
|
3
|
+
import { Response, IApplication, IAppData } from '@shuvi/platform-shared/shared';
|
|
4
|
+
export declare type IRenderViewOptions = {
|
|
4
5
|
app: IApplication;
|
|
5
|
-
req?: IRequest;
|
|
6
6
|
};
|
|
7
|
-
export interface IRenderOptions extends
|
|
7
|
+
export interface IRenderOptions extends IRenderViewOptions {
|
|
8
8
|
}
|
|
9
9
|
export interface IView<RenderOption extends IRenderOptions = any, RenderResult = void> {
|
|
10
10
|
renderApp(options: RenderOption): RenderResult;
|
|
@@ -35,8 +35,9 @@ export interface IClientRendererOptions<ExtraAppData = {}> extends IRenderOption
|
|
|
35
35
|
appData: IAppData<ExtraAppData>;
|
|
36
36
|
}
|
|
37
37
|
export interface IServerRendererOptions extends IRenderOptions {
|
|
38
|
+
req: ShuviRequest;
|
|
38
39
|
manifest: IManifest;
|
|
39
|
-
|
|
40
|
+
isDev?: boolean;
|
|
40
41
|
}
|
|
41
42
|
export interface IViewClient<ExtraAppData = {}> extends IView<IClientRendererOptions<ExtraAppData>> {
|
|
42
43
|
}
|
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { IRequestHandlerWithNext } from '@shuvi/service';
|
|
3
|
-
import { IResponse, IRequest } from '@shuvi/service';
|
|
1
|
+
import { ShuviRequest, ShuviResponse, ShuviRequestHandler } from '@shuvi/service';
|
|
4
2
|
export interface IApiReq {
|
|
5
|
-
pathname: string;
|
|
6
|
-
query: IURLQuery;
|
|
7
|
-
params: IURLParams;
|
|
8
3
|
cookies: {
|
|
9
4
|
[key: string]: string;
|
|
10
5
|
};
|
|
@@ -12,7 +7,7 @@ export interface IApiReq {
|
|
|
12
7
|
[key: string]: any;
|
|
13
8
|
};
|
|
14
9
|
}
|
|
15
|
-
export declare type IApiRequest =
|
|
10
|
+
export declare type IApiRequest = ShuviRequest & IApiReq;
|
|
16
11
|
export declare type Send<T> = (body: T) => void;
|
|
17
12
|
export declare type IApiRes<T = any> = {
|
|
18
13
|
send: Send<T>;
|
|
@@ -21,7 +16,7 @@ export declare type IApiRes<T = any> = {
|
|
|
21
16
|
redirect(url: string): IApiRes<T>;
|
|
22
17
|
redirect(status: number, url: string): IApiRes<T>;
|
|
23
18
|
};
|
|
24
|
-
export declare type IApiResponse<T = any> =
|
|
19
|
+
export declare type IApiResponse<T = any> = ShuviResponse & IApiRes<T>;
|
|
25
20
|
export declare type IApiRequestHandler<T = any> = (req: IApiRequest, res: IApiResponse<T>) => void | Promise<void>;
|
|
26
21
|
export interface IApiConfig {
|
|
27
22
|
api?: {
|
|
@@ -35,7 +30,7 @@ export interface IApiHandler {
|
|
|
35
30
|
config?: IApiConfig;
|
|
36
31
|
}
|
|
37
32
|
export interface IMiddlewareConfig {
|
|
38
|
-
default:
|
|
33
|
+
default: ShuviRequestHandler;
|
|
39
34
|
}
|
|
40
35
|
export declare type IApiRoutes = {
|
|
41
36
|
path: string;
|
|
@@ -8,19 +8,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import { getRoutes } from '@shuvi/app/core/platform';
|
|
11
|
-
import { runPreload, runLoaders, getRouteMatchesWithInvalidLoader,
|
|
12
|
-
import application from '@shuvi/platform-shared/shuvi-app/application';
|
|
11
|
+
import { runPreload, runLoaders, getRouteMatchesWithInvalidLoader, isRedirect, isResponse } from '@shuvi/platform-shared/shared';
|
|
12
|
+
import application, { Application } from '@shuvi/platform-shared/shuvi-app/application';
|
|
13
13
|
import { createRouter, createBrowserHistory, createHashHistory } from '@shuvi/router';
|
|
14
14
|
import pageLoaders from '@shuvi/app/files/page-loaders';
|
|
15
15
|
import { historyMode } from '@shuvi/app/files/routerConfig';
|
|
16
16
|
import { SHUVI_ERROR } from '@shuvi/shared/lib/constants';
|
|
17
|
+
import { serializeServerError } from '../helper/serializeServerError';
|
|
17
18
|
let app;
|
|
19
|
+
export { Application };
|
|
18
20
|
export const createApp = ({ routes, appData, appComponent }) => {
|
|
19
21
|
// app is a singleton in client side
|
|
20
22
|
if (app) {
|
|
21
23
|
return app;
|
|
22
24
|
}
|
|
23
|
-
const {
|
|
25
|
+
const { appState, ssr } = appData;
|
|
24
26
|
let history;
|
|
25
27
|
if (historyMode === 'hash') {
|
|
26
28
|
history = createHashHistory();
|
|
@@ -37,14 +39,14 @@ export const createApp = ({ routes, appData, appComponent }) => {
|
|
|
37
39
|
AppComponent: appComponent,
|
|
38
40
|
router
|
|
39
41
|
});
|
|
42
|
+
const loadersData = app.getLoadersData();
|
|
40
43
|
const hasHydrateData = Object.keys(loadersData).length > 0;
|
|
41
|
-
const loaderManager = getLoaderManager();
|
|
42
44
|
let shouldHydrate = ssr && hasHydrateData;
|
|
43
|
-
let hasServerError = app.error
|
|
45
|
+
let hasServerError = !!app.error;
|
|
44
46
|
router.beforeResolve((to, from, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
45
47
|
if (shouldHydrate) {
|
|
46
48
|
shouldHydrate = false;
|
|
47
|
-
|
|
49
|
+
app.setLoadersData(loadersData);
|
|
48
50
|
return next();
|
|
49
51
|
}
|
|
50
52
|
if (hasServerError) {
|
|
@@ -52,12 +54,11 @@ export const createApp = ({ routes, appData, appComponent }) => {
|
|
|
52
54
|
return next();
|
|
53
55
|
}
|
|
54
56
|
if (!to.matches.length) {
|
|
55
|
-
app.
|
|
57
|
+
app.setError(SHUVI_ERROR.PAGE_NOT_FOUND);
|
|
56
58
|
next();
|
|
57
59
|
return;
|
|
58
60
|
}
|
|
59
61
|
const matches = getRouteMatchesWithInvalidLoader(to, from, pageLoaders);
|
|
60
|
-
let isPreloadError = false;
|
|
61
62
|
try {
|
|
62
63
|
const loaderDatas = yield new Promise((resolve, reject) => {
|
|
63
64
|
let value;
|
|
@@ -78,11 +79,9 @@ export const createApp = ({ routes, appData, appComponent }) => {
|
|
|
78
79
|
runPreload(to)
|
|
79
80
|
.then(tryResolve)
|
|
80
81
|
.catch(err => {
|
|
81
|
-
isPreloadError = true;
|
|
82
82
|
reject(err);
|
|
83
83
|
});
|
|
84
84
|
runLoaders(matches, pageLoaders, {
|
|
85
|
-
isServer: false,
|
|
86
85
|
query: to.query,
|
|
87
86
|
getAppContext: () => app.context
|
|
88
87
|
})
|
|
@@ -95,7 +94,7 @@ export const createApp = ({ routes, appData, appComponent }) => {
|
|
|
95
94
|
tryResolve();
|
|
96
95
|
});
|
|
97
96
|
});
|
|
98
|
-
|
|
97
|
+
app.setLoadersData(loaderDatas);
|
|
99
98
|
}
|
|
100
99
|
catch (error) {
|
|
101
100
|
if (isRedirect(error)) {
|
|
@@ -103,22 +102,19 @@ export const createApp = ({ routes, appData, appComponent }) => {
|
|
|
103
102
|
return;
|
|
104
103
|
}
|
|
105
104
|
if (isResponse(error) && error.status >= 400 && error.status < 600) {
|
|
106
|
-
app.
|
|
105
|
+
app.setError({
|
|
107
106
|
code: error.status,
|
|
108
107
|
message: error.data
|
|
109
108
|
});
|
|
110
109
|
next();
|
|
111
110
|
return;
|
|
112
111
|
}
|
|
113
|
-
app.error.
|
|
114
|
-
code: SHUVI_ERROR.APP_ERROR.code,
|
|
115
|
-
message: error.message || isPreloadError ? 'Preload Error' : 'Loader Error'
|
|
116
|
-
});
|
|
112
|
+
app.setError(serializeServerError(error, process.env.NODE_ENV === 'development'));
|
|
117
113
|
next();
|
|
118
114
|
return;
|
|
119
115
|
}
|
|
120
116
|
next(() => {
|
|
121
|
-
app.
|
|
117
|
+
app.clearError();
|
|
122
118
|
});
|
|
123
119
|
}));
|
|
124
120
|
return app;
|
|
@@ -9,12 +9,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import routes from '@shuvi/app/files/routes';
|
|
11
11
|
import { getRoutes, app as AppComponent } from '@shuvi/app/core/platform';
|
|
12
|
-
import { runLoaders, getRouteMatchesWithInvalidLoader, isResponse, isRedirect
|
|
12
|
+
import { runLoaders, getRouteMatchesWithInvalidLoader, isResponse, isRedirect } from '@shuvi/platform-shared/shared';
|
|
13
13
|
import pageLoaders from '@shuvi/app/files/page-loaders';
|
|
14
14
|
import application from '@shuvi/platform-shared/shuvi-app/application';
|
|
15
15
|
import { createRouter, createMemoryHistory } from '@shuvi/router';
|
|
16
|
+
import chalk from '@shuvi/utils/lib/chalk';
|
|
17
|
+
import { serializeServerError } from '../helper/serializeServerError';
|
|
16
18
|
export const createApp = options => {
|
|
17
|
-
const { req, ssr } = options;
|
|
19
|
+
const { req, ssr, isDev } = options;
|
|
18
20
|
const history = createMemoryHistory({
|
|
19
21
|
initialEntries: [(req && req.url) || '/'],
|
|
20
22
|
initialIndex: 0
|
|
@@ -24,18 +26,16 @@ export const createApp = options => {
|
|
|
24
26
|
routes: getRoutes(routes)
|
|
25
27
|
});
|
|
26
28
|
let app;
|
|
27
|
-
const loaderManager = getLoaderManager();
|
|
28
29
|
if (ssr) {
|
|
29
30
|
router.beforeResolve((to, from, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
30
31
|
const matches = getRouteMatchesWithInvalidLoader(to, from, pageLoaders);
|
|
31
32
|
try {
|
|
32
33
|
const loaderResult = yield runLoaders(matches, pageLoaders, {
|
|
33
|
-
isServer: true,
|
|
34
34
|
req,
|
|
35
35
|
query: to.query,
|
|
36
36
|
getAppContext: () => app.context
|
|
37
37
|
});
|
|
38
|
-
|
|
38
|
+
app.setLoadersData(loaderResult);
|
|
39
39
|
}
|
|
40
40
|
catch (error) {
|
|
41
41
|
if (isRedirect(error)) {
|
|
@@ -43,16 +43,17 @@ export const createApp = options => {
|
|
|
43
43
|
return;
|
|
44
44
|
}
|
|
45
45
|
if (isResponse(error) && error.status >= 400 && error.status < 600) {
|
|
46
|
-
app.
|
|
46
|
+
app.setError({
|
|
47
47
|
code: error.status,
|
|
48
48
|
message: error.data
|
|
49
49
|
});
|
|
50
50
|
next();
|
|
51
51
|
return;
|
|
52
52
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
53
|
+
if (isDev) {
|
|
54
|
+
console.error(chalk.red('error') + ' - ' + error.stack);
|
|
55
|
+
}
|
|
56
|
+
app.setError(serializeServerError(error, isDev));
|
|
56
57
|
next();
|
|
57
58
|
return;
|
|
58
59
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function getEventSourceWrapper(options: any): any;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const eventCallbacks = [];
|
|
2
|
+
function EventSourceWrapper(options) {
|
|
3
|
+
var source;
|
|
4
|
+
var lastActivity = new Date();
|
|
5
|
+
var listeners = [];
|
|
6
|
+
if (!options.timeout) {
|
|
7
|
+
options.timeout = 20 * 1000;
|
|
8
|
+
}
|
|
9
|
+
init();
|
|
10
|
+
var timer = setInterval(function () {
|
|
11
|
+
if (new Date() - lastActivity > options.timeout) {
|
|
12
|
+
handleDisconnect();
|
|
13
|
+
}
|
|
14
|
+
}, options.timeout / 2);
|
|
15
|
+
function init() {
|
|
16
|
+
source = new window.EventSource(options.path);
|
|
17
|
+
source.onopen = handleOnline;
|
|
18
|
+
source.onerror = handleDisconnect;
|
|
19
|
+
source.onmessage = handleMessage;
|
|
20
|
+
}
|
|
21
|
+
function handleOnline() {
|
|
22
|
+
if (options.log)
|
|
23
|
+
console.log('[HMR] connected');
|
|
24
|
+
lastActivity = new Date();
|
|
25
|
+
}
|
|
26
|
+
function handleMessage(event) {
|
|
27
|
+
lastActivity = new Date();
|
|
28
|
+
for (var i = 0; i < listeners.length; i++) {
|
|
29
|
+
listeners[i](event);
|
|
30
|
+
}
|
|
31
|
+
if (event.data.indexOf('action') !== -1) {
|
|
32
|
+
eventCallbacks.forEach(cb => cb(event));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function handleDisconnect() {
|
|
36
|
+
clearInterval(timer);
|
|
37
|
+
source.close();
|
|
38
|
+
setTimeout(init, options.timeout);
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
onclose: () => {
|
|
42
|
+
clearTimeout(timer);
|
|
43
|
+
source.close();
|
|
44
|
+
},
|
|
45
|
+
addMessageListener: function (fn) {
|
|
46
|
+
listeners.push(fn);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
export function getEventSourceWrapper(options) {
|
|
51
|
+
if (!window.__whmEventSourceWrapper) {
|
|
52
|
+
window.__whmEventSourceWrapper = {};
|
|
53
|
+
}
|
|
54
|
+
if (!window.__whmEventSourceWrapper[options.path]) {
|
|
55
|
+
// cache the wrapper for other entries loaded on
|
|
56
|
+
// the same page with the same options.path
|
|
57
|
+
window.__whmEventSourceWrapper[options.path] = EventSourceWrapper(options);
|
|
58
|
+
}
|
|
59
|
+
return window.__whmEventSourceWrapper[options.path];
|
|
60
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
MIT License
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2013-present, Facebook, Inc.
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
export declare type HotDevClient = {
|
|
25
|
+
sendMessage: (data: any) => void;
|
|
26
|
+
subscribeToHmrEvent: (handler: any) => void;
|
|
27
|
+
};
|
|
28
|
+
export default function connect(options: {
|
|
29
|
+
launchEditorEndpoint: string;
|
|
30
|
+
path: string;
|
|
31
|
+
location: Location;
|
|
32
|
+
}): HotDevClient;
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
/* eslint-disable camelcase */
|
|
2
|
+
/**
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2013-present, Facebook, Inc.
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
|
24
|
+
*/
|
|
25
|
+
// This file is based on https://github.com/facebook/create-react-app/blob/v1.1.4/packages/react-dev-utils/webpackHotDevClient.js
|
|
26
|
+
// It's been edited to rely on webpack-hot-middleware.
|
|
27
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
28
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
29
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
30
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
31
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
32
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
33
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
// @ts-nocheck
|
|
37
|
+
import stripAnsi from 'strip-ansi';
|
|
38
|
+
import formatWebpackMessages from '@shuvi/toolpack/lib/utils/formatWebpackMessages';
|
|
39
|
+
import { startReportingRuntimeErrors, stopReportingRuntimeErrors, onBuildError, onBuildOk, onRefresh } from '@shuvi/error-overlay';
|
|
40
|
+
import { DEV_SOCKET_TIMEOUT_MS } from '@shuvi/shared/esm/constants';
|
|
41
|
+
import { connectHMR, addMessageListener, sendMessage } from './websocket';
|
|
42
|
+
// This alternative WebpackDevServer combines the functionality of:
|
|
43
|
+
// https://github.com/webpack/webpack-dev-server/blob/webpack-1/client/index.js
|
|
44
|
+
// https://github.com/webpack/webpack/blob/webpack-1/hot/dev-server.js
|
|
45
|
+
// It only supports their simplest configuration (hot updates on same server).
|
|
46
|
+
// It makes some opinionated choices on top, like adding a syntax error overlay
|
|
47
|
+
// that looks similar to our console output. The error overlay is inspired by:
|
|
48
|
+
// https://github.com/glenjamin/webpack-hot-middleware
|
|
49
|
+
// This is a modified version of create-react-app's webpackHotDevClient.js
|
|
50
|
+
// It implements webpack-hot-middleware's EventSource events instead of webpack-dev-server's websocket.
|
|
51
|
+
// https://github.com/facebook/create-react-app/blob/25184c4e91ebabd16fe1cde3d8630830e4a36a01/packages/react-dev-utils/webpackHotDevClient.js
|
|
52
|
+
let hadRuntimeError = false;
|
|
53
|
+
let customHmrEventHandler;
|
|
54
|
+
export default function connect(options) {
|
|
55
|
+
startReportingRuntimeErrors({
|
|
56
|
+
onError: function () {
|
|
57
|
+
hadRuntimeError = true;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
if (module.hot && typeof module.hot.dispose === 'function') {
|
|
61
|
+
module.hot.dispose(function () {
|
|
62
|
+
stopReportingRuntimeErrors();
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
connectHMR(Object.assign(Object.assign({}, options), { log: true }));
|
|
66
|
+
addMessageListener(event => {
|
|
67
|
+
// This is the heartbeat event
|
|
68
|
+
const obj = JSON.parse(event.data);
|
|
69
|
+
if (obj.action === 'pong') {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
processMessage(event);
|
|
74
|
+
}
|
|
75
|
+
catch (ex) {
|
|
76
|
+
console.warn('Invalid HMR message: ' + event.data + '\n' + ex);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
setInterval(() => {
|
|
80
|
+
sendMessage(JSON.stringify({ event: 'ping' }));
|
|
81
|
+
}, DEV_SOCKET_TIMEOUT_MS / 2);
|
|
82
|
+
return {
|
|
83
|
+
sendMessage(data) {
|
|
84
|
+
sendMessage(data);
|
|
85
|
+
},
|
|
86
|
+
subscribeToHmrEvent(handler) {
|
|
87
|
+
customHmrEventHandler = handler;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
// Remember some state related to hot module replacement.
|
|
92
|
+
var isFirstCompilation = true;
|
|
93
|
+
var mostRecentCompilationHash = null;
|
|
94
|
+
var hasCompileErrors = false;
|
|
95
|
+
function clearOutdatedErrors() {
|
|
96
|
+
// Clean up outdated compile errors, if any.
|
|
97
|
+
if (typeof console !== 'undefined' && typeof console.clear === 'function') {
|
|
98
|
+
if (hasCompileErrors) {
|
|
99
|
+
console.clear();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
let startLatency = undefined;
|
|
104
|
+
function onFastRefresh(hasUpdates) {
|
|
105
|
+
onBuildOk();
|
|
106
|
+
if (hasUpdates) {
|
|
107
|
+
onRefresh();
|
|
108
|
+
}
|
|
109
|
+
if (startLatency) {
|
|
110
|
+
const endLatency = Date.now();
|
|
111
|
+
const latency = endLatency - startLatency;
|
|
112
|
+
console.log(`[Fast Refresh] done in ${latency}ms`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// Successful compilation.
|
|
116
|
+
function handleSuccess() {
|
|
117
|
+
clearOutdatedErrors();
|
|
118
|
+
const isHotUpdate = !isFirstCompilation;
|
|
119
|
+
isFirstCompilation = false;
|
|
120
|
+
hasCompileErrors = false;
|
|
121
|
+
// Attempt to apply hot updates or reload.
|
|
122
|
+
if (isHotUpdate) {
|
|
123
|
+
tryApplyUpdates(function onHotUpdateSuccess(hasUpdates) {
|
|
124
|
+
// Only dismiss it when we're sure it's a hot update.
|
|
125
|
+
// Otherwise it would flicker right before the reload.
|
|
126
|
+
onFastRefresh(hasUpdates);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// Compilation with warnings (e.g. ESLint).
|
|
131
|
+
function handleWarnings(warnings) {
|
|
132
|
+
clearOutdatedErrors();
|
|
133
|
+
var isHotUpdate = !isFirstCompilation;
|
|
134
|
+
isFirstCompilation = false;
|
|
135
|
+
hasCompileErrors = false;
|
|
136
|
+
// Print warnings to the console.
|
|
137
|
+
const formatted = formatWebpackMessages({
|
|
138
|
+
warnings: warnings,
|
|
139
|
+
errors: []
|
|
140
|
+
});
|
|
141
|
+
if (typeof console !== 'undefined' && typeof console.warn === 'function') {
|
|
142
|
+
for (let i = 0; i < formatted.warnings.length; i++) {
|
|
143
|
+
if (i === 5) {
|
|
144
|
+
console.warn('There were more warnings in other files.\n' +
|
|
145
|
+
'You can find a complete log in the terminal.');
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
console.warn(stripAnsi(formatted.warnings[i]));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
// Attempt to apply hot updates or reload.
|
|
152
|
+
if (isHotUpdate) {
|
|
153
|
+
tryApplyUpdates(function onSuccessfulHotUpdate(hasUpdates) {
|
|
154
|
+
// Only dismiss it when we're sure it's a hot update.
|
|
155
|
+
// Otherwise it would flicker right before the reload.
|
|
156
|
+
onFastRefresh(hasUpdates);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// Compilation with errors (e.g. syntax error or missing modules).
|
|
161
|
+
function handleErrors(errors) {
|
|
162
|
+
if (!Boolean(errors && errors.length)) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
clearOutdatedErrors();
|
|
166
|
+
isFirstCompilation = false;
|
|
167
|
+
hasCompileErrors = true;
|
|
168
|
+
// "Massage" webpack messages.
|
|
169
|
+
var formatted = formatWebpackMessages({
|
|
170
|
+
errors: errors,
|
|
171
|
+
warnings: []
|
|
172
|
+
});
|
|
173
|
+
// Only show the first error.
|
|
174
|
+
onBuildError(formatted.errors[0]);
|
|
175
|
+
// Also log them to the console.
|
|
176
|
+
if (typeof console !== 'undefined' && typeof console.error === 'function') {
|
|
177
|
+
for (var i = 0; i < formatted.errors.length; i++) {
|
|
178
|
+
console.error(stripAnsi(formatted.errors[i]));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
// There is a newer version of the code available.
|
|
183
|
+
function handleAvailableHash(hash) {
|
|
184
|
+
// Update last known compilation hash.
|
|
185
|
+
mostRecentCompilationHash = hash;
|
|
186
|
+
}
|
|
187
|
+
// Handle messages from the server.
|
|
188
|
+
function processMessage(e) {
|
|
189
|
+
const obj = JSON.parse(e.data);
|
|
190
|
+
switch (obj.action) {
|
|
191
|
+
case 'building': {
|
|
192
|
+
startLatency = Date.now();
|
|
193
|
+
console.log('[Fast Refresh] bundle ' +
|
|
194
|
+
(obj.name ? "'" + obj.name + "' " : '') +
|
|
195
|
+
'rebuilding');
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
case 'built':
|
|
199
|
+
case 'sync': {
|
|
200
|
+
if (obj.hash) {
|
|
201
|
+
handleAvailableHash(obj.hash);
|
|
202
|
+
}
|
|
203
|
+
const { errors, warnings } = obj;
|
|
204
|
+
const hasErrors = Boolean(errors && errors.length);
|
|
205
|
+
if (hasErrors) {
|
|
206
|
+
return handleErrors(errors);
|
|
207
|
+
}
|
|
208
|
+
const hasWarnings = Boolean(warnings && warnings.length);
|
|
209
|
+
if (hasWarnings) {
|
|
210
|
+
return handleWarnings(warnings);
|
|
211
|
+
}
|
|
212
|
+
return handleSuccess();
|
|
213
|
+
}
|
|
214
|
+
case 'warnings':
|
|
215
|
+
handleWarnings(obj.data);
|
|
216
|
+
break;
|
|
217
|
+
case 'errors':
|
|
218
|
+
handleErrors(obj.data);
|
|
219
|
+
break;
|
|
220
|
+
default: {
|
|
221
|
+
if (customHmrEventHandler) {
|
|
222
|
+
customHmrEventHandler(obj);
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
// Is there a newer version of this code available?
|
|
230
|
+
function isUpdateAvailable() {
|
|
231
|
+
/* globals __webpack_hash__ */
|
|
232
|
+
// __webpack_hash__ is the hash of the current compilation.
|
|
233
|
+
// It's a global variable injected by Webpack.
|
|
234
|
+
return mostRecentCompilationHash !== __webpack_hash__;
|
|
235
|
+
}
|
|
236
|
+
// Webpack disallows updates in other states.
|
|
237
|
+
function canApplyUpdates() {
|
|
238
|
+
return module.hot.status() === 'idle';
|
|
239
|
+
}
|
|
240
|
+
function waitForReady() {
|
|
241
|
+
return new Promise((resolve, reject) => {
|
|
242
|
+
if (module.hot.status() !== 'prepare') {
|
|
243
|
+
reject();
|
|
244
|
+
}
|
|
245
|
+
const handler = status => {
|
|
246
|
+
module.hot.removeStatusHandler(handler);
|
|
247
|
+
if (status === 'ready') {
|
|
248
|
+
resolve();
|
|
249
|
+
}
|
|
250
|
+
if (status === 'abort' || status === 'fail') {
|
|
251
|
+
reject();
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
module.hot.addStatusHandler(handler);
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
// Attempt to update code on the fly, fall back to a hard reload.
|
|
258
|
+
function tryApplyUpdates(onHotUpdateSuccess) {
|
|
259
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
260
|
+
if (!module.hot) {
|
|
261
|
+
// HotModuleReplacementPlugin is not in Webpack configuration.
|
|
262
|
+
console.error('HotModuleReplacementPlugin is not in Webpack configuration.');
|
|
263
|
+
window.location.reload();
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
if (!isUpdateAvailable() || !canApplyUpdates()) {
|
|
267
|
+
onBuildOk();
|
|
268
|
+
// HMR failed, need to refresh
|
|
269
|
+
const hmrStatus = module.hot.status();
|
|
270
|
+
if (hmrStatus === 'abort' || hmrStatus === 'fail') {
|
|
271
|
+
window.location.reload();
|
|
272
|
+
}
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
function handleApplyUpdates(err, updatedModules) {
|
|
276
|
+
const hasUpdates = Boolean(updatedModules === null || updatedModules === void 0 ? void 0 : updatedModules.length);
|
|
277
|
+
if (hadRuntimeError) {
|
|
278
|
+
hadRuntimeError = false;
|
|
279
|
+
window.location.reload();
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
if (err) {
|
|
283
|
+
hadRuntimeError = true;
|
|
284
|
+
}
|
|
285
|
+
if (typeof onHotUpdateSuccess === 'function') {
|
|
286
|
+
// Maybe we want to do something.
|
|
287
|
+
onHotUpdateSuccess(hasUpdates);
|
|
288
|
+
}
|
|
289
|
+
if (isUpdateAvailable()) {
|
|
290
|
+
// While we were updating, there was a new update! Do it again.
|
|
291
|
+
tryApplyUpdates(hasUpdates ? onBuildOk : onHotUpdateSuccess);
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
onBuildOk();
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
try {
|
|
298
|
+
// https://webpack.js.org/api/hot-module-replacement/#check
|
|
299
|
+
let updatedModules = yield module.hot.check(/* autoApply */ false);
|
|
300
|
+
if (!updatedModules) {
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
// if there is another updating, delay the update
|
|
304
|
+
// multiple hotupdate occurs during import() will cause hmr error
|
|
305
|
+
// so we delay the adjacent hotupdates
|
|
306
|
+
// import() == loade module script ----> require(module)
|
|
307
|
+
// |
|
|
308
|
+
// |
|
|
309
|
+
// if applyUpdate happens here, require will cause a error
|
|
310
|
+
if (isUpdateAvailable()) {
|
|
311
|
+
yield new Promise(resolve => {
|
|
312
|
+
setTimeout(resolve, 50);
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
if (updatedModules) {
|
|
316
|
+
if (module.hot.status() !== 'ready') {
|
|
317
|
+
yield waitForReady();
|
|
318
|
+
}
|
|
319
|
+
updatedModules = yield module.hot.apply();
|
|
320
|
+
}
|
|
321
|
+
handleApplyUpdates(null, updatedModules);
|
|
322
|
+
}
|
|
323
|
+
catch (err) {
|
|
324
|
+
handleApplyUpdates(err, null);
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
}
|