@modern-js/app-tools 2.49.3-alpha.1 → 2.49.3-alpha.11
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/cjs/analyze/getServerRoutes.js +4 -3
- package/dist/cjs/plugins/deploy/index.js +60 -25
- package/dist/cjs/plugins/deploy/platforms/nodeEntry.js +36 -0
- package/dist/cjs/plugins/deploy/platforms/platform.js +16 -0
- package/dist/cjs/plugins/deploy/platforms/vercel.js +66 -0
- package/dist/cjs/plugins/deploy/platforms/vercelEntry.js +55 -0
- package/dist/cjs/utils/routes.js +7 -2
- package/dist/esm/analyze/getServerRoutes.js +5 -4
- package/dist/esm/plugins/deploy/index.js +124 -74
- package/dist/esm/plugins/deploy/platforms/nodeEntry.js +99 -0
- package/dist/esm/plugins/deploy/platforms/platform.js +0 -0
- package/dist/esm/plugins/deploy/platforms/vercel.js +56 -0
- package/dist/esm/plugins/deploy/platforms/vercelEntry.js +197 -0
- package/dist/esm/utils/routes.js +6 -2
- package/dist/esm-node/analyze/getServerRoutes.js +5 -4
- package/dist/esm-node/plugins/deploy/index.js +60 -25
- package/dist/esm-node/plugins/deploy/platforms/nodeEntry.js +35 -0
- package/dist/esm-node/plugins/deploy/platforms/platform.js +0 -0
- package/dist/esm-node/plugins/deploy/platforms/vercel.js +32 -0
- package/dist/esm-node/plugins/deploy/platforms/vercelEntry.js +63 -0
- package/dist/esm-node/utils/routes.js +6 -2
- package/dist/types/plugins/deploy/platforms/nodeEntry.d.ts +1 -0
- package/dist/types/plugins/deploy/platforms/platform.d.ts +1 -0
- package/dist/types/plugins/deploy/{entrys → platforms}/vercel.d.ts +2 -3
- package/dist/types/plugins/deploy/platforms/vercelEntry.d.ts +2 -0
- package/dist/types/utils/routes.d.ts +3 -3
- package/package.json +8 -8
- package/dist/cjs/plugins/deploy/entrys/vercel.js +0 -97
- package/dist/esm/plugins/deploy/entrys/vercel.js +0 -43
- package/dist/esm-node/plugins/deploy/entrys/vercel.js +0 -73
- /package/dist/cjs/plugins/deploy/{entrys → platforms}/netlify.js +0 -0
- /package/dist/cjs/plugins/deploy/{entrys → platforms}/node.js +0 -0
- /package/dist/esm/plugins/deploy/{entrys → platforms}/netlify.js +0 -0
- /package/dist/esm/plugins/deploy/{entrys → platforms}/node.js +0 -0
- /package/dist/esm-node/plugins/deploy/{entrys → platforms}/netlify.js +0 -0
- /package/dist/esm-node/plugins/deploy/{entrys → platforms}/node.js +0 -0
- /package/dist/types/plugins/deploy/{entrys → platforms}/netlify.d.ts +0 -0
- /package/dist/types/plugins/deploy/{entrys → platforms}/node.d.ts +0 -0
@@ -0,0 +1,197 @@
|
|
1
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
2
|
+
var __commonJS = (cb, mod) => function __require() {
|
3
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
4
|
+
};
|
5
|
+
var require_vercelEntry = __commonJS({
|
6
|
+
"src/plugins/deploy/platforms/vercelEntry.js"(exports, module) {
|
7
|
+
var _async_to_generator = require("@swc/helpers/_/_async_to_generator");
|
8
|
+
var _object_spread = require("@swc/helpers/_/_object_spread");
|
9
|
+
var _ts_generator = require("@swc/helpers/_/_ts_generator");
|
10
|
+
var fs = require("node:fs/promises");
|
11
|
+
var path = require("node:path");
|
12
|
+
var createProdServer = require("@modern-js/prod-server").createProdServer;
|
13
|
+
p_genPluginImportsCode;
|
14
|
+
if (!process.env.NODE_ENV) {
|
15
|
+
process.env.NODE_ENV = "production";
|
16
|
+
}
|
17
|
+
var requestHandler = null;
|
18
|
+
var handlerCreationPromise = null;
|
19
|
+
function loadRoutes(routeFilepath) {
|
20
|
+
return _loadRoutes.apply(this, arguments);
|
21
|
+
}
|
22
|
+
function _loadRoutes() {
|
23
|
+
_loadRoutes = _async_to_generator._(function(routeFilepath) {
|
24
|
+
var content, routeSpec, error;
|
25
|
+
return _ts_generator._(this, function(_state) {
|
26
|
+
switch (_state.label) {
|
27
|
+
case 0:
|
28
|
+
_state.trys.push([
|
29
|
+
0,
|
30
|
+
3,
|
31
|
+
,
|
32
|
+
4
|
33
|
+
]);
|
34
|
+
return [
|
35
|
+
4,
|
36
|
+
fs.access(routeFilepath)
|
37
|
+
];
|
38
|
+
case 1:
|
39
|
+
_state.sent();
|
40
|
+
return [
|
41
|
+
4,
|
42
|
+
fs.readFile(routeFilepath, "utf-8")
|
43
|
+
];
|
44
|
+
case 2:
|
45
|
+
content = _state.sent();
|
46
|
+
routeSpec = JSON.parse(content);
|
47
|
+
return [
|
48
|
+
2,
|
49
|
+
routeSpec.routes || []
|
50
|
+
];
|
51
|
+
case 3:
|
52
|
+
error = _state.sent();
|
53
|
+
console.warn("route.json not found or invalid, continuing with empty routes.");
|
54
|
+
return [
|
55
|
+
2,
|
56
|
+
[]
|
57
|
+
];
|
58
|
+
case 4:
|
59
|
+
return [
|
60
|
+
2
|
61
|
+
];
|
62
|
+
}
|
63
|
+
});
|
64
|
+
});
|
65
|
+
return _loadRoutes.apply(this, arguments);
|
66
|
+
}
|
67
|
+
function initServer() {
|
68
|
+
return _initServer.apply(this, arguments);
|
69
|
+
}
|
70
|
+
function _initServer() {
|
71
|
+
_initServer = _async_to_generator._(function() {
|
72
|
+
var routeFilepath, routes, dynamicProdOptions, prodServerOptions, app;
|
73
|
+
return _ts_generator._(this, function(_state) {
|
74
|
+
switch (_state.label) {
|
75
|
+
case 0:
|
76
|
+
routeFilepath = path.join(__dirname, p_ROUTE_SPEC_FILE);
|
77
|
+
return [
|
78
|
+
4,
|
79
|
+
loadRoutes(routeFilepath)
|
80
|
+
];
|
81
|
+
case 1:
|
82
|
+
routes = _state.sent();
|
83
|
+
dynamicProdOptions = p_dynamicProdOptions;
|
84
|
+
prodServerOptions = _object_spread._({
|
85
|
+
pwd: __dirname,
|
86
|
+
routes,
|
87
|
+
disableCustomHook: true
|
88
|
+
}, dynamicProdOptions);
|
89
|
+
return [
|
90
|
+
4,
|
91
|
+
createProdServer(prodServerOptions)
|
92
|
+
];
|
93
|
+
case 2:
|
94
|
+
app = _state.sent();
|
95
|
+
return [
|
96
|
+
2,
|
97
|
+
app.getRequestListener()
|
98
|
+
];
|
99
|
+
}
|
100
|
+
});
|
101
|
+
});
|
102
|
+
return _initServer.apply(this, arguments);
|
103
|
+
}
|
104
|
+
function createHandler() {
|
105
|
+
return _createHandler.apply(this, arguments);
|
106
|
+
}
|
107
|
+
function _createHandler() {
|
108
|
+
_createHandler = _async_to_generator._(function() {
|
109
|
+
return _ts_generator._(this, function(_state) {
|
110
|
+
switch (_state.label) {
|
111
|
+
case 0:
|
112
|
+
if (!handlerCreationPromise) {
|
113
|
+
handlerCreationPromise = _async_to_generator._(function() {
|
114
|
+
var error;
|
115
|
+
return _ts_generator._(this, function(_state2) {
|
116
|
+
switch (_state2.label) {
|
117
|
+
case 0:
|
118
|
+
_state2.trys.push([
|
119
|
+
0,
|
120
|
+
2,
|
121
|
+
,
|
122
|
+
3
|
123
|
+
]);
|
124
|
+
return [
|
125
|
+
4,
|
126
|
+
initServer()
|
127
|
+
];
|
128
|
+
case 1:
|
129
|
+
requestHandler = _state2.sent();
|
130
|
+
return [
|
131
|
+
3,
|
132
|
+
3
|
133
|
+
];
|
134
|
+
case 2:
|
135
|
+
error = _state2.sent();
|
136
|
+
console.error("Error creating server:", error);
|
137
|
+
process.exit(1);
|
138
|
+
return [
|
139
|
+
3,
|
140
|
+
3
|
141
|
+
];
|
142
|
+
case 3:
|
143
|
+
return [
|
144
|
+
2
|
145
|
+
];
|
146
|
+
}
|
147
|
+
});
|
148
|
+
})();
|
149
|
+
}
|
150
|
+
return [
|
151
|
+
4,
|
152
|
+
handlerCreationPromise
|
153
|
+
];
|
154
|
+
case 1:
|
155
|
+
_state.sent();
|
156
|
+
return [
|
157
|
+
2,
|
158
|
+
requestHandler
|
159
|
+
];
|
160
|
+
}
|
161
|
+
});
|
162
|
+
});
|
163
|
+
return _createHandler.apply(this, arguments);
|
164
|
+
}
|
165
|
+
createHandler();
|
166
|
+
module.exports = function() {
|
167
|
+
var _ref = _async_to_generator._(function(req, res) {
|
168
|
+
return _ts_generator._(this, function(_state) {
|
169
|
+
switch (_state.label) {
|
170
|
+
case 0:
|
171
|
+
if (!!requestHandler)
|
172
|
+
return [
|
173
|
+
3,
|
174
|
+
2
|
175
|
+
];
|
176
|
+
return [
|
177
|
+
4,
|
178
|
+
createHandler()
|
179
|
+
];
|
180
|
+
case 1:
|
181
|
+
_state.sent();
|
182
|
+
_state.label = 2;
|
183
|
+
case 2:
|
184
|
+
return [
|
185
|
+
2,
|
186
|
+
requestHandler(req, res)
|
187
|
+
];
|
188
|
+
}
|
189
|
+
});
|
190
|
+
});
|
191
|
+
return function(req, res) {
|
192
|
+
return _ref.apply(this, arguments);
|
193
|
+
};
|
194
|
+
}();
|
195
|
+
}
|
196
|
+
});
|
197
|
+
export default require_vercelEntry();
|
package/dist/esm/utils/routes.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
2
2
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
3
3
|
import path from "path";
|
4
|
-
import { fs, ROUTE_SPEC_FILE } from "@modern-js/utils";
|
4
|
+
import { fs, MAIN_ENTRY_NAME, ROUTE_SPEC_FILE } from "@modern-js/utils";
|
5
5
|
var generateRoutes = function() {
|
6
6
|
var _ref = _async_to_generator(function(appContext) {
|
7
7
|
var serverRoutes, distDirectory, output;
|
@@ -32,7 +32,11 @@ var getPathWithoutExt = function(filename) {
|
|
32
32
|
var extname = path.extname(filename);
|
33
33
|
return filename.slice(0, -extname.length);
|
34
34
|
};
|
35
|
+
var isMainEntry = function(entryName, mainEntryName) {
|
36
|
+
return entryName === (mainEntryName || MAIN_ENTRY_NAME);
|
37
|
+
};
|
35
38
|
export {
|
36
39
|
generateRoutes,
|
37
|
-
getPathWithoutExt
|
40
|
+
getPathWithoutExt,
|
41
|
+
isMainEntry
|
38
42
|
};
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import path from "path";
|
2
2
|
import fs from "fs";
|
3
|
-
import { urlJoin, isPlainObject, removeLeadingSlash, getEntryOptions, SERVER_BUNDLE_DIRECTORY,
|
3
|
+
import { urlJoin, isPlainObject, removeLeadingSlash, getEntryOptions, SERVER_BUNDLE_DIRECTORY, removeTailSlash, SERVER_WORKER_BUNDLE_DIRECTORY } from "@modern-js/utils";
|
4
|
+
import { isMainEntry } from "../utils/routes";
|
4
5
|
import { walkDirectory } from "./utils";
|
5
6
|
const applyBaseUrl = (baseUrl, routes) => {
|
6
7
|
if (baseUrl) {
|
@@ -73,14 +74,14 @@ const collectHtmlRoutes = (entrypoints, appContext, config) => {
|
|
73
74
|
const { packageName } = appContext;
|
74
75
|
const workerSSR = deploy === null || deploy === void 0 ? void 0 : (_deploy_worker = deploy.worker) === null || _deploy_worker === void 0 ? void 0 : _deploy_worker.ssr;
|
75
76
|
let htmlRoutes = entrypoints.reduce((previous, { entryName }) => {
|
76
|
-
const
|
77
|
-
const entryOptions = getEntryOptions(entryName,
|
77
|
+
const isMain = isMainEntry(entryName, mainEntryName);
|
78
|
+
const entryOptions = getEntryOptions(entryName, isMain, ssr, ssrByEntries, packageName);
|
78
79
|
const isSSR = Boolean(entryOptions);
|
79
80
|
const isWorker = Boolean(workerSSR);
|
80
81
|
const isStream = typeof entryOptions === "object" && (entryOptions.mode === "stream" || Boolean(entryOptions.preload));
|
81
82
|
const { resHeaders } = (routes === null || routes === void 0 ? void 0 : routes[entryName]) || {};
|
82
83
|
let route = {
|
83
|
-
urlPath: `/${
|
84
|
+
urlPath: `/${isMain ? "" : entryName}`,
|
84
85
|
entryName,
|
85
86
|
entryPath: removeLeadingSlash(path.posix.normalize(`${htmlPath}/${entryName}${disableHtmlFolder ? ".html" : "/index.html"}`)),
|
86
87
|
isSPA: true,
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import path from "path";
|
2
2
|
import { fs as fse, getInternalPlugins } from "@modern-js/utils";
|
3
3
|
import { provider } from "std-env";
|
4
|
+
import { isMainEntry } from "../../utils/routes";
|
4
5
|
import { getProjectUsage } from "./utils";
|
5
6
|
import { handleDependencies } from "./dependencies";
|
6
7
|
var deploy_default = () => ({
|
@@ -14,10 +15,11 @@ var deploy_default = () => ({
|
|
14
15
|
return {
|
15
16
|
async beforeDeploy() {
|
16
17
|
const appContext = api.useAppContext();
|
18
|
+
const modernConfig = api.useResolvedConfigContext();
|
19
|
+
const { source: { mainEntryName } } = modernConfig;
|
17
20
|
const { appDirectory, distDirectory, serverInternalPlugins, sharedDirectory, apiDirectory, lambdaDirectory, metaName, entrypoints } = appContext;
|
18
21
|
const { useSSR, useAPI, useWebServer } = getProjectUsage(appDirectory, distDirectory);
|
19
22
|
const needModernServer = useSSR || useAPI || useWebServer;
|
20
|
-
const configContext = api.useResolvedConfigContext();
|
21
23
|
let outputDirectory = path.join(appDirectory, ".output");
|
22
24
|
let funcsDirectory = outputDirectory;
|
23
25
|
let staticDirectory = path.join(outputDirectory, "static");
|
@@ -25,11 +27,48 @@ var deploy_default = () => ({
|
|
25
27
|
await fse.remove(outputDirectory);
|
26
28
|
await fse.copy(distDirectory, outputDirectory);
|
27
29
|
}
|
30
|
+
if (deployTarget === "netlify") {
|
31
|
+
const netlifyOutput = path.join(appDirectory, ".netlify");
|
32
|
+
funcsDirectory = path.join(netlifyOutput, "functions");
|
33
|
+
const routes = [];
|
34
|
+
if (!needModernServer) {
|
35
|
+
entrypoints.forEach((entry) => {
|
36
|
+
const isMain = isMainEntry(entry.entryName, mainEntryName);
|
37
|
+
routes.push({
|
38
|
+
src: `/${isMain ? "" : `${entry.entryName}/`}*`,
|
39
|
+
dest: `/html/${entry.entryName}/index.html`,
|
40
|
+
status: 200
|
41
|
+
});
|
42
|
+
});
|
43
|
+
} else {
|
44
|
+
routes.push({
|
45
|
+
src: `/*`,
|
46
|
+
dest: `/.netlify/functions/index`,
|
47
|
+
status: 200
|
48
|
+
});
|
49
|
+
throw new Error("Currently on the Netlify platform, only CSR projects are supporte, Support for SSR and BFF projects will be available later");
|
50
|
+
}
|
51
|
+
console.log("routes", routes, needModernServer);
|
52
|
+
const redirectContent = routes.map((route) => {
|
53
|
+
return `${route.src} ${route.dest} ${route.status}`;
|
54
|
+
}).join("\n");
|
55
|
+
console.log("redirectContent", redirectContent);
|
56
|
+
await fse.remove(outputDirectory);
|
57
|
+
await fse.ensureDir(funcsDirectory);
|
58
|
+
await fse.copy(distDirectory, funcsDirectory, {
|
59
|
+
filter: (src) => {
|
60
|
+
const distStaticDirectory = path.join(distDirectory, "static");
|
61
|
+
return !src.includes(distStaticDirectory);
|
62
|
+
}
|
63
|
+
});
|
64
|
+
const redirectFilePath = path.join(distDirectory, "_redirects");
|
65
|
+
await fse.writeFile(redirectFilePath, redirectContent);
|
66
|
+
}
|
28
67
|
if (deployTarget === "vercel") {
|
29
68
|
const vercelOutput = path.join(appDirectory, ".vercel");
|
30
69
|
await fse.remove(vercelOutput);
|
31
70
|
outputDirectory = path.join(vercelOutput, "output");
|
32
|
-
const
|
71
|
+
const config = {
|
33
72
|
version: 3,
|
34
73
|
routes: [
|
35
74
|
{
|
@@ -46,8 +85,9 @@ var deploy_default = () => ({
|
|
46
85
|
};
|
47
86
|
if (!needModernServer) {
|
48
87
|
entrypoints.forEach((entry) => {
|
49
|
-
|
50
|
-
|
88
|
+
const isMain = isMainEntry(entry.entryName, mainEntryName);
|
89
|
+
config.routes.push({
|
90
|
+
src: `/${isMain ? "" : entry.entryName}(?:/.*)?`,
|
51
91
|
headers: {
|
52
92
|
"cache-control": "s-maxage=0"
|
53
93
|
},
|
@@ -55,13 +95,13 @@ var deploy_default = () => ({
|
|
55
95
|
});
|
56
96
|
});
|
57
97
|
} else {
|
58
|
-
|
98
|
+
config.routes.push({
|
59
99
|
src: "/(.*)",
|
60
100
|
dest: `/index`
|
61
101
|
});
|
62
102
|
}
|
63
103
|
await fse.ensureDir(outputDirectory);
|
64
|
-
await fse.writeJSON(path.join(outputDirectory, "config.json"),
|
104
|
+
await fse.writeJSON(path.join(outputDirectory, "config.json"), config, {
|
65
105
|
spaces: 2
|
66
106
|
});
|
67
107
|
staticDirectory = path.join(outputDirectory, "static/static");
|
@@ -79,10 +119,6 @@ var deploy_default = () => ({
|
|
79
119
|
return !src.includes(distStaticDirectory);
|
80
120
|
}
|
81
121
|
});
|
82
|
-
const apiDirectory2 = path.join(funcsDirectory, "api");
|
83
|
-
if (await fse.pathExists(apiDirectory2)) {
|
84
|
-
await fse.rename(apiDirectory2, path.join(funcsDirectory, "_api"));
|
85
|
-
}
|
86
122
|
await fse.writeJSON(path.join(funcsDirectory, ".vc-config.json"), {
|
87
123
|
runtime: "nodejs16.x",
|
88
124
|
handler: "index.js",
|
@@ -92,13 +128,6 @@ var deploy_default = () => ({
|
|
92
128
|
});
|
93
129
|
}
|
94
130
|
}
|
95
|
-
const { bff } = configContext;
|
96
|
-
const config = {
|
97
|
-
output: {
|
98
|
-
path: "."
|
99
|
-
},
|
100
|
-
bff
|
101
|
-
};
|
102
131
|
const plugins = getInternalPlugins(appDirectory, serverInternalPlugins);
|
103
132
|
const serverAppContext = {
|
104
133
|
sharedDirectory: `path.join(__dirname, "${path.relative(appDirectory, sharedDirectory)}")`,
|
@@ -110,28 +139,34 @@ var deploy_default = () => ({
|
|
110
139
|
console.log("deployTarget111111111", deployTarget);
|
111
140
|
switch (deployTarget) {
|
112
141
|
case "node": {
|
113
|
-
const { genNodeEntry } = await import("./
|
142
|
+
const { genNodeEntry } = await import("./platforms/node");
|
114
143
|
code = genNodeEntry({
|
115
144
|
plugins,
|
116
|
-
config,
|
145
|
+
config: modernConfig,
|
117
146
|
appContext: serverAppContext
|
118
147
|
});
|
119
148
|
break;
|
120
149
|
}
|
121
150
|
case "vercel": {
|
122
|
-
const { genVercelEntry } = await import("./
|
123
|
-
|
151
|
+
const { genVercelEntry } = await import("./platforms/vercel");
|
152
|
+
const serverAppContext2 = {
|
153
|
+
sharedDirectory: path.join(__dirname, path.relative(appDirectory, sharedDirectory)),
|
154
|
+
apiDirectory: path.join(__dirname, path.relative(appDirectory, apiDirectory)),
|
155
|
+
lambdaDirectory: path.join(__dirname, path.relative(appDirectory, lambdaDirectory)),
|
156
|
+
metaName
|
157
|
+
};
|
158
|
+
code = await genVercelEntry({
|
124
159
|
plugins,
|
125
|
-
config,
|
126
|
-
appContext:
|
160
|
+
config: modernConfig,
|
161
|
+
appContext: serverAppContext2
|
127
162
|
});
|
128
163
|
break;
|
129
164
|
}
|
130
165
|
case "netlify": {
|
131
|
-
const { genNetlifyEntry } = await import("./
|
166
|
+
const { genNetlifyEntry } = await import("./platforms/netlify");
|
132
167
|
code = genNetlifyEntry({
|
133
168
|
plugins,
|
134
|
-
config,
|
169
|
+
config: modernConfig,
|
135
170
|
appContext: serverAppContext
|
136
171
|
});
|
137
172
|
break;
|
@@ -0,0 +1,35 @@
|
|
1
|
+
const fs = require("node:fs/promises");
|
2
|
+
const path = require("node:path");
|
3
|
+
const { createProdServer } = require("@modern-js/prod-server");
|
4
|
+
p_genPluginImportsCode;
|
5
|
+
if (!process.env.NODE_ENV) {
|
6
|
+
process.env.NODE_ENV = "production";
|
7
|
+
}
|
8
|
+
async function loadRoutes(routeFilepath) {
|
9
|
+
try {
|
10
|
+
await fs.access(routeFilepath);
|
11
|
+
const content = await fs.readFile(routeFilepath, "utf-8");
|
12
|
+
const routeSpec = JSON.parse(content);
|
13
|
+
return routeSpec.routes || [];
|
14
|
+
} catch (error) {
|
15
|
+
console.warn("route.json not found or invalid, continuing with empty routes.");
|
16
|
+
return [];
|
17
|
+
}
|
18
|
+
}
|
19
|
+
async function main() {
|
20
|
+
const routeFilepath = path.join(__dirname, p_ROUTE_SPEC_FILE);
|
21
|
+
const routes = await loadRoutes(routeFilepath);
|
22
|
+
const dynamicProdOptions = p_dynamicProdOptions;
|
23
|
+
const prodServerOptions = {
|
24
|
+
pwd: __dirname,
|
25
|
+
routes,
|
26
|
+
disableCustomHook: true,
|
27
|
+
...dynamicProdOptions
|
28
|
+
};
|
29
|
+
const app = await createProdServer(prodServerOptions);
|
30
|
+
const port = process.env.PORT || 8080;
|
31
|
+
app.listen(port, () => {
|
32
|
+
console.log("\\x1b[32mServer is listening on port", port, "\\x1b[0m");
|
33
|
+
});
|
34
|
+
}
|
35
|
+
main();
|
File without changes
|
@@ -0,0 +1,32 @@
|
|
1
|
+
import path from "node:path";
|
2
|
+
import { ROUTE_SPEC_FILE, DEFAULT_SERVER_CONFIG, fs as fse } from "@modern-js/utils";
|
3
|
+
import { genPluginImportsCode } from "../utils";
|
4
|
+
async function genVercelEntry({ config, plugins, appContext } = {}) {
|
5
|
+
const defaultConfig = {
|
6
|
+
output: {
|
7
|
+
path: "."
|
8
|
+
}
|
9
|
+
};
|
10
|
+
console.log("appContext.sharedDirectory", appContext === null || appContext === void 0 ? void 0 : appContext.sharedDirectory);
|
11
|
+
const pluginImportCode = genPluginImportsCode(plugins || []);
|
12
|
+
const dynamicProdOptions = {
|
13
|
+
config: config || defaultConfig,
|
14
|
+
serverConfigFile: DEFAULT_SERVER_CONFIG,
|
15
|
+
plugins,
|
16
|
+
appContext: appContext ? {
|
17
|
+
sharedDirectory: appContext.sharedDirectory,
|
18
|
+
apiDirectory: appContext.apiDirectory,
|
19
|
+
lambdaDirectory: appContext.lambdaDirectory
|
20
|
+
} : "undefined"
|
21
|
+
};
|
22
|
+
let entryCode = (await fse.readFile(path.join(__dirname, "./vercelEntry.js"))).toString();
|
23
|
+
entryCode = entryCode.replace("p_genPluginImportsCode", pluginImportCode).replace("p_ROUTE_SPEC_FILE", `"${ROUTE_SPEC_FILE}"`).replace(
|
24
|
+
"p_dynamicProdOptions",
|
25
|
+
// JSON.stringify(dynamicProdOptions),
|
26
|
+
JSON.stringify(dynamicProdOptions)
|
27
|
+
);
|
28
|
+
return entryCode;
|
29
|
+
}
|
30
|
+
export {
|
31
|
+
genVercelEntry
|
32
|
+
};
|
@@ -0,0 +1,63 @@
|
|
1
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
2
|
+
var __commonJS = (cb, mod) => function __require() {
|
3
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
4
|
+
};
|
5
|
+
var require_vercelEntry = __commonJS({
|
6
|
+
"src/plugins/deploy/platforms/vercelEntry.js"(exports, module) {
|
7
|
+
const fs = require("node:fs/promises");
|
8
|
+
const path = require("node:path");
|
9
|
+
const { createProdServer } = require("@modern-js/prod-server");
|
10
|
+
p_genPluginImportsCode;
|
11
|
+
if (!process.env.NODE_ENV) {
|
12
|
+
process.env.NODE_ENV = "production";
|
13
|
+
}
|
14
|
+
let requestHandler = null;
|
15
|
+
let handlerCreationPromise = null;
|
16
|
+
async function loadRoutes(routeFilepath) {
|
17
|
+
try {
|
18
|
+
await fs.access(routeFilepath);
|
19
|
+
const content = await fs.readFile(routeFilepath, "utf-8");
|
20
|
+
const routeSpec = JSON.parse(content);
|
21
|
+
return routeSpec.routes || [];
|
22
|
+
} catch (error) {
|
23
|
+
console.warn("route.json not found or invalid, continuing with empty routes.");
|
24
|
+
return [];
|
25
|
+
}
|
26
|
+
}
|
27
|
+
async function initServer() {
|
28
|
+
const routeFilepath = path.join(__dirname, p_ROUTE_SPEC_FILE);
|
29
|
+
const routes = await loadRoutes(routeFilepath);
|
30
|
+
const dynamicProdOptions = p_dynamicProdOptions;
|
31
|
+
const prodServerOptions = {
|
32
|
+
pwd: __dirname,
|
33
|
+
routes,
|
34
|
+
disableCustomHook: true,
|
35
|
+
...dynamicProdOptions
|
36
|
+
};
|
37
|
+
const app = await createProdServer(prodServerOptions);
|
38
|
+
return app.getRequestListener();
|
39
|
+
}
|
40
|
+
async function createHandler() {
|
41
|
+
if (!handlerCreationPromise) {
|
42
|
+
handlerCreationPromise = (async () => {
|
43
|
+
try {
|
44
|
+
requestHandler = await initServer();
|
45
|
+
} catch (error) {
|
46
|
+
console.error("Error creating server:", error);
|
47
|
+
process.exit(1);
|
48
|
+
}
|
49
|
+
})();
|
50
|
+
}
|
51
|
+
await handlerCreationPromise;
|
52
|
+
return requestHandler;
|
53
|
+
}
|
54
|
+
createHandler();
|
55
|
+
module.exports = async (req, res) => {
|
56
|
+
if (!requestHandler) {
|
57
|
+
await createHandler();
|
58
|
+
}
|
59
|
+
return requestHandler(req, res);
|
60
|
+
};
|
61
|
+
}
|
62
|
+
});
|
63
|
+
export default require_vercelEntry();
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import path from "path";
|
2
|
-
import { fs, ROUTE_SPEC_FILE } from "@modern-js/utils";
|
2
|
+
import { fs, MAIN_ENTRY_NAME, ROUTE_SPEC_FILE } from "@modern-js/utils";
|
3
3
|
const generateRoutes = async (appContext) => {
|
4
4
|
const { serverRoutes, distDirectory } = appContext;
|
5
5
|
const output = JSON.stringify({
|
@@ -11,7 +11,11 @@ const getPathWithoutExt = (filename) => {
|
|
11
11
|
const extname = path.extname(filename);
|
12
12
|
return filename.slice(0, -extname.length);
|
13
13
|
};
|
14
|
+
const isMainEntry = (entryName, mainEntryName) => {
|
15
|
+
return entryName === (mainEntryName || MAIN_ENTRY_NAME);
|
16
|
+
};
|
14
17
|
export {
|
15
18
|
generateRoutes,
|
16
|
-
getPathWithoutExt
|
19
|
+
getPathWithoutExt,
|
20
|
+
isMainEntry
|
17
21
|
};
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -1,6 +1,5 @@
|
|
1
|
-
import { ServerAppContext } from '../utils';
|
2
1
|
export declare function genVercelEntry({ config, plugins, appContext, }?: {
|
3
2
|
config?: Record<string, any>;
|
4
3
|
plugins?: string[];
|
5
|
-
appContext?:
|
6
|
-
}): string
|
4
|
+
appContext?: Record<string, any>;
|
5
|
+
}): Promise<string>;
|
@@ -1,4 +1,4 @@
|
|
1
1
|
import type { IAppContext } from '@modern-js/core';
|
2
|
-
declare const generateRoutes: (appContext: IAppContext) => Promise<void>;
|
3
|
-
declare const getPathWithoutExt: (filename: string) => string;
|
4
|
-
export
|
2
|
+
export declare const generateRoutes: (appContext: IAppContext) => Promise<void>;
|
3
|
+
export declare const getPathWithoutExt: (filename: string) => string;
|
4
|
+
export declare const isMainEntry: (entryName: string, mainEntryName?: string) => boolean;
|
package/package.json
CHANGED
@@ -15,7 +15,7 @@
|
|
15
15
|
"modern",
|
16
16
|
"modern.js"
|
17
17
|
],
|
18
|
-
"version": "2.49.3-alpha.
|
18
|
+
"version": "2.49.3-alpha.11",
|
19
19
|
"jsnext:source": "./src/index.ts",
|
20
20
|
"types": "./dist/types/index.d.ts",
|
21
21
|
"main": "./dist/cjs/index.js",
|
@@ -80,20 +80,20 @@
|
|
80
80
|
"mlly": "^1.6.1",
|
81
81
|
"pkg-types": "^1.1.0",
|
82
82
|
"std-env": "^3.7.0",
|
83
|
+
"@modern-js/core": "2.49.2",
|
84
|
+
"@modern-js/plugin": "2.49.2",
|
83
85
|
"@modern-js/plugin-data-loader": "2.49.2",
|
86
|
+
"@modern-js/plugin-i18n": "2.49.2",
|
87
|
+
"@modern-js/rsbuild-plugin-esbuild": "2.49.2",
|
84
88
|
"@modern-js/prod-server": "2.49.2",
|
85
89
|
"@modern-js/plugin-lint": "2.49.2",
|
86
|
-
"@modern-js/plugin": "2.49.2",
|
87
|
-
"@modern-js/rsbuild-plugin-esbuild": "2.49.2",
|
88
90
|
"@modern-js/server-core": "2.49.2",
|
89
|
-
"@modern-js/
|
90
|
-
"@modern-js/
|
91
|
+
"@modern-js/node-bundle-require": "2.49.2",
|
92
|
+
"@modern-js/server-utils": "2.49.2",
|
91
93
|
"@modern-js/types": "2.49.2",
|
92
94
|
"@modern-js/uni-builder": "2.49.2",
|
93
95
|
"@modern-js/utils": "2.49.2",
|
94
|
-
"@modern-js/server": "2.49.2"
|
95
|
-
"@modern-js/node-bundle-require": "2.49.2",
|
96
|
-
"@modern-js/server-utils": "2.49.2"
|
96
|
+
"@modern-js/server": "2.49.2"
|
97
97
|
},
|
98
98
|
"devDependencies": {
|
99
99
|
"@rsbuild/plugin-swc": "0.6.10",
|