@shuvi/platform-web 1.0.0-rc.13 → 1.0.0-rc.16

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 (62) hide show
  1. package/esm/shared/appTypes.d.ts +9 -5
  2. package/esm/shared/constants.d.ts +8 -0
  3. package/esm/shared/constants.js +8 -0
  4. package/esm/shared/htmlRenderer.d.ts +33 -0
  5. package/esm/shared/htmlRenderer.js +1 -0
  6. package/esm/shared/index.d.ts +2 -0
  7. package/esm/shared/index.js +2 -0
  8. package/esm/shared/renderTypes.d.ts +4 -4
  9. package/esm/shuvi-app/app/client.d.ts +2 -3
  10. package/esm/shuvi-app/app/client.js +4 -4
  11. package/esm/shuvi-app/app/server.js +7 -4
  12. package/esm/shuvi-app/dev/index.d.ts +2 -2
  13. package/esm/shuvi-app/entry/client/app.d.ts +1 -1
  14. package/esm/shuvi-app/helper/serializeServerError.d.ts +1 -1
  15. package/esm/shuvi-app/helper/serializeServerError.js +2 -2
  16. package/esm/shuvi-app/react/AppContainer.d.ts +2 -2
  17. package/esm/shuvi-app/react/AppContainer.jsx +1 -1
  18. package/esm/shuvi-app/react/ApplicationContext.d.ts +7 -0
  19. package/esm/shuvi-app/react/{applicationContext.jsx → ApplicationContext.jsx} +0 -0
  20. package/esm/shuvi-app/react/Error.jsx +4 -1
  21. package/esm/shuvi-app/react/Link.d.ts +2 -1
  22. package/esm/shuvi-app/react/Link.jsx +1 -1
  23. package/esm/shuvi-app/react/view/ReactView.server.jsx +15 -3
  24. package/lib/node/features/custom-server/server.js +16 -8
  25. package/lib/node/features/filesystem-routes/api/middleware.js +5 -2
  26. package/lib/node/features/filesystem-routes/index.js +17 -8
  27. package/lib/node/features/filesystem-routes/middleware/middleware.js +5 -2
  28. package/lib/node/features/html-render/index.js +44 -18
  29. package/lib/node/features/html-render/lib/buildHtml.js +2 -1
  30. package/lib/node/features/html-render/lib/generateFilesByRoutId.d.ts +1 -1
  31. package/lib/node/features/html-render/lib/generateResource.js +12 -8
  32. package/lib/node/features/html-render/lib/getPageMiddleware.d.ts +1 -1
  33. package/lib/node/features/html-render/lib/getPageMiddleware.js +14 -12
  34. package/lib/node/features/html-render/lib/renderToHTML.js +7 -6
  35. package/lib/node/features/html-render/lib/renderer/base.d.ts +3 -4
  36. package/lib/node/features/html-render/lib/renderer/base.js +7 -6
  37. package/lib/node/features/html-render/lib/renderer/index.js +5 -2
  38. package/lib/node/features/html-render/lib/renderer/ssr.d.ts +1 -1
  39. package/lib/node/features/html-render/lib/renderer/ssr.js +7 -5
  40. package/lib/node/features/html-render/lib/renderer/types.d.ts +4 -5
  41. package/lib/node/features/html-render/lib/webpack/build-manifest-plugin.d.ts +27 -0
  42. package/lib/node/features/html-render/lib/webpack/build-manifest-plugin.js +223 -0
  43. package/lib/node/features/middlewares.d.ts +1 -1
  44. package/lib/node/features/middlewares.js +12 -3
  45. package/lib/node/features/on-demand-compile-page/onDemandRouteManager.js +3 -3
  46. package/lib/node/index.d.ts +1 -0
  47. package/lib/node/index.js +15 -0
  48. package/lib/node/shuvi-runtime-server.d.ts +4 -1
  49. package/lib/node/shuvi-type-extensions-node.d.ts +16 -15
  50. package/lib/node/targets/react/bundler/index.js +13 -11
  51. package/lib/shared/appTypes.d.ts +9 -5
  52. package/lib/shared/constants.d.ts +8 -0
  53. package/lib/shared/constants.js +15 -0
  54. package/lib/shared/htmlRenderer.d.ts +33 -0
  55. package/lib/shared/htmlRenderer.js +2 -0
  56. package/lib/shared/index.d.ts +2 -0
  57. package/lib/shared/index.js +2 -0
  58. package/lib/shared/renderTypes.d.ts +4 -4
  59. package/package.json +19 -15
  60. package/esm/shuvi-app/react/applicationContext.d.ts +0 -7
  61. package/esm/shuvi-app/shuvi-runtime-index.d.ts +0 -4
  62. package/esm/shuvi-app/shuvi-runtime-index.js +0 -2
@@ -1,17 +1,21 @@
1
- import { IPageRouteRecord, IAppData, IAppState } from '@shuvi/platform-shared/shared';
2
- import { Application } from '@shuvi/platform-shared/shuvi-app/application';
1
+ import { IPageRouteRecord, IAppData, IAppState, Application as _Application } from '@shuvi/platform-shared/shared';
2
+ import { ApplicationImpl as _ApplicationImpl } from '@shuvi/platform-shared/shuvi-app/application';
3
3
  import type { ShuviRequest } from '@shuvi/service';
4
+ export interface AppConfig {
5
+ ssr: boolean;
6
+ }
7
+ export declare type InternalApplication = _ApplicationImpl<AppConfig>;
8
+ export declare type Application = _Application<AppConfig>;
4
9
  export interface CreateAppServer {
5
10
  (options: {
6
11
  req: ShuviRequest;
7
12
  ssr: boolean;
8
- isDev: boolean;
9
- }): Application;
13
+ }): InternalApplication;
10
14
  }
11
15
  export interface CreateAppClient {
12
16
  (options: {
13
17
  routes: IPageRouteRecord[];
14
18
  appComponent: any;
15
19
  appData: IAppData<any, IAppState>;
16
- }): Application;
20
+ }): InternalApplication;
17
21
  }
@@ -0,0 +1,8 @@
1
+ export { BUNDLER_TARGET_CLIENT, BUNDLER_TARGET_SERVER, CLIENT_OUTPUT_DIR, SERVER_OUTPUT_DIR } from '@shuvi/shared/lib/constants';
2
+ export declare const CLIENT_BUILD_MANIFEST_PATH = "../build-manifest.client.json";
3
+ export declare const SERVER_BUILD_MANIFEST_PATH = "../build-manifest.server.json";
4
+ export declare const BUILD_CLIENT_RUNTIME_POLYFILLS = "static/polyfills";
5
+ export declare const BUILD_CLIENT_RUNTIME_POLYFILLS_SYMBOL: unique symbol;
6
+ export declare const BUILD_CLIENT_RUNTIME_WEBPACK = "static/webpack-runtime";
7
+ export declare const BUILD_CLIENT_RUNTIME_MAIN = "static/main";
8
+ export declare const BUILD_SERVER_FILE_SERVER = "server";
@@ -0,0 +1,8 @@
1
+ export { BUNDLER_TARGET_CLIENT, BUNDLER_TARGET_SERVER, CLIENT_OUTPUT_DIR, SERVER_OUTPUT_DIR } from '@shuvi/shared/lib/constants';
2
+ export const CLIENT_BUILD_MANIFEST_PATH = '../build-manifest.client.json';
3
+ export const SERVER_BUILD_MANIFEST_PATH = '../build-manifest.server.json';
4
+ export const BUILD_CLIENT_RUNTIME_POLYFILLS = `static/polyfills`;
5
+ export const BUILD_CLIENT_RUNTIME_POLYFILLS_SYMBOL = Symbol(`polyfills`);
6
+ export const BUILD_CLIENT_RUNTIME_WEBPACK = `static/webpack-runtime`;
7
+ export const BUILD_CLIENT_RUNTIME_MAIN = `static/main`;
8
+ export const BUILD_SERVER_FILE_SERVER = `server`;
@@ -0,0 +1,33 @@
1
+ export interface IModuleItem {
2
+ id: string;
3
+ name: string;
4
+ }
5
+ export interface IModule {
6
+ files: string[];
7
+ children: IModuleItem[];
8
+ }
9
+ export declare type IAssetMap = {
10
+ js: string[];
11
+ css?: string[];
12
+ } & {
13
+ [ext: string]: string[];
14
+ };
15
+ export interface IChunk {
16
+ file: string;
17
+ request: string;
18
+ }
19
+ export interface IManifest {
20
+ polyfillFiles?: string[];
21
+ entries: {
22
+ [s: string]: IAssetMap;
23
+ };
24
+ bundles: {
25
+ [name: string]: string;
26
+ };
27
+ chunkRequest: {
28
+ [file: string]: string;
29
+ };
30
+ loadble: {
31
+ [s: string]: IModule;
32
+ };
33
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,5 +1,7 @@
1
1
  export * from './appTypes';
2
2
  export * from './configTypes';
3
+ export * from './constants';
4
+ export * from './htmlRenderer';
3
5
  export * from './renderTypes';
4
6
  export * from './routeTypes';
5
7
  export * from './serverTypes';
@@ -1,5 +1,7 @@
1
1
  export * from './appTypes';
2
2
  export * from './configTypes';
3
+ export * from './constants';
4
+ export * from './htmlRenderer';
3
5
  export * from './renderTypes';
4
6
  export * from './routeTypes';
5
7
  export * from './serverTypes';
@@ -1,8 +1,9 @@
1
- import { IManifest } from '@shuvi/toolpack/lib/webpack/types';
2
1
  import type { ShuviRequest } from '@shuvi/service';
3
- import { Response, IApplication, IAppData } from '@shuvi/platform-shared/shared';
2
+ import { Response, IAppData } from '@shuvi/platform-shared/shared';
3
+ import { IManifest } from '../shared';
4
+ import { Application } from './appTypes';
4
5
  export declare type IRenderViewOptions = {
5
- app: IApplication;
6
+ app: Application;
6
7
  };
7
8
  export interface IRenderOptions extends IRenderViewOptions {
8
9
  }
@@ -37,7 +38,6 @@ export interface IClientRendererOptions<ExtraAppData = {}> extends IRenderOption
37
38
  export interface IServerRendererOptions extends IRenderOptions {
38
39
  req: ShuviRequest;
39
40
  manifest: IManifest;
40
- isDev?: boolean;
41
41
  }
42
42
  export interface IViewClient<ExtraAppData = {}> extends IView<IClientRendererOptions<ExtraAppData>> {
43
43
  }
@@ -1,4 +1,3 @@
1
- import { Application } from '@shuvi/platform-shared/shuvi-app/application';
2
- import { CreateAppClient } from '../../shared';
3
- export { Application };
1
+ import { InternalApplication, CreateAppClient } from '../../shared';
2
+ export { InternalApplication };
4
3
  export declare const createApp: CreateAppClient;
@@ -9,14 +9,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import { getRoutes } from '@shuvi/app/core/platform';
11
11
  import { runPreload, runLoaders, getRouteMatchesWithInvalidLoader, isRedirect, isResponse } from '@shuvi/platform-shared/shared';
12
- import application, { Application } from '@shuvi/platform-shared/shuvi-app/application';
12
+ import 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
17
  import { serializeServerError } from '../helper/serializeServerError';
18
18
  let app;
19
- export { Application };
20
19
  export const createApp = ({ routes, appData, appComponent }) => {
21
20
  // app is a singleton in client side
22
21
  if (app) {
@@ -37,7 +36,8 @@ export const createApp = ({ routes, appData, appComponent }) => {
37
36
  app = application({
38
37
  initialState: appState,
39
38
  AppComponent: appComponent,
40
- router
39
+ router,
40
+ config: { ssr }
41
41
  });
42
42
  const loadersData = app.getLoadersData();
43
43
  const hasHydrateData = Object.keys(loadersData).length > 0;
@@ -109,7 +109,7 @@ export const createApp = ({ routes, appData, appComponent }) => {
109
109
  next();
110
110
  return;
111
111
  }
112
- app.setError(serializeServerError(error, process.env.NODE_ENV === 'development'));
112
+ app.setError(serializeServerError(error));
113
113
  next();
114
114
  return;
115
115
  }
@@ -16,7 +16,7 @@ import { createRouter, createMemoryHistory } from '@shuvi/router';
16
16
  import chalk from '@shuvi/utils/lib/chalk';
17
17
  import { serializeServerError } from '../helper/serializeServerError';
18
18
  export const createApp = options => {
19
- const { req, ssr, isDev } = options;
19
+ const { req, ssr } = options;
20
20
  const history = createMemoryHistory({
21
21
  initialEntries: [(req && req.url) || '/'],
22
22
  initialIndex: 0
@@ -50,10 +50,10 @@ export const createApp = options => {
50
50
  next();
51
51
  return;
52
52
  }
53
- if (isDev) {
53
+ if (process.env.NODE_ENV === 'development') {
54
54
  console.error(chalk.red('error') + ' - ' + error.stack);
55
55
  }
56
- app.setError(serializeServerError(error, isDev));
56
+ app.setError(serializeServerError(error));
57
57
  next();
58
58
  return;
59
59
  }
@@ -62,7 +62,10 @@ export const createApp = options => {
62
62
  }
63
63
  app = application({
64
64
  AppComponent,
65
- router
65
+ router,
66
+ config: {
67
+ ssr
68
+ }
66
69
  });
67
70
  return app;
68
71
  };
@@ -1,3 +1,3 @@
1
- import { Application } from '@shuvi/platform-shared/shuvi-app/application';
1
+ import { InternalApplication } from '../../shared';
2
2
  import { HotDevClient } from './hotDevClient';
3
- export declare const initHMRAndDevClient: (app: Application) => HotDevClient;
3
+ export declare const initHMRAndDevClient: (app: InternalApplication) => HotDevClient;
@@ -1,3 +1,3 @@
1
- declare const app: import("../../app/client").Application;
1
+ declare const app: import("../../app/client").InternalApplication;
2
2
  declare const run: () => Promise<void>;
3
3
  export { run, app };
@@ -1,2 +1,2 @@
1
1
  import { IError } from '@shuvi/platform-shared/shared';
2
- export declare function serializeServerError(err: Error, dev: boolean | undefined): IError;
2
+ export declare function serializeServerError(err: Error): IError;
@@ -9,8 +9,8 @@ function errorToJSON(err) {
9
9
  stack: err.stack
10
10
  };
11
11
  }
12
- export function serializeServerError(err, dev) {
13
- if (dev) {
12
+ export function serializeServerError(err) {
13
+ if (process.env.NODE_ENV === 'development') {
14
14
  return errorToJSON(err);
15
15
  }
16
16
  return {
@@ -1,5 +1,5 @@
1
1
  import * as React from 'react';
2
- import { IApplication } from '@shuvi/platform-shared/shared';
2
+ import { Application } from '../../shared';
3
3
  export default function AppContainer({ app, children }: React.PropsWithChildren<{
4
- app: IApplication;
4
+ app: Application;
5
5
  }>): JSX.Element;
@@ -1,6 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import { errorModel } from '@shuvi/platform-shared/shared';
3
- import { AppProvider } from './applicationContext';
3
+ import { AppProvider } from './ApplicationContext';
4
4
  import ErrorPage from './ErrorPage';
5
5
  import { ErrorBoundary } from './ErrorBoundary';
6
6
  import { Provider, useSharedModel } from './store';
@@ -0,0 +1,7 @@
1
+ import * as React from 'react';
2
+ import { Application } from '../../shared';
3
+ export declare const ApplicationContext: React.Context<Application>;
4
+ export declare function AppProvider({ app, children }: React.PropsWithChildren<{
5
+ app: Application;
6
+ }>): JSX.Element;
7
+ export declare function useApp(): Application;
@@ -1,5 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import { Head } from './head';
3
+ import { useApp } from './ApplicationContext';
3
4
  const style = {
4
5
  container: {
5
6
  color: '#000',
@@ -25,13 +26,15 @@ const style = {
25
26
  }
26
27
  };
27
28
  export default function Error({ errorCode, errorDesc }) {
29
+ const app = useApp();
28
30
  return (<div style={style.container}>
29
31
  <Head>
30
32
  <title>Page Error</title>
31
33
  </Head>
32
34
 
33
35
  <div style={style.error}>
34
- <div style={style.errorCode}>{errorCode}</div>
36
+ {/* 500 will cause confusion for SPA application, so only show error code on SSR */}
37
+ {app.config.ssr && <div style={style.errorCode}>{errorCode}</div>}
35
38
  <div style={style.errorDesc}>{errorDesc || 'Error'}</div>
36
39
  </div>
37
40
  </div>);
@@ -1,7 +1,8 @@
1
1
  /// <reference types="react" />
2
2
  import { LinkProps } from '@shuvi/router-react';
3
- export declare const Link: ({ prefetch, onMouseEnter, to, ref, ...rest }: LinkWrapperProps) => JSX.Element;
3
+ export declare const Link: ({ to, ref, prefetch, onMouseEnter, ...rest }: LinkWrapperProps) => JSX.Element;
4
4
  interface LinkWrapperProps extends LinkProps {
5
+ prefetch?: boolean;
5
6
  ref?: any;
6
7
  }
7
8
  export {};
@@ -71,7 +71,7 @@ const isAbsoluteUrl = (url) => {
71
71
  return ABSOLUTE_URL_REGEX.test(url);
72
72
  };
73
73
  export const Link = function LinkWithPrefetch(_a) {
74
- var { prefetch, onMouseEnter, to, ref } = _a, rest = __rest(_a, ["prefetch", "onMouseEnter", "to", "ref"]);
74
+ var { to, ref, prefetch, onMouseEnter } = _a, rest = __rest(_a, ["to", "ref", "prefetch", "onMouseEnter"]);
75
75
  const isHrefValid = typeof to === 'string' && !isAbsoluteUrl(to);
76
76
  const previousHref = React.useRef(to);
77
77
  const [setIntersectionRef, isVisible, resetVisible] = useIntersection({});
@@ -19,7 +19,7 @@ import { Head } from '../head';
19
19
  import { serializeServerError } from '../../helper/serializeServerError';
20
20
  export class ReactServerView {
21
21
  constructor() {
22
- this.renderApp = ({ req, app, manifest, isDev }) => __awaiter(this, void 0, void 0, function* () {
22
+ this.renderApp = ({ req, app, manifest }) => __awaiter(this, void 0, void 0, function* () {
23
23
  yield Loadable.preloadAll();
24
24
  const { router, appComponent: AppComponent, setError: setAppError } = app;
25
25
  yield router.ready;
@@ -46,10 +46,10 @@ export class ReactServerView {
46
46
  htmlContent = renderToString(RootApp);
47
47
  }
48
48
  catch (error) {
49
- if (isDev) {
49
+ if (process.env.NODE_ENV === 'development') {
50
50
  console.error(chalk.red('error') + ' - ' + error.stack);
51
51
  }
52
- setAppError(serializeServerError(error, isDev));
52
+ setAppError(serializeServerError(error));
53
53
  htmlContent = renderToString(RootApp); // Consistency on both server and client side
54
54
  }
55
55
  finally {
@@ -72,6 +72,18 @@ export class ReactServerView {
72
72
  const preloadDynamicChunks = [];
73
73
  const styles = [];
74
74
  for (const file of dynamicImportChunkSet) {
75
+ // Safari Bug: https://bugs.webkit.org/show_bug.cgi?id=187726
76
+ // If a request is preloaded, Safari will always retrieve it from the
77
+ // cache, regardless of the cache headers for that request.
78
+ // disable preload for safari on dev
79
+ if (process.env.NODE_ENV === 'development') {
80
+ const ua = req.headers['user-agent'] || '';
81
+ if (/safari/i.test(ua) && !/chrome/i.test(ua)) {
82
+ if (/\.js$/.test(file)) {
83
+ continue;
84
+ }
85
+ }
86
+ }
75
87
  if (/\.js$/.test(file)) {
76
88
  preloadDynamicChunks.push({
77
89
  tagName: 'link',
@@ -8,21 +8,29 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  const service_1 = require("@shuvi/service");
13
- const resources_1 = require("@shuvi/service/lib/resources");
16
+ const resources_1 = __importDefault(require("@shuvi/service/lib/resources"));
17
+ let isWarnedhandlePageRequest = false;
14
18
  exports.default = (0, service_1.createServerPlugin)({
15
19
  getPageData: (appContext, context) => {
16
- var _a, _b;
17
- return ((_b = (_a = resources_1.server === null || resources_1.server === void 0 ? void 0 : resources_1.server.server) === null || _a === void 0 ? void 0 : _a.getPageData) === null || _b === void 0 ? void 0 : _b.call(_a, appContext, context)) || {};
20
+ var _a, _b, _c;
21
+ return ((_c = (_b = (_a = resources_1.default.server) === null || _a === void 0 ? void 0 : _a.server) === null || _b === void 0 ? void 0 : _b.getPageData) === null || _c === void 0 ? void 0 : _c.call(_b, appContext, context)) || {};
18
22
  },
19
23
  handlePageRequest: (originalHandlePageRequest, context) => {
20
- var _a, _b;
21
- return (((_b = (_a = resources_1.server === null || resources_1.server === void 0 ? void 0 : resources_1.server.server) === null || _a === void 0 ? void 0 : _a.handlePageRequest) === null || _b === void 0 ? void 0 : _b.call(_a, originalHandlePageRequest, context)) ||
22
- originalHandlePageRequest);
24
+ var _a, _b, _c, _d, _e;
25
+ if (!isWarnedhandlePageRequest &&
26
+ ((_b = (_a = resources_1.default.server) === null || _a === void 0 ? void 0 : _a.server) === null || _b === void 0 ? void 0 : _b.handlePageRequest) !== undefined) {
27
+ isWarnedhandlePageRequest = true;
28
+ console.warn('Warning: handlePageRequest is an experimental feature, please use with caution.');
29
+ }
30
+ return (((_e = (_d = (_c = resources_1.default.server) === null || _c === void 0 ? void 0 : _c.server) === null || _d === void 0 ? void 0 : _d.handlePageRequest) === null || _e === void 0 ? void 0 : _e.call(_d, originalHandlePageRequest, context)) || originalHandlePageRequest);
23
31
  },
24
32
  modifyHtml: (document, context, pluginContext) => __awaiter(void 0, void 0, void 0, function* () {
25
- var _a, _b;
26
- yield ((_b = (_a = resources_1.server === null || resources_1.server === void 0 ? void 0 : resources_1.server.server) === null || _a === void 0 ? void 0 : _a.modifyHtml) === null || _b === void 0 ? void 0 : _b.call(_a, document, context, pluginContext));
33
+ var _a, _b, _c;
34
+ yield ((_c = (_b = (_a = resources_1.default.server) === null || _a === void 0 ? void 0 : _a.server) === null || _b === void 0 ? void 0 : _b.modifyHtml) === null || _c === void 0 ? void 0 : _c.call(_b, document, context, pluginContext));
27
35
  })
28
36
  });
@@ -8,15 +8,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.middleware = void 0;
13
16
  const router_1 = require("@shuvi/router");
14
- const resources_1 = require("@shuvi/service/lib/resources");
17
+ const resources_1 = __importDefault(require("@shuvi/service/lib/resources"));
15
18
  const apiRouteHandler_1 = require("./apiRouteHandler");
16
19
  function middleware(_ctx) {
17
20
  return function (req, res, next) {
18
21
  return __awaiter(this, void 0, void 0, function* () {
19
- const { apiRoutes } = resources_1.server;
22
+ const { apiRoutes } = resources_1.default.server;
20
23
  let tempApiModule;
21
24
  for (const { path, api } of apiRoutes) {
22
25
  const match = (0, router_1.matchPathname)(path, req.pathname);
@@ -20,6 +20,7 @@ const middleware_1 = require("./middleware");
20
20
  Object.defineProperty(exports, "getMiddlewareMiddleware", { enumerable: true, get: function () { return middleware_1.middleware; } });
21
21
  const api_1 = require("./api");
22
22
  Object.defineProperty(exports, "getApiMiddleware", { enumerable: true, get: function () { return api_1.middleware; } });
23
+ let isWarnedAddMiddlewareRoutes = false;
23
24
  const plugin = (0, service_1.createPlugin)({
24
25
  setup: ({ addHooks }) => {
25
26
  addHooks({ addRoutes: hooks_1.addRoutes, addApiRoutes: hooks_1.addApiRoutes, addMiddlewareRoutes: hooks_1.addMiddlewareRoutes });
@@ -79,15 +80,23 @@ const plugin = (0, service_1.createPlugin)({
79
80
  const middlewareRoutesFile = defineFile({
80
81
  name: 'middlewareRoutes.js',
81
82
  content: () => __awaiter(void 0, void 0, void 0, function* () {
82
- const { routes: _routes, warnings } = yield (0, node_1.getMiddlewareRoutes)(getContent(rawRoutes), conventionRoutes.exclude);
83
- if (isBuildPhase) {
84
- warnings.forEach(warning => {
85
- console.warn(warning);
86
- });
87
- }
88
- const fsRoutes = _routes;
83
+ // Remove the 'middleware' file convention first, and deal with it in a future major update.
84
+ // const { routes: _routes, warnings } = await getMiddlewareRoutes(
85
+ // getContent(rawRoutes),
86
+ // conventionRoutes.exclude
87
+ // );
88
+ // if (isBuildPhase) {
89
+ // warnings.forEach(warning => {
90
+ // console.warn(warning);
91
+ // });
92
+ // }
93
+ // let fsRoutes = _routes;
89
94
  const pluginRoutes = (yield pluginRunner.addMiddlewareRoutes()).flat();
90
- return (0, middleware_1.generateRoutesContent)(pluginRoutes.concat(fsRoutes), {
95
+ if (!isWarnedAddMiddlewareRoutes && pluginRoutes.length > 0) {
96
+ console.warn('Warning: addMiddlewareRoutes is an experimental feature, we recommend using api routes instead.');
97
+ isWarnedAddMiddlewareRoutes = true;
98
+ }
99
+ return (0, middleware_1.generateRoutesContent)(pluginRoutes, {
91
100
  baseDir: paths.routesDir
92
101
  });
93
102
  }),
@@ -8,14 +8,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.middleware = void 0;
13
16
  const router_1 = require("@shuvi/router");
14
- const resources_1 = require("@shuvi/service/lib/resources");
17
+ const resources_1 = __importDefault(require("@shuvi/service/lib/resources"));
15
18
  function middleware(_api) {
16
19
  return function (req, res, next) {
17
20
  return __awaiter(this, void 0, void 0, function* () {
18
- const { middlewareRoutes = [] } = resources_1.server;
21
+ const { middlewareRoutes = [] } = resources_1.default.server;
19
22
  // match path for get middlewares
20
23
  let middlewares = [];
21
24
  for (let i = 0; i < middlewareRoutes.length; i++) {
@@ -14,18 +14,32 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.getPlugin = exports.getPageMiddleware = void 0;
16
16
  const service_1 = require("@shuvi/service");
17
- const constants_1 = require("@shuvi/shared/lib/constants");
18
17
  const config_1 = require("@shuvi/toolpack/lib/webpack/config");
19
18
  const copy_file_plugin_1 = require("@shuvi/toolpack/lib/webpack/plugins/copy-file-plugin");
19
+ const shared_1 = require("../../../shared");
20
20
  const paths_1 = require("../../paths");
21
21
  const version_1 = require("../../version");
22
22
  const middlewares_1 = require("../middlewares");
23
23
  const generateResource_1 = __importDefault(require("./lib/generateResource"));
24
24
  const buildHtml_1 = require("./lib/buildHtml");
25
+ const build_manifest_plugin_1 = __importDefault(require("./lib/webpack/build-manifest-plugin"));
25
26
  const server_1 = __importDefault(require("./server"));
27
+ const ENTRY_FLAG = 'shuviEntry';
28
+ function makeEntryRequest(req) {
29
+ return `${req}?${ENTRY_FLAG}=true`;
30
+ }
31
+ function getClientEntry() {
32
+ return {
33
+ [shared_1.BUILD_CLIENT_RUNTIME_MAIN]: [
34
+ makeEntryRequest((0, paths_1.resolvePkgFile)('esm/shuvi-app/entry/client'))
35
+ ]
36
+ };
37
+ }
26
38
  function getServerEntry() {
27
39
  return {
28
- [service_1.BUILD_SERVER_FILE_SERVER]: [(0, paths_1.resolvePkgFile)('esm/shuvi-app/entry/server')]
40
+ [shared_1.BUILD_SERVER_FILE_SERVER]: [
41
+ makeEntryRequest((0, paths_1.resolvePkgFile)('esm/shuvi-app/entry/server'))
42
+ ]
29
43
  };
30
44
  }
31
45
  var lib_1 = require("./lib");
@@ -33,42 +47,58 @@ Object.defineProperty(exports, "getPageMiddleware", { enumerable: true, get: fun
33
47
  /** This plugin uses `platformContext` so that it is set to a plugin getter */
34
48
  const getPlugin = (platformContext) => {
35
49
  const core = (0, service_1.createPlugin)({
36
- configWebpack: (chain, { name }) => {
50
+ configWebpack: (chain, { name, mode }) => {
51
+ const isDev = mode === 'development';
37
52
  const pkgVersion = (0, version_1.getVersion)();
38
- if (name === constants_1.BUNDLER_DEFAULT_TARGET) {
53
+ const isServer = name === shared_1.BUNDLER_TARGET_SERVER;
54
+ const isClient = name === shared_1.BUNDLER_TARGET_CLIENT;
55
+ if (isClient) {
39
56
  chain.merge({
40
- entry: {
41
- [service_1.BUILD_CLIENT_RUNTIME_MAIN]: [
42
- (0, paths_1.resolvePkgFile)('esm/shuvi-app/entry/client')
43
- ]
44
- }
57
+ entry: getClientEntry()
45
58
  });
59
+ chain.optimization.runtimeChunk({ name: shared_1.BUILD_CLIENT_RUNTIME_WEBPACK });
46
60
  chain.plugin('polyfills').use(copy_file_plugin_1.CopyFilePlugin, [
47
61
  {
48
62
  filePath: (0, paths_1.resolvePkgFile)('polyfills/polyfills.js'),
49
63
  cacheKey: pkgVersion,
50
- name: service_1.BUILD_CLIENT_RUNTIME_POLYFILLS,
64
+ name: shared_1.BUILD_CLIENT_RUNTIME_POLYFILLS,
51
65
  info: {
52
- [constants_1.BUILD_CLIENT_RUNTIME_POLYFILLS_SYMBOL]: 1,
66
+ [shared_1.BUILD_CLIENT_RUNTIME_POLYFILLS_SYMBOL]: 1,
53
67
  // This file is already minified
54
68
  minimized: true
55
69
  }
56
70
  }
57
71
  ]);
72
+ chain.plugin('private/build-manifest').use(build_manifest_plugin_1.default, [
73
+ {
74
+ filename: shared_1.CLIENT_BUILD_MANIFEST_PATH,
75
+ modules: true,
76
+ chunkRequest: isDev
77
+ }
78
+ ]);
79
+ }
80
+ else if (isServer) {
81
+ chain.plugin('private/build-manifest').use(build_manifest_plugin_1.default, [
82
+ {
83
+ filename: shared_1.SERVER_BUILD_MANIFEST_PATH,
84
+ modules: false,
85
+ chunkRequest: isDev
86
+ }
87
+ ]);
58
88
  }
59
89
  return chain;
60
90
  },
61
91
  addExtraTarget: ({ createConfig }, context) => {
62
92
  const serverWebpackHelpers = (0, config_1.webpackHelpers)();
63
93
  const serverChain = createConfig({
64
- name: constants_1.BUNDLER_TARGET_SERVER,
94
+ name: shared_1.BUNDLER_TARGET_SERVER,
65
95
  node: true,
66
96
  entry: getServerEntry(),
67
- outputDir: service_1.BUILD_SERVER_DIR,
97
+ outputDir: shared_1.SERVER_OUTPUT_DIR,
68
98
  webpackHelpers: serverWebpackHelpers
69
99
  });
70
100
  return {
71
- name: constants_1.BUNDLER_TARGET_SERVER,
101
+ name: shared_1.BUNDLER_TARGET_SERVER,
72
102
  chain: serverChain
73
103
  };
74
104
  },
@@ -83,10 +113,6 @@ const getPlugin = (platformContext) => {
83
113
  return [routerConfigFile];
84
114
  },
85
115
  addRuntimeService: () => [
86
- {
87
- source: (0, paths_1.resolvePkgFile)('esm/shuvi-app/shuvi-runtime-index'),
88
- exported: '*'
89
- },
90
116
  {
91
117
  source: (0, paths_1.resolvePkgFile)('lib/node/shuvi-runtime-server'),
92
118
  filepath: 'server.ts',
@@ -38,6 +38,7 @@ const fse = __importStar(require("fs-extra"));
38
38
  const node_mocks_http_1 = require("node-mocks-http");
39
39
  const path = __importStar(require("path"));
40
40
  const service_1 = require("@shuvi/service");
41
+ const shared_1 = require("../../../../shared");
41
42
  const buildHtml = ({ context, serverPlugins, getMiddlewares, pathname, filename }) => __awaiter(void 0, void 0, void 0, function* () {
42
43
  return new Promise(resolve => {
43
44
  const request = (0, node_mocks_http_1.createRequest)({
@@ -48,7 +49,7 @@ const buildHtml = ({ context, serverPlugins, getMiddlewares, pathname, filename
48
49
  });
49
50
  response.on('end', () => {
50
51
  const html = response._getBuffer();
51
- fse.writeFileSync(path.resolve(context.paths.buildDir, service_1.BUILD_DEFAULT_DIR, filename), html);
52
+ fse.writeFileSync(path.resolve(context.paths.buildDir, shared_1.CLIENT_OUTPUT_DIR, filename), html);
52
53
  resolve();
53
54
  });
54
55
  (0, service_1.createShuviServer)({
@@ -1,3 +1,3 @@
1
- import { IManifest } from '@shuvi/toolpack/lib/webpack/types';
2
1
  import { IPageRouteRecord } from '@shuvi/platform-shared/shared';
2
+ import { IManifest } from '../../../../shared';
3
3
  export default function generateFilesByRoutId(assetMap: IManifest, routes: IPageRouteRecord[]): Record<string, string[]>;