@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,75 @@
1
+ import { IApiConfig, IConfig, IApi, IApiRouteConfig, IUserRouteConfig, IPaths, IShuviMode, IPhase } from './types';
2
+ import { Bundler } from '../types/bundler';
3
+ import * as Runtime from '../types/runtime';
4
+ import { UserModule, TargetModule, FileOptions } from '../project';
5
+ import { Hookable } from '@shuvi/hook';
6
+ import { IBuiltResource } from './types';
7
+ import { Server } from '../server';
8
+ import { PluginApi } from './pluginApi';
9
+ interface IApiOPtions {
10
+ cwd?: string;
11
+ mode?: IShuviMode;
12
+ config?: IConfig;
13
+ configFile?: string;
14
+ phase?: IPhase;
15
+ }
16
+ declare class Api extends Hookable implements IApi {
17
+ private _cwd;
18
+ private _mode;
19
+ private _userConfig?;
20
+ private _config;
21
+ private _configFile?;
22
+ private _paths;
23
+ private _projectBuilder;
24
+ private _server;
25
+ private _resources;
26
+ private _routes;
27
+ private _presetPlugins;
28
+ private _plugins;
29
+ private _presets;
30
+ private _pluginApi;
31
+ private _phase;
32
+ private _platform;
33
+ helpers: IApi['helpers'];
34
+ constructor({ cwd, mode, config, configFile, phase }: IApiOPtions);
35
+ get mode(): IShuviMode;
36
+ get phase(): IPhase;
37
+ get config(): IApiConfig;
38
+ get paths(): IPaths;
39
+ get platform(): Runtime.IRuntime<unknown>;
40
+ initConfig(): void;
41
+ init(): Promise<void>;
42
+ get server(): Server;
43
+ get assetPublicPath(): string;
44
+ get resources(): IBuiltResource;
45
+ get clientManifest(): Bundler.IManifest;
46
+ setRuntimeConfigContent(content: string | null): void;
47
+ setUserModule(userModule: Partial<UserModule>): void;
48
+ setPlatformModule(module: string): void;
49
+ setRoutes(routes: IUserRouteConfig[]): Promise<void>;
50
+ getRoutes(): IUserRouteConfig[];
51
+ setApiRoutes(apiRoutes: IApiRouteConfig[]): Promise<void>;
52
+ removeBuiltFiles(): void;
53
+ buildApp(): Promise<void>;
54
+ addResoure(identifier: string, loader: () => any): void;
55
+ addEntryCode(content: string): void;
56
+ setEntryWrapperContent(content: string): void;
57
+ addAppFile(options: FileOptions): void;
58
+ addAppExport(source: string, exported: string): void;
59
+ addAppService(source: string, exported: string, filepath: string): void;
60
+ addAppPolyfill(file: string): void;
61
+ setClientModule(module: TargetModule): void;
62
+ setServerModule(module: TargetModule): void;
63
+ addRuntimePlugin(name: string, runtimePlugin: string): void;
64
+ getAssetPublicUrl(...paths: string[]): string;
65
+ resolveAppFile(...paths: string[]): string;
66
+ resolveUserFile(...paths: string[]): string;
67
+ resolveBuildFile(...paths: string[]): string;
68
+ resolvePublicFile(...paths: string[]): string;
69
+ destory(): Promise<void>;
70
+ getPluginApi(): PluginApi;
71
+ private _initPresetsAndPlugins;
72
+ private _initPreset;
73
+ }
74
+ export { Api };
75
+ export declare function getApi({ cwd, mode, config, configFile, phase }: IApiOPtions): Promise<Api>;
package/lib/api/api.js ADDED
@@ -0,0 +1,370 @@
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 path_1 = __importDefault(require("path"));
16
+ const project_1 = require("../project");
17
+ const string_1 = require("@shuvi/utils/lib/string");
18
+ const deepmerge_1 = require("@shuvi/utils/lib/deepmerge");
19
+ const invariant_1 = __importDefault(require("@shuvi/utils/lib/invariant"));
20
+ const hook_1 = require("@shuvi/hook");
21
+ const runtimeConfig_1 = require("../lib/runtimeConfig");
22
+ const routes_1 = require("../lib/routes");
23
+ const apiRoutes_1 = require("../lib/apiRoutes");
24
+ const constants_1 = require("../constants");
25
+ const config_1 = require("../config");
26
+ const server_1 = require("../server");
27
+ const setupApp_1 = require("./setupApp");
28
+ const initCoreResource_1 = require("./initCoreResource");
29
+ const plugin_1 = require("./plugin");
30
+ const pluginApi_1 = require("./pluginApi");
31
+ const paths_1 = require("./paths");
32
+ const rimraf_1 = __importDefault(require("rimraf"));
33
+ const getPlatform_1 = __importDefault(require("../lib/getPlatform"));
34
+ const ServiceModes = ['development', 'production'];
35
+ class Api extends hook_1.Hookable {
36
+ constructor({ cwd, mode, config, configFile, phase }) {
37
+ super();
38
+ this._resources = {};
39
+ this._routes = [];
40
+ this._presetPlugins = [];
41
+ if (mode) {
42
+ this._mode = mode;
43
+ }
44
+ else if (ServiceModes.includes(process.env.NODE_ENV)) {
45
+ this._mode = process.env.NODE_ENV;
46
+ }
47
+ else {
48
+ this._mode = 'production';
49
+ }
50
+ this._cwd = path_1.default.resolve(cwd || '.');
51
+ this._configFile = configFile;
52
+ this._userConfig = config;
53
+ this.helpers = { fileSnippets: project_1.fileSnippets };
54
+ if (phase) {
55
+ this._phase = phase;
56
+ }
57
+ else {
58
+ if (this._mode === 'development') {
59
+ this._phase = 'PHASE_DEVELOPMENT_SERVER';
60
+ }
61
+ else {
62
+ this._phase = 'PHASE_PRODUCTION_SERVER';
63
+ }
64
+ }
65
+ this.initConfig();
66
+ this._platform = getPlatform_1.default(this.config.platform.name);
67
+ }
68
+ get mode() {
69
+ return this._mode;
70
+ }
71
+ get phase() {
72
+ return this._phase;
73
+ }
74
+ get config() {
75
+ return this._config;
76
+ }
77
+ get paths() {
78
+ return this._paths;
79
+ }
80
+ get platform() {
81
+ return this._platform;
82
+ }
83
+ initConfig() {
84
+ const configFromFile = config_1.loadConfig({
85
+ rootDir: this._cwd,
86
+ configFile: this._configFile,
87
+ overrides: this._userConfig
88
+ });
89
+ this._config = deepmerge_1.deepmerge(config_1.createDefaultConfig(), configFromFile);
90
+ const { ssr, router: { history } } = this._config;
91
+ // ensure apiRouteConfigPrefix starts with '/'
92
+ const apiRouteConfigPrefix = this._config.apiConfig.prefix;
93
+ if (apiRouteConfigPrefix) {
94
+ this._config.apiConfig.prefix = path_1.default.resolve('/', apiRouteConfigPrefix);
95
+ }
96
+ // set history to a specific value
97
+ if (history === 'auto') {
98
+ this._config.router.history = ssr ? 'browser' : 'hash';
99
+ }
100
+ }
101
+ init() {
102
+ return __awaiter(this, void 0, void 0, function* () {
103
+ this._projectBuilder = new project_1.ProjectBuilder({
104
+ static: this.mode === 'production'
105
+ });
106
+ yield this._initPresetsAndPlugins();
107
+ initCoreResource_1.initCoreResource(this);
108
+ // TODO?: move into application
109
+ if (typeof this._config.runtimeConfig === 'object') {
110
+ runtimeConfig_1.setRuntimeConfig(this._config.runtimeConfig);
111
+ }
112
+ });
113
+ }
114
+ get server() {
115
+ if (!this._server) {
116
+ this._server = new server_1.Server();
117
+ }
118
+ return this._server;
119
+ }
120
+ get assetPublicPath() {
121
+ let prefix = this.mode === 'development'
122
+ ? constants_1.PUBLIC_PATH
123
+ : this.config.publicPath || constants_1.PUBLIC_PATH;
124
+ if (!prefix.endsWith('/')) {
125
+ prefix += '/';
126
+ }
127
+ return prefix;
128
+ }
129
+ get resources() {
130
+ return this._resources;
131
+ }
132
+ get clientManifest() {
133
+ return this._resources.clientManifest;
134
+ }
135
+ setRuntimeConfigContent(content) {
136
+ this._projectBuilder.setRuntimeConfigContent(content);
137
+ }
138
+ setUserModule(userModule) {
139
+ this._projectBuilder.setUserModule(userModule);
140
+ }
141
+ setPlatformModule(module) {
142
+ this._projectBuilder.setPlatformModule(module);
143
+ }
144
+ setRoutes(routes) {
145
+ return __awaiter(this, void 0, void 0, function* () {
146
+ routes = yield this.callHook({
147
+ name: 'app:routes',
148
+ initialValue: routes
149
+ });
150
+ routes = routes_1.normalizeRoutes(routes, {
151
+ componentDir: this.paths.pagesDir
152
+ });
153
+ routes.push({
154
+ path: ':other(.*)',
155
+ component: this.resolveAppFile('core', '404'),
156
+ name: constants_1.ROUTE_NOT_FOUND_NAME
157
+ });
158
+ this._routes = routes;
159
+ const serialized = routes_1.serializeRoutes(routes);
160
+ const RoutesContent = `export default ${serialized}`;
161
+ this._projectBuilder.setRoutesContent(RoutesContent);
162
+ });
163
+ }
164
+ getRoutes() {
165
+ return this._routes;
166
+ }
167
+ setApiRoutes(apiRoutes) {
168
+ return __awaiter(this, void 0, void 0, function* () {
169
+ apiRoutes = apiRoutes_1.normalizeApiRoutes(apiRoutes, {
170
+ apisDir: this.paths.apisDir
171
+ });
172
+ const { prefix } = this.config.apiConfig || {};
173
+ const serialized = apiRoutes_1.serializeApiRoutes(apiRoutes, prefix);
174
+ let content = `export default ${serialized}`;
175
+ this._projectBuilder.setApiRoutesContent(content);
176
+ });
177
+ }
178
+ removeBuiltFiles() {
179
+ rimraf_1.default.sync(this.paths.appDir);
180
+ rimraf_1.default.sync(this.paths.buildDir);
181
+ }
182
+ buildApp() {
183
+ return __awaiter(this, void 0, void 0, function* () {
184
+ yield setupApp_1.setupApp(this);
185
+ this.removeBuiltFiles();
186
+ this._projectBuilder.validateCompleteness('api');
187
+ yield this._projectBuilder.build(this.paths.appDir);
188
+ this.emitEvent('app:ready');
189
+ });
190
+ }
191
+ addResoure(identifier, loader) {
192
+ const cacheable = this.mode === 'production';
193
+ const api = this;
194
+ // TODO: warn exitsed identifier
195
+ if (cacheable) {
196
+ Object.defineProperty(api._resources, identifier, {
197
+ get() {
198
+ const value = loader();
199
+ Object.defineProperty(api._resources, identifier, {
200
+ value,
201
+ enumerable: true,
202
+ configurable: false,
203
+ writable: false
204
+ });
205
+ return value;
206
+ },
207
+ enumerable: true,
208
+ configurable: true
209
+ });
210
+ }
211
+ else {
212
+ Object.defineProperty(this._resources, identifier, {
213
+ get() {
214
+ return loader();
215
+ },
216
+ enumerable: true,
217
+ configurable: false
218
+ });
219
+ }
220
+ }
221
+ addEntryCode(content) {
222
+ this._projectBuilder.addEntryCode(content);
223
+ }
224
+ setEntryWrapperContent(content) {
225
+ this._projectBuilder.setEntryWrapperContent(content);
226
+ }
227
+ addAppFile(options) {
228
+ // make addAppFile root as files/
229
+ options.name = path_1.default.join('files', path_1.default.resolve('/', options.name));
230
+ this._projectBuilder.addFile(options);
231
+ }
232
+ addAppExport(source, exported) {
233
+ this._projectBuilder.addExport(source, exported);
234
+ }
235
+ addAppService(source, exported, filepath) {
236
+ // make addAppService root as services/
237
+ const targetPath = path_1.default.join('services', path_1.default.resolve('/', filepath));
238
+ this._projectBuilder.addService(source, exported, targetPath);
239
+ }
240
+ addAppPolyfill(file) {
241
+ this._projectBuilder.addPolyfill(file);
242
+ }
243
+ setClientModule(module) {
244
+ this._projectBuilder.setClientModule(module);
245
+ }
246
+ setServerModule(module) {
247
+ this._projectBuilder.setServerModule(module);
248
+ }
249
+ addRuntimePlugin(name, runtimePlugin) {
250
+ this._projectBuilder.addRuntimePlugin(name, runtimePlugin);
251
+ }
252
+ getAssetPublicUrl(...paths) {
253
+ return string_1.joinPath(this.assetPublicPath, ...paths);
254
+ }
255
+ resolveAppFile(...paths) {
256
+ return string_1.joinPath(this.paths.appDir, ...paths);
257
+ }
258
+ resolveUserFile(...paths) {
259
+ return string_1.joinPath(this.paths.srcDir, ...paths);
260
+ }
261
+ resolveBuildFile(...paths) {
262
+ return string_1.joinPath(this.paths.buildDir, ...paths);
263
+ }
264
+ resolvePublicFile(...paths) {
265
+ return string_1.joinPath(this.paths.publicDir, ...paths);
266
+ }
267
+ destory() {
268
+ return __awaiter(this, void 0, void 0, function* () {
269
+ if (this._server) {
270
+ yield this._server.close();
271
+ }
272
+ yield this._projectBuilder.stopBuild();
273
+ yield this.callHook('destroy');
274
+ });
275
+ }
276
+ getPluginApi() {
277
+ if (!this._pluginApi) {
278
+ this._pluginApi = pluginApi_1.createPluginApi(this);
279
+ }
280
+ return this._pluginApi;
281
+ }
282
+ _initPresetsAndPlugins() {
283
+ return __awaiter(this, void 0, void 0, function* () {
284
+ const { phase, _config: config } = this;
285
+ // init presets
286
+ this._presets = plugin_1.resolvePresets(config.presets || [], {
287
+ dir: config.rootDir
288
+ });
289
+ for (const preset of this._presets) {
290
+ this._initPreset(preset);
291
+ }
292
+ // init plugins
293
+ this._plugins = plugin_1.resolvePlugins(this._config.plugins || [], {
294
+ dir: this._config.rootDir
295
+ });
296
+ const allPlugins = this._presetPlugins.concat(this._plugins);
297
+ let runPlugins = function runNext(next, cur = () => void 0) {
298
+ if (next) {
299
+ return (n) => runNext(n, () => {
300
+ cur();
301
+ next();
302
+ });
303
+ }
304
+ return cur();
305
+ };
306
+ for (const plugin of allPlugins) {
307
+ const pluginInst = plugin.get();
308
+ if (typeof pluginInst.modifyConfig === 'function') {
309
+ this.tap('getConfig', {
310
+ name: 'pluginModifyConfig',
311
+ fn(config) {
312
+ pluginInst.modifyConfig(config, phase);
313
+ return config;
314
+ }
315
+ });
316
+ }
317
+ runPlugins = runPlugins(() => {
318
+ pluginInst.apply(this.getPluginApi());
319
+ });
320
+ }
321
+ // prepare all properties befofre run plugins, so plugin can use all api of Api
322
+ this._config = yield this.callHook({
323
+ name: 'getConfig',
324
+ initialValue: config
325
+ });
326
+ // do not allow to modify config
327
+ Object.freeze(this._config);
328
+ this._paths = paths_1.getPaths({
329
+ rootDir: this._config.rootDir,
330
+ outputPath: this._config.outputPath,
331
+ publicDir: this._config.publicDir
332
+ });
333
+ // do not allow to modify paths
334
+ Object.freeze(this._paths);
335
+ // Runtime installation need to be executed before initializing presets and plugins
336
+ // to make sure shuvi entry file at the top.
337
+ //this.runtime.install(this);
338
+ this.platform.install(this);
339
+ runPlugins();
340
+ });
341
+ }
342
+ _initPreset(preset) {
343
+ const { id, get: getPreset } = preset;
344
+ const { presets, plugins } = getPreset()(this.getPluginApi());
345
+ if (presets) {
346
+ invariant_1.default(Array.isArray(presets), `presets returned from preset ${id} must be Array.`);
347
+ const resolvedPresets = plugin_1.resolvePresets(presets, {
348
+ dir: this._config.rootDir
349
+ });
350
+ for (const preset of resolvedPresets) {
351
+ this._initPreset(preset);
352
+ }
353
+ }
354
+ if (plugins) {
355
+ invariant_1.default(Array.isArray(plugins), `presets returned from preset ${id} must be Array.`);
356
+ this._presetPlugins.push(...plugin_1.resolvePlugins(plugins, {
357
+ dir: this._config.rootDir
358
+ }));
359
+ }
360
+ }
361
+ }
362
+ exports.Api = Api;
363
+ function getApi({ cwd, mode, config, configFile, phase }) {
364
+ return __awaiter(this, void 0, void 0, function* () {
365
+ const api = new Api({ cwd, mode, config, configFile, phase });
366
+ yield api.init();
367
+ return api;
368
+ });
369
+ }
370
+ exports.getApi = getApi;
@@ -0,0 +1,6 @@
1
+ import type { Api } from './api';
2
+ import { getApi } from './api';
3
+ import { PluginApi } from './pluginApi';
4
+ export type { Api };
5
+ export { getApi, PluginApi };
6
+ export * from './types';
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const api_1 = require("./api");
4
+ exports.getApi = api_1.getApi;
@@ -0,0 +1,2 @@
1
+ import { Api } from './api';
2
+ export declare function initCoreResource(api: Api): void;
@@ -0,0 +1,27 @@
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 fs_extra_1 = __importDefault(require("fs-extra"));
8
+ const viewTemplate_1 = require("../lib/viewTemplate");
9
+ const constants_1 = require("../constants");
10
+ function resolveServerDist(api, name) {
11
+ const manifest = api.resources.serverManifest;
12
+ return path_1.default.join(api.paths.buildDir, constants_1.BUILD_SERVER_DIR, manifest.bundles[name]);
13
+ }
14
+ function resolveDocument(api) {
15
+ const customDoc = api.resolveUserFile('document.ejs');
16
+ if (fs_extra_1.default.existsSync(customDoc)) {
17
+ return customDoc;
18
+ }
19
+ return require.resolve('@shuvi/platform-core/template/document.ejs');
20
+ }
21
+ function initCoreResource(api) {
22
+ api.addResoure('clientManifest', () => require(path_1.default.join(api.paths.buildDir, constants_1.BUILD_CLIENT_DIR, constants_1.BUILD_MANIFEST_PATH)));
23
+ api.addResoure('serverManifest', () => require(path_1.default.join(api.paths.buildDir, constants_1.BUILD_SERVER_DIR, constants_1.BUILD_MANIFEST_PATH)));
24
+ api.addResoure('server', () => require(resolveServerDist(api, constants_1.BUILD_SERVER_FILE_SERVER)));
25
+ api.addResoure('documentTemplate', () => viewTemplate_1.parseTemplateFile(resolveDocument(api)));
26
+ }
27
+ exports.initCoreResource = initCoreResource;
@@ -0,0 +1,8 @@
1
+ import { IPaths } from '.';
2
+ interface IPathsOpts {
3
+ rootDir: string;
4
+ outputPath: string;
5
+ publicDir: string;
6
+ }
7
+ export declare function getPaths(opts: IPathsOpts): IPaths;
8
+ export {};
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const path_1 = require("path");
4
+ const constants_1 = require("@shuvi/shared/lib/constants");
5
+ function getPaths(opts) {
6
+ const { rootDir, outputPath, publicDir } = opts;
7
+ const toAbsolute = (p) => (path_1.isAbsolute(p) ? p : path_1.join(rootDir, p));
8
+ const srcDir = toAbsolute('src');
9
+ const srcChildDir = (p) => path_1.join(srcDir, p);
10
+ return {
11
+ rootDir,
12
+ srcDir,
13
+ pagesDir: srcChildDir('pages'),
14
+ apisDir: srcChildDir('apis'),
15
+ appDir: toAbsolute(`.${constants_1.NAME}/app`),
16
+ buildDir: toAbsolute(outputPath),
17
+ publicDir: toAbsolute(publicDir)
18
+ };
19
+ }
20
+ exports.getPaths = getPaths;
@@ -0,0 +1,6 @@
1
+ import { IPluginConfig, IPresetConfig, IPlugin, IPreset } from '.';
2
+ export interface ResolvePluginOptions {
3
+ dir: string;
4
+ }
5
+ export declare function resolvePlugins(plugins: IPluginConfig[], options: ResolvePluginOptions): IPlugin[];
6
+ export declare function resolvePresets(presets: IPresetConfig[], options: ResolvePluginOptions): IPreset[];
@@ -0,0 +1,108 @@
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 resolve_1 = __importDefault(require("@shuvi/utils/lib/resolve"));
7
+ let uid = 0;
8
+ function resolvePlugin(pluginConfig, resolveOptions) {
9
+ let pluginPath;
10
+ let name;
11
+ let options;
12
+ if (Array.isArray(pluginConfig)) {
13
+ if (pluginConfig.length === 2) {
14
+ pluginPath = pluginConfig[0];
15
+ const nameOrOption = pluginConfig[1];
16
+ if (typeof nameOrOption === 'string') {
17
+ name = nameOrOption;
18
+ options = {};
19
+ }
20
+ else {
21
+ options = nameOrOption;
22
+ name = '';
23
+ }
24
+ }
25
+ else {
26
+ [pluginPath, options = {}, name = ''] = pluginConfig;
27
+ }
28
+ }
29
+ else if (typeof pluginConfig === 'string') {
30
+ pluginPath = pluginConfig;
31
+ name = '';
32
+ options = {};
33
+ }
34
+ else if (typeof pluginConfig === 'function') {
35
+ return {
36
+ id: `InlinePlugin${uid++}`,
37
+ get: () => ({
38
+ apply(api) {
39
+ pluginConfig(api);
40
+ }
41
+ })
42
+ };
43
+ }
44
+ else {
45
+ throw new Error(`Plugin must be one of type [string, array, function]`);
46
+ }
47
+ pluginPath = resolve_1.default.sync(pluginPath, { basedir: resolveOptions.dir });
48
+ const id = name ? `${pluginPath}@${name}` : pluginPath;
49
+ let pluginInst;
50
+ let plugin = require(pluginPath);
51
+ plugin = plugin.default || plugin;
52
+ if (plugin.prototype && typeof plugin.prototype.apply === 'function') {
53
+ // class plugin
54
+ pluginInst = new plugin(options);
55
+ }
56
+ else {
57
+ // function plugin
58
+ pluginInst = {
59
+ apply(...args) {
60
+ plugin(...args);
61
+ }
62
+ };
63
+ }
64
+ return {
65
+ id,
66
+ get: () => pluginInst
67
+ };
68
+ }
69
+ function resolvePreset(presetConfig, resolveOptions) {
70
+ let presetPath;
71
+ let options;
72
+ if (Array.isArray(presetConfig)) {
73
+ presetPath = presetConfig[0];
74
+ const nameOrOption = presetConfig[1];
75
+ if (typeof nameOrOption === 'string') {
76
+ options = {};
77
+ }
78
+ else {
79
+ options = nameOrOption;
80
+ }
81
+ }
82
+ else if (typeof presetConfig === 'string') {
83
+ presetPath = presetConfig;
84
+ options = {};
85
+ }
86
+ else {
87
+ throw new Error(`Plugin must be one of type [string, array, function]`);
88
+ }
89
+ presetPath = resolve_1.default.sync(presetPath, { basedir: resolveOptions.dir });
90
+ const id = presetPath;
91
+ let preset = require(presetPath);
92
+ preset = preset.default || preset;
93
+ const presetFn = (api) => {
94
+ return preset(api, options);
95
+ };
96
+ return {
97
+ id,
98
+ get: () => presetFn
99
+ };
100
+ }
101
+ function resolvePlugins(plugins, options) {
102
+ return plugins.map(plugin => resolvePlugin(plugin, options));
103
+ }
104
+ exports.resolvePlugins = resolvePlugins;
105
+ function resolvePresets(presets, options) {
106
+ return presets.map(preset => resolvePreset(preset, options));
107
+ }
108
+ exports.resolvePresets = resolvePresets;
@@ -0,0 +1,29 @@
1
+ import { Api, IApi } from '.';
2
+ declare class PluginApi implements IApi {
3
+ mode: any;
4
+ paths: any;
5
+ config: any;
6
+ phase: any;
7
+ helpers: any;
8
+ clientManifest: any;
9
+ tap: any;
10
+ callHook: any;
11
+ on: any;
12
+ emitEvent: any;
13
+ addEntryCode: any;
14
+ addAppFile: any;
15
+ addAppExport: any;
16
+ addAppService: any;
17
+ addAppPolyfill: any;
18
+ addRuntimePlugin: any;
19
+ setPlatformModule: any;
20
+ setClientModule: any;
21
+ setServerModule: any;
22
+ resolveAppFile: any;
23
+ resolveUserFile: any;
24
+ resolveBuildFile: any;
25
+ resolvePublicFile: any;
26
+ getAssetPublicUrl: any;
27
+ }
28
+ export type { PluginApi };
29
+ export declare function createPluginApi(api: Api): PluginApi;