@modern-js/app-tools 2.49.3-alpha.14 → 2.49.3-alpha.16
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/cjs/plugins/deploy/dependencies.js +1 -11
- package/dist/cjs/plugins/deploy/index copy.js +216 -0
- package/dist/cjs/plugins/deploy/index.js +17 -168
- package/dist/cjs/plugins/deploy/platforms/netlify.js +112 -64
- package/dist/cjs/plugins/deploy/platforms/netlifyEntry.js +60 -0
- package/dist/cjs/plugins/deploy/platforms/node.js +43 -21
- package/dist/cjs/plugins/deploy/platforms/vercel.js +102 -19
- package/dist/cjs/plugins/deploy/utils.js +22 -8
- package/dist/esm/plugins/deploy/dependencies.js +1 -46
- package/dist/esm/plugins/deploy/index copy.js +367 -0
- package/dist/esm/plugins/deploy/index.js +21 -313
- package/dist/esm/plugins/deploy/platforms/netlify.js +203 -36
- package/dist/esm/plugins/deploy/platforms/netlifyEntry.js +202 -0
- package/dist/esm/plugins/deploy/platforms/node.js +105 -46
- package/dist/esm/plugins/deploy/platforms/vercel.js +203 -43
- package/dist/esm/plugins/deploy/utils.js +55 -3
- package/dist/esm-node/plugins/deploy/dependencies.js +1 -11
- package/dist/esm-node/plugins/deploy/index copy.js +186 -0
- package/dist/esm-node/plugins/deploy/index.js +15 -156
- package/dist/esm-node/plugins/deploy/platforms/netlify.js +103 -65
- package/dist/esm-node/plugins/deploy/platforms/netlifyEntry.js +68 -0
- package/dist/esm-node/plugins/deploy/platforms/node.js +44 -22
- package/dist/esm-node/plugins/deploy/platforms/vercel.js +103 -20
- package/dist/esm-node/plugins/deploy/utils.js +20 -7
- package/dist/types/plugins/deploy/index copy.d.ts +4 -0
- package/dist/types/plugins/deploy/platforms/netlify.d.ts +2 -5
- package/dist/types/plugins/deploy/platforms/netlifyEntry.d.ts +2 -0
- package/dist/types/plugins/deploy/platforms/node.d.ts +2 -8
- package/dist/types/plugins/deploy/platforms/platform.d.ts +8 -4
- package/dist/types/plugins/deploy/platforms/vercel.d.ts +2 -8
- package/dist/types/plugins/deploy/utils.d.ts +8 -1
- package/package.json +8 -8
@@ -4,8 +4,14 @@ import path from "path";
|
|
4
4
|
import os from "node:os";
|
5
5
|
import { ROUTE_SPEC_FILE, fs as fse, isDepExists } from "@modern-js/utils";
|
6
6
|
import { parseNodeModulePath } from "mlly";
|
7
|
-
var
|
8
|
-
|
7
|
+
var serverAppContenxtTemplate = function(appContext) {
|
8
|
+
var appDirectory = appContext.appDirectory, sharedDirectory = appContext.sharedDirectory, apiDirectory = appContext.apiDirectory, lambdaDirectory = appContext.lambdaDirectory, metaName = appContext.metaName;
|
9
|
+
return {
|
10
|
+
sharedDirectory: 'path.join(__dirname, "'.concat(path.relative(appDirectory, sharedDirectory), '")'),
|
11
|
+
apiDirectory: 'path.join(__dirname, "'.concat(path.relative(appDirectory, apiDirectory), '")'),
|
12
|
+
lambdaDirectory: 'path.join(__dirname, "'.concat(path.relative(appDirectory, lambdaDirectory), '")'),
|
13
|
+
metaName
|
14
|
+
};
|
9
15
|
};
|
10
16
|
var getPluginsCode = function(plugins) {
|
11
17
|
return "[".concat(plugins.map(function(_, index) {
|
@@ -232,6 +238,51 @@ var linkPackage = function() {
|
|
232
238
|
return _ref.apply(this, arguments);
|
233
239
|
};
|
234
240
|
}();
|
241
|
+
var readDirRecursive = function() {
|
242
|
+
var _ref = _async_to_generator(function(dir) {
|
243
|
+
var files, filesAndDirs;
|
244
|
+
return _ts_generator(this, function(_state) {
|
245
|
+
switch (_state.label) {
|
246
|
+
case 0:
|
247
|
+
return [
|
248
|
+
4,
|
249
|
+
fse.readdir(dir, {
|
250
|
+
withFileTypes: true
|
251
|
+
})
|
252
|
+
];
|
253
|
+
case 1:
|
254
|
+
files = _state.sent();
|
255
|
+
return [
|
256
|
+
4,
|
257
|
+
Promise.all(files.map(function() {
|
258
|
+
var _ref2 = _async_to_generator(function(file) {
|
259
|
+
var resPath;
|
260
|
+
return _ts_generator(this, function(_state2) {
|
261
|
+
resPath = path.resolve(dir, file.name);
|
262
|
+
return [
|
263
|
+
2,
|
264
|
+
file.isDirectory() ? readDirRecursive(resPath) : resPath
|
265
|
+
];
|
266
|
+
});
|
267
|
+
});
|
268
|
+
return function(file) {
|
269
|
+
return _ref2.apply(this, arguments);
|
270
|
+
};
|
271
|
+
}()))
|
272
|
+
];
|
273
|
+
case 2:
|
274
|
+
filesAndDirs = _state.sent();
|
275
|
+
return [
|
276
|
+
2,
|
277
|
+
filesAndDirs.flat()
|
278
|
+
];
|
279
|
+
}
|
280
|
+
});
|
281
|
+
});
|
282
|
+
return function readDirRecursive2(dir) {
|
283
|
+
return _ref.apply(this, arguments);
|
284
|
+
};
|
285
|
+
}();
|
235
286
|
export {
|
236
287
|
applyProductionCondition,
|
237
288
|
applyPublicCondition,
|
@@ -239,6 +290,7 @@ export {
|
|
239
290
|
getPluginsCode,
|
240
291
|
getProjectUsage,
|
241
292
|
linkPackage,
|
242
|
-
|
293
|
+
readDirRecursive,
|
294
|
+
serverAppContenxtTemplate,
|
243
295
|
writePackage
|
244
296
|
};
|
@@ -3,17 +3,7 @@ import { nodeFileTrace, resolve } from "@vercel/nft";
|
|
3
3
|
import { fs as fse, pkgUp, semver } from "@modern-js/utils";
|
4
4
|
import { readPackageJSON } from "pkg-types";
|
5
5
|
import { parseNodeModulePath } from "mlly";
|
6
|
-
import { linkPackage, writePackage } from "./utils";
|
7
|
-
const readDirRecursive = async (dir) => {
|
8
|
-
const files = await fse.readdir(dir, {
|
9
|
-
withFileTypes: true
|
10
|
-
});
|
11
|
-
const filesAndDirs = await Promise.all(files.map(async (file) => {
|
12
|
-
const resPath = path.resolve(dir, file.name);
|
13
|
-
return file.isDirectory() ? readDirRecursive(resPath) : resPath;
|
14
|
-
}));
|
15
|
-
return filesAndDirs.flat();
|
16
|
-
};
|
6
|
+
import { linkPackage, readDirRecursive, writePackage } from "./utils";
|
17
7
|
async function isFile(file) {
|
18
8
|
try {
|
19
9
|
const stat = await fse.stat(file);
|
@@ -0,0 +1,186 @@
|
|
1
|
+
import path from "path";
|
2
|
+
import { fs as fse, getInternalPlugins } from "@modern-js/utils";
|
3
|
+
import { provider } from "std-env";
|
4
|
+
import { isMainEntry } from "../../utils/routes";
|
5
|
+
import { getProjectUsage } from "./utils";
|
6
|
+
import { handleDependencies } from "./dependencies";
|
7
|
+
var index_copy_default = () => ({
|
8
|
+
name: "@modern-js/plugin-deploy",
|
9
|
+
pre: [
|
10
|
+
"@modern-js/plugin-bff",
|
11
|
+
"@modern-js/plugin-server"
|
12
|
+
],
|
13
|
+
setup: (api) => {
|
14
|
+
const deployTarget = process.env.MODERNJS_DEPLOY || provider || "node";
|
15
|
+
return {
|
16
|
+
async beforeDeploy() {
|
17
|
+
const appContext = api.useAppContext();
|
18
|
+
const modernConfig = api.useResolvedConfigContext();
|
19
|
+
const { source: { mainEntryName } } = modernConfig;
|
20
|
+
const { appDirectory, distDirectory, serverInternalPlugins, sharedDirectory, apiDirectory, lambdaDirectory, metaName, entrypoints } = appContext;
|
21
|
+
const { useSSR, useAPI, useWebServer } = getProjectUsage(appDirectory, distDirectory);
|
22
|
+
const needModernServer = useSSR || useAPI || useWebServer;
|
23
|
+
let outputDirectory = path.join(appDirectory, ".output");
|
24
|
+
let funcsDirectory = outputDirectory;
|
25
|
+
let staticDirectory = path.join(outputDirectory, "static");
|
26
|
+
if (deployTarget === "node") {
|
27
|
+
await fse.remove(outputDirectory);
|
28
|
+
await fse.copy(distDirectory, outputDirectory);
|
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 supported, 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
|
+
}
|
67
|
+
if (deployTarget === "vercel") {
|
68
|
+
const vercelOutput = path.join(appDirectory, ".vercel");
|
69
|
+
await fse.remove(vercelOutput);
|
70
|
+
outputDirectory = path.join(vercelOutput, "output");
|
71
|
+
const config = {
|
72
|
+
version: 3,
|
73
|
+
routes: [
|
74
|
+
{
|
75
|
+
src: "/static/(.*)",
|
76
|
+
headers: {
|
77
|
+
"cache-control": "s-maxage=31536000, immutable"
|
78
|
+
},
|
79
|
+
continue: true
|
80
|
+
},
|
81
|
+
{
|
82
|
+
handle: "filesystem"
|
83
|
+
}
|
84
|
+
]
|
85
|
+
};
|
86
|
+
if (!needModernServer) {
|
87
|
+
entrypoints.forEach((entry) => {
|
88
|
+
const isMain = isMainEntry(entry.entryName, mainEntryName);
|
89
|
+
config.routes.push({
|
90
|
+
src: `/${isMain ? "" : entry.entryName}(?:/.*)?`,
|
91
|
+
headers: {
|
92
|
+
"cache-control": "s-maxage=0"
|
93
|
+
},
|
94
|
+
dest: `/html/${entry.entryName}/index.html`
|
95
|
+
});
|
96
|
+
});
|
97
|
+
} else {
|
98
|
+
config.routes.push({
|
99
|
+
src: "/(.*)",
|
100
|
+
dest: `/index`
|
101
|
+
});
|
102
|
+
}
|
103
|
+
await fse.ensureDir(outputDirectory);
|
104
|
+
await fse.writeJSON(path.join(outputDirectory, "config.json"), config, {
|
105
|
+
spaces: 2
|
106
|
+
});
|
107
|
+
staticDirectory = path.join(outputDirectory, "static/static");
|
108
|
+
await fse.copy(path.join(distDirectory, "static"), staticDirectory);
|
109
|
+
if (!needModernServer) {
|
110
|
+
const destHtmlDirectory = path.join(distDirectory, "html");
|
111
|
+
const outputHtmlDirectory = path.join(path.join(outputDirectory, "static"), "html");
|
112
|
+
await fse.copy(destHtmlDirectory, outputHtmlDirectory);
|
113
|
+
} else {
|
114
|
+
funcsDirectory = path.join(outputDirectory, "functions", "index.func");
|
115
|
+
await fse.ensureDir(funcsDirectory);
|
116
|
+
await fse.copy(distDirectory, funcsDirectory, {
|
117
|
+
filter: (src) => {
|
118
|
+
const distStaticDirectory = path.join(distDirectory, "static");
|
119
|
+
return !src.includes(distStaticDirectory);
|
120
|
+
}
|
121
|
+
});
|
122
|
+
await fse.writeJSON(path.join(funcsDirectory, ".vc-config.json"), {
|
123
|
+
runtime: "nodejs16.x",
|
124
|
+
handler: "index.js",
|
125
|
+
launcherType: "Nodejs",
|
126
|
+
shouldAddHelpers: false,
|
127
|
+
supportsResponseStreaming: true
|
128
|
+
});
|
129
|
+
}
|
130
|
+
}
|
131
|
+
const plugins = getInternalPlugins(appDirectory, serverInternalPlugins);
|
132
|
+
const serverAppContext = {
|
133
|
+
sharedDirectory: `path.join(__dirname, "${path.relative(appDirectory, sharedDirectory)}")`,
|
134
|
+
apiDirectory: `path.join(__dirname, "${path.relative(appDirectory, apiDirectory)}")`,
|
135
|
+
lambdaDirectory: `path.join(__dirname, "${path.relative(appDirectory, lambdaDirectory)}")`,
|
136
|
+
metaName
|
137
|
+
};
|
138
|
+
console.log("serverAppContext", serverAppContext);
|
139
|
+
let code = ``;
|
140
|
+
console.log("deployTarget111111111", deployTarget);
|
141
|
+
switch (deployTarget) {
|
142
|
+
case "node": {
|
143
|
+
const { genNodeEntry } = await import("./platforms/node");
|
144
|
+
code = await genNodeEntry({
|
145
|
+
plugins,
|
146
|
+
config: modernConfig,
|
147
|
+
appContext: serverAppContext
|
148
|
+
});
|
149
|
+
break;
|
150
|
+
}
|
151
|
+
case "vercel": {
|
152
|
+
const { genVercelEntry } = await import("./platforms/vercel");
|
153
|
+
code = await genVercelEntry({
|
154
|
+
plugins,
|
155
|
+
config: modernConfig,
|
156
|
+
appContext: serverAppContext
|
157
|
+
});
|
158
|
+
break;
|
159
|
+
}
|
160
|
+
case "netlify": {
|
161
|
+
const { genNetlifyEntry } = await import("./platforms/netlify");
|
162
|
+
code = await genNetlifyEntry({
|
163
|
+
plugins,
|
164
|
+
config: modernConfig,
|
165
|
+
appContext: serverAppContext
|
166
|
+
});
|
167
|
+
break;
|
168
|
+
}
|
169
|
+
default: {
|
170
|
+
code = `throw new Error("unknown deploy target, MODERNJS_DEPLOY should be set");`;
|
171
|
+
}
|
172
|
+
}
|
173
|
+
const entryFilePath = path.join(funcsDirectory, "index.js");
|
174
|
+
if (needModernServer) {
|
175
|
+
await fse.writeFile(entryFilePath, code);
|
176
|
+
await handleDependencies(appDirectory, funcsDirectory, [
|
177
|
+
"@modern-js/prod-server"
|
178
|
+
]);
|
179
|
+
}
|
180
|
+
}
|
181
|
+
};
|
182
|
+
}
|
183
|
+
});
|
184
|
+
export {
|
185
|
+
index_copy_default as default
|
186
|
+
};
|
@@ -1,9 +1,8 @@
|
|
1
|
-
import path from "path";
|
2
|
-
import { fs as fse, getInternalPlugins } from "@modern-js/utils";
|
3
1
|
import { provider } from "std-env";
|
4
|
-
import { isMainEntry } from "../../utils/routes";
|
5
2
|
import { getProjectUsage } from "./utils";
|
6
|
-
import {
|
3
|
+
import { createNodePreset } from "./platforms/node";
|
4
|
+
import { createVercelPreset } from "./platforms/vercel";
|
5
|
+
import { createNetlifyPreset } from "./platforms/netlify";
|
7
6
|
var deploy_default = () => ({
|
8
7
|
name: "@modern-js/plugin-deploy",
|
9
8
|
pre: [
|
@@ -15,168 +14,28 @@ var deploy_default = () => ({
|
|
15
14
|
return {
|
16
15
|
async beforeDeploy() {
|
17
16
|
const appContext = api.useAppContext();
|
17
|
+
const { appDirectory, distDirectory } = appContext;
|
18
18
|
const modernConfig = api.useResolvedConfigContext();
|
19
|
-
const { source: { mainEntryName } } = modernConfig;
|
20
|
-
const { appDirectory, distDirectory, serverInternalPlugins, sharedDirectory, apiDirectory, lambdaDirectory, metaName, entrypoints } = appContext;
|
21
19
|
const { useSSR, useAPI, useWebServer } = getProjectUsage(appDirectory, distDirectory);
|
22
20
|
const needModernServer = useSSR || useAPI || useWebServer;
|
23
|
-
let
|
24
|
-
let funcsDirectory = outputDirectory;
|
25
|
-
let staticDirectory = path.join(outputDirectory, "static");
|
26
|
-
if (deployTarget === "node") {
|
27
|
-
await fse.remove(outputDirectory);
|
28
|
-
await fse.copy(distDirectory, outputDirectory);
|
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 supported, 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
|
-
}
|
67
|
-
if (deployTarget === "vercel") {
|
68
|
-
const vercelOutput = path.join(appDirectory, ".vercel");
|
69
|
-
await fse.remove(vercelOutput);
|
70
|
-
outputDirectory = path.join(vercelOutput, "output");
|
71
|
-
const config = {
|
72
|
-
version: 3,
|
73
|
-
routes: [
|
74
|
-
{
|
75
|
-
src: "/static/(.*)",
|
76
|
-
headers: {
|
77
|
-
"cache-control": "s-maxage=31536000, immutable"
|
78
|
-
},
|
79
|
-
continue: true
|
80
|
-
},
|
81
|
-
{
|
82
|
-
handle: "filesystem"
|
83
|
-
}
|
84
|
-
]
|
85
|
-
};
|
86
|
-
if (!needModernServer) {
|
87
|
-
entrypoints.forEach((entry) => {
|
88
|
-
const isMain = isMainEntry(entry.entryName, mainEntryName);
|
89
|
-
config.routes.push({
|
90
|
-
src: `/${isMain ? "" : entry.entryName}(?:/.*)?`,
|
91
|
-
headers: {
|
92
|
-
"cache-control": "s-maxage=0"
|
93
|
-
},
|
94
|
-
dest: `/html/${entry.entryName}/index.html`
|
95
|
-
});
|
96
|
-
});
|
97
|
-
} else {
|
98
|
-
config.routes.push({
|
99
|
-
src: "/(.*)",
|
100
|
-
dest: `/index`
|
101
|
-
});
|
102
|
-
}
|
103
|
-
await fse.ensureDir(outputDirectory);
|
104
|
-
await fse.writeJSON(path.join(outputDirectory, "config.json"), config, {
|
105
|
-
spaces: 2
|
106
|
-
});
|
107
|
-
staticDirectory = path.join(outputDirectory, "static/static");
|
108
|
-
await fse.copy(path.join(distDirectory, "static"), staticDirectory);
|
109
|
-
if (!needModernServer) {
|
110
|
-
const destHtmlDirectory = path.join(distDirectory, "html");
|
111
|
-
const outputHtmlDirectory = path.join(path.join(outputDirectory, "static"), "html");
|
112
|
-
await fse.copy(destHtmlDirectory, outputHtmlDirectory);
|
113
|
-
} else {
|
114
|
-
funcsDirectory = path.join(outputDirectory, "functions", "index.func");
|
115
|
-
await fse.ensureDir(funcsDirectory);
|
116
|
-
await fse.copy(distDirectory, funcsDirectory, {
|
117
|
-
filter: (src) => {
|
118
|
-
const distStaticDirectory = path.join(distDirectory, "static");
|
119
|
-
return !src.includes(distStaticDirectory);
|
120
|
-
}
|
121
|
-
});
|
122
|
-
await fse.writeJSON(path.join(funcsDirectory, ".vc-config.json"), {
|
123
|
-
runtime: "nodejs16.x",
|
124
|
-
handler: "index.js",
|
125
|
-
launcherType: "Nodejs",
|
126
|
-
shouldAddHelpers: false,
|
127
|
-
supportsResponseStreaming: true
|
128
|
-
});
|
129
|
-
}
|
130
|
-
}
|
131
|
-
const plugins = getInternalPlugins(appDirectory, serverInternalPlugins);
|
132
|
-
const serverAppContext = {
|
133
|
-
sharedDirectory: `path.join(__dirname, "${path.relative(appDirectory, sharedDirectory)}")`,
|
134
|
-
apiDirectory: `path.join(__dirname, "${path.relative(appDirectory, apiDirectory)}")`,
|
135
|
-
lambdaDirectory: `path.join(__dirname, "${path.relative(appDirectory, lambdaDirectory)}")`,
|
136
|
-
metaName
|
137
|
-
};
|
138
|
-
console.log("serverAppContext", serverAppContext);
|
139
|
-
let code = ``;
|
140
|
-
console.log("deployTarget111111111", deployTarget);
|
21
|
+
let deployPreset;
|
141
22
|
switch (deployTarget) {
|
142
|
-
case "node":
|
143
|
-
|
144
|
-
code = await genNodeEntry({
|
145
|
-
plugins,
|
146
|
-
config: modernConfig,
|
147
|
-
appContext: serverAppContext
|
148
|
-
});
|
23
|
+
case "node":
|
24
|
+
deployPreset = createNodePreset(appContext, modernConfig, needModernServer);
|
149
25
|
break;
|
150
|
-
|
151
|
-
|
152
|
-
const { genVercelEntry } = await import("./platforms/vercel");
|
153
|
-
code = await genVercelEntry({
|
154
|
-
plugins,
|
155
|
-
config: modernConfig,
|
156
|
-
appContext: serverAppContext
|
157
|
-
});
|
26
|
+
case "vercel":
|
27
|
+
deployPreset = createVercelPreset(appContext, modernConfig, needModernServer);
|
158
28
|
break;
|
159
|
-
|
160
|
-
|
161
|
-
const { genNetlifyEntry } = await import("./platforms/netlify");
|
162
|
-
code = await genNetlifyEntry({
|
163
|
-
plugins,
|
164
|
-
config: modernConfig,
|
165
|
-
appContext: serverAppContext
|
166
|
-
});
|
29
|
+
case "netlify":
|
30
|
+
deployPreset = createNetlifyPreset(appContext, modernConfig, needModernServer);
|
167
31
|
break;
|
168
|
-
}
|
169
32
|
default: {
|
170
|
-
|
33
|
+
throw new Error("unknown deploy target, MODERNJS_DEPLOY should be set");
|
171
34
|
}
|
172
35
|
}
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
await handleDependencies(appDirectory, funcsDirectory, [
|
177
|
-
"@modern-js/prod-server"
|
178
|
-
]);
|
179
|
-
}
|
36
|
+
await (deployPreset === null || deployPreset === void 0 ? void 0 : deployPreset.prepare());
|
37
|
+
await (deployPreset === null || deployPreset === void 0 ? void 0 : deployPreset.writeOutput());
|
38
|
+
await (deployPreset === null || deployPreset === void 0 ? void 0 : deployPreset.genEntry());
|
180
39
|
}
|
181
40
|
};
|
182
41
|
}
|
@@ -1,71 +1,109 @@
|
|
1
|
-
import
|
2
|
-
import {
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
import path from "node:path";
|
2
|
+
import { ROUTE_SPEC_FILE, DEFAULT_SERVER_CONFIG, fs as fse, getInternalPlugins } from "@modern-js/utils";
|
3
|
+
import { isMainEntry } from "../../../utils/routes";
|
4
|
+
import { genPluginImportsCode, serverAppContenxtTemplate } from "../utils";
|
5
|
+
import { handleDependencies } from "../dependencies";
|
6
|
+
const createNetlifyPreset = (appContext, config, needModernServer) => {
|
7
|
+
const { appDirectory, distDirectory, serverInternalPlugins, sharedDirectory, apiDirectory, lambdaDirectory, metaName, entrypoints } = appContext;
|
8
|
+
const plugins = getInternalPlugins(appDirectory, serverInternalPlugins);
|
9
|
+
const netlifyOutput = path.join(appDirectory, ".netlify");
|
10
|
+
const outputDirectory = path.join(netlifyOutput, "output");
|
11
|
+
const funcsDirectory = path.join(outputDirectory, "functions", "index.func");
|
12
|
+
const entryFilePath = path.join(funcsDirectory, "index.js");
|
13
|
+
return {
|
14
|
+
async prepare() {
|
15
|
+
await fse.remove(netlifyOutput);
|
7
16
|
},
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
17
|
+
async writeOutput() {
|
18
|
+
const config2 = {
|
19
|
+
version: 3,
|
20
|
+
routes: [
|
21
|
+
{
|
22
|
+
src: "/static/(.*)",
|
23
|
+
headers: {
|
24
|
+
"cache-control": "s-maxage=31536000, immutable"
|
25
|
+
},
|
26
|
+
continue: true
|
27
|
+
},
|
28
|
+
{
|
29
|
+
handle: "filesystem"
|
30
|
+
}
|
31
|
+
]
|
32
|
+
};
|
33
|
+
if (!needModernServer) {
|
34
|
+
const { source: { mainEntryName } } = config2;
|
35
|
+
entrypoints.forEach((entry) => {
|
36
|
+
const isMain = isMainEntry(entry.entryName, mainEntryName);
|
37
|
+
config2.routes.push({
|
38
|
+
src: `/${isMain ? "" : entry.entryName}(?:/.*)?`,
|
39
|
+
headers: {
|
40
|
+
"cache-control": "s-maxage=0"
|
41
|
+
},
|
42
|
+
dest: `/html/${entry.entryName}/index.html`
|
43
|
+
});
|
44
|
+
});
|
45
|
+
} else {
|
46
|
+
config2.routes.push({
|
47
|
+
src: "/(.*)",
|
48
|
+
dest: `/index`
|
49
|
+
});
|
36
50
|
}
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
51
|
+
await fse.ensureDir(outputDirectory);
|
52
|
+
await fse.writeJSON(path.join(outputDirectory, "config.json"), config2, {
|
53
|
+
spaces: 2
|
54
|
+
});
|
55
|
+
const staticDirectory = path.join(outputDirectory, "static/static");
|
56
|
+
await fse.copy(path.join(distDirectory, "static"), staticDirectory);
|
57
|
+
if (!needModernServer) {
|
58
|
+
const destHtmlDirectory = path.join(distDirectory, "html");
|
59
|
+
const outputHtmlDirectory = path.join(path.join(outputDirectory, "static"), "html");
|
60
|
+
await fse.copy(destHtmlDirectory, outputHtmlDirectory);
|
61
|
+
} else {
|
62
|
+
await fse.ensureDir(funcsDirectory);
|
63
|
+
await fse.copy(distDirectory, funcsDirectory, {
|
64
|
+
filter: (src) => {
|
65
|
+
const distStaticDirectory = path.join(distDirectory, "static");
|
66
|
+
return !src.includes(distStaticDirectory);
|
67
|
+
}
|
68
|
+
});
|
69
|
+
await fse.writeJSON(path.join(funcsDirectory, ".vc-config.json"), {
|
70
|
+
runtime: "nodejs16.x",
|
71
|
+
handler: "index.js",
|
72
|
+
launcherType: "Nodejs",
|
73
|
+
shouldAddHelpers: false,
|
74
|
+
supportsResponseStreaming: true
|
75
|
+
});
|
46
76
|
}
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
77
|
+
},
|
78
|
+
async genEntry() {
|
79
|
+
var _config_bff;
|
80
|
+
if (!needModernServer) {
|
81
|
+
return;
|
82
|
+
}
|
83
|
+
const serverConfig = {
|
84
|
+
bff: {
|
85
|
+
prefix: config === null || config === void 0 ? void 0 : (_config_bff = config.bff) === null || _config_bff === void 0 ? void 0 : _config_bff.prefix
|
86
|
+
},
|
87
|
+
output: {
|
88
|
+
path: "."
|
89
|
+
}
|
90
|
+
};
|
91
|
+
const pluginImportCode = genPluginImportsCode(plugins || []);
|
92
|
+
const dynamicProdOptions = {
|
93
|
+
config: serverConfig,
|
94
|
+
serverConfigFile: DEFAULT_SERVER_CONFIG,
|
95
|
+
plugins
|
96
|
+
};
|
97
|
+
let entryCode = (await fse.readFile(path.join(__dirname, "./netlifyEntry.js"))).toString();
|
98
|
+
const serverAppContext = serverAppContenxtTemplate(appContext);
|
99
|
+
entryCode = entryCode.replace("p_genPluginImportsCode", pluginImportCode).replace("p_ROUTE_SPEC_FILE", `"${ROUTE_SPEC_FILE}"`).replace("p_dynamicProdOptions", JSON.stringify(dynamicProdOptions)).replace("p_sharedDirectory", serverAppContext.sharedDirectory).replace("p_apiDirectory", serverAppContext.apiDirectory).replace("p_lambdaDirectory", serverAppContext.lambdaDirectory);
|
100
|
+
await fse.writeFile(entryFilePath, entryCode);
|
101
|
+
await handleDependencies(appDirectory, funcsDirectory, [
|
102
|
+
"@modern-js/prod-server"
|
103
|
+
]);
|
62
104
|
}
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
export default handleRequest;
|
67
|
-
`;
|
68
|
-
}
|
105
|
+
};
|
106
|
+
};
|
69
107
|
export {
|
70
|
-
|
108
|
+
createNetlifyPreset
|
71
109
|
};
|