@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,85 @@
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 path_1 = __importDefault(require("path"));
18
+ const config_1 = require("@shuvi/toolpack/lib/webpack/config");
19
+ const constants_1 = require("../constants");
20
+ function createWebpackConfig({ mode, assetPublicPath, paths, config }, _a) {
21
+ var { webpackHelpers } = _a, opts = __rest(_a, ["webpackHelpers"]);
22
+ const dev = mode === 'development';
23
+ let chain;
24
+ const srcDirs = [paths.appDir, paths.srcDir, ...(opts.srcDirs || [])];
25
+ if (opts.node) {
26
+ chain = config_1.createNodeWebpackChain({
27
+ buildManifestFilename: constants_1.BUILD_MANIFEST_PATH,
28
+ dev,
29
+ env: config.env,
30
+ mediaFilename: constants_1.BUILD_MEDIA_PATH,
31
+ name: opts.name,
32
+ projectRoot: paths.rootDir,
33
+ srcDirs,
34
+ publicPath: assetPublicPath,
35
+ webpackHelpers
36
+ });
37
+ chain.output.path(`${paths.buildDir}/${opts.outputDir}`);
38
+ }
39
+ else {
40
+ chain = config_1.createBrowserWebpackChain({
41
+ analyze: config.analyze,
42
+ buildManifestFilename: constants_1.BUILD_MANIFEST_PATH,
43
+ dev,
44
+ env: config.env,
45
+ mediaFilename: constants_1.BUILD_MEDIA_PATH,
46
+ name: opts.name,
47
+ projectRoot: paths.rootDir,
48
+ srcDirs,
49
+ publicPath: assetPublicPath,
50
+ webpackHelpers
51
+ });
52
+ chain.output.path(`${paths.buildDir}/${opts.outputDir}`);
53
+ chain.optimization.runtimeChunk({ name: constants_1.BUILD_CLIENT_RUNTIME_WEBPACK });
54
+ }
55
+ chain.name(opts.name);
56
+ chain.merge({
57
+ entry: opts.entry
58
+ });
59
+ chain.resolve.alias.set('@shuvi/app', paths.appDir);
60
+ chain.resolve.alias.set('@shuvi/user', paths.srcDir);
61
+ chain.resolve.alias.set('@shuvi/services', path_1.default.join(paths.appDir, 'services'));
62
+ chain.resolve.alias.set('@shuvi/platform-core', path_1.default.dirname(require.resolve('@shuvi/platform-core/package.json')));
63
+ chain.output.set('filename', ({ chunk }) => {
64
+ // Use `[name]-[contenthash].js` in production
65
+ if (!dev) {
66
+ return `[name]-[contenthash].js`;
67
+ }
68
+ return '[name].js';
69
+ });
70
+ return chain;
71
+ }
72
+ exports.createWebpackConfig = createWebpackConfig;
73
+ function getClientEntry(_api) {
74
+ return {
75
+ [constants_1.BUILD_CLIENT_RUNTIME_MAIN]: ['@shuvi/app/entry.client-wrapper'],
76
+ [constants_1.BUILD_CLIENT_RUNTIME_POLYFILL]: ['@shuvi/app/core/polyfill']
77
+ };
78
+ }
79
+ exports.getClientEntry = getClientEntry;
80
+ function getServerEntry(_api) {
81
+ return {
82
+ [constants_1.BUILD_SERVER_FILE_SERVER]: ['@shuvi/app/entry.server']
83
+ };
84
+ }
85
+ exports.getServerEntry = getServerEntry;
@@ -0,0 +1,2 @@
1
+ export { BundlerResult } from './runCompiler';
2
+ export { getBundler } from './bundler';
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var bundler_1 = require("./bundler");
4
+ exports.getBundler = bundler_1.getBundler;
@@ -0,0 +1,7 @@
1
+ import { Compiler, MultiCompiler } from 'webpack';
2
+ import type webpack from 'webpack';
3
+ export declare type BundlerResult = {
4
+ errors: webpack.StatsError[];
5
+ warnings: webpack.StatsError[];
6
+ };
7
+ export declare function runCompiler(compiler: Compiler | MultiCompiler): Promise<BundlerResult>;
@@ -0,0 +1,43 @@
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
+ function generateStats(result, stat) {
13
+ const { errors, warnings } = stat.toJson({
14
+ all: false,
15
+ warnings: true,
16
+ errors: true
17
+ });
18
+ if (errors && errors.length > 0) {
19
+ result.errors.push(...errors);
20
+ }
21
+ if (warnings && warnings.length > 0) {
22
+ result.warnings.push(...warnings);
23
+ }
24
+ return result;
25
+ }
26
+ function runCompiler(compiler) {
27
+ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
28
+ compiler.run((err, statsOrMultiStats) => {
29
+ compiler.close(() => {
30
+ if (err) {
31
+ return reject(err);
32
+ }
33
+ if (statsOrMultiStats.stats) {
34
+ const result = statsOrMultiStats.stats.reduce(generateStats, { errors: [], warnings: [] });
35
+ return resolve(result);
36
+ }
37
+ const result = generateStats({ errors: [], warnings: [] }, statsOrMultiStats);
38
+ return resolve(result);
39
+ });
40
+ });
41
+ }));
42
+ }
43
+ exports.runCompiler = runCompiler;
@@ -0,0 +1,8 @@
1
+ import { IConfig } from '../api';
2
+ export interface IBuildOptions {
3
+ cwd?: string;
4
+ config?: IConfig;
5
+ configFile?: string;
6
+ target?: 'spa' | 'ssr';
7
+ }
8
+ export declare function build(options: IBuildOptions): Promise<void>;
@@ -0,0 +1,90 @@
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 fs_extra_1 = __importDefault(require("fs-extra"));
17
+ const formatWebpackMessages_1 = __importDefault(require("@shuvi/toolpack/lib/utils/formatWebpackMessages"));
18
+ const api_1 = require("../api");
19
+ const bundler_1 = require("../bundler/bundler");
20
+ const constants_1 = require("../constants");
21
+ const renderToHTML_1 = require("../lib/renderToHTML");
22
+ const defaultBuildOptions = {
23
+ target: 'ssr'
24
+ };
25
+ function bundle({ api }) {
26
+ return __awaiter(this, void 0, void 0, function* () {
27
+ const bundler = bundler_1.getBundler(api);
28
+ const result = yield bundler.build();
29
+ const messages = formatWebpackMessages_1.default(result);
30
+ // If errors exist, only show errors.
31
+ if (messages.errors.length) {
32
+ // Only keep the first error. Others are often indicative
33
+ // of the same problem, but confuse the reader with noise.
34
+ throw new Error(messages.errors[0]);
35
+ }
36
+ // Show warnings if no errors were found.
37
+ if (messages.warnings.length) {
38
+ console.log('Compiled with warnings.\n');
39
+ console.log(messages.warnings.join('\n\n'));
40
+ }
41
+ });
42
+ }
43
+ function copyPublicFolder(api) {
44
+ if (!fs_extra_1.default.existsSync(api.paths.publicDir)) {
45
+ return;
46
+ }
47
+ fs_extra_1.default.copySync(api.paths.publicDir, path_1.default.join(api.paths.buildDir, constants_1.BUILD_CLIENT_DIR), {
48
+ dereference: true
49
+ });
50
+ }
51
+ function buildHtml({ api, pathname, filename }) {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ const { html } = yield renderToHTML_1.renderToHTML({
54
+ req: {
55
+ url: pathname,
56
+ headers: {}
57
+ },
58
+ api
59
+ });
60
+ if (html) {
61
+ yield fs_extra_1.default.writeFile(path_1.default.resolve(api.paths.buildDir, constants_1.BUILD_CLIENT_DIR, filename), html);
62
+ }
63
+ });
64
+ }
65
+ function build(options) {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ const opts = Object.assign(Object.assign({}, defaultBuildOptions), options);
68
+ const api = yield api_1.getApi({
69
+ cwd: opts.cwd,
70
+ mode: 'production',
71
+ config: opts.config,
72
+ configFile: opts.configFile,
73
+ phase: 'PHASE_PRODUCTION_BUILD'
74
+ });
75
+ // generate application
76
+ yield api.buildApp();
77
+ // Remove all content but keep the directory so that
78
+ // if you're in it, you don't end up in Trash
79
+ fs_extra_1.default.emptyDirSync(api.paths.buildDir);
80
+ // Merge with the public folder
81
+ copyPublicFolder(api);
82
+ // transpile the application
83
+ yield bundle({ api });
84
+ if (opts.target === 'spa') {
85
+ yield buildHtml({ api, pathname: '/', filename: 'index.html' });
86
+ }
87
+ api.emitEvent('afterBuild');
88
+ });
89
+ }
90
+ exports.build = build;
@@ -0,0 +1,8 @@
1
+ import { IConfig, IApiConfig } from '../api';
2
+ export interface LoadConfigOptions {
3
+ rootDir?: string;
4
+ configFile?: string;
5
+ overrides?: IConfig;
6
+ }
7
+ export declare const createDefaultConfig: () => IApiConfig;
8
+ export declare function loadConfig({ rootDir, configFile, overrides }?: LoadConfigOptions): IConfig;
@@ -0,0 +1,50 @@
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 constants_1 = require("../constants");
8
+ const loadDotenvConfig_1 = require("./loadDotenvConfig");
9
+ const deepmerge_1 = require("@shuvi/utils/lib/deepmerge");
10
+ exports.createDefaultConfig = () => ({
11
+ ssr: true,
12
+ env: {},
13
+ rootDir: process.cwd(),
14
+ outputPath: 'dist',
15
+ platform: {
16
+ name: 'web',
17
+ framework: 'react',
18
+ target: 'ssr'
19
+ },
20
+ publicDir: 'public',
21
+ publicPath: constants_1.PUBLIC_PATH,
22
+ router: {
23
+ history: 'auto'
24
+ },
25
+ apiConfig: {
26
+ prefix: '/api',
27
+ bodyParser: true
28
+ }
29
+ });
30
+ function loadConfig({ rootDir = '.', configFile = constants_1.CONFIG_FILE, overrides = {} } = {}) {
31
+ rootDir = path_1.default.resolve(rootDir);
32
+ configFile = path_1.default.resolve(rootDir, configFile);
33
+ // read dotenv so we can get env in shuvi.config.js
34
+ loadDotenvConfig_1.loadDotenvConfig(rootDir);
35
+ let fileConfig = {};
36
+ try {
37
+ fileConfig = require(configFile);
38
+ fileConfig = fileConfig.default || fileConfig;
39
+ }
40
+ catch (err) {
41
+ if (err.message.indexOf(`Cannot find module '${configFile}'`) < 0) {
42
+ throw err;
43
+ }
44
+ else if (configFile !== path_1.default.resolve(rootDir, constants_1.CONFIG_FILE)) {
45
+ console.warn('Config file not found: ' + configFile);
46
+ }
47
+ }
48
+ return deepmerge_1.deepmerge({ rootDir }, fileConfig, overrides);
49
+ }
50
+ exports.loadConfig = loadConfig;
@@ -0,0 +1 @@
1
+ export declare const loadDotenvConfig: (dir: string) => void;
@@ -0,0 +1,50 @@
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 dotenv_1 = __importDefault(require("dotenv"));
7
+ const dotenv_expand_1 = __importDefault(require("dotenv-expand"));
8
+ const fs_1 = __importDefault(require("fs"));
9
+ const path_1 = __importDefault(require("path"));
10
+ exports.loadDotenvConfig = (dir) => {
11
+ const mode = process.env.NODE_ENV;
12
+ // Priority top to bottom
13
+ const dotenvFiles = [
14
+ `.env.${mode}.local`,
15
+ `.env.local`,
16
+ `.env.${mode}`,
17
+ '.env'
18
+ ];
19
+ let envsFromDotEnv = {};
20
+ for (const envFile of dotenvFiles) {
21
+ const dotEnvPath = path_1.default.join(dir, envFile);
22
+ try {
23
+ const stats = fs_1.default.statSync(dotEnvPath);
24
+ if (!stats.isFile()) {
25
+ continue;
26
+ }
27
+ const contents = fs_1.default.readFileSync(dotEnvPath, 'utf8');
28
+ try {
29
+ let result = {};
30
+ result.parsed = dotenv_1.default.parse(contents);
31
+ // dotenvExpand will not replace env that is already saved in process.env
32
+ // https://github.com/motdotla/dotenv-expand/blob/de9e5cb0215495452f475f5be4dea1580b8217cd/lib/main.js#L22
33
+ result = dotenv_expand_1.default(result);
34
+ if (result.parsed) {
35
+ console.log(`Loaded env from ${path_1.default.join(dir, envFile)}`);
36
+ envsFromDotEnv = Object.assign(Object.assign({}, envsFromDotEnv), result.parsed);
37
+ }
38
+ }
39
+ catch (err) {
40
+ console.error(`Failed to load env from ${path_1.default.join(dir, envFile)}`, err);
41
+ }
42
+ }
43
+ catch (err) {
44
+ if (err.code !== 'ENOENT') {
45
+ throw new Error(`Failed to load env from ${envFile}\n${err}`);
46
+ }
47
+ }
48
+ }
49
+ Object.assign(process.env, envsFromDotEnv);
50
+ };
@@ -0,0 +1,14 @@
1
+ export * from '@shuvi/shared/lib/constants';
2
+ export declare const PUBLIC_PATH: string;
3
+ export declare const BUILD_MANIFEST_PATH = "build-manifest.json";
4
+ export declare const BUILD_MEDIA_PATH = "static/media/[name].[hash:8].[ext]";
5
+ export declare const BUILD_CLIENT_DIR = "client";
6
+ export declare const BUILD_CLIENT_RUNTIME_MAIN = "static/runtime/main";
7
+ export declare const BUILD_CLIENT_RUNTIME_WEBPACK = "static/runtime/webpack";
8
+ export declare const BUILD_CLIENT_RUNTIME_POLYFILL = "static/runtime/polyfill";
9
+ export declare const BUILD_SERVER_DIR = "server";
10
+ export declare const BUILD_SERVER_FILE_SERVER = "server";
11
+ export declare const PHASE_PRODUCTION_BUILD = "PHASE_PRODUCTION_BUILD";
12
+ export declare const PHASE_PRODUCTION_SERVER = "PHASE_PRODUCTION_SERVER";
13
+ export declare const PHASE_DEVELOPMENT_SERVER = "PHASE_DEVELOPMENT_SERVER";
14
+ export declare const PHASE_INSPECT_WEBPACK = "PHASE_INSPECT_WEBPACK";
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ function __export(m) {
3
+ for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4
+ }
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const constants_1 = require("@shuvi/shared/lib/constants");
7
+ __export(require("@shuvi/shared/lib/constants"));
8
+ exports.PUBLIC_PATH = `${constants_1.PATH_PREFIX}/`;
9
+ exports.BUILD_MANIFEST_PATH = 'build-manifest.json';
10
+ exports.BUILD_MEDIA_PATH = 'static/media/[name].[hash:8].[ext]';
11
+ exports.BUILD_CLIENT_DIR = 'client';
12
+ exports.BUILD_CLIENT_RUNTIME_MAIN = `static/runtime/main`;
13
+ exports.BUILD_CLIENT_RUNTIME_WEBPACK = `static/runtime/webpack`;
14
+ exports.BUILD_CLIENT_RUNTIME_POLYFILL = `static/runtime/polyfill`;
15
+ exports.BUILD_SERVER_DIR = 'server';
16
+ exports.BUILD_SERVER_FILE_SERVER = `server`;
17
+ exports.PHASE_PRODUCTION_BUILD = 'PHASE_PRODUCTION_BUILD';
18
+ exports.PHASE_PRODUCTION_SERVER = 'PHASE_PRODUCTION_SERVER';
19
+ exports.PHASE_DEVELOPMENT_SERVER = 'PHASE_DEVELOPMENT_SERVER';
20
+ exports.PHASE_INSPECT_WEBPACK = 'PHASE_INSPECT_WEBPACK';
package/lib/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export { shuvi, Shuvi } from './shuvi';
2
+ export { IApi, Api, getApi, PluginApi, IApiConfig, IConfig } from './api';
3
+ export { ProjectBuilder } from './project';
4
+ export * from './types';
5
+ export { BUNDLER_TARGET_CLIENT, BUNDLER_TARGET_SERVER, IDENTITY_RUNTIME_PUBLICPATH, PHASE_PRODUCTION_BUILD, PHASE_PRODUCTION_SERVER, PHASE_DEVELOPMENT_SERVER, PHASE_INSPECT_WEBPACK } from './constants';
package/lib/index.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ function __export(m) {
3
+ for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4
+ }
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var shuvi_1 = require("./shuvi");
7
+ exports.shuvi = shuvi_1.shuvi;
8
+ exports.Shuvi = shuvi_1.Shuvi;
9
+ var api_1 = require("./api");
10
+ exports.getApi = api_1.getApi;
11
+ var project_1 = require("./project");
12
+ exports.ProjectBuilder = project_1.ProjectBuilder;
13
+ __export(require("./types"));
14
+ var constants_1 = require("./constants");
15
+ exports.BUNDLER_TARGET_CLIENT = constants_1.BUNDLER_TARGET_CLIENT;
16
+ exports.BUNDLER_TARGET_SERVER = constants_1.BUNDLER_TARGET_SERVER;
17
+ exports.IDENTITY_RUNTIME_PUBLICPATH = constants_1.IDENTITY_RUNTIME_PUBLICPATH;
18
+ exports.PHASE_PRODUCTION_BUILD = constants_1.PHASE_PRODUCTION_BUILD;
19
+ exports.PHASE_PRODUCTION_SERVER = constants_1.PHASE_PRODUCTION_SERVER;
20
+ exports.PHASE_DEVELOPMENT_SERVER = constants_1.PHASE_DEVELOPMENT_SERVER;
21
+ exports.PHASE_INSPECT_WEBPACK = constants_1.PHASE_INSPECT_WEBPACK;
@@ -0,0 +1,80 @@
1
+ /// <reference types="node" />
2
+ import { IncomingMessage, ServerResponse } from 'http';
3
+ import { IParams, ParsedQuery } from '@shuvi/router';
4
+ export interface IResponse extends ServerResponse {
5
+ }
6
+ export interface IRequest extends IncomingMessage {
7
+ url: string;
8
+ pathname: string;
9
+ query: ParsedQuery;
10
+ params: IParams;
11
+ }
12
+ export interface IApiReq {
13
+ pathname: string;
14
+ query: ParsedQuery;
15
+ params: IParams;
16
+ cookies: {
17
+ [key: string]: string;
18
+ };
19
+ body?: {
20
+ [key: string]: any;
21
+ };
22
+ }
23
+ export declare type IApiRequest = IRequest & IApiReq;
24
+ declare type Send<T> = (body: T) => void;
25
+ declare type IApiRes<T = any> = {
26
+ send: Send<T>;
27
+ json: Send<T>;
28
+ status: (statusCode: number) => IResponse;
29
+ redirect(url: string): IResponse;
30
+ redirect(status: number, url: string): IResponse;
31
+ };
32
+ export declare type IApiResponse<T = any> = ServerResponse & IApiRes<T>;
33
+ export declare type IApiRequestHandler<T = any> = (req: IApiRequest, res: IApiResponse<T>) => void | Promise<void>;
34
+ export declare function apiRouteHandler(req: IRequest, res: IResponse, resolver: IApiRequestHandler, apiRoutesConfig: any): Promise<void>;
35
+ /**
36
+ * Parse incoming message like `json` or `urlencoded`
37
+ * @param req request object
38
+ */
39
+ export declare function parseBody(req: IRequest, limit: string | number): Promise<any>;
40
+ /**
41
+ * Parse cookies from `req` header
42
+ * @param req request object
43
+ */
44
+ export declare function getCookieParser(req: IncomingMessage): {
45
+ [key: string]: string;
46
+ };
47
+ /**
48
+ *
49
+ * @param res response object
50
+ * @param statusCode `HTTP` status code of response
51
+ */
52
+ export declare function sendStatusCode(res: IResponse, statusCode: number): IResponse;
53
+ /**
54
+ *
55
+ * @param res response object
56
+ * @param [statusOrUrl] `HTTP` status code of redirect
57
+ * @param url URL of redirect
58
+ */
59
+ export declare function redirect(res: IResponse, statusOrUrl: string | number, url?: string): IResponse;
60
+ /**
61
+ * Send `any` body to response
62
+ * @param req request object
63
+ * @param res response object
64
+ * @param body of response
65
+ */
66
+ export declare function sendData(req: IRequest, res: IResponse, body: any): void;
67
+ /**
68
+ * Send `JSON` object
69
+ * @param res response object
70
+ * @param jsonBody of data
71
+ */
72
+ export declare function sendJson(req: IRequest, res: IResponse, jsonBody: any): void;
73
+ /**
74
+ * Custom error class
75
+ */
76
+ export declare class ApiError extends Error {
77
+ readonly statusCode: number;
78
+ constructor(statusCode: number, message: string);
79
+ }
80
+ export {};