@shuvi/platform-shared 1.0.18 → 1.0.20

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.
@@ -1,5 +1,5 @@
1
1
  import { IRouter, IPageRouteRecord } from './routerTypes';
2
- import { Doura, Application, IAppContext, ApplicationOptions, IRerenderConfig, IError } from './applicationTypes';
2
+ import { Doura, Application, IAppContext, ApplicationInternalOptions, IRerenderConfig, IError } from './applicationTypes';
3
3
  export declare class ApplicationImpl<Config extends {} = {}> {
4
4
  private _router;
5
5
  private _appComponent;
@@ -9,7 +9,8 @@ export declare class ApplicationImpl<Config extends {} = {}> {
9
9
  private _store;
10
10
  private _error;
11
11
  private _loader;
12
- constructor(options: ApplicationOptions<Config>);
12
+ private _getLoaders;
13
+ constructor(options: ApplicationInternalOptions<Config>);
13
14
  get router(): IRouter<IPageRouteRecord>;
14
15
  get config(): Config;
15
16
  get context(): IAppContext;
@@ -18,6 +19,7 @@ export declare class ApplicationImpl<Config extends {} = {}> {
18
19
  get error(): IError | null;
19
20
  setError(err: IError): void;
20
21
  clearError(): void;
22
+ getLoaders(): Promise<Record<string, import("./index").Loader<any>>>;
21
23
  getLoadersData(): Record<string, any>;
22
24
  setLoadersData(datas: Record<string, any>): void;
23
25
  init(): Promise<void>;
@@ -20,6 +20,7 @@ export class ApplicationImpl {
20
20
  this._store = doura({ initialState: options.initialState });
21
21
  this._error = this._store.getModel(errorModelName, errorModel);
22
22
  this._loader = this._store.getModel(loaderModelName, loaderModel);
23
+ this._getLoaders = options.getLoaders;
23
24
  this._appComponent = options.AppComponent;
24
25
  this._pluginManager = getManager();
25
26
  initPlugins(this._pluginManager, options.plugins || []);
@@ -48,6 +49,9 @@ export class ApplicationImpl {
48
49
  clearError() {
49
50
  this._error.clear();
50
51
  }
52
+ getLoaders() {
53
+ return this._getLoaders();
54
+ }
51
55
  getLoadersData() {
52
56
  return this._loader.getAllData;
53
57
  }
@@ -2,6 +2,8 @@ import { Doura } from 'doura';
2
2
  import { CustomAppContext } from '@shuvi/runtime';
3
3
  import { IRouter, IPageRouteRecord } from './routerTypes';
4
4
  import { IPluginList } from './runtimPlugin';
5
+ import { Loader } from './loader';
6
+ export declare type Loaders = Record<string, Loader>;
5
7
  export interface IAppContext extends CustomAppContext {
6
8
  [x: string]: unknown;
7
9
  }
@@ -35,10 +37,15 @@ export interface Application<Config extends {} = {}> {
35
37
  getLoadersData(): Record<string, any>;
36
38
  setLoadersData(datas: Record<string, any>): void;
37
39
  }
38
- export interface ApplicationOptions<C extends {}> {
40
+ export interface GetLoadersFn {
41
+ (): Promise<Record<string, Loader>>;
42
+ }
43
+ export interface ApplicationInternalOptions<C extends {}> {
39
44
  config: C;
40
45
  router: IRouter;
41
46
  AppComponent: any;
47
+ getLoaders: GetLoadersFn;
42
48
  initialState?: IAppState;
43
49
  plugins?: IPluginList;
44
50
  }
51
+ export declare type ApplicationlOptions<C extends {}> = Omit<ApplicationInternalOptions<C>, 'getLoaders' | 'plugins'>;
@@ -84,20 +84,26 @@ export function runLoaders(matches, loadersByRouteId, { pathname, query, params,
84
84
  return;
85
85
  }
86
86
  let res;
87
+ let error;
87
88
  try {
88
89
  const value = yield loaderFn(Object.assign({ pathname,
89
90
  params,
90
91
  query, redirect: redirectHelper, error: errorHelper, appContext }, (req ? { req } : {})));
91
92
  if (value === undefined) {
92
- throw new Error(`You defined a loader for route "${match.route.path}" but didn't return ` +
93
+ error = new Error(`You defined a loader for route "${match.route.path}" but didn't return ` +
93
94
  `anything from your \`loader\` function. Please return a value or \`null\`.`);
94
95
  }
95
- res = createJson(value);
96
+ else {
97
+ res = createJson(value);
98
+ }
96
99
  }
97
- catch (error) {
98
- if (process.env.NODE_ENV === 'development' && !isResponse(error)) {
99
- console.error(`loader function error of route "${match.route.path}"`);
100
+ catch (err) {
101
+ if (process.env.NODE_ENV === 'development' && !isResponse(err)) {
102
+ console.error(`error occurs in loader function of route "${match.route.path}"`);
100
103
  }
104
+ error = err;
105
+ }
106
+ if (error) {
101
107
  throw error;
102
108
  }
103
109
  return res;
@@ -1,4 +1,4 @@
1
1
  import { ApplicationImpl } from '../shared/application';
2
- import { ApplicationOptions } from './shared';
2
+ import { ApplicationlOptions } from './shared';
3
3
  export { ApplicationImpl };
4
- export default function application<C extends {}>(options: ApplicationOptions<C>): ApplicationImpl<C>;
4
+ export default function application<C extends {}>(options: ApplicationlOptions<C>): ApplicationImpl<C>;
@@ -1,6 +1,6 @@
1
1
  import { ApplicationImpl } from '../shared/application';
2
2
  export { ApplicationImpl };
3
3
  export default function application(options) {
4
- const application = new ApplicationImpl(Object.assign({}, options));
4
+ const application = new ApplicationImpl(Object.assign(Object.assign({}, options), { getLoaders: () => Promise.resolve({}) }));
5
5
  return application;
6
6
  }
@@ -1,4 +1,4 @@
1
1
  import { ApplicationImpl } from '../shared/application';
2
- import { ApplicationOptions } from './shared';
2
+ import { ApplicationlOptions } from './shared';
3
3
  export { ApplicationImpl };
4
- export default function application<C extends {}>(options: ApplicationOptions<C>): ApplicationImpl<C>;
4
+ export default function application<C extends {}>(options: ApplicationlOptions<C>): ApplicationImpl<C>;
@@ -1,3 +1,12 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
1
10
  import * as customApp from '@shuvi/app/user/app';
2
11
  import { pluginRecord } from '@shuvi/app/core/plugins';
3
12
  import { ApplicationImpl } from '../shared/application';
@@ -29,6 +38,14 @@ function getPlugins(runtime, pluginRecords) {
29
38
  return plugins;
30
39
  }
31
40
  export default function application(options) {
32
- const application = new ApplicationImpl(Object.assign(Object.assign({}, options), { plugins: getPlugins(customApp, pluginRecord) }));
41
+ const application = new ApplicationImpl(Object.assign(Object.assign({}, options), { getLoaders: () => __awaiter(this, void 0, void 0, function* () {
42
+ const mod = yield import(
43
+ /* webpackChunkName: "loaders" */
44
+ '@shuvi/app/files/page-loaders');
45
+ return mod.default || mod;
46
+ }), plugins: getPlugins(customApp, pluginRecord) }));
33
47
  return application;
34
48
  }
49
+ if (module.hot) {
50
+ module.hot.accept('@shuvi/app/files/page-loaders');
51
+ }
@@ -84,7 +84,7 @@ exports.getRawRoutesFromDir = getRawRoutesFromDir;
84
84
  const getPageRoutes = (dir, { includes, excludes } = {}) => __awaiter(void 0, void 0, void 0, function* () {
85
85
  let raw;
86
86
  if (typeof dir === 'string') {
87
- raw = yield (0, exports.getRawRoutesFromDir)(dir, { excludes });
87
+ raw = yield (0, exports.getRawRoutesFromDir)(dir, { includes, excludes });
88
88
  }
89
89
  else {
90
90
  raw = dir;
@@ -131,7 +131,7 @@ exports.getPageRoutes = getPageRoutes;
131
131
  const getApiRoutes = (dir, { includes, excludes } = {}) => __awaiter(void 0, void 0, void 0, function* () {
132
132
  let raw;
133
133
  if (typeof dir === 'string') {
134
- raw = yield (0, exports.getRawRoutesFromDir)(dir, { excludes });
134
+ raw = yield (0, exports.getRawRoutesFromDir)(dir, { includes, excludes });
135
135
  }
136
136
  else {
137
137
  raw = dir;
@@ -184,7 +184,7 @@ exports.getApiRoutes = getApiRoutes;
184
184
  const getMiddlewareRoutes = (dir, { includes, excludes } = {}) => __awaiter(void 0, void 0, void 0, function* () {
185
185
  let raw;
186
186
  if (typeof dir === 'string') {
187
- raw = yield (0, exports.getRawRoutesFromDir)(dir, { excludes });
187
+ raw = yield (0, exports.getRawRoutesFromDir)(dir, { includes, excludes });
188
188
  }
189
189
  else {
190
190
  raw = dir;
@@ -1,5 +1,5 @@
1
1
  import { IRouter, IPageRouteRecord } from './routerTypes';
2
- import { Doura, Application, IAppContext, ApplicationOptions, IRerenderConfig, IError } from './applicationTypes';
2
+ import { Doura, Application, IAppContext, ApplicationInternalOptions, IRerenderConfig, IError } from './applicationTypes';
3
3
  export declare class ApplicationImpl<Config extends {} = {}> {
4
4
  private _router;
5
5
  private _appComponent;
@@ -9,7 +9,8 @@ export declare class ApplicationImpl<Config extends {} = {}> {
9
9
  private _store;
10
10
  private _error;
11
11
  private _loader;
12
- constructor(options: ApplicationOptions<Config>);
12
+ private _getLoaders;
13
+ constructor(options: ApplicationInternalOptions<Config>);
13
14
  get router(): IRouter<IPageRouteRecord>;
14
15
  get config(): Config;
15
16
  get context(): IAppContext;
@@ -18,6 +19,7 @@ export declare class ApplicationImpl<Config extends {} = {}> {
18
19
  get error(): IError | null;
19
20
  setError(err: IError): void;
20
21
  clearError(): void;
22
+ getLoaders(): Promise<Record<string, import("./index").Loader<any>>>;
21
23
  getLoadersData(): Record<string, any>;
22
24
  setLoadersData(datas: Record<string, any>): void;
23
25
  init(): Promise<void>;
@@ -23,6 +23,7 @@ class ApplicationImpl {
23
23
  this._store = (0, doura_1.doura)({ initialState: options.initialState });
24
24
  this._error = this._store.getModel(error_1.errorModelName, error_1.errorModel);
25
25
  this._loader = this._store.getModel(loader_1.loaderModelName, loader_1.loaderModel);
26
+ this._getLoaders = options.getLoaders;
26
27
  this._appComponent = options.AppComponent;
27
28
  this._pluginManager = (0, runtimPlugin_1.getManager)();
28
29
  (0, runtimPlugin_2.initPlugins)(this._pluginManager, options.plugins || []);
@@ -51,6 +52,9 @@ class ApplicationImpl {
51
52
  clearError() {
52
53
  this._error.clear();
53
54
  }
55
+ getLoaders() {
56
+ return this._getLoaders();
57
+ }
54
58
  getLoadersData() {
55
59
  return this._loader.getAllData;
56
60
  }
@@ -2,6 +2,8 @@ import { Doura } from 'doura';
2
2
  import { CustomAppContext } from '@shuvi/runtime';
3
3
  import { IRouter, IPageRouteRecord } from './routerTypes';
4
4
  import { IPluginList } from './runtimPlugin';
5
+ import { Loader } from './loader';
6
+ export declare type Loaders = Record<string, Loader>;
5
7
  export interface IAppContext extends CustomAppContext {
6
8
  [x: string]: unknown;
7
9
  }
@@ -35,10 +37,15 @@ export interface Application<Config extends {} = {}> {
35
37
  getLoadersData(): Record<string, any>;
36
38
  setLoadersData(datas: Record<string, any>): void;
37
39
  }
38
- export interface ApplicationOptions<C extends {}> {
40
+ export interface GetLoadersFn {
41
+ (): Promise<Record<string, Loader>>;
42
+ }
43
+ export interface ApplicationInternalOptions<C extends {}> {
39
44
  config: C;
40
45
  router: IRouter;
41
46
  AppComponent: any;
47
+ getLoaders: GetLoadersFn;
42
48
  initialState?: IAppState;
43
49
  plugins?: IPluginList;
44
50
  }
51
+ export declare type ApplicationlOptions<C extends {}> = Omit<ApplicationInternalOptions<C>, 'getLoaders' | 'plugins'>;
@@ -91,20 +91,26 @@ function runLoaders(matches, loadersByRouteId, { pathname, query, params, req, g
91
91
  return;
92
92
  }
93
93
  let res;
94
+ let error;
94
95
  try {
95
96
  const value = yield loaderFn(Object.assign({ pathname,
96
97
  params,
97
98
  query, redirect: redirectHelper, error: errorHelper, appContext }, (req ? { req } : {})));
98
99
  if (value === undefined) {
99
- throw new Error(`You defined a loader for route "${match.route.path}" but didn't return ` +
100
+ error = new Error(`You defined a loader for route "${match.route.path}" but didn't return ` +
100
101
  `anything from your \`loader\` function. Please return a value or \`null\`.`);
101
102
  }
102
- res = (0, response_1.json)(value);
103
+ else {
104
+ res = (0, response_1.json)(value);
105
+ }
103
106
  }
104
- catch (error) {
105
- if (process.env.NODE_ENV === 'development' && !(0, response_1.isResponse)(error)) {
106
- console.error(`loader function error of route "${match.route.path}"`);
107
+ catch (err) {
108
+ if (process.env.NODE_ENV === 'development' && !(0, response_1.isResponse)(err)) {
109
+ console.error(`error occurs in loader function of route "${match.route.path}"`);
107
110
  }
111
+ error = err;
112
+ }
113
+ if (error) {
108
114
  throw error;
109
115
  }
110
116
  return res;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shuvi/platform-shared",
3
- "version": "1.0.18",
3
+ "version": "1.0.20",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/shuvijs/shuvi.git",
@@ -83,17 +83,17 @@
83
83
  "node": ">= 16.0.0"
84
84
  },
85
85
  "dependencies": {
86
- "@shuvi/hook": "1.0.18",
87
- "doura": "0.0.8",
88
- "@shuvi/router": "1.0.18",
89
- "@shuvi/runtime": "1.0.18",
90
- "@shuvi/shared": "1.0.18",
91
- "@shuvi/toolpack": "1.0.18",
92
- "@shuvi/utils": "1.0.18",
86
+ "@shuvi/hook": "1.0.20",
87
+ "doura": "0.0.9",
88
+ "@shuvi/router": "1.0.20",
89
+ "@shuvi/runtime": "1.0.20",
90
+ "@shuvi/shared": "1.0.20",
91
+ "@shuvi/toolpack": "1.0.20",
92
+ "@shuvi/utils": "1.0.20",
93
93
  "redux": "4.1.2"
94
94
  },
95
95
  "peerDependencies": {
96
- "@shuvi/service": "1.0.18"
96
+ "@shuvi/service": "1.0.20"
97
97
  },
98
98
  "devDependencies": {
99
99
  "@shuvi/service": "workspace:*",
@@ -42,3 +42,10 @@ declare module '@shuvi/app/user/app' {
42
42
  export const appContext: IAppModule['appContext'];
43
43
  export const dispose: IAppModule['dispose'];
44
44
  }
45
+
46
+ declare module '@shuvi/app/files/page-loaders' {
47
+ import { Loaders } from '@shuvi/platform-shared/shared';
48
+
49
+ const loaders: Loaders;
50
+ export default loaders;
51
+ }