@shuvi/service 0.0.1-pre.1

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 (192) hide show
  1. package/lib/api/api.d.ts +75 -0
  2. package/lib/api/api.js +370 -0
  3. package/lib/api/index.d.ts +6 -0
  4. package/lib/api/index.js +4 -0
  5. package/lib/api/initCoreResource.d.ts +2 -0
  6. package/lib/api/initCoreResource.js +27 -0
  7. package/lib/api/paths.d.ts +8 -0
  8. package/lib/api/paths.js +20 -0
  9. package/lib/api/plugin.d.ts +6 -0
  10. package/lib/api/plugin.js +108 -0
  11. package/lib/api/pluginApi.d.ts +29 -0
  12. package/lib/api/pluginApi.js +44 -0
  13. package/lib/api/setupApp.d.ts +2 -0
  14. package/lib/api/setupApp.js +132 -0
  15. package/lib/api/types.d.ts +152 -0
  16. package/lib/api/types.js +2 -0
  17. package/lib/bundler/bundler.d.ts +33 -0
  18. package/lib/bundler/bundler.js +293 -0
  19. package/lib/bundler/config.d.ts +18 -0
  20. package/lib/bundler/config.js +85 -0
  21. package/lib/bundler/index.d.ts +2 -0
  22. package/lib/bundler/index.js +4 -0
  23. package/lib/bundler/runCompiler.d.ts +7 -0
  24. package/lib/bundler/runCompiler.js +43 -0
  25. package/lib/cli/build.d.ts +8 -0
  26. package/lib/cli/build.js +90 -0
  27. package/lib/config/index.d.ts +8 -0
  28. package/lib/config/index.js +50 -0
  29. package/lib/config/loadDotenvConfig.d.ts +1 -0
  30. package/lib/config/loadDotenvConfig.js +50 -0
  31. package/lib/constants.d.ts +14 -0
  32. package/lib/constants.js +20 -0
  33. package/lib/index.d.ts +5 -0
  34. package/lib/index.js +21 -0
  35. package/lib/lib/apiRouteHandler.d.ts +80 -0
  36. package/lib/lib/apiRouteHandler.js +216 -0
  37. package/lib/lib/apiRoutes.d.ts +11 -0
  38. package/lib/lib/apiRoutes.js +76 -0
  39. package/lib/lib/devMiddleware.d.ts +10 -0
  40. package/lib/lib/devMiddleware.js +81 -0
  41. package/lib/lib/getPlatform.d.ts +2 -0
  42. package/lib/lib/getPlatform.js +8 -0
  43. package/lib/lib/getPublicRuntimeConfig.d.ts +2 -0
  44. package/lib/lib/getPublicRuntimeConfig.js +14 -0
  45. package/lib/lib/hotMiddleware.d.ts +31 -0
  46. package/lib/lib/hotMiddleware.js +123 -0
  47. package/lib/lib/httpProxyMiddleware.d.ts +7 -0
  48. package/lib/lib/httpProxyMiddleware.js +49 -0
  49. package/lib/lib/initRuntime.d.ts +5 -0
  50. package/lib/lib/initRuntime.js +16 -0
  51. package/lib/lib/launchEditorMiddleware.d.ts +2 -0
  52. package/lib/lib/launchEditorMiddleware.js +39 -0
  53. package/lib/lib/matchRoutes.d.ts +2 -0
  54. package/lib/lib/matchRoutes.js +8 -0
  55. package/lib/lib/onDemandRouteManager.d.ts +11 -0
  56. package/lib/lib/onDemandRouteManager.js +69 -0
  57. package/lib/lib/renderToHTML.d.ts +10 -0
  58. package/lib/lib/renderToHTML.js +52 -0
  59. package/lib/lib/resolveRuntimeCoreFile.d.ts +2 -0
  60. package/lib/lib/resolveRuntimeCoreFile.js +17 -0
  61. package/lib/lib/routes.d.ts +12 -0
  62. package/lib/lib/routes.js +90 -0
  63. package/lib/lib/runtimeConfig.d.ts +4 -0
  64. package/lib/lib/runtimeConfig.js +10 -0
  65. package/lib/lib/serveStatic.d.ts +3 -0
  66. package/lib/lib/serveStatic.js +24 -0
  67. package/lib/lib/utils.d.ts +6 -0
  68. package/lib/lib/utils.js +25 -0
  69. package/lib/lib/viewTemplate.d.ts +5 -0
  70. package/lib/lib/viewTemplate.js +16 -0
  71. package/lib/project/file-manager/errorHandling.d.ts +1 -0
  72. package/lib/project/file-manager/errorHandling.js +13 -0
  73. package/lib/project/file-manager/file.d.ts +17 -0
  74. package/lib/project/file-manager/file.js +146 -0
  75. package/lib/project/file-manager/fileManager.d.ts +11 -0
  76. package/lib/project/file-manager/fileManager.js +86 -0
  77. package/lib/project/file-manager/fileTypes.d.ts +56 -0
  78. package/lib/project/file-manager/fileTypes.js +2 -0
  79. package/lib/project/file-manager/index.d.ts +7 -0
  80. package/lib/project/file-manager/index.js +9 -0
  81. package/lib/project/file-manager/lifecycle.d.ts +7 -0
  82. package/lib/project/file-manager/lifecycle.js +42 -0
  83. package/lib/project/file-manager/mount.d.ts +3 -0
  84. package/lib/project/file-manager/mount.js +80 -0
  85. package/lib/project/file-manager/scheduler.d.ts +35 -0
  86. package/lib/project/file-manager/scheduler.js +169 -0
  87. package/lib/project/file-manager/utils.d.ts +1 -0
  88. package/lib/project/file-manager/utils.js +7 -0
  89. package/lib/project/file-presets/files/core/404.js.d.ts +4 -0
  90. package/lib/project/file-presets/files/core/404.js.js +9 -0
  91. package/lib/project/file-presets/files/core/apiRoutes.js.d.ts +5 -0
  92. package/lib/project/file-presets/files/core/apiRoutes.js.js +5 -0
  93. package/lib/project/file-presets/files/core/app.js.d.ts +4 -0
  94. package/lib/project/file-presets/files/core/app.js.js +9 -0
  95. package/lib/project/file-presets/files/core/client/application.js.d.ts +5 -0
  96. package/lib/project/file-presets/files/core/client/application.js.js +7 -0
  97. package/lib/project/file-presets/files/core/client/entry.js.d.ts +5 -0
  98. package/lib/project/file-presets/files/core/client/entry.js.js +7 -0
  99. package/lib/project/file-presets/files/core/platform.js.d.ts +5 -0
  100. package/lib/project/file-presets/files/core/platform.js.js +7 -0
  101. package/lib/project/file-presets/files/core/plugins.js.d.ts +5 -0
  102. package/lib/project/file-presets/files/core/plugins.js.js +16 -0
  103. package/lib/project/file-presets/files/core/polyfill.js.d.ts +5 -0
  104. package/lib/project/file-presets/files/core/polyfill.js.js +5 -0
  105. package/lib/project/file-presets/files/core/routes.js.d.ts +5 -0
  106. package/lib/project/file-presets/files/core/routes.js.js +5 -0
  107. package/lib/project/file-presets/files/core/runtimeConfig.js.d.ts +5 -0
  108. package/lib/project/file-presets/files/core/runtimeConfig.js.js +5 -0
  109. package/lib/project/file-presets/files/core/server/application.js.d.ts +5 -0
  110. package/lib/project/file-presets/files/core/server/application.js.js +7 -0
  111. package/lib/project/file-presets/files/core/setRuntimeConfig.js.d.ts +4 -0
  112. package/lib/project/file-presets/files/core/setRuntimeConfig.js.js +5 -0
  113. package/lib/project/file-presets/files/entry.client-wrapper.js.d.ts +5 -0
  114. package/lib/project/file-presets/files/entry.client-wrapper.js.js +5 -0
  115. package/lib/project/file-presets/files/entry.client.js.d.ts +5 -0
  116. package/lib/project/file-presets/files/entry.client.js.js +25 -0
  117. package/lib/project/file-presets/files/entry.server.js.d.ts +5 -0
  118. package/lib/project/file-presets/files/entry.server.js.js +6 -0
  119. package/lib/project/file-presets/files/index.d.ts.d.ts +5 -0
  120. package/lib/project/file-presets/files/index.d.ts.js +6 -0
  121. package/lib/project/file-presets/files/index.js.d.ts +5 -0
  122. package/lib/project/file-presets/files/index.js.js +6 -0
  123. package/lib/project/file-presets/files/main.server.js.d.ts +4 -0
  124. package/lib/project/file-presets/files/main.server.js.js +13 -0
  125. package/lib/project/file-presets/files/user/404.js.d.ts +7 -0
  126. package/lib/project/file-presets/files/user/404.js.js +9 -0
  127. package/lib/project/file-presets/files/user/app.js.d.ts +7 -0
  128. package/lib/project/file-presets/files/user/app.js.js +9 -0
  129. package/lib/project/file-presets/files/user/document.js.d.ts +7 -0
  130. package/lib/project/file-presets/files/user/document.js.js +9 -0
  131. package/lib/project/file-presets/files/user/plugin.js.d.ts +7 -0
  132. package/lib/project/file-presets/files/user/plugin.js.js +9 -0
  133. package/lib/project/file-presets/files/user/server.js.d.ts +7 -0
  134. package/lib/project/file-presets/files/user/server.js.js +9 -0
  135. package/lib/project/file-presets/index.d.ts +2 -0
  136. package/lib/project/file-presets/index.js +33 -0
  137. package/lib/project/file-snippets/helpers.d.ts +9 -0
  138. package/lib/project/file-snippets/helpers.js +72 -0
  139. package/lib/project/file-snippets/index.d.ts +15 -0
  140. package/lib/project/file-snippets/index.js +22 -0
  141. package/lib/project/file-snippets/moduleExportProxy.d.ts +9 -0
  142. package/lib/project/file-snippets/moduleExportProxy.js +124 -0
  143. package/lib/project/index.d.ts +3 -0
  144. package/lib/project/index.js +15 -0
  145. package/lib/project/projectBuilder.d.ts +36 -0
  146. package/lib/project/projectBuilder.js +199 -0
  147. package/lib/project/projectContext.d.ts +66 -0
  148. package/lib/project/projectContext.js +30 -0
  149. package/lib/renderer/base.d.ts +22 -0
  150. package/lib/renderer/base.js +147 -0
  151. package/lib/renderer/htmlTag.d.ts +4 -0
  152. package/lib/renderer/htmlTag.js +69 -0
  153. package/lib/renderer/index.d.ts +11 -0
  154. package/lib/renderer/index.js +20 -0
  155. package/lib/renderer/spa.d.ts +10 -0
  156. package/lib/renderer/spa.js +20 -0
  157. package/lib/renderer/ssr.d.ts +15 -0
  158. package/lib/renderer/ssr.js +76 -0
  159. package/lib/renderer/types.d.ts +14 -0
  160. package/lib/renderer/types.js +2 -0
  161. package/lib/route/index.d.ts +1 -0
  162. package/lib/route/index.js +4 -0
  163. package/lib/route/parseDynamicPath.d.ts +1 -0
  164. package/lib/route/parseDynamicPath.js +42 -0
  165. package/lib/route/route.d.ts +13 -0
  166. package/lib/route/route.js +161 -0
  167. package/lib/server/index.d.ts +2 -0
  168. package/lib/server/index.js +4 -0
  169. package/lib/server/router.d.ts +14 -0
  170. package/lib/server/router.js +111 -0
  171. package/lib/server/server.d.ts +17 -0
  172. package/lib/server/server.js +139 -0
  173. package/lib/server/serverTypes.d.ts +9 -0
  174. package/lib/server/serverTypes.js +2 -0
  175. package/lib/shuvi/index.d.ts +10 -0
  176. package/lib/shuvi/index.js +18 -0
  177. package/lib/shuvi/shuvi.base.d.ts +26 -0
  178. package/lib/shuvi/shuvi.base.js +152 -0
  179. package/lib/shuvi/shuvi.dev.d.ts +9 -0
  180. package/lib/shuvi/shuvi.dev.js +94 -0
  181. package/lib/shuvi/shuvi.prod.d.ts +6 -0
  182. package/lib/shuvi/shuvi.prod.js +55 -0
  183. package/lib/types/bundler.d.ts +1 -0
  184. package/lib/types/bundler.js +10 -0
  185. package/lib/types/hooks.d.ts +67 -0
  186. package/lib/types/hooks.js +2 -0
  187. package/lib/types/index.d.ts +3 -0
  188. package/lib/types/index.js +12 -0
  189. package/lib/types/runtime.d.ts +168 -0
  190. package/lib/types/runtime.js +2 -0
  191. package/package.json +65 -0
  192. package/types/app-env.d.ts +66 -0
@@ -0,0 +1,39 @@
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 launch_editor_1 = __importDefault(require("launch-editor"));
7
+ function getSourcePath(source) {
8
+ // Webpack prefixes certain source paths with this path
9
+ if (source.startsWith('webpack:///')) {
10
+ return source.substring(11);
11
+ }
12
+ // Make sure library name is filtered out as well
13
+ if (source.startsWith('webpack://_N_E/')) {
14
+ return source.substring(15);
15
+ }
16
+ if (source.startsWith('webpack://')) {
17
+ return source.substring(10);
18
+ }
19
+ if (source.startsWith('/')) {
20
+ return source.substring(1);
21
+ }
22
+ return source;
23
+ }
24
+ function createLaunchEditorMiddleware(launchEditorEndpoint) {
25
+ return function launchEditorMiddleware(req, res, next) {
26
+ if (req.url.startsWith(launchEditorEndpoint)) {
27
+ const { query } = req;
28
+ const lineNumber = parseInt(query.lineNumber, 10) || 1;
29
+ const colNumber = parseInt(query.colNumber, 10) || 1;
30
+ launch_editor_1.default(getSourcePath(`${query.fileName}:${lineNumber}:${colNumber}`));
31
+ res.end();
32
+ return;
33
+ }
34
+ else {
35
+ next();
36
+ }
37
+ };
38
+ }
39
+ exports.createLaunchEditorMiddleware = createLaunchEditorMiddleware;
@@ -0,0 +1,2 @@
1
+ import { PartialLocation, IRouteMatch, IRouteBaseObject } from '@shuvi/router';
2
+ export declare function matchRoutes<T extends IRouteBaseObject>(routes: T[], location: string | PartialLocation, basename?: string): IRouteMatch<T>[];
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const router_1 = require("@shuvi/router");
4
+ // matchRoutes can support any `routes` object
5
+ function matchRoutes(routes, location, basename = '') {
6
+ return router_1.matchRoutes(routes, location, basename) || [];
7
+ }
8
+ exports.matchRoutes = matchRoutes;
@@ -0,0 +1,11 @@
1
+ import { IRequestHandlerWithNext } from '../server';
2
+ import { DevMiddleware } from './devMiddleware';
3
+ import { Api } from '../api/api';
4
+ export declare class OnDemandRouteManager {
5
+ devMiddleware: DevMiddleware | null;
6
+ _api: Api;
7
+ constructor(api: Api);
8
+ getServerMiddleware(): IRequestHandlerWithNext;
9
+ ensureRoutes(pathname: string): Promise<void>;
10
+ private _activateModules;
11
+ }
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const module_replace_plugin_1 = __importDefault(require("@shuvi/toolpack/lib/webpack/plugins/module-replace-plugin"));
16
+ const constants_1 = require("../constants");
17
+ const router_1 = require("@shuvi/router");
18
+ class OnDemandRouteManager {
19
+ constructor(api) {
20
+ this.devMiddleware = null;
21
+ this._api = api;
22
+ }
23
+ getServerMiddleware() {
24
+ return (req, res, next) => __awaiter(this, void 0, void 0, function* () {
25
+ const pathname = req.pathname;
26
+ if (!pathname.startsWith(this._api.assetPublicPath)) {
27
+ return next();
28
+ }
29
+ if (!this.devMiddleware) {
30
+ return next();
31
+ }
32
+ const chunkName = pathname.replace(this._api.assetPublicPath, '');
33
+ const chunkInitiatorModule = this._api.clientManifest.chunkRequest[chunkName];
34
+ if (!chunkInitiatorModule) {
35
+ return next();
36
+ }
37
+ const task = module_replace_plugin_1.default.restoreModule(chunkInitiatorModule);
38
+ if (task) {
39
+ yield this.devMiddleware.invalidate();
40
+ yield task;
41
+ }
42
+ next();
43
+ });
44
+ }
45
+ ensureRoutes(pathname) {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ const matchedRoutes = router_1.matchRoutes(this._api.getRoutes(), pathname) || [];
48
+ const modulesToActivate = matchedRoutes
49
+ .map(({ route: { component } }) => component ? `${component}?${constants_1.ROUTE_RESOURCE_QUERYSTRING}` : '')
50
+ .filter(Boolean);
51
+ return this._activateModules(modulesToActivate);
52
+ });
53
+ }
54
+ _activateModules(modules) {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ if (!this.devMiddleware) {
57
+ return;
58
+ }
59
+ const tasks = modules
60
+ .map(m => module_replace_plugin_1.default.restoreModule(m))
61
+ .filter(Boolean);
62
+ if (tasks.length) {
63
+ this.devMiddleware.invalidate();
64
+ yield Promise.all(tasks);
65
+ }
66
+ });
67
+ }
68
+ }
69
+ exports.OnDemandRouteManager = OnDemandRouteManager;
@@ -0,0 +1,10 @@
1
+ import { Runtime } from '../types';
2
+ import { Api } from '../api';
3
+ export declare function renderToHTML({ req, api, onRedirect }: {
4
+ req: Runtime.IRequest;
5
+ api: Api;
6
+ onRedirect?(redirect: Runtime.IRenderResultRedirect): void;
7
+ }): Promise<{
8
+ html: string | null;
9
+ appContext: any;
10
+ }>;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const renderer_1 = require("../renderer");
13
+ function renderToHTML({ req, api, onRedirect }) {
14
+ return __awaiter(this, void 0, void 0, function* () {
15
+ let html = null;
16
+ const renderer = new renderer_1.Renderer({ api });
17
+ const { application, server: { render } } = api.resources.server;
18
+ const app = application.create({
19
+ req
20
+ }, {
21
+ render({ appContext, AppComponent, router }) {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ const result = yield renderer.renderDocument({
24
+ router,
25
+ app,
26
+ AppComponent,
27
+ appContext,
28
+ render
29
+ });
30
+ if (renderer_1.isRedirect(result)) {
31
+ onRedirect && onRedirect(result);
32
+ }
33
+ else {
34
+ html = result;
35
+ }
36
+ });
37
+ }
38
+ });
39
+ let appContext;
40
+ try {
41
+ appContext = yield app.run();
42
+ }
43
+ catch (error) {
44
+ throw error;
45
+ }
46
+ finally {
47
+ yield app.dispose();
48
+ }
49
+ return { appContext, html };
50
+ });
51
+ }
52
+ exports.renderToHTML = renderToHTML;
@@ -0,0 +1,2 @@
1
+ declare const _default: (...paths: string[]) => string;
2
+ export default _default;
@@ -0,0 +1,17 @@
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 path_1 = __importDefault(require("path"));
7
+ const runtimeCoreDir = path_1.default.dirname(require.resolve('@shuvi/platform-core/package.json'));
8
+ exports.default = (...paths) => {
9
+ const filePath = `${path_1.default.join(runtimeCoreDir, 'lib', ...paths)}`;
10
+ try {
11
+ require.resolve(filePath);
12
+ }
13
+ catch (_a) {
14
+ throw new Error(`[shuvi-app building] Module under @shuvi/platform-core does not exist (${filePath}). Please check the file path is correct.`);
15
+ }
16
+ return filePath;
17
+ };
@@ -0,0 +1,12 @@
1
+ import { IUserRouteConfig } from '../api';
2
+ import { IRouteRecord } from '@shuvi/router';
3
+ export declare type Templates<T extends {}> = {
4
+ [K in keyof T]?: (v: T[K], route: T & {
5
+ id: string;
6
+ }) => string;
7
+ };
8
+ export declare function serializeRoutes(routes: IUserRouteConfig[], parentPath?: string): string;
9
+ export declare function renameFilepathToComponent(routes: IRouteRecord[]): IUserRouteConfig[];
10
+ export declare function normalizeRoutes(routes: IUserRouteConfig[], option: {
11
+ componentDir: string;
12
+ }): IUserRouteConfig[];
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ const crypto_1 = require("crypto");
18
+ const path_1 = __importDefault(require("path"));
19
+ const constants_1 = require("../constants");
20
+ function genRouteId(filepath) {
21
+ return crypto_1.createHash('md4').update(filepath).digest('hex').substr(0, 4);
22
+ }
23
+ function serializeRoutes(routes, parentPath = '') {
24
+ let res = '';
25
+ for (let index = 0; index < routes.length; index++) {
26
+ const _a = routes[index], { children: childRoutes } = _a, route = __rest(_a, ["children"]);
27
+ const fullpath = route.path ? parentPath + '/' + route.path : parentPath;
28
+ const id = genRouteId(fullpath);
29
+ let strRoute = `id: ${JSON.stringify(id)},\n`;
30
+ const keys = Object.keys(route);
31
+ for (let index = 0; index < keys.length; index++) {
32
+ const key = keys[index];
33
+ if (key === 'component') {
34
+ const { component } = route;
35
+ const componentSource = component;
36
+ const componentSourceWithAffix = `${componentSource}?${constants_1.ROUTE_RESOURCE_QUERYSTRING}`;
37
+ strRoute += `__componentSourceWithAffix__: "${componentSourceWithAffix}",
38
+ __componentSource__: "${componentSource}",
39
+ __import__: () => import(/* webpackChunkName: "page-${id}" */"${componentSourceWithAffix}"),
40
+ __resolveWeak__: () => [require.resolveWeak("${componentSourceWithAffix}")]`.trim();
41
+ }
42
+ else {
43
+ strRoute += `${key}: ${JSON.stringify(route[key])}`;
44
+ }
45
+ strRoute += `,\n`;
46
+ }
47
+ if (childRoutes && childRoutes.length > 0) {
48
+ strRoute += `children: ${serializeRoutes(childRoutes, fullpath)},\n`;
49
+ }
50
+ res += `{${strRoute}},\n`;
51
+ }
52
+ return `[${res}]`;
53
+ }
54
+ exports.serializeRoutes = serializeRoutes;
55
+ function renameFilepathToComponent(routes) {
56
+ const res = [];
57
+ for (let index = 0; index < routes.length; index++) {
58
+ const { path, filepath, children } = routes[index];
59
+ const route = {
60
+ path
61
+ };
62
+ if (filepath) {
63
+ route.component = filepath;
64
+ }
65
+ if (children && children.length > 0) {
66
+ route.children = renameFilepathToComponent(children);
67
+ }
68
+ res.push(route);
69
+ }
70
+ return res;
71
+ }
72
+ exports.renameFilepathToComponent = renameFilepathToComponent;
73
+ function normalizeRoutes(routes, option) {
74
+ const res = [];
75
+ for (let index = 0; index < routes.length; index++) {
76
+ const route = Object.assign({}, routes[index]);
77
+ if (route.component) {
78
+ const absPath = path_1.default.isAbsolute(route.component)
79
+ ? route.component
80
+ : path_1.default.resolve(option.componentDir, route.component);
81
+ route.component = absPath.replace(/\\/g, '/');
82
+ }
83
+ if (route.children && route.children.length > 0) {
84
+ route.children = normalizeRoutes(route.children, option);
85
+ }
86
+ res.push(route);
87
+ }
88
+ return res;
89
+ }
90
+ exports.normalizeRoutes = normalizeRoutes;
@@ -0,0 +1,4 @@
1
+ import { IRuntimeConfig } from '../api';
2
+ declare const _default: () => Record<string, string>;
3
+ export default _default;
4
+ export declare function setRuntimeConfig(config: IRuntimeConfig): void;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ let runtimeConfig;
4
+ exports.default = () => {
5
+ return runtimeConfig || {};
6
+ };
7
+ function setRuntimeConfig(config) {
8
+ runtimeConfig = config;
9
+ }
10
+ exports.setRuntimeConfig = setRuntimeConfig;
@@ -0,0 +1,3 @@
1
+ /// <reference types="node" />
2
+ import { IncomingMessage, ServerResponse } from 'http';
3
+ export declare function serveStatic(req: IncomingMessage, res: ServerResponse, path: string): Promise<void>;
@@ -0,0 +1,24 @@
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 send_1 = __importDefault(require("send"));
7
+ function serveStatic(req, res, path) {
8
+ return new Promise((resolve, reject) => {
9
+ send_1.default(req, path)
10
+ .on('file', () => {
11
+ res.statusCode = 200;
12
+ })
13
+ .on('directory', () => {
14
+ // We don't allow directories to be read.
15
+ const err = new Error('No directory access');
16
+ err.code = 'ENOENT';
17
+ reject(err);
18
+ })
19
+ .on('error', reject)
20
+ .pipe(res)
21
+ .on('finish', resolve);
22
+ });
23
+ }
24
+ exports.serveStatic = serveStatic;
@@ -0,0 +1,6 @@
1
+ /// <reference types="node" />
2
+ import { IncomingMessage, ServerResponse } from 'http';
3
+ export declare function acceptsHtml(header: string, { htmlAcceptHeaders }?: {
4
+ htmlAcceptHeaders?: string[];
5
+ }): boolean;
6
+ export declare function sendHTML(req: IncomingMessage, res: ServerResponse, html: string): void;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function acceptsHtml(header, { htmlAcceptHeaders = ['text/html', '*/*'] } = {}) {
4
+ for (var i = 0; i < htmlAcceptHeaders.length; i++) {
5
+ if (header.indexOf(htmlAcceptHeaders[i]) !== -1) {
6
+ return true;
7
+ }
8
+ }
9
+ return false;
10
+ }
11
+ exports.acceptsHtml = acceptsHtml;
12
+ function sendHTML(req, res, html) {
13
+ if (res.writableEnded || res.headersSent)
14
+ return;
15
+ if (!res.getHeader('Content-Type')) {
16
+ res.setHeader('Content-Type', 'text/html; charset=utf-8');
17
+ }
18
+ const buffer = Buffer.from(html);
19
+ res.setHeader('Content-Length', buffer.length);
20
+ // ctx.body will set ctx.status to 200, if ctx.status is not set before
21
+ if (!res.statusCode)
22
+ res.statusCode = 200;
23
+ res.end(req.method === 'HEAD' ? null : buffer);
24
+ }
25
+ exports.sendHTML = sendHTML;
@@ -0,0 +1,5 @@
1
+ import { TemplateFunction } from 'ejs';
2
+ export { TemplateFunction };
3
+ declare function parseTemplateFile(templateFile: string): TemplateFunction;
4
+ declare function renderTemplate(templateFn: TemplateFunction, data: any): string;
5
+ export { parseTemplateFile, renderTemplate };
@@ -0,0 +1,16 @@
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 ejs_1 = __importDefault(require("ejs"));
7
+ const fs_1 = __importDefault(require("fs"));
8
+ function parseTemplateFile(templateFile) {
9
+ const content = fs_1.default.readFileSync(templateFile, 'utf8');
10
+ return ejs_1.default.compile(content);
11
+ }
12
+ exports.parseTemplateFile = parseTemplateFile;
13
+ function renderTemplate(templateFn, data) {
14
+ return templateFn(data);
15
+ }
16
+ exports.renderTemplate = renderTemplate;
@@ -0,0 +1 @@
1
+ export declare function callWithErrorHandling(fn: Function, args?: unknown[]): any;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function callWithErrorHandling(fn, args) {
4
+ let res;
5
+ try {
6
+ res = args ? fn(...args) : fn();
7
+ }
8
+ catch (err) {
9
+ console.error(err);
10
+ }
11
+ return res;
12
+ }
13
+ exports.callWithErrorHandling = callWithErrorHandling;
@@ -0,0 +1,17 @@
1
+ import { ReactiveEffect } from '@vue/reactivity';
2
+ import { FileOptions, FileInternalInstance } from './fileTypes';
3
+ export * from './fileTypes';
4
+ export declare let currentInstance: FileInternalInstance | null;
5
+ export declare const getCurrentInstance: () => FileInternalInstance | null;
6
+ export declare const setCurrentInstance: (instance: FileInternalInstance | null) => void;
7
+ export declare function recordInstanceBoundEffect(effect: ReactiveEffect): void;
8
+ interface ComponentRenderContext {
9
+ [key: string]: any;
10
+ _: FileInternalInstance;
11
+ }
12
+ export declare const PublicInstanceProxyHandlers: {
13
+ get({ _: instance }: ComponentRenderContext, key: string): any;
14
+ set({ _: instance }: ComponentRenderContext, key: string, value: any): boolean;
15
+ has({ _: { setupState, accessCache, ctx } }: ComponentRenderContext, key: string): boolean;
16
+ };
17
+ export declare function createFileInstance(options: FileOptions): FileInternalInstance;
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utils_1 = require("@shuvi/utils");
4
+ const lifecycle_1 = require("./lifecycle");
5
+ const scheduler_1 = require("./scheduler");
6
+ const AccessTypes = {
7
+ SETUP: 0,
8
+ PROPS: 1,
9
+ CONTEXT: 2,
10
+ OTHER: 3,
11
+ LISTENERS: 4
12
+ };
13
+ exports.currentInstance = null;
14
+ exports.getCurrentInstance = () => exports.currentInstance;
15
+ exports.setCurrentInstance = (instance) => {
16
+ exports.currentInstance = instance;
17
+ };
18
+ // record effects created during a component's setup() so that they can be
19
+ // stopped when the component unmounts
20
+ function recordInstanceBoundEffect(effect) {
21
+ if (exports.currentInstance) {
22
+ (exports.currentInstance.effects || (exports.currentInstance.effects = [])).push(effect);
23
+ }
24
+ }
25
+ exports.recordInstanceBoundEffect = recordInstanceBoundEffect;
26
+ const publicPropertiesMap = utils_1.extend(Object.create(null), {
27
+ $setup: i => i.setupState,
28
+ $forceUpdate: i => () => scheduler_1.queueJob(i.update)
29
+ });
30
+ exports.PublicInstanceProxyHandlers = {
31
+ get({ _: instance }, key) {
32
+ const { ctx, setupState, accessCache } = instance;
33
+ // let @vue/reactivity know it should never observe Vue public instances.
34
+ if (key === '__v_skip' /* ReactiveFlags.SKIP */) {
35
+ return true;
36
+ }
37
+ const publicGetter = publicPropertiesMap[key];
38
+ // public $xxx properties
39
+ if (publicGetter) {
40
+ return publicGetter(instance);
41
+ }
42
+ // data / props / ctx
43
+ // This getter gets called for every property access on the render context
44
+ // during render and is a major hotspot. The most expensive part of this
45
+ // is the multiple hasOwn() calls. It's much faster to do a simple property
46
+ // access on a plain object, so we use an accessCache object (with null
47
+ // prototype) to memoize what access type a key corresponds to.
48
+ if (key[0] !== '$') {
49
+ const n = accessCache[key];
50
+ if (n !== undefined) {
51
+ switch (n) {
52
+ case AccessTypes.SETUP:
53
+ return setupState[key];
54
+ case AccessTypes.CONTEXT:
55
+ return ctx[key];
56
+ // default: just fallthrough
57
+ }
58
+ }
59
+ else if (setupState !== utils_1.EMPTY_OBJ && utils_1.hasOwn(setupState, key)) {
60
+ accessCache[key] = AccessTypes.SETUP;
61
+ return setupState[key];
62
+ }
63
+ else if (ctx !== utils_1.EMPTY_OBJ && utils_1.hasOwn(ctx, key)) {
64
+ accessCache[key] = AccessTypes.CONTEXT;
65
+ return ctx[key];
66
+ }
67
+ }
68
+ if (ctx !== utils_1.EMPTY_OBJ && utils_1.hasOwn(ctx, key)) {
69
+ // user may set custom properties to `this` that start with `$`
70
+ accessCache[key] = AccessTypes.CONTEXT;
71
+ return ctx[key];
72
+ }
73
+ },
74
+ set({ _: instance }, key, value) {
75
+ const { setupState, ctx } = instance;
76
+ if (setupState !== utils_1.EMPTY_OBJ && utils_1.hasOwn(setupState, key)) {
77
+ setupState[key] = value;
78
+ }
79
+ if (key[0] === '$' && key.slice(1) in instance) {
80
+ return false;
81
+ }
82
+ else {
83
+ ctx[key] = value;
84
+ }
85
+ return true;
86
+ },
87
+ has({ _: { setupState, accessCache, ctx } }, key) {
88
+ return (accessCache[key] !== undefined ||
89
+ (setupState !== utils_1.EMPTY_OBJ && utils_1.hasOwn(setupState, key)) ||
90
+ utils_1.hasOwn(ctx, key) ||
91
+ utils_1.hasOwn(publicPropertiesMap, key));
92
+ }
93
+ };
94
+ function createFileInstance(options) {
95
+ const instance = {
96
+ type: options,
97
+ name: options.name,
98
+ content: options.content,
99
+ update: null,
100
+ destroy: null,
101
+ proxy: null,
102
+ accessCache: {},
103
+ // state
104
+ ctx: utils_1.EMPTY_OBJ,
105
+ setupState: utils_1.EMPTY_OBJ,
106
+ // lifecycle
107
+ isMounted: false,
108
+ isUnmounted: false,
109
+ mounted: [],
110
+ unmounted: []
111
+ };
112
+ instance.ctx = { _: instance };
113
+ instance.proxy = new Proxy(instance.ctx, exports.PublicInstanceProxyHandlers);
114
+ const { setup } = options;
115
+ exports.currentInstance = instance;
116
+ if (setup) {
117
+ const setupResult = setup();
118
+ if (utils_1.isObject(setupResult)) {
119
+ instance.setupState = setupResult;
120
+ }
121
+ }
122
+ applyOptions(instance, options);
123
+ exports.currentInstance = null;
124
+ return instance;
125
+ }
126
+ exports.createFileInstance = createFileInstance;
127
+ function applyOptions(instance, options) {
128
+ const publicThis = instance.proxy;
129
+ const { methods: methodsOptions,
130
+ // lifecycle
131
+ mounted, unmounted } = options;
132
+ const ctx = instance.ctx;
133
+ if (methodsOptions) {
134
+ const publicThis = instance.proxy;
135
+ for (const key in methodsOptions) {
136
+ // todo: check duplicated
137
+ ctx[key] = methodsOptions[key].bind(publicThis);
138
+ }
139
+ }
140
+ if (mounted) {
141
+ lifecycle_1.injectHook('mounted', mounted.bind(publicThis));
142
+ }
143
+ if (unmounted) {
144
+ lifecycle_1.injectHook('unmounted', unmounted.bind(publicThis));
145
+ }
146
+ }
@@ -0,0 +1,11 @@
1
+ import { FileOptions } from './file';
2
+ export interface FileManager {
3
+ addFile(options: FileOptions): void;
4
+ mount(dir: string): Promise<void>;
5
+ unmount(): Promise<void>;
6
+ }
7
+ export interface FileManagerOptions {
8
+ watch?: boolean;
9
+ context?: any;
10
+ }
11
+ export declare function getFileManager({ watch, context }: FileManagerOptions): FileManager;