@shuvi/platform-web 1.0.0-rc.4 → 1.0.0-rc.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/esm/shared/renderTypes.d.ts +2 -2
  2. package/esm/shuvi-app/app/client.js +10 -10
  3. package/esm/shuvi-app/app/server.js +4 -5
  4. package/esm/shuvi-app/react/AppContainer.d.ts +2 -3
  5. package/esm/shuvi-app/react/AppContainer.jsx +3 -4
  6. package/esm/shuvi-app/react/redox-react/RedoxWrapper.d.ts +2 -2
  7. package/esm/shuvi-app/react/redox-react/RedoxWrapper.jsx +3 -3
  8. package/esm/shuvi-app/react/redox-react/runtime.js +1 -1
  9. package/esm/shuvi-app/react/store.d.ts +5 -0
  10. package/esm/shuvi-app/react/store.js +3 -0
  11. package/esm/shuvi-app/react/types.d.ts +0 -7
  12. package/esm/shuvi-app/react/useLoaderData.js +11 -20
  13. package/esm/shuvi-app/react/view/ReactView.client.jsx +4 -4
  14. package/esm/shuvi-app/react/view/ReactView.server.jsx +4 -10
  15. package/lib/node/features/html-render/index.js +1 -1
  16. package/lib/node/features/html-render/lib/index.d.ts +0 -1
  17. package/lib/node/features/html-render/lib/index.js +1 -3
  18. package/lib/node/features/html-render/lib/renderToHTML.js +4 -47
  19. package/lib/node/features/html-render/lib/renderer/base.d.ts +4 -4
  20. package/lib/node/features/html-render/lib/renderer/index.d.ts +5 -4
  21. package/lib/node/features/html-render/lib/renderer/index.js +69 -6
  22. package/lib/node/features/html-render/lib/renderer/spa.d.ts +2 -2
  23. package/lib/node/features/html-render/lib/renderer/spa.js +2 -4
  24. package/lib/node/features/html-render/lib/renderer/ssr.d.ts +2 -2
  25. package/lib/node/features/html-render/lib/renderer/ssr.js +2 -2
  26. package/lib/node/features/html-render/lib/renderer/types.d.ts +6 -4
  27. package/lib/node/features/index.js +1 -3
  28. package/lib/node/shuvi-runtime-server.d.ts +18 -0
  29. package/lib/node/shuvi-runtime-server.js +2 -0
  30. package/lib/node/shuvi-type-extensions-node.d.ts +0 -6
  31. package/lib/shared/renderTypes.d.ts +2 -2
  32. package/package.json +11 -11
  33. package/esm/shuvi-app/shuvi-runtime-server.d.ts +0 -6
  34. package/esm/shuvi-app/shuvi-runtime-server.js +0 -1
  35. package/lib/node/features/webpack-watch-wait-for-file-builder/index.d.ts +0 -16
  36. package/lib/node/features/webpack-watch-wait-for-file-builder/index.js +0 -25
  37. package/lib/node/features/webpack-watch-wait-for-file-builder/webpack-watch-wait-for-file-builder-plugin.d.ts +0 -12
  38. package/lib/node/features/webpack-watch-wait-for-file-builder/webpack-watch-wait-for-file-builder-plugin.js +0 -32
@@ -1,10 +1,10 @@
1
1
  import { IManifest } from '@shuvi/toolpack/lib/webpack/types';
2
2
  import { Response, IApplication, IRequest, IAppData } from '@shuvi/platform-shared/shared';
3
- export declare type IRenderDocumentOptions = {
3
+ export declare type IRenderViewOptions = {
4
4
  app: IApplication;
5
5
  req?: IRequest;
6
6
  };
7
- export interface IRenderOptions extends IRenderDocumentOptions {
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;
@@ -8,7 +8,7 @@ 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, getLoaderManager, isRedirect, isResponse } from '@shuvi/platform-shared/shared';
11
+ import { runPreload, runLoaders, getRouteMatchesWithInvalidLoader, isRedirect, isResponse } from '@shuvi/platform-shared/shared';
12
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';
@@ -21,7 +21,7 @@ export const createApp = ({ routes, appData, appComponent }) => {
21
21
  if (app) {
22
22
  return app;
23
23
  }
24
- const { loadersData = {}, appState, ssr } = appData;
24
+ const { appState, ssr } = appData;
25
25
  let history;
26
26
  if (historyMode === 'hash') {
27
27
  history = createHashHistory();
@@ -38,14 +38,14 @@ export const createApp = ({ routes, appData, appComponent }) => {
38
38
  AppComponent: appComponent,
39
39
  router
40
40
  });
41
+ const loadersData = app.getLoadersData();
41
42
  const hasHydrateData = Object.keys(loadersData).length > 0;
42
- const loaderManager = getLoaderManager();
43
43
  let shouldHydrate = ssr && hasHydrateData;
44
- let hasServerError = app.error.hasError;
44
+ let hasServerError = !!app.error;
45
45
  router.beforeResolve((to, from, next) => __awaiter(void 0, void 0, void 0, function* () {
46
46
  if (shouldHydrate) {
47
47
  shouldHydrate = false;
48
- loaderManager.setDatas(loadersData);
48
+ app.setLoadersData(loadersData);
49
49
  return next();
50
50
  }
51
51
  if (hasServerError) {
@@ -53,7 +53,7 @@ export const createApp = ({ routes, appData, appComponent }) => {
53
53
  return next();
54
54
  }
55
55
  if (!to.matches.length) {
56
- app.error.error(SHUVI_ERROR.PAGE_NOT_FOUND);
56
+ app.setError(SHUVI_ERROR.PAGE_NOT_FOUND);
57
57
  next();
58
58
  return;
59
59
  }
@@ -96,7 +96,7 @@ export const createApp = ({ routes, appData, appComponent }) => {
96
96
  tryResolve();
97
97
  });
98
98
  });
99
- loaderManager.setDatas(loaderDatas);
99
+ app.setLoadersData(loaderDatas);
100
100
  }
101
101
  catch (error) {
102
102
  if (isRedirect(error)) {
@@ -104,14 +104,14 @@ export const createApp = ({ routes, appData, appComponent }) => {
104
104
  return;
105
105
  }
106
106
  if (isResponse(error) && error.status >= 400 && error.status < 600) {
107
- app.error.error({
107
+ app.setError({
108
108
  code: error.status,
109
109
  message: error.data
110
110
  });
111
111
  next();
112
112
  return;
113
113
  }
114
- app.error.error({
114
+ app.setError({
115
115
  code: SHUVI_ERROR.APP_ERROR.code,
116
116
  message: error.message || isPreloadError ? 'Preload Error' : 'Loader Error'
117
117
  });
@@ -119,7 +119,7 @@ export const createApp = ({ routes, appData, appComponent }) => {
119
119
  return;
120
120
  }
121
121
  next(() => {
122
- app.error.clear();
122
+ app.clearError();
123
123
  });
124
124
  }));
125
125
  return app;
@@ -9,7 +9,7 @@ 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, getLoaderManager } from '@shuvi/platform-shared/shared';
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';
@@ -24,7 +24,6 @@ export const createApp = options => {
24
24
  routes: getRoutes(routes)
25
25
  });
26
26
  let app;
27
- const loaderManager = getLoaderManager();
28
27
  if (ssr) {
29
28
  router.beforeResolve((to, from, next) => __awaiter(void 0, void 0, void 0, function* () {
30
29
  const matches = getRouteMatchesWithInvalidLoader(to, from, pageLoaders);
@@ -35,7 +34,7 @@ export const createApp = options => {
35
34
  query: to.query,
36
35
  getAppContext: () => app.context
37
36
  });
38
- loaderManager.setDatas(loaderResult);
37
+ app.setLoadersData(loaderResult);
39
38
  }
40
39
  catch (error) {
41
40
  if (isRedirect(error)) {
@@ -43,14 +42,14 @@ export const createApp = options => {
43
42
  return;
44
43
  }
45
44
  if (isResponse(error) && error.status >= 400 && error.status < 600) {
46
- app.error.error({
45
+ app.setError({
47
46
  code: error.status,
48
47
  message: error.data
49
48
  });
50
49
  next();
51
50
  return;
52
51
  }
53
- app.error.error({
52
+ app.setError({
54
53
  message: error.message || 'Loader Error'
55
54
  });
56
55
  next();
@@ -1,6 +1,5 @@
1
1
  import * as React from 'react';
2
2
  import { IApplication } from '@shuvi/platform-shared/shared';
3
- export default function AppContainer({ children, app }: {
3
+ export default function AppContainer({ app, children }: React.PropsWithChildren<{
4
4
  app: IApplication;
5
- children: React.ReactElement;
6
- }): JSX.Element;
5
+ }>): JSX.Element;
@@ -1,10 +1,9 @@
1
1
  import * as React from 'react';
2
2
  import { errorModel } from '@shuvi/platform-shared/shared';
3
- import { createContainer } from '@shuvi/redox-react';
4
3
  import { AppProvider } from './applicationContext';
5
4
  import ErrorPage from './ErrorPage';
6
5
  import { ErrorBoundary } from './ErrorBoundary';
7
- const { Provider, useSharedModel } = createContainer();
6
+ import { Provider, useSharedModel } from './store';
8
7
  function ErrorGuard({ children = null }) {
9
8
  const [errorState] = useSharedModel(errorModel);
10
9
  if (errorState.error !== undefined) {
@@ -12,10 +11,10 @@ function ErrorGuard({ children = null }) {
12
11
  }
13
12
  return <>{children}</>;
14
13
  }
15
- export default function AppContainer({ children, app }) {
14
+ export default function AppContainer({ app, children }) {
16
15
  return (<ErrorBoundary>
17
16
  <AppProvider app={app}>
18
- <Provider storeManager={app.storeManager}>
17
+ <Provider storeManager={app.store}>
19
18
  <ErrorGuard>{children}</ErrorGuard>
20
19
  </Provider>
21
20
  </AppProvider>
@@ -1,8 +1,8 @@
1
1
  /// <reference types="react" />
2
2
  import type { IStoreManager } from '@shuvi/redox';
3
3
  export declare const RedoxWrapper: (App: any, appContext: {
4
- storeManager: IStoreManager;
4
+ store: IStoreManager;
5
5
  }) => {
6
- (appProps: any): JSX.Element;
6
+ (): JSX.Element;
7
7
  displayName: string;
8
8
  };
@@ -1,9 +1,9 @@
1
1
  import * as React from 'react';
2
2
  import { RedoxRoot } from '@shuvi/redox-react';
3
3
  export const RedoxWrapper = (App, appContext) => {
4
- function RedoxAppWrapper(appProps) {
5
- return (<RedoxRoot storeManager={appContext.storeManager}>
6
- <App {...appProps}/>
4
+ function RedoxAppWrapper() {
5
+ return (<RedoxRoot storeManager={appContext.store}>
6
+ <App />
7
7
  </RedoxRoot>);
8
8
  }
9
9
  RedoxAppWrapper.displayName = 'RedoxAppWrapper';
@@ -12,7 +12,7 @@ import { RedoxWrapper } from './RedoxWrapper';
12
12
  export default createRuntimePlugin({
13
13
  appComponent: (App, appContext) => __awaiter(void 0, void 0, void 0, function* () {
14
14
  return RedoxWrapper(App, {
15
- storeManager: appContext.store
15
+ store: appContext.store
16
16
  });
17
17
  })
18
18
  });
@@ -0,0 +1,5 @@
1
+ /// <reference types="react" />
2
+ declare const Provider: (props: import("react").PropsWithChildren<{
3
+ storeManager?: import("@shuvi/redox").IStoreManager | undefined;
4
+ }>) => JSX.Element, useSharedModel: import("@shuvi/redox-react/esm/types").IUseModel;
5
+ export { Provider, useSharedModel };
@@ -0,0 +1,3 @@
1
+ import { createContainer } from '@shuvi/redox-react';
2
+ const { Provider, useSharedModel } = createContainer();
3
+ export { Provider, useSharedModel };
@@ -1,14 +1,7 @@
1
- import { IAppState } from '@shuvi/platform-shared/shared';
2
1
  import { IHtmlTag, IViewClient, IViewServer } from '../../shared';
3
2
  export { IHtmlTag };
4
3
  export declare type IReactAppData = {
5
- appProps?: Record<string, any>;
6
- errorProps?: {
7
- notFound: boolean;
8
- };
9
4
  dynamicIds?: Array<string | number>;
10
- appState?: IAppState;
11
- loadersData: any;
12
5
  };
13
6
  export declare type IReactServerView = IViewServer<IReactAppData>;
14
7
  export declare type IReactClientView = IViewClient<IReactAppData>;
@@ -1,27 +1,18 @@
1
1
  import { useMatchedRoute } from '@shuvi/router-react';
2
- import { getLoaderManager } from '@shuvi/platform-shared/shared';
3
- import { useRef, useEffect, useReducer } from 'react';
2
+ import { loaderModel } from '@shuvi/platform-shared/shared';
3
+ import { useSharedModel } from './store';
4
4
  export const noLoaderMessage = 'Warning: no loader found. Please make sure the page component where `useLoaderData` is called has a `loader` export.';
5
5
  export const useLoaderData = () => {
6
- var _a;
7
6
  const currentMatch = useMatchedRoute();
8
- const loaderManager = getLoaderManager();
9
- const id = (_a = currentMatch.route) === null || _a === void 0 ? void 0 : _a.id;
10
- const data = loaderManager.getData(id);
11
- if (data === null) {
7
+ const id = currentMatch.route.id;
8
+ const [loader] = useSharedModel(loaderModel, s => {
9
+ return {
10
+ hasLoader: Object.prototype.hasOwnProperty.call(s.dataByRouteId, id),
11
+ data: s.dataByRouteId[id]
12
+ };
13
+ }, [id]);
14
+ if (!loader.hasLoader) {
12
15
  throw Error(noLoaderMessage);
13
16
  }
14
- const dataRef = useRef(data);
15
- const [_, forceUpdate] = useReducer(state => state * -1, 1);
16
- useEffect(() => {
17
- const cancel = loaderManager.subscribe(() => {
18
- const newData = loaderManager.getData(id);
19
- if (newData !== data) {
20
- dataRef.current = newData;
21
- forceUpdate();
22
- }
23
- });
24
- return cancel;
25
- }, []);
26
- return dataRef.current;
17
+ return loader.data;
27
18
  };
@@ -20,8 +20,8 @@ export class ReactClientView {
20
20
  this._isInitialRender = true;
21
21
  this.renderApp = ({ appContainer, app, appData }) => __awaiter(this, void 0, void 0, function* () {
22
22
  const { _isInitialRender: isInitialRender } = this;
23
- const { router, appComponent: AppComponent, error } = app;
24
- let { ssr, appProps, dynamicIds } = appData;
23
+ const { router, appComponent: AppComponent, setError: setAppError } = app;
24
+ let { ssr, dynamicIds } = appData;
25
25
  // For e2e test
26
26
  if (window.__SHUVI) {
27
27
  window.__SHUVI.router = router;
@@ -39,13 +39,13 @@ export class ReactClientView {
39
39
  const { matches } = router.current;
40
40
  if (!matches.length) {
41
41
  // no handler no matches
42
- error.error(SHUVI_ERROR.PAGE_NOT_FOUND);
42
+ setAppError(SHUVI_ERROR.PAGE_NOT_FOUND);
43
43
  }
44
44
  }
45
45
  const root = (<Router router={router}>
46
46
  <AppContainer app={app}>
47
47
  <HeadManagerContext.Provider value={headManager.updateHead}>
48
- <TypedAppComponent {...appProps}/>
48
+ <TypedAppComponent />
49
49
  </HeadManagerContext.Provider>
50
50
  </AppContainer>
51
51
  </Router>);
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import * as React from 'react';
11
11
  import { renderToString } from 'react-dom/server';
12
- import { getLoaderManager, redirect } from '@shuvi/platform-shared/shared';
12
+ import { redirect } from '@shuvi/platform-shared/shared';
13
13
  import { SHUVI_ERROR } from '@shuvi/shared/lib/constants';
14
14
  import { Router } from '@shuvi/router-react';
15
15
  import Loadable, { LoadableContext } from '../loadable';
@@ -19,20 +19,17 @@ export class ReactServerView {
19
19
  constructor() {
20
20
  this.renderApp = ({ app, manifest, getAssetPublicUrl }) => __awaiter(this, void 0, void 0, function* () {
21
21
  yield Loadable.preloadAll();
22
- const { storeManager, router, error: appError, appComponent: AppComponent } = app;
22
+ const { router, appComponent: AppComponent, setError: setAppError } = app;
23
23
  yield router.ready;
24
24
  // todo: move these into renderer
25
25
  let { pathname, matches, redirected } = router.current;
26
26
  // handler no matches
27
27
  if (!matches.length) {
28
- appError.error(SHUVI_ERROR.PAGE_NOT_FOUND);
28
+ setAppError(SHUVI_ERROR.PAGE_NOT_FOUND);
29
29
  }
30
30
  if (redirected) {
31
31
  return redirect(pathname);
32
32
  }
33
- // todo: move loader into app, avoid using global module
34
- const loaderManager = getLoaderManager();
35
- const loadersData = yield loaderManager.getAllData();
36
33
  const loadableModules = [];
37
34
  let htmlContent;
38
35
  let head;
@@ -47,7 +44,6 @@ export class ReactServerView {
47
44
  htmlContent = renderToString(RootApp);
48
45
  }
49
46
  finally {
50
- loaderManager.clearAllData();
51
47
  head = Head.rewind() || [];
52
48
  }
53
49
  const { loadble } = manifest;
@@ -88,13 +84,11 @@ export class ReactServerView {
88
84
  }
89
85
  }
90
86
  const appData = {
91
- dynamicIds: [...dynamicImportIdSet],
92
- loadersData
87
+ dynamicIds: [...dynamicImportIdSet]
93
88
  };
94
89
  if (dynamicImportIdSet.size) {
95
90
  appData.dynamicIds = Array.from(dynamicImportIdSet);
96
91
  }
97
- appData.appState = storeManager.getState();
98
92
  return {
99
93
  appData,
100
94
  content: htmlContent,
@@ -74,7 +74,7 @@ const getPlugin = (platformContext) => {
74
74
  exported: '*'
75
75
  },
76
76
  {
77
- source: (0, paths_1.resolvePkgFile)('esm/shuvi-app/shuvi-runtime-server'),
77
+ source: (0, paths_1.resolvePkgFile)('lib/node/shuvi-runtime-server'),
78
78
  filepath: 'server.ts',
79
79
  exported: '*'
80
80
  }
@@ -1,4 +1,3 @@
1
1
  export { getPageMiddleware } from './getPageMiddleware';
2
- export { renderToHTML } from './renderToHTML';
3
2
  export * from './renderer';
4
3
  export * from './pageLoader';
@@ -14,10 +14,8 @@ 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.renderToHTML = exports.getPageMiddleware = void 0;
17
+ exports.getPageMiddleware = void 0;
18
18
  var getPageMiddleware_1 = require("./getPageMiddleware");
19
19
  Object.defineProperty(exports, "getPageMiddleware", { enumerable: true, get: function () { return getPageMiddleware_1.getPageMiddleware; } });
20
- var renderToHTML_1 = require("./renderToHTML");
21
- Object.defineProperty(exports, "renderToHTML", { enumerable: true, get: function () { return renderToHTML_1.renderToHTML; } });
22
20
  __exportStar(require("./renderer"), exports);
23
21
  __exportStar(require("./pageLoader"), exports);
@@ -11,38 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.renderToHTML = void 0;
13
13
  const resources_1 = require("@shuvi/service/lib/resources");
14
- const shared_1 = require("@shuvi/platform-shared/shared");
15
14
  const renderer_1 = require("./renderer");
16
- const htmlTag_1 = require("./renderer/htmlTag");
17
- function addEssentialTagsIfMissing(document) {
18
- let hasMetaCharset = false;
19
- let hasMetaViewport = false;
20
- for (const { tagName, attrs } of document.headTags) {
21
- if (hasMetaCharset && hasMetaViewport) {
22
- break;
23
- }
24
- if (tagName === 'meta') {
25
- if (attrs.charset) {
26
- hasMetaCharset = true;
27
- }
28
- else if (attrs.name === 'viewport') {
29
- hasMetaViewport = true;
30
- }
31
- }
32
- }
33
- if (!hasMetaCharset) {
34
- document.headTags.unshift((0, htmlTag_1.tag)('meta', {
35
- charset: 'utf-8'
36
- }));
37
- }
38
- if (!hasMetaViewport) {
39
- document.headTags.unshift((0, htmlTag_1.tag)('meta', {
40
- name: 'viewport',
41
- content: 'width=device-width,minimum-scale=1,initial-scale=1'
42
- }));
43
- }
44
- return document;
45
- }
46
15
  function renderToHTML({ req, serverPluginContext }) {
47
16
  return __awaiter(this, void 0, void 0, function* () {
48
17
  let result;
@@ -54,23 +23,11 @@ function renderToHTML({ req, serverPluginContext }) {
54
23
  });
55
24
  try {
56
25
  yield app.init();
57
- const publicApp = app.getPublicAPI();
58
- let doc = yield renderer.renderDocument({
59
- app: publicApp,
60
- req
26
+ result = yield renderer.renderView({
27
+ req,
28
+ app: app.getPublicAPI(),
29
+ ssr: serverPluginContext.config.ssr
61
30
  });
62
- if ((0, shared_1.isResponse)(doc)) {
63
- result = doc;
64
- }
65
- else {
66
- addEssentialTagsIfMissing(doc);
67
- yield serverPluginContext.serverPluginRunner.modifyHtml(doc, app.context);
68
- const htmlStr = renderer.renderDocumentToString(doc);
69
- const appError = app.error.getError;
70
- result = (0, shared_1.text)(htmlStr, {
71
- status: appError && typeof appError.code !== 'undefined' ? appError.code : 200
72
- });
73
- }
74
31
  }
75
32
  finally {
76
33
  yield app.dispose();
@@ -1,13 +1,13 @@
1
- import { IAppData, Response } from '@shuvi/platform-shared/shared';
1
+ import { IAppData } from '@shuvi/platform-shared/shared';
2
2
  import { IServerPluginContext } from '@shuvi/service';
3
- import { IHtmlDocument, IHtmlTag, IApplication } from './types';
4
- import { IRendererConstructorOptions, IRenderDocumentOptions } from './types';
3
+ import { IHtmlTag, IApplication } from './types';
4
+ import { IRendererConstructorOptions, IRenderViewOptions, IRenderDocumentResult } from './types';
5
5
  export declare type AppData = Omit<IAppData, 'filesByRoutId' | 'publicPath'>;
6
6
  export declare abstract class BaseRenderer {
7
7
  protected _serverPluginContext: IServerPluginContext;
8
8
  protected _app?: IApplication;
9
9
  constructor({ serverPluginContext }: IRendererConstructorOptions);
10
- abstract renderDocument({ app, req }: IRenderDocumentOptions): Promise<IHtmlDocument | Response> | IHtmlDocument | Response;
10
+ abstract renderDocument({ app, req }: IRenderViewOptions): IRenderDocumentResult;
11
11
  protected _getMainAssetTags(): {
12
12
  styles: IHtmlTag<any>[];
13
13
  scripts: IHtmlTag<any>[];
@@ -1,5 +1,5 @@
1
- import { IRendererConstructorOptions, IRenderDocumentOptions } from './types';
2
- import { IHtmlDocument } from './types';
1
+ import { Response } from '@shuvi/platform-shared/shared';
2
+ import { IRendererConstructorOptions, IRenderViewOptions } from './types';
3
3
  export * from './types';
4
4
  export interface ITemplateData {
5
5
  [x: string]: any;
@@ -10,6 +10,7 @@ export declare class Renderer {
10
10
  private _ssrRenderer;
11
11
  private _spaRenderer;
12
12
  constructor(options: IRendererConstructorOptions);
13
- renderDocument(options: IRenderDocumentOptions): import("@shuvi/platform-shared/shared").Response | IHtmlDocument | Promise<import("@shuvi/platform-shared/shared").Response | IHtmlDocument>;
14
- renderDocumentToString(document: IHtmlDocument, templateData?: ITemplateData): string;
13
+ renderView(options: IRenderViewOptions): Promise<Response>;
14
+ private _renderDocument;
15
+ private _renderDocumentToString;
15
16
  }
@@ -13,14 +13,54 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
13
13
  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
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
+ return new (P || (P = Promise))(function (resolve, reject) {
19
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
23
+ });
24
+ };
16
25
  Object.defineProperty(exports, "__esModule", { value: true });
17
26
  exports.Renderer = void 0;
18
27
  const resources_1 = require("@shuvi/service/lib/resources");
28
+ const shared_1 = require("@shuvi/platform-shared/shared");
19
29
  const htmlTag_1 = require("./htmlTag");
20
30
  const viewTemplate_1 = require("../viewTemplate");
21
31
  const spa_1 = require("./spa");
22
32
  const ssr_1 = require("./ssr");
33
+ const htmlTag_2 = require("./htmlTag");
23
34
  __exportStar(require("./types"), exports);
35
+ function addEssentialTagsIfMissing(document) {
36
+ let hasMetaCharset = false;
37
+ let hasMetaViewport = false;
38
+ for (const { tagName, attrs } of document.headTags) {
39
+ if (hasMetaCharset && hasMetaViewport) {
40
+ break;
41
+ }
42
+ if (tagName === 'meta') {
43
+ if (attrs.charset) {
44
+ hasMetaCharset = true;
45
+ }
46
+ else if (attrs.name === 'viewport') {
47
+ hasMetaViewport = true;
48
+ }
49
+ }
50
+ }
51
+ if (!hasMetaCharset) {
52
+ document.headTags.unshift((0, htmlTag_2.tag)('meta', {
53
+ charset: 'utf-8'
54
+ }));
55
+ }
56
+ if (!hasMetaViewport) {
57
+ document.headTags.unshift((0, htmlTag_2.tag)('meta', {
58
+ name: 'viewport',
59
+ content: 'width=device-width,minimum-scale=1,initial-scale=1'
60
+ }));
61
+ }
62
+ return document;
63
+ }
24
64
  class Renderer {
25
65
  constructor(options) {
26
66
  this._serverPluginContext = options.serverPluginContext;
@@ -28,13 +68,36 @@ class Renderer {
28
68
  this._ssrRenderer = new ssr_1.SsrRenderer(options);
29
69
  this._spaRenderer = new spa_1.SpaRenderer(options);
30
70
  }
31
- renderDocument(options) {
32
- if (this._serverPluginContext.config.ssr) {
33
- return this._ssrRenderer.renderDocument(options);
34
- }
35
- return this._spaRenderer.renderDocument(options);
71
+ renderView(options) {
72
+ var _a, _b;
73
+ return __awaiter(this, void 0, void 0, function* () {
74
+ let result;
75
+ const { app } = options;
76
+ const doc = yield this._renderDocument(options);
77
+ if ((0, shared_1.isResponse)(doc)) {
78
+ result = doc;
79
+ }
80
+ else {
81
+ addEssentialTagsIfMissing(doc);
82
+ yield this._serverPluginContext.serverPluginRunner.modifyHtml(doc, app.context);
83
+ const htmlStr = this._renderDocumentToString(doc);
84
+ result = (0, shared_1.text)(htmlStr, {
85
+ status: (_b = (_a = app.error) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : 200
86
+ });
87
+ }
88
+ return result;
89
+ });
90
+ }
91
+ _renderDocument(options) {
92
+ return __awaiter(this, void 0, void 0, function* () {
93
+ // todo: fallback to spa
94
+ if (options.ssr) {
95
+ return yield this._ssrRenderer.renderDocument(options);
96
+ }
97
+ return yield this._spaRenderer.renderDocument(options);
98
+ });
36
99
  }
37
- renderDocumentToString(document, templateData = {}) {
100
+ _renderDocumentToString(document, templateData = {}) {
38
101
  const htmlAttrs = (0, htmlTag_1.stringifyAttrs)(document.htmlAttrs);
39
102
  const head = document.headTags.map(tag => (0, htmlTag_1.stringifyTag)(tag)).join('');
40
103
  const main = document.mainTags.map(tag => (0, htmlTag_1.stringifyTag)(tag)).join('');
@@ -1,5 +1,5 @@
1
1
  import { BaseRenderer } from './base';
2
- import { IRenderDocumentOptions, IHtmlDocument } from './types';
2
+ import { IRenderViewOptions, IHtmlDocument } from './types';
3
3
  export declare class SpaRenderer extends BaseRenderer {
4
- renderDocument({ app }: IRenderDocumentOptions): IHtmlDocument;
4
+ renderDocument({ app }: IRenderViewOptions): IHtmlDocument;
5
5
  }
@@ -5,11 +5,9 @@ const base_1 = require("./base");
5
5
  class SpaRenderer extends base_1.BaseRenderer {
6
6
  renderDocument({ app }) {
7
7
  const assets = this._getMainAssetTags();
8
- const serverPluginContext = this._serverPluginContext;
9
8
  const appData = {
10
- pageData: {},
11
- ssr: serverPluginContext.config.ssr,
12
- loadersData: {}
9
+ ssr: false,
10
+ pageData: {}
13
11
  };
14
12
  const document = {
15
13
  htmlAttrs: {},
@@ -1,5 +1,5 @@
1
1
  import { BaseRenderer } from './base';
2
- import { IHtmlDocument, IRenderDocumentOptions } from './types';
2
+ import { IHtmlDocument, IRenderViewOptions } from './types';
3
3
  export declare class SsrRenderer extends BaseRenderer {
4
- renderDocument({ app, req }: IRenderDocumentOptions): Promise<import("@shuvi/platform-shared/shared").Response | IHtmlDocument>;
4
+ renderDocument({ app, req }: IRenderViewOptions): Promise<import("@shuvi/platform-shared/shared").Response | IHtmlDocument>;
5
5
  }
@@ -19,7 +19,7 @@ const htmlTag_1 = require("./htmlTag");
19
19
  class SsrRenderer extends base_1.BaseRenderer {
20
20
  renderDocument({ app, req }) {
21
21
  return __awaiter(this, void 0, void 0, function* () {
22
- const { router, context } = app;
22
+ const { store, router, context } = app;
23
23
  const serverPluginContext = this._serverPluginContext;
24
24
  const { view } = resources_1.server;
25
25
  const { getAssetPublicUrl } = serverPluginContext;
@@ -41,7 +41,7 @@ class SsrRenderer extends base_1.BaseRenderer {
41
41
  Object.assign(acc, data);
42
42
  return acc;
43
43
  }, {});
44
- const appData = Object.assign(Object.assign({}, result.appData), { pageData, ssr: serverPluginContext.config.ssr });
44
+ const appData = Object.assign(Object.assign({}, result.appData), { ssr: true, appState: store.getState(), pageData });
45
45
  appData.runtimeConfig = (0, shuvi_singleton_runtimeConfig_1.getPublicRuntimeConfig)() || {};
46
46
  const document = {
47
47
  htmlAttrs: Object.assign({}, result.htmlAttrs),
@@ -1,20 +1,22 @@
1
- import { IApplication, IRequest } from '@shuvi/platform-shared/shared';
2
- import { IHtmlAttrs, IHtmlTag, IClientRendererOptions, IServerRendererOptions, IViewClient, IViewServer, IRenderAppServerResult } from '../../../../../shared';
1
+ import { IApplication, IRequest, Response } from '@shuvi/platform-shared/shared';
3
2
  import { IServerPluginContext } from '@shuvi/service';
3
+ import { IHtmlAttrs, IHtmlTag, IClientRendererOptions, IServerRendererOptions, IViewClient, IViewServer, IRenderAppServerResult } from '../../../../../shared';
4
4
  export { IHtmlAttrs, IHtmlTag, IApplication };
5
5
  export interface IRendererConstructorOptions {
6
6
  serverPluginContext: IServerPluginContext;
7
7
  }
8
- export declare type IRenderDocumentOptions = {
8
+ export declare type IRenderViewOptions = {
9
9
  app: IApplication;
10
10
  req: IRequest;
11
+ ssr: boolean;
11
12
  };
13
+ export declare type IRenderDocumentResult = Promise<IHtmlDocument | Response> | IHtmlDocument | Response;
12
14
  export interface IHtmlDocument {
13
15
  htmlAttrs: IHtmlAttrs;
14
16
  headTags: IHtmlTag<'meta' | 'link' | 'style' | 'script' | 'noscript' | 'title'>[];
15
17
  mainTags: IHtmlTag[];
16
18
  scriptTags: IHtmlTag<'script'>[];
17
19
  }
18
- export interface IRenderOptions extends IRenderDocumentOptions {
20
+ export interface IRenderOptions extends IRenderViewOptions {
19
21
  }
20
22
  export { IRenderAppServerResult, IClientRendererOptions, IServerRendererOptions, IViewClient, IViewServer };
@@ -9,7 +9,6 @@ const html_render_1 = require("./html-render");
9
9
  const custom_server_1 = __importDefault(require("./custom-server"));
10
10
  const model_1 = __importDefault(require("./model"));
11
11
  const filesystem_routes_1 = __importDefault(require("./filesystem-routes"));
12
- const webpack_watch_wait_for_file_builder_1 = __importDefault(require("./webpack-watch-wait-for-file-builder"));
13
12
  var buildHtml_1 = require("./html-render/lib/buildHtml");
14
13
  Object.defineProperty(exports, "buildHtml", { enumerable: true, get: function () { return buildHtml_1.buildHtml; } });
15
14
  var middlewares_1 = require("./middlewares");
@@ -20,7 +19,6 @@ const getPlugins = (platformContext) => [
20
19
  on_demand_compile_page_1.default,
21
20
  filesystem_routes_1.default,
22
21
  custom_server_1.default,
23
- model_1.default,
24
- webpack_watch_wait_for_file_builder_1.default
22
+ model_1.default
25
23
  ];
26
24
  exports.getPlugins = getPlugins;
@@ -0,0 +1,18 @@
1
+ import { IServerModule as _IServerModule } from '../shared/index';
2
+ import { extendedHooks } from './features/html-render/serverHooks';
3
+ declare type Head<T extends any[]> = T extends [...infer Head, any] ? Head : never;
4
+ declare type RemoveLast<T extends (...args: any) => any> = (...args: Head<Parameters<T>>) => ReturnType<T>;
5
+ declare global {
6
+ namespace ShuviService {
7
+ interface CustomServerPluginHooks {
8
+ getPageData: typeof extendedHooks.getPageData;
9
+ handlePageRequest: typeof extendedHooks.handlePageRequest;
10
+ modifyHtml: typeof extendedHooks.modifyHtml;
11
+ }
12
+ }
13
+ }
14
+ declare type ServerModule = Required<_IServerModule>;
15
+ export declare type GetPageDataFunction = RemoveLast<ServerModule['getPageData']>;
16
+ export declare type HandlePageRequestFunction = RemoveLast<ServerModule['handlePageRequest']>;
17
+ export declare type ModifyHtmlFunction = RemoveLast<ServerModule['modifyHtml']>;
18
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -4,7 +4,6 @@ import { IManifest } from '@shuvi/toolpack/lib/webpack/types';
4
4
  import { IMiddlewareRoutes, CreateAppServer, IApiRoutes, IServerModule, PlatformWebCustomConfig } from '../shared/index';
5
5
  import { IViewServer } from './features/html-render/index';
6
6
  import { addRoutes, addMiddlewareRoutes } from './features/filesystem-routes/hooks';
7
- import { extendedHooks } from './features/html-render/serverHooks';
8
7
  export {};
9
8
  declare module '@shuvi/service/lib/resources' {
10
9
  const server: {
@@ -35,10 +34,5 @@ declare global {
35
34
  addRoutes: typeof addRoutes;
36
35
  addMiddlewareRoutes: typeof addMiddlewareRoutes;
37
36
  }
38
- interface CustomServerPluginHooks {
39
- getPageData: typeof extendedHooks.getPageData;
40
- handlePageRequest: typeof extendedHooks.handlePageRequest;
41
- modifyHtml: typeof extendedHooks.modifyHtml;
42
- }
43
37
  }
44
38
  }
@@ -1,10 +1,10 @@
1
1
  import { IManifest } from '@shuvi/toolpack/lib/webpack/types';
2
2
  import { Response, IApplication, IRequest, IAppData } from '@shuvi/platform-shared/shared';
3
- export declare type IRenderDocumentOptions = {
3
+ export declare type IRenderViewOptions = {
4
4
  app: IApplication;
5
5
  req?: IRequest;
6
6
  };
7
- export interface IRenderOptions extends IRenderDocumentOptions {
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shuvi/platform-web",
3
- "version": "1.0.0-rc.4",
3
+ "version": "1.0.0-rc.5",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/shuvijs/shuvi.git",
@@ -65,17 +65,17 @@
65
65
  },
66
66
  "dependencies": {
67
67
  "@next/react-refresh-utils": "12.1.6",
68
- "@shuvi/hook": "1.0.0-rc.4",
69
- "@shuvi/platform-shared": "1.0.0-rc.4",
68
+ "@shuvi/hook": "1.0.0-rc.5",
69
+ "@shuvi/platform-shared": "1.0.0-rc.5",
70
70
  "@shuvi/redox": "0.0.3",
71
71
  "@shuvi/redox-react": "0.0.3",
72
- "@shuvi/router": "1.0.0-rc.4",
73
- "@shuvi/router-react": "1.0.0-rc.4",
74
- "@shuvi/runtime": "1.0.0-rc.4",
75
- "@shuvi/service": "1.0.0-rc.4",
76
- "@shuvi/shared": "1.0.0-rc.4",
77
- "@shuvi/toolpack": "1.0.0-rc.4",
78
- "@shuvi/utils": "1.0.0-rc.4",
72
+ "@shuvi/router": "1.0.0-rc.5",
73
+ "@shuvi/router-react": "1.0.0-rc.5",
74
+ "@shuvi/runtime": "1.0.0-rc.5",
75
+ "@shuvi/service": "1.0.0-rc.5",
76
+ "@shuvi/shared": "1.0.0-rc.5",
77
+ "@shuvi/toolpack": "1.0.0-rc.5",
78
+ "@shuvi/utils": "1.0.0-rc.5",
79
79
  "content-type": "1.0.4",
80
80
  "cookie": "0.4.1",
81
81
  "ejs": "3.1.5",
@@ -91,7 +91,7 @@
91
91
  "use-sync-external-store": "1.1.0"
92
92
  },
93
93
  "peerDependencies": {
94
- "@shuvi/service": "1.0.0-rc.4"
94
+ "@shuvi/service": "1.0.0-rc.5"
95
95
  },
96
96
  "devDependencies": {
97
97
  "@types/react": "18.0.9",
@@ -1,6 +0,0 @@
1
- import { IServerModule as _IServerModule } from '../shared';
2
- declare type ServerModule = Required<_IServerModule>;
3
- export declare type GetPageDataFunction = ServerModule['getPageData'];
4
- export declare type HandlePageRequestFunction = ServerModule['handlePageRequest'];
5
- export declare type ModifyHtmlFunction = ServerModule['modifyHtml'];
6
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,16 +0,0 @@
1
- declare const _default: {
2
- core: import("@shuvi/hook").IPluginInstance<{
3
- extendConfig: import("@shuvi/hook").SyncWaterfallHook<import("@shuvi/service").Config, void>;
4
- afterInit: import("@shuvi/hook").AsyncParallelHook<void, void, void>;
5
- afterBuild: import("@shuvi/hook").AsyncParallelHook<void, void, void>;
6
- afterDestroy: import("@shuvi/hook").AsyncParallelHook<void, void, void>;
7
- afterBundlerDone: import("@shuvi/hook").AsyncParallelHook<import("@shuvi/service/lib/core/lifecycleTypes").BundlerDoneExtra, void, void>;
8
- afterBundlerTargetDone: import("@shuvi/hook").AsyncParallelHook<import("@shuvi/service/lib/core/lifecycleTypes").BundlerTargetDoneExtra, void, void>;
9
- configWebpack: import("@shuvi/hook").AsyncSeriesWaterfallHook<import("@shuvi/service/lib/core/lifecycleTypes").WebpackChainType, import("@shuvi/service/lib/core/lifecycleTypes").ConfigWebpackAssistant>;
10
- addExtraTarget: import("@shuvi/hook").AsyncParallelHook<import("@shuvi/service/lib/core/lifecycleTypes").ExtraTargetAssistant, void, import("@shuvi/service/lib/core/lifecycleTypes").TargetChain>;
11
- addResource: import("@shuvi/hook").AsyncParallelHook<void, void, import("@shuvi/service/lib/core/lifecycleTypes").Resources | import("@shuvi/service/lib/core/lifecycleTypes").Resources[]>;
12
- addRuntimeFile: import("@shuvi/hook").AsyncParallelHook<void, import("@shuvi/service/lib/core/lifecycleTypes").AddRuntimeFileUtils, import("@shuvi/service/lib/project/index").FileOption<any, any> | import("@shuvi/service/lib/project/index").FileOption<any, any>[]>;
13
- addRuntimeService: import("@shuvi/hook").AsyncParallelHook<void, void, import("@shuvi/service/lib/core/lifecycleTypes").RuntimeService | import("@shuvi/service/lib/core/lifecycleTypes").RuntimeService[]>;
14
- } & import("@shuvi/service/lib/core/apiTypes").CustomCorePluginHooks, import("@shuvi/service").IPluginContext>;
15
- };
16
- export default _default;
@@ -1,25 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const service_1 = require("@shuvi/service");
7
- const webpack_watch_wait_for_file_builder_plugin_1 = __importDefault(require("./webpack-watch-wait-for-file-builder-plugin"));
8
- const plugin = (0, service_1.createPlugin)({
9
- configWebpack(config, _, ctx) {
10
- if (ctx.mode === 'development') {
11
- config
12
- .plugin('webpack-watch-wait-for-file-builder-plugin')
13
- .use(webpack_watch_wait_for_file_builder_plugin_1.default, [
14
- {
15
- onBuildStart: ctx.onBuildStart,
16
- onBuildEnd: ctx.onBuildEnd
17
- }
18
- ]);
19
- }
20
- return config;
21
- }
22
- });
23
- exports.default = {
24
- core: plugin
25
- };
@@ -1,12 +0,0 @@
1
- import { IPluginContext } from '@shuvi/service';
2
- import { Compiler, Plugin } from '@shuvi/toolpack/lib/webpack';
3
- declare type Options = {
4
- onBuildStart: IPluginContext['onBuildStart'];
5
- onBuildEnd: IPluginContext['onBuildEnd'];
6
- };
7
- export default class WebpackWatchWaitForFileBuilderPlugin implements Plugin {
8
- options: Options;
9
- constructor(options: Options);
10
- apply(compiler: Compiler): void;
11
- }
12
- export {};
@@ -1,32 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- class WebpackWatchWaitForFileBuilderPlugin {
4
- constructor(options) {
5
- this.options = options;
6
- }
7
- apply(compiler) {
8
- const { onBuildEnd, onBuildStart } = this.options;
9
- /**
10
- * watching.suspend will pause the real action in the watcher handler but still collecting changed files.
11
- * watching.resume will resume its action
12
- *
13
- * We make sure onBuildStart is faster than webpack's watcher and make it suspend.
14
- *
15
- * And resume when onBuildEnd.
16
- *
17
- * In this way, during build of fileBuilder, webpack will not trigger any watchRun event but keep watching changed files.
18
- */
19
- onBuildStart(() => {
20
- compiler.watching.suspend();
21
- });
22
- onBuildEnd(({ buildStatus }) => {
23
- if (buildStatus === 'fulfilled') {
24
- setTimeout(() => {
25
- compiler.watching.resume();
26
- // FIXME: timeout for resuming need further investigation
27
- }, 100);
28
- }
29
- });
30
- }
31
- }
32
- exports.default = WebpackWatchWaitForFileBuilderPlugin;