@modern-js/app-tools 2.48.5 → 2.49.0
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/dist/cjs/commands/build.js +3 -0
- package/dist/cjs/commands/dev.js +16 -16
- package/dist/cjs/commands/serve.js +14 -22
- package/dist/cjs/utils/createServer.js +5 -27
- package/dist/cjs/utils/env.js +2 -2
- package/dist/cjs/utils/register.js +103 -0
- package/dist/esm/commands/build.js +16 -9
- package/dist/esm/commands/dev.js +34 -38
- package/dist/esm/commands/serve.js +28 -32
- package/dist/esm/utils/createServer.js +14 -50
- package/dist/esm/utils/env.js +1 -1
- package/dist/esm/utils/register.js +129 -0
- package/dist/esm-node/commands/build.js +3 -0
- package/dist/esm-node/commands/dev.js +17 -17
- package/dist/esm-node/commands/serve.js +14 -12
- package/dist/esm-node/utils/createServer.js +6 -17
- package/dist/esm-node/utils/env.js +1 -1
- package/dist/esm-node/utils/register.js +69 -0
- package/dist/types/utils/createServer.d.ts +4 -9
- package/dist/types/utils/register.d.ts +3 -0
- package/package.json +24 -21
@@ -3,20 +3,19 @@ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
3
3
|
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
4
4
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
5
5
|
import { logger, isApiOnly, getTargetDir } from "@modern-js/utils";
|
6
|
-
import
|
6
|
+
import { createProdServer } from "@modern-js/prod-server";
|
7
7
|
import { printInstructions } from "../utils/printInstructions";
|
8
|
-
import { injectDataLoaderPlugin } from "../utils/createServer";
|
9
8
|
import { getServerInternalPlugins } from "../utils/getServerInternalPlugins";
|
10
9
|
var start = function() {
|
11
10
|
var _ref = _async_to_generator(function(api) {
|
12
|
-
var _userConfig_source, _userConfig_output_distPath, appContext, userConfig, hookRunners, appDirectory, port, serverConfigFile, metaName, apiOnly, serverInternalPlugins, app;
|
11
|
+
var _userConfig_source, _userConfig_output_distPath, appContext, userConfig, hookRunners, distDirectory, appDirectory, port, serverConfigFile, metaName, serverRoutes, apiOnly, runMode, serverInternalPlugins, app;
|
13
12
|
return _ts_generator(this, function(_state) {
|
14
13
|
switch (_state.label) {
|
15
14
|
case 0:
|
16
15
|
appContext = api.useAppContext();
|
17
16
|
userConfig = api.useResolvedConfigContext();
|
18
17
|
hookRunners = api.useHookRunners();
|
19
|
-
appDirectory = appContext.appDirectory, port = appContext.port, serverConfigFile = appContext.serverConfigFile, metaName = appContext.metaName;
|
18
|
+
distDirectory = appContext.distDirectory, appDirectory = appContext.appDirectory, port = appContext.port, serverConfigFile = appContext.serverConfigFile, metaName = appContext.metaName, serverRoutes = appContext.serverRoutes;
|
20
19
|
logger.info("Starting production server...");
|
21
20
|
return [
|
22
21
|
4,
|
@@ -24,6 +23,9 @@ var start = function() {
|
|
24
23
|
];
|
25
24
|
case 1:
|
26
25
|
apiOnly = _state.sent();
|
26
|
+
if (apiOnly) {
|
27
|
+
runMode = "apiOnly";
|
28
|
+
}
|
27
29
|
return [
|
28
30
|
4,
|
29
31
|
getServerInternalPlugins(api)
|
@@ -32,51 +34,45 @@ var start = function() {
|
|
32
34
|
serverInternalPlugins = _state.sent();
|
33
35
|
return [
|
34
36
|
4,
|
35
|
-
|
36
|
-
|
37
|
+
createProdServer({
|
38
|
+
metaName,
|
39
|
+
pwd: distDirectory,
|
37
40
|
config: _object_spread_props(_object_spread({}, userConfig), {
|
38
41
|
dev: userConfig.dev,
|
39
42
|
output: _object_spread({
|
40
43
|
path: (_userConfig_output_distPath = userConfig.output.distPath) === null || _userConfig_output_distPath === void 0 ? void 0 : _userConfig_output_distPath.root
|
41
44
|
}, userConfig.output || {})
|
42
45
|
}),
|
46
|
+
routes: serverRoutes,
|
43
47
|
appContext: {
|
44
|
-
|
48
|
+
appDirectory,
|
45
49
|
sharedDirectory: getTargetDir(appContext.sharedDirectory, appContext.appDirectory, appContext.distDirectory),
|
46
50
|
apiDirectory: getTargetDir(appContext.apiDirectory, appContext.appDirectory, appContext.distDirectory),
|
47
51
|
lambdaDirectory: getTargetDir(appContext.lambdaDirectory, appContext.appDirectory, appContext.distDirectory)
|
48
52
|
},
|
49
53
|
serverConfigFile,
|
50
|
-
internalPlugins:
|
51
|
-
|
54
|
+
internalPlugins: serverInternalPlugins,
|
55
|
+
runMode
|
52
56
|
})
|
53
57
|
];
|
54
58
|
case 3:
|
55
59
|
app = _state.sent();
|
56
|
-
app.listen(port, function() {
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
return [
|
71
|
-
2
|
72
|
-
];
|
73
|
-
}
|
74
|
-
});
|
60
|
+
app.listen(port, /* @__PURE__ */ _async_to_generator(function() {
|
61
|
+
return _ts_generator(this, function(_state2) {
|
62
|
+
switch (_state2.label) {
|
63
|
+
case 0:
|
64
|
+
return [
|
65
|
+
4,
|
66
|
+
printInstructions(hookRunners, appContext, userConfig)
|
67
|
+
];
|
68
|
+
case 1:
|
69
|
+
_state2.sent();
|
70
|
+
return [
|
71
|
+
2
|
72
|
+
];
|
73
|
+
}
|
75
74
|
});
|
76
|
-
|
77
|
-
return _ref2.apply(this, arguments);
|
78
|
-
};
|
79
|
-
}());
|
75
|
+
}));
|
80
76
|
return [
|
81
77
|
2
|
82
78
|
];
|
@@ -1,8 +1,7 @@
|
|
1
1
|
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
2
|
-
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
3
|
-
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
4
2
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
5
|
-
import {
|
3
|
+
import { createDevServer } from "@modern-js/server";
|
4
|
+
import { initProdMiddlewares } from "@modern-js/prod-server";
|
6
5
|
var server = null;
|
7
6
|
var getServer = function() {
|
8
7
|
return server;
|
@@ -13,26 +12,13 @@ var setServer = function(newServer) {
|
|
13
12
|
var closeServer = function() {
|
14
13
|
var _ref = _async_to_generator(function() {
|
15
14
|
return _ts_generator(this, function(_state) {
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
return [
|
20
|
-
3,
|
21
|
-
2
|
22
|
-
];
|
23
|
-
return [
|
24
|
-
4,
|
25
|
-
server.close()
|
26
|
-
];
|
27
|
-
case 1:
|
28
|
-
_state.sent();
|
29
|
-
server = null;
|
30
|
-
_state.label = 2;
|
31
|
-
case 2:
|
32
|
-
return [
|
33
|
-
2
|
34
|
-
];
|
15
|
+
if (server) {
|
16
|
+
server.close();
|
17
|
+
server = null;
|
35
18
|
}
|
19
|
+
return [
|
20
|
+
2
|
21
|
+
];
|
36
22
|
});
|
37
23
|
});
|
38
24
|
return function closeServer2() {
|
@@ -41,33 +27,21 @@ var closeServer = function() {
|
|
41
27
|
}();
|
42
28
|
var createServer = function() {
|
43
29
|
var _ref = _async_to_generator(function(options) {
|
44
|
-
var app;
|
45
30
|
return _ts_generator(this, function(_state) {
|
46
31
|
switch (_state.label) {
|
47
32
|
case 0:
|
48
|
-
if (
|
49
|
-
|
50
|
-
|
51
|
-
2
|
52
|
-
];
|
33
|
+
if (server) {
|
34
|
+
server.close();
|
35
|
+
}
|
53
36
|
return [
|
54
37
|
4,
|
55
|
-
|
38
|
+
createDevServer(options, initProdMiddlewares)
|
56
39
|
];
|
57
40
|
case 1:
|
58
|
-
_state.sent();
|
59
|
-
_state.label = 2;
|
60
|
-
case 2:
|
61
|
-
server = new Server(options);
|
62
|
-
return [
|
63
|
-
4,
|
64
|
-
server.init()
|
65
|
-
];
|
66
|
-
case 3:
|
67
|
-
app = _state.sent();
|
41
|
+
server = _state.sent();
|
68
42
|
return [
|
69
43
|
2,
|
70
|
-
|
44
|
+
server
|
71
45
|
];
|
72
46
|
}
|
73
47
|
});
|
@@ -76,19 +50,9 @@ var createServer = function() {
|
|
76
50
|
return _ref.apply(this, arguments);
|
77
51
|
};
|
78
52
|
}();
|
79
|
-
var injectDataLoaderPlugin = function(internalPlugins) {
|
80
|
-
var DataLoaderPlugin = require.resolve("@modern-js/plugin-data-loader/server");
|
81
|
-
return _object_spread_props(_object_spread({}, internalPlugins), {
|
82
|
-
"@modern-js/plugin-data-loader": {
|
83
|
-
path: DataLoaderPlugin,
|
84
|
-
forced: true
|
85
|
-
}
|
86
|
-
});
|
87
|
-
};
|
88
53
|
export {
|
89
54
|
closeServer,
|
90
55
|
createServer,
|
91
56
|
getServer,
|
92
|
-
injectDataLoaderPlugin,
|
93
57
|
setServer
|
94
58
|
};
|
package/dist/esm/utils/env.js
CHANGED
@@ -0,0 +1,129 @@
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
2
|
+
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
3
|
+
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
4
|
+
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
5
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
6
|
+
import path from "node:path";
|
7
|
+
import { fs, getAliasConfig, readTsConfigByFile } from "@modern-js/utils";
|
8
|
+
var registerCompiler = function() {
|
9
|
+
var _ref = _async_to_generator(function() {
|
10
|
+
var appDir, distDir, alias, TS_CONFIG_FILENAME, tsconfigPath, isTsProject, aliasConfig, _aliasConfig_paths, paths, _aliasConfig_absoluteBaseUrl, absoluteBaseUrl, tsPaths, tsConfig, tsNode, tsNodeOptions, error, esbuildRegister, tsConfigPaths, loaderRes;
|
11
|
+
var _arguments = arguments;
|
12
|
+
return _ts_generator(this, function(_state) {
|
13
|
+
switch (_state.label) {
|
14
|
+
case 0:
|
15
|
+
appDir = _arguments.length > 0 && _arguments[0] !== void 0 ? _arguments[0] : process.cwd(), distDir = _arguments.length > 1 ? _arguments[1] : void 0, alias = _arguments.length > 2 ? _arguments[2] : void 0;
|
16
|
+
TS_CONFIG_FILENAME = "tsconfig.json";
|
17
|
+
tsconfigPath = path.resolve(appDir, TS_CONFIG_FILENAME);
|
18
|
+
return [
|
19
|
+
4,
|
20
|
+
fs.pathExists(tsconfigPath)
|
21
|
+
];
|
22
|
+
case 1:
|
23
|
+
isTsProject = _state.sent();
|
24
|
+
aliasConfig = getAliasConfig(alias, {
|
25
|
+
appDirectory: appDir,
|
26
|
+
tsconfigPath
|
27
|
+
});
|
28
|
+
_aliasConfig_paths = aliasConfig.paths, paths = _aliasConfig_paths === void 0 ? {} : _aliasConfig_paths, _aliasConfig_absoluteBaseUrl = aliasConfig.absoluteBaseUrl, absoluteBaseUrl = _aliasConfig_absoluteBaseUrl === void 0 ? "./" : _aliasConfig_absoluteBaseUrl;
|
29
|
+
tsPaths = Object.keys(paths).reduce(function(o, key) {
|
30
|
+
var tsPath = paths[key];
|
31
|
+
if (typeof tsPath === "string" && path.isAbsolute(tsPath)) {
|
32
|
+
tsPath = path.relative(absoluteBaseUrl, tsPath);
|
33
|
+
}
|
34
|
+
if (typeof tsPath === "string") {
|
35
|
+
tsPath = [
|
36
|
+
tsPath
|
37
|
+
];
|
38
|
+
}
|
39
|
+
return _object_spread_props(_object_spread({}, o), _define_property({}, "".concat(key), tsPath));
|
40
|
+
}, {});
|
41
|
+
tsConfig = {};
|
42
|
+
if (isTsProject) {
|
43
|
+
tsConfig = readTsConfigByFile(tsconfigPath);
|
44
|
+
}
|
45
|
+
_state.label = 2;
|
46
|
+
case 2:
|
47
|
+
_state.trys.push([
|
48
|
+
2,
|
49
|
+
4,
|
50
|
+
,
|
51
|
+
6
|
52
|
+
]);
|
53
|
+
return [
|
54
|
+
4,
|
55
|
+
import("ts-node")
|
56
|
+
];
|
57
|
+
case 3:
|
58
|
+
tsNode = _state.sent();
|
59
|
+
tsNodeOptions = tsConfig["ts-node"];
|
60
|
+
if (isTsProject) {
|
61
|
+
tsNode.register(_object_spread({
|
62
|
+
project: tsconfigPath,
|
63
|
+
scope: true,
|
64
|
+
// for env.d.ts, https://www.npmjs.com/package/ts-node#missing-types
|
65
|
+
files: true,
|
66
|
+
transpileOnly: true,
|
67
|
+
ignore: [
|
68
|
+
"(?:^|/)node_modules/",
|
69
|
+
"(?:^|/)".concat(distDir, "/")
|
70
|
+
]
|
71
|
+
}, tsNodeOptions));
|
72
|
+
}
|
73
|
+
return [
|
74
|
+
3,
|
75
|
+
6
|
76
|
+
];
|
77
|
+
case 4:
|
78
|
+
error = _state.sent();
|
79
|
+
return [
|
80
|
+
4,
|
81
|
+
import("esbuild-register/dist/node")
|
82
|
+
];
|
83
|
+
case 5:
|
84
|
+
esbuildRegister = _state.sent();
|
85
|
+
esbuildRegister.register({
|
86
|
+
tsconfigRaw: isTsProject ? tsConfig : void 0,
|
87
|
+
hookIgnoreNodeModules: true,
|
88
|
+
hookMatcher: function(fileName) {
|
89
|
+
return !fileName.startsWith(distDir);
|
90
|
+
}
|
91
|
+
});
|
92
|
+
return [
|
93
|
+
3,
|
94
|
+
6
|
95
|
+
];
|
96
|
+
case 6:
|
97
|
+
return [
|
98
|
+
4,
|
99
|
+
import("@modern-js/utils/tsconfig-paths")
|
100
|
+
];
|
101
|
+
case 7:
|
102
|
+
tsConfigPaths = _state.sent();
|
103
|
+
return [
|
104
|
+
4,
|
105
|
+
fs.pathExists(appDir)
|
106
|
+
];
|
107
|
+
case 8:
|
108
|
+
if (_state.sent()) {
|
109
|
+
loaderRes = tsConfigPaths.loadConfig(appDir);
|
110
|
+
if (loaderRes.resultType === "success") {
|
111
|
+
tsConfigPaths.register({
|
112
|
+
baseUrl: absoluteBaseUrl || "./",
|
113
|
+
paths: tsPaths
|
114
|
+
});
|
115
|
+
}
|
116
|
+
}
|
117
|
+
return [
|
118
|
+
2
|
119
|
+
];
|
120
|
+
}
|
121
|
+
});
|
122
|
+
});
|
123
|
+
return function registerCompiler2() {
|
124
|
+
return _ref.apply(this, arguments);
|
125
|
+
};
|
126
|
+
}();
|
127
|
+
export {
|
128
|
+
registerCompiler
|
129
|
+
};
|
@@ -2,13 +2,16 @@ import { ResolvedConfigContext } from "@modern-js/core";
|
|
2
2
|
import { logger } from "@modern-js/utils";
|
3
3
|
import { generateRoutes } from "../utils/routes";
|
4
4
|
import { buildServerConfig } from "../utils/config";
|
5
|
+
import { registerCompiler } from "../utils/register";
|
5
6
|
const build = async (api, options) => {
|
7
|
+
var _resolvedConfig_source;
|
6
8
|
if (options === null || options === void 0 ? void 0 : options.analyze) {
|
7
9
|
process.env.BUNDLE_ANALYZE = "true";
|
8
10
|
}
|
9
11
|
let resolvedConfig = api.useResolvedConfigContext();
|
10
12
|
const appContext = api.useAppContext();
|
11
13
|
const hookRunners = api.useHookRunners();
|
14
|
+
await registerCompiler(appContext.appDirectory, appContext.distDirectory, resolvedConfig === null || resolvedConfig === void 0 ? void 0 : (_resolvedConfig_source = resolvedConfig.source) === null || _resolvedConfig_source === void 0 ? void 0 : _resolvedConfig_source.alias);
|
12
15
|
const { apiOnly } = appContext;
|
13
16
|
if (apiOnly) {
|
14
17
|
const { appDirectory: appDirectory2, distDirectory: distDirectory2, serverConfigFile: serverConfigFile2 } = appContext;
|
@@ -1,24 +1,28 @@
|
|
1
1
|
import { ResolvedConfigContext } from "@modern-js/core";
|
2
2
|
import { DEFAULT_DEV_HOST } from "@modern-js/utils";
|
3
|
+
import { createDevServer } from "@modern-js/server";
|
4
|
+
import { initProdMiddlewares } from "@modern-js/prod-server";
|
5
|
+
import { registerCompiler } from "../utils/register";
|
3
6
|
import { printInstructions } from "../utils/printInstructions";
|
4
|
-
import { setServer
|
7
|
+
import { setServer } from "../utils/createServer";
|
5
8
|
import { generateRoutes } from "../utils/routes";
|
6
9
|
import { buildServerConfig } from "../utils/config";
|
7
10
|
import { getServerInternalPlugins } from "../utils/getServerInternalPlugins";
|
8
11
|
const dev = async (api, options, devServerOptions = {}) => {
|
9
|
-
var _normalizedConfig_tools;
|
12
|
+
var _normalizedConfig_source, _normalizedConfig_tools;
|
10
13
|
if (options.analyze) {
|
11
14
|
process.env.BUNDLE_ANALYZE = "true";
|
12
15
|
}
|
13
16
|
let normalizedConfig = api.useResolvedConfigContext();
|
14
17
|
const appContext = api.useAppContext();
|
15
18
|
const hookRunners = api.useHookRunners();
|
19
|
+
await registerCompiler(appContext.appDirectory, appContext.distDirectory, normalizedConfig === null || normalizedConfig === void 0 ? void 0 : (_normalizedConfig_source = normalizedConfig.source) === null || _normalizedConfig_source === void 0 ? void 0 : _normalizedConfig_source.alias);
|
16
20
|
normalizedConfig = {
|
17
21
|
...normalizedConfig,
|
18
22
|
cliOptions: options
|
19
23
|
};
|
20
24
|
ResolvedConfigContext.set(normalizedConfig);
|
21
|
-
const { appDirectory, distDirectory, port, apiOnly, serverConfigFile, metaName } = appContext;
|
25
|
+
const { appDirectory, distDirectory, port, apiOnly, serverConfigFile, metaName, serverRoutes } = appContext;
|
22
26
|
await buildServerConfig({
|
23
27
|
appDirectory,
|
24
28
|
distDirectory,
|
@@ -32,6 +36,7 @@ const dev = async (api, options, devServerOptions = {}) => {
|
|
32
36
|
await generateRoutes(appContext);
|
33
37
|
const serverInternalPlugins = await getServerInternalPlugins(api);
|
34
38
|
const serverOptions = {
|
39
|
+
metaName,
|
35
40
|
dev: {
|
36
41
|
port,
|
37
42
|
https: normalizedConfig.dev.https,
|
@@ -39,37 +44,32 @@ const dev = async (api, options, devServerOptions = {}) => {
|
|
39
44
|
...(_normalizedConfig_tools = normalizedConfig.tools) === null || _normalizedConfig_tools === void 0 ? void 0 : _normalizedConfig_tools.devServer
|
40
45
|
},
|
41
46
|
appContext: {
|
42
|
-
|
43
|
-
appDirectory: appContext.appDirectory,
|
44
|
-
sharedDirectory: appContext.sharedDirectory,
|
47
|
+
appDirectory,
|
45
48
|
apiDirectory: appContext.apiDirectory,
|
46
|
-
lambdaDirectory: appContext.lambdaDirectory
|
49
|
+
lambdaDirectory: appContext.lambdaDirectory,
|
50
|
+
sharedDirectory: appContext.sharedDirectory
|
47
51
|
},
|
52
|
+
routes: serverRoutes,
|
48
53
|
pwd: appDirectory,
|
49
54
|
config: normalizedConfig,
|
50
55
|
serverConfigFile,
|
51
|
-
internalPlugins:
|
56
|
+
internalPlugins: serverInternalPlugins,
|
52
57
|
...devServerOptions
|
53
58
|
};
|
54
59
|
if (apiOnly) {
|
55
60
|
var _normalizedConfig_dev;
|
56
|
-
const app = await
|
57
|
-
...serverOptions,
|
58
|
-
compiler: null
|
59
|
-
});
|
61
|
+
const app = await createDevServer(serverOptions, initProdMiddlewares);
|
60
62
|
const host = ((_normalizedConfig_dev = normalizedConfig.dev) === null || _normalizedConfig_dev === void 0 ? void 0 : _normalizedConfig_dev.host) || DEFAULT_DEV_HOST;
|
61
63
|
app.listen({
|
62
64
|
port,
|
63
65
|
host
|
64
|
-
},
|
65
|
-
if (err) {
|
66
|
-
throw err;
|
67
|
-
}
|
66
|
+
}, () => {
|
68
67
|
printInstructions(hookRunners, appContext, normalizedConfig);
|
69
68
|
});
|
70
69
|
} else {
|
71
70
|
const { server } = await appContext.builder.startDevServer({
|
72
|
-
serverOptions
|
71
|
+
serverOptions,
|
72
|
+
initProdMiddlewares
|
73
73
|
});
|
74
74
|
setServer(server);
|
75
75
|
}
|
@@ -1,19 +1,23 @@
|
|
1
1
|
import { logger, isApiOnly, getTargetDir } from "@modern-js/utils";
|
2
|
-
import
|
2
|
+
import { createProdServer } from "@modern-js/prod-server";
|
3
3
|
import { printInstructions } from "../utils/printInstructions";
|
4
|
-
import { injectDataLoaderPlugin } from "../utils/createServer";
|
5
4
|
import { getServerInternalPlugins } from "../utils/getServerInternalPlugins";
|
6
5
|
const start = async (api) => {
|
7
6
|
var _userConfig_source, _userConfig_output_distPath;
|
8
7
|
const appContext = api.useAppContext();
|
9
8
|
const userConfig = api.useResolvedConfigContext();
|
10
9
|
const hookRunners = api.useHookRunners();
|
11
|
-
const { appDirectory, port, serverConfigFile, metaName } = appContext;
|
10
|
+
const { distDirectory, appDirectory, port, serverConfigFile, metaName, serverRoutes } = appContext;
|
12
11
|
logger.info(`Starting production server...`);
|
13
12
|
const apiOnly = await isApiOnly(appContext.appDirectory, userConfig === null || userConfig === void 0 ? void 0 : (_userConfig_source = userConfig.source) === null || _userConfig_source === void 0 ? void 0 : _userConfig_source.entriesDir, appContext.apiDirectory);
|
13
|
+
let runMode;
|
14
|
+
if (apiOnly) {
|
15
|
+
runMode = "apiOnly";
|
16
|
+
}
|
14
17
|
const serverInternalPlugins = await getServerInternalPlugins(api);
|
15
|
-
const app = await
|
16
|
-
|
18
|
+
const app = await createProdServer({
|
19
|
+
metaName,
|
20
|
+
pwd: distDirectory,
|
17
21
|
config: {
|
18
22
|
...userConfig,
|
19
23
|
dev: userConfig.dev,
|
@@ -22,20 +26,18 @@ const start = async (api) => {
|
|
22
26
|
...userConfig.output || {}
|
23
27
|
}
|
24
28
|
},
|
29
|
+
routes: serverRoutes,
|
25
30
|
appContext: {
|
26
|
-
|
31
|
+
appDirectory,
|
27
32
|
sharedDirectory: getTargetDir(appContext.sharedDirectory, appContext.appDirectory, appContext.distDirectory),
|
28
33
|
apiDirectory: getTargetDir(appContext.apiDirectory, appContext.appDirectory, appContext.distDirectory),
|
29
34
|
lambdaDirectory: getTargetDir(appContext.lambdaDirectory, appContext.appDirectory, appContext.distDirectory)
|
30
35
|
},
|
31
36
|
serverConfigFile,
|
32
|
-
internalPlugins:
|
33
|
-
|
37
|
+
internalPlugins: serverInternalPlugins,
|
38
|
+
runMode
|
34
39
|
});
|
35
|
-
app.listen(port, async (
|
36
|
-
if (err) {
|
37
|
-
throw err;
|
38
|
-
}
|
40
|
+
app.listen(port, async () => {
|
39
41
|
await printInstructions(hookRunners, appContext, userConfig);
|
40
42
|
});
|
41
43
|
};
|
@@ -1,4 +1,5 @@
|
|
1
|
-
import {
|
1
|
+
import { createDevServer } from "@modern-js/server";
|
2
|
+
import { initProdMiddlewares } from "@modern-js/prod-server";
|
2
3
|
let server = null;
|
3
4
|
const getServer = () => server;
|
4
5
|
const setServer = (newServer) => {
|
@@ -6,32 +7,20 @@ const setServer = (newServer) => {
|
|
6
7
|
};
|
7
8
|
const closeServer = async () => {
|
8
9
|
if (server) {
|
9
|
-
|
10
|
+
server.close();
|
10
11
|
server = null;
|
11
12
|
}
|
12
13
|
};
|
13
14
|
const createServer = async (options) => {
|
14
15
|
if (server) {
|
15
|
-
|
16
|
+
server.close();
|
16
17
|
}
|
17
|
-
server =
|
18
|
-
|
19
|
-
return app;
|
20
|
-
};
|
21
|
-
const injectDataLoaderPlugin = (internalPlugins) => {
|
22
|
-
const DataLoaderPlugin = require.resolve("@modern-js/plugin-data-loader/server");
|
23
|
-
return {
|
24
|
-
...internalPlugins,
|
25
|
-
"@modern-js/plugin-data-loader": {
|
26
|
-
path: DataLoaderPlugin,
|
27
|
-
forced: true
|
28
|
-
}
|
29
|
-
};
|
18
|
+
server = await createDevServer(options, initProdMiddlewares);
|
19
|
+
return server;
|
30
20
|
};
|
31
21
|
export {
|
32
22
|
closeServer,
|
33
23
|
createServer,
|
34
24
|
getServer,
|
35
|
-
injectDataLoaderPlugin,
|
36
25
|
setServer
|
37
26
|
};
|
@@ -0,0 +1,69 @@
|
|
1
|
+
import path from "node:path";
|
2
|
+
import { fs, getAliasConfig, readTsConfigByFile } from "@modern-js/utils";
|
3
|
+
const registerCompiler = async (appDir = process.cwd(), distDir, alias) => {
|
4
|
+
const TS_CONFIG_FILENAME = `tsconfig.json`;
|
5
|
+
const tsconfigPath = path.resolve(appDir, TS_CONFIG_FILENAME);
|
6
|
+
const isTsProject = await fs.pathExists(tsconfigPath);
|
7
|
+
const aliasConfig = getAliasConfig(alias, {
|
8
|
+
appDirectory: appDir,
|
9
|
+
tsconfigPath
|
10
|
+
});
|
11
|
+
const { paths = {}, absoluteBaseUrl = "./" } = aliasConfig;
|
12
|
+
const tsPaths = Object.keys(paths).reduce((o, key) => {
|
13
|
+
let tsPath = paths[key];
|
14
|
+
if (typeof tsPath === "string" && path.isAbsolute(tsPath)) {
|
15
|
+
tsPath = path.relative(absoluteBaseUrl, tsPath);
|
16
|
+
}
|
17
|
+
if (typeof tsPath === "string") {
|
18
|
+
tsPath = [
|
19
|
+
tsPath
|
20
|
+
];
|
21
|
+
}
|
22
|
+
return {
|
23
|
+
...o,
|
24
|
+
[`${key}`]: tsPath
|
25
|
+
};
|
26
|
+
}, {});
|
27
|
+
let tsConfig = {};
|
28
|
+
if (isTsProject) {
|
29
|
+
tsConfig = readTsConfigByFile(tsconfigPath);
|
30
|
+
}
|
31
|
+
try {
|
32
|
+
const tsNode = await import("ts-node");
|
33
|
+
const tsNodeOptions = tsConfig["ts-node"];
|
34
|
+
if (isTsProject) {
|
35
|
+
tsNode.register({
|
36
|
+
project: tsconfigPath,
|
37
|
+
scope: true,
|
38
|
+
// for env.d.ts, https://www.npmjs.com/package/ts-node#missing-types
|
39
|
+
files: true,
|
40
|
+
transpileOnly: true,
|
41
|
+
ignore: [
|
42
|
+
"(?:^|/)node_modules/",
|
43
|
+
`(?:^|/)${distDir}/`
|
44
|
+
],
|
45
|
+
...tsNodeOptions
|
46
|
+
});
|
47
|
+
}
|
48
|
+
} catch (error) {
|
49
|
+
const esbuildRegister = await import("esbuild-register/dist/node");
|
50
|
+
esbuildRegister.register({
|
51
|
+
tsconfigRaw: isTsProject ? tsConfig : void 0,
|
52
|
+
hookIgnoreNodeModules: true,
|
53
|
+
hookMatcher: (fileName) => !fileName.startsWith(distDir)
|
54
|
+
});
|
55
|
+
}
|
56
|
+
const tsConfigPaths = await import("@modern-js/utils/tsconfig-paths");
|
57
|
+
if (await fs.pathExists(appDir)) {
|
58
|
+
const loaderRes = tsConfigPaths.loadConfig(appDir);
|
59
|
+
if (loaderRes.resultType === "success") {
|
60
|
+
tsConfigPaths.register({
|
61
|
+
baseUrl: absoluteBaseUrl || "./",
|
62
|
+
paths: tsPaths
|
63
|
+
});
|
64
|
+
}
|
65
|
+
}
|
66
|
+
};
|
67
|
+
export {
|
68
|
+
registerCompiler
|
69
|
+
};
|
@@ -1,12 +1,7 @@
|
|
1
|
-
|
2
|
-
import type {
|
3
|
-
|
1
|
+
/// <reference types="node" />
|
2
|
+
import type { Server } from 'node:http';
|
3
|
+
import { ModernDevServerOptions } from '@modern-js/server';
|
4
|
+
export declare const getServer: () => Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse> | null;
|
4
5
|
export declare const setServer: (newServer: Server) => void;
|
5
6
|
export declare const closeServer: () => Promise<void>;
|
6
7
|
export declare const createServer: (options: ModernDevServerOptions) => Promise<Server>;
|
7
|
-
export declare const injectDataLoaderPlugin: (internalPlugins: InternalPlugins) => {
|
8
|
-
'@modern-js/plugin-data-loader': {
|
9
|
-
path: string;
|
10
|
-
forced: boolean;
|
11
|
-
};
|
12
|
-
};
|