@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.
- package/lib/api/api.d.ts +75 -0
- package/lib/api/api.js +370 -0
- package/lib/api/index.d.ts +6 -0
- package/lib/api/index.js +4 -0
- package/lib/api/initCoreResource.d.ts +2 -0
- package/lib/api/initCoreResource.js +27 -0
- package/lib/api/paths.d.ts +8 -0
- package/lib/api/paths.js +20 -0
- package/lib/api/plugin.d.ts +6 -0
- package/lib/api/plugin.js +108 -0
- package/lib/api/pluginApi.d.ts +29 -0
- package/lib/api/pluginApi.js +44 -0
- package/lib/api/setupApp.d.ts +2 -0
- package/lib/api/setupApp.js +132 -0
- package/lib/api/types.d.ts +152 -0
- package/lib/api/types.js +2 -0
- package/lib/bundler/bundler.d.ts +33 -0
- package/lib/bundler/bundler.js +293 -0
- package/lib/bundler/config.d.ts +18 -0
- package/lib/bundler/config.js +85 -0
- package/lib/bundler/index.d.ts +2 -0
- package/lib/bundler/index.js +4 -0
- package/lib/bundler/runCompiler.d.ts +7 -0
- package/lib/bundler/runCompiler.js +43 -0
- package/lib/cli/build.d.ts +8 -0
- package/lib/cli/build.js +90 -0
- package/lib/config/index.d.ts +8 -0
- package/lib/config/index.js +50 -0
- package/lib/config/loadDotenvConfig.d.ts +1 -0
- package/lib/config/loadDotenvConfig.js +50 -0
- package/lib/constants.d.ts +14 -0
- package/lib/constants.js +20 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.js +21 -0
- package/lib/lib/apiRouteHandler.d.ts +80 -0
- package/lib/lib/apiRouteHandler.js +216 -0
- package/lib/lib/apiRoutes.d.ts +11 -0
- package/lib/lib/apiRoutes.js +76 -0
- package/lib/lib/devMiddleware.d.ts +10 -0
- package/lib/lib/devMiddleware.js +81 -0
- package/lib/lib/getPlatform.d.ts +2 -0
- package/lib/lib/getPlatform.js +8 -0
- package/lib/lib/getPublicRuntimeConfig.d.ts +2 -0
- package/lib/lib/getPublicRuntimeConfig.js +14 -0
- package/lib/lib/hotMiddleware.d.ts +31 -0
- package/lib/lib/hotMiddleware.js +123 -0
- package/lib/lib/httpProxyMiddleware.d.ts +7 -0
- package/lib/lib/httpProxyMiddleware.js +49 -0
- package/lib/lib/initRuntime.d.ts +5 -0
- package/lib/lib/initRuntime.js +16 -0
- package/lib/lib/launchEditorMiddleware.d.ts +2 -0
- package/lib/lib/launchEditorMiddleware.js +39 -0
- package/lib/lib/matchRoutes.d.ts +2 -0
- package/lib/lib/matchRoutes.js +8 -0
- package/lib/lib/onDemandRouteManager.d.ts +11 -0
- package/lib/lib/onDemandRouteManager.js +69 -0
- package/lib/lib/renderToHTML.d.ts +10 -0
- package/lib/lib/renderToHTML.js +52 -0
- package/lib/lib/resolveRuntimeCoreFile.d.ts +2 -0
- package/lib/lib/resolveRuntimeCoreFile.js +17 -0
- package/lib/lib/routes.d.ts +12 -0
- package/lib/lib/routes.js +90 -0
- package/lib/lib/runtimeConfig.d.ts +4 -0
- package/lib/lib/runtimeConfig.js +10 -0
- package/lib/lib/serveStatic.d.ts +3 -0
- package/lib/lib/serveStatic.js +24 -0
- package/lib/lib/utils.d.ts +6 -0
- package/lib/lib/utils.js +25 -0
- package/lib/lib/viewTemplate.d.ts +5 -0
- package/lib/lib/viewTemplate.js +16 -0
- package/lib/project/file-manager/errorHandling.d.ts +1 -0
- package/lib/project/file-manager/errorHandling.js +13 -0
- package/lib/project/file-manager/file.d.ts +17 -0
- package/lib/project/file-manager/file.js +146 -0
- package/lib/project/file-manager/fileManager.d.ts +11 -0
- package/lib/project/file-manager/fileManager.js +86 -0
- package/lib/project/file-manager/fileTypes.d.ts +56 -0
- package/lib/project/file-manager/fileTypes.js +2 -0
- package/lib/project/file-manager/index.d.ts +7 -0
- package/lib/project/file-manager/index.js +9 -0
- package/lib/project/file-manager/lifecycle.d.ts +7 -0
- package/lib/project/file-manager/lifecycle.js +42 -0
- package/lib/project/file-manager/mount.d.ts +3 -0
- package/lib/project/file-manager/mount.js +80 -0
- package/lib/project/file-manager/scheduler.d.ts +35 -0
- package/lib/project/file-manager/scheduler.js +169 -0
- package/lib/project/file-manager/utils.d.ts +1 -0
- package/lib/project/file-manager/utils.js +7 -0
- package/lib/project/file-presets/files/core/404.js.d.ts +4 -0
- package/lib/project/file-presets/files/core/404.js.js +9 -0
- package/lib/project/file-presets/files/core/apiRoutes.js.d.ts +5 -0
- package/lib/project/file-presets/files/core/apiRoutes.js.js +5 -0
- package/lib/project/file-presets/files/core/app.js.d.ts +4 -0
- package/lib/project/file-presets/files/core/app.js.js +9 -0
- package/lib/project/file-presets/files/core/client/application.js.d.ts +5 -0
- package/lib/project/file-presets/files/core/client/application.js.js +7 -0
- package/lib/project/file-presets/files/core/client/entry.js.d.ts +5 -0
- package/lib/project/file-presets/files/core/client/entry.js.js +7 -0
- package/lib/project/file-presets/files/core/platform.js.d.ts +5 -0
- package/lib/project/file-presets/files/core/platform.js.js +7 -0
- package/lib/project/file-presets/files/core/plugins.js.d.ts +5 -0
- package/lib/project/file-presets/files/core/plugins.js.js +16 -0
- package/lib/project/file-presets/files/core/polyfill.js.d.ts +5 -0
- package/lib/project/file-presets/files/core/polyfill.js.js +5 -0
- package/lib/project/file-presets/files/core/routes.js.d.ts +5 -0
- package/lib/project/file-presets/files/core/routes.js.js +5 -0
- package/lib/project/file-presets/files/core/runtimeConfig.js.d.ts +5 -0
- package/lib/project/file-presets/files/core/runtimeConfig.js.js +5 -0
- package/lib/project/file-presets/files/core/server/application.js.d.ts +5 -0
- package/lib/project/file-presets/files/core/server/application.js.js +7 -0
- package/lib/project/file-presets/files/core/setRuntimeConfig.js.d.ts +4 -0
- package/lib/project/file-presets/files/core/setRuntimeConfig.js.js +5 -0
- package/lib/project/file-presets/files/entry.client-wrapper.js.d.ts +5 -0
- package/lib/project/file-presets/files/entry.client-wrapper.js.js +5 -0
- package/lib/project/file-presets/files/entry.client.js.d.ts +5 -0
- package/lib/project/file-presets/files/entry.client.js.js +25 -0
- package/lib/project/file-presets/files/entry.server.js.d.ts +5 -0
- package/lib/project/file-presets/files/entry.server.js.js +6 -0
- package/lib/project/file-presets/files/index.d.ts.d.ts +5 -0
- package/lib/project/file-presets/files/index.d.ts.js +6 -0
- package/lib/project/file-presets/files/index.js.d.ts +5 -0
- package/lib/project/file-presets/files/index.js.js +6 -0
- package/lib/project/file-presets/files/main.server.js.d.ts +4 -0
- package/lib/project/file-presets/files/main.server.js.js +13 -0
- package/lib/project/file-presets/files/user/404.js.d.ts +7 -0
- package/lib/project/file-presets/files/user/404.js.js +9 -0
- package/lib/project/file-presets/files/user/app.js.d.ts +7 -0
- package/lib/project/file-presets/files/user/app.js.js +9 -0
- package/lib/project/file-presets/files/user/document.js.d.ts +7 -0
- package/lib/project/file-presets/files/user/document.js.js +9 -0
- package/lib/project/file-presets/files/user/plugin.js.d.ts +7 -0
- package/lib/project/file-presets/files/user/plugin.js.js +9 -0
- package/lib/project/file-presets/files/user/server.js.d.ts +7 -0
- package/lib/project/file-presets/files/user/server.js.js +9 -0
- package/lib/project/file-presets/index.d.ts +2 -0
- package/lib/project/file-presets/index.js +33 -0
- package/lib/project/file-snippets/helpers.d.ts +9 -0
- package/lib/project/file-snippets/helpers.js +72 -0
- package/lib/project/file-snippets/index.d.ts +15 -0
- package/lib/project/file-snippets/index.js +22 -0
- package/lib/project/file-snippets/moduleExportProxy.d.ts +9 -0
- package/lib/project/file-snippets/moduleExportProxy.js +124 -0
- package/lib/project/index.d.ts +3 -0
- package/lib/project/index.js +15 -0
- package/lib/project/projectBuilder.d.ts +36 -0
- package/lib/project/projectBuilder.js +199 -0
- package/lib/project/projectContext.d.ts +66 -0
- package/lib/project/projectContext.js +30 -0
- package/lib/renderer/base.d.ts +22 -0
- package/lib/renderer/base.js +147 -0
- package/lib/renderer/htmlTag.d.ts +4 -0
- package/lib/renderer/htmlTag.js +69 -0
- package/lib/renderer/index.d.ts +11 -0
- package/lib/renderer/index.js +20 -0
- package/lib/renderer/spa.d.ts +10 -0
- package/lib/renderer/spa.js +20 -0
- package/lib/renderer/ssr.d.ts +15 -0
- package/lib/renderer/ssr.js +76 -0
- package/lib/renderer/types.d.ts +14 -0
- package/lib/renderer/types.js +2 -0
- package/lib/route/index.d.ts +1 -0
- package/lib/route/index.js +4 -0
- package/lib/route/parseDynamicPath.d.ts +1 -0
- package/lib/route/parseDynamicPath.js +42 -0
- package/lib/route/route.d.ts +13 -0
- package/lib/route/route.js +161 -0
- package/lib/server/index.d.ts +2 -0
- package/lib/server/index.js +4 -0
- package/lib/server/router.d.ts +14 -0
- package/lib/server/router.js +111 -0
- package/lib/server/server.d.ts +17 -0
- package/lib/server/server.js +139 -0
- package/lib/server/serverTypes.d.ts +9 -0
- package/lib/server/serverTypes.js +2 -0
- package/lib/shuvi/index.d.ts +10 -0
- package/lib/shuvi/index.js +18 -0
- package/lib/shuvi/shuvi.base.d.ts +26 -0
- package/lib/shuvi/shuvi.base.js +152 -0
- package/lib/shuvi/shuvi.dev.d.ts +9 -0
- package/lib/shuvi/shuvi.dev.js +94 -0
- package/lib/shuvi/shuvi.prod.d.ts +6 -0
- package/lib/shuvi/shuvi.prod.js +55 -0
- package/lib/types/bundler.d.ts +1 -0
- package/lib/types/bundler.js +10 -0
- package/lib/types/hooks.d.ts +67 -0
- package/lib/types/hooks.js +2 -0
- package/lib/types/index.d.ts +3 -0
- package/lib/types/index.js +12 -0
- package/lib/types/runtime.d.ts +168 -0
- package/lib/types/runtime.js +2 -0
- package/package.json +65 -0
- 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,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;
|
package/lib/cli/build.js
ADDED
|
@@ -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";
|
package/lib/constants.js
ADDED
|
@@ -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 {};
|