@modern-js/app-tools 2.49.1 → 2.49.3-alpha.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. package/dist/cjs/builder/shared/builderPlugins/adapterSSR.js +1 -1
  2. package/dist/cjs/config/default.js +11 -1
  3. package/dist/cjs/index.js +4 -2
  4. package/dist/cjs/locale/en.js +2 -1
  5. package/dist/cjs/locale/zh.js +2 -1
  6. package/dist/cjs/plugins/deploy/dependencies.js +256 -0
  7. package/dist/cjs/plugins/deploy/entrys/netlify.js +95 -0
  8. package/dist/cjs/plugins/deploy/entrys/node.js +88 -0
  9. package/dist/cjs/plugins/deploy/entrys/vercel.js +94 -0
  10. package/dist/cjs/plugins/deploy/index.js +186 -0
  11. package/dist/cjs/plugins/deploy/utils.js +150 -0
  12. package/dist/cjs/utils/register.js +1 -1
  13. package/dist/esm/builder/shared/builderPlugins/adapterSSR.js +2 -2
  14. package/dist/esm/config/default.js +11 -1
  15. package/dist/esm/index.js +4 -2
  16. package/dist/esm/locale/en.js +2 -1
  17. package/dist/esm/locale/zh.js +2 -1
  18. package/dist/esm/plugins/deploy/dependencies.js +725 -0
  19. package/dist/esm/plugins/deploy/entrys/netlify.js +41 -0
  20. package/dist/esm/plugins/deploy/entrys/node.js +39 -0
  21. package/dist/esm/plugins/deploy/entrys/vercel.js +40 -0
  22. package/dist/esm/plugins/deploy/index.js +297 -0
  23. package/dist/esm/plugins/deploy/utils.js +244 -0
  24. package/dist/esm/utils/register.js +1 -1
  25. package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.js +2 -2
  26. package/dist/esm-node/config/default.js +11 -1
  27. package/dist/esm-node/index.js +4 -2
  28. package/dist/esm-node/locale/en.js +2 -1
  29. package/dist/esm-node/locale/zh.js +2 -1
  30. package/dist/esm-node/plugins/deploy/dependencies.js +222 -0
  31. package/dist/esm-node/plugins/deploy/entrys/netlify.js +71 -0
  32. package/dist/esm-node/plugins/deploy/entrys/node.js +64 -0
  33. package/dist/esm-node/plugins/deploy/entrys/vercel.js +70 -0
  34. package/dist/esm-node/plugins/deploy/index.js +156 -0
  35. package/dist/esm-node/plugins/deploy/utils.js +109 -0
  36. package/dist/esm-node/utils/register.js +1 -1
  37. package/dist/types/locale/en.d.ts +1 -0
  38. package/dist/types/locale/index.d.ts +2 -0
  39. package/dist/types/locale/zh.d.ts +1 -0
  40. package/dist/types/plugins/deploy/dependencies.d.ts +1 -0
  41. package/dist/types/plugins/deploy/entrys/netlify.d.ts +5 -0
  42. package/dist/types/plugins/deploy/entrys/node.d.ts +5 -0
  43. package/dist/types/plugins/deploy/entrys/vercel.d.ts +5 -0
  44. package/dist/types/plugins/deploy/index.d.ts +4 -0
  45. package/dist/types/plugins/deploy/utils.d.ts +27 -0
  46. package/dist/types/utils/types.d.ts +1 -0
  47. package/package.json +26 -22
@@ -70,7 +70,17 @@ function createDefaultConfig(appContext) {
70
70
  server,
71
71
  dev,
72
72
  html,
73
- tools: {},
73
+ tools: {
74
+ tsChecker: {
75
+ issue: {
76
+ exclude: [
77
+ {
78
+ file: "**/api/lambda/**/*"
79
+ }
80
+ ]
81
+ }
82
+ }
83
+ },
74
84
  plugins: [],
75
85
  builderPlugins: [],
76
86
  runtime: {},
@@ -8,6 +8,7 @@ import initializePlugin from "./initialize";
8
8
  import { hooks } from "./hooks";
9
9
  import { i18n, localeKeys } from "./locale";
10
10
  import serverBuildPlugin from "./plugins/serverBuild";
11
+ import deployPlugin from "./plugins/deploy";
11
12
  import { restart } from "./utils/restart";
12
13
  import { generateWatchFiles } from "./utils/generateWatchFiles";
13
14
  import { mergeConfig } from "@modern-js/core";
@@ -81,7 +82,8 @@ const appTools = (options = {
81
82
  bundler: (options === null || options === void 0 ? void 0 : options.bundler) === "experimental-rspack" ? "rspack" : "webpack"
82
83
  }),
83
84
  serverBuildPlugin(),
84
- lintPlugin()
85
+ lintPlugin(),
86
+ deployPlugin()
85
87
  ],
86
88
  setup: (api) => {
87
89
  const appContext = api.useAppContext();
@@ -113,7 +115,7 @@ const appTools = (options = {
113
115
  const { start } = await import("./commands/serve");
114
116
  await start(api);
115
117
  });
116
- program.command("deploy").usage("[options]").option("-c --config <config>", i18n.t(localeKeys.command.shared.config)).option("-s --skip-build", i18n.t(localeKeys.command.shared.skipBuild)).description(i18n.t(localeKeys.command.deploy.describe)).action(async (options2) => {
118
+ program.command("deploy").usage("[options]").option("-c --config <config>", i18n.t(localeKeys.command.shared.config)).option("-s --skip-build", i18n.t(localeKeys.command.shared.skipBuild)).option("-o --output <path>", i18n.t(localeKeys.command.deploy.output)).description(i18n.t(localeKeys.command.deploy.describe)).action(async (options2) => {
117
119
  if (!options2.skipBuild) {
118
120
  const { build } = await import("./commands/build");
119
121
  await build(api);
@@ -21,7 +21,8 @@ const EN_LOCALE = {
21
21
  describe: "preview the production build locally"
22
22
  },
23
23
  deploy: {
24
- describe: "deploy the application"
24
+ describe: "deploy the application",
25
+ output: "output path"
25
26
  },
26
27
  new: {
27
28
  describe: "enable optional features or add a new entry",
@@ -21,7 +21,8 @@ const ZH_LOCALE = {
21
21
  describe: "启动生产环境服务"
22
22
  },
23
23
  deploy: {
24
- describe: "部署应用"
24
+ describe: "部署应用",
25
+ output: "输出路径"
25
26
  },
26
27
  new: {
27
28
  describe: "Web App 项目中执行生成器",
@@ -0,0 +1,222 @@
1
+ import path, { isAbsolute } from "node:path";
2
+ import { nodeFileTrace, resolve } from "@vercel/nft";
3
+ import { fs as fse, pkgUp, semver } from "@modern-js/utils";
4
+ import { readPackageJSON } from "pkg-types";
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
+ };
17
+ async function isFile(file) {
18
+ try {
19
+ const stat = await fse.stat(file);
20
+ return stat.isFile();
21
+ } catch (error) {
22
+ if (error.code === "ENOENT") {
23
+ return false;
24
+ }
25
+ throw error;
26
+ }
27
+ }
28
+ const findEntryFiles = async (rootDir) => {
29
+ const files = await readDirRecursive(rootDir);
30
+ return files;
31
+ };
32
+ const handleDependencies = async (appDir, serverRootDir, include) => {
33
+ const base = "/";
34
+ const entryFiles = await findEntryFiles(serverRootDir);
35
+ const includeEntries = include.map((item) => {
36
+ if (isAbsolute(item)) {
37
+ return item;
38
+ }
39
+ try {
40
+ return require.resolve(item);
41
+ } catch (error) {
42
+ }
43
+ return item;
44
+ });
45
+ const _fileTrace = await nodeFileTrace(entryFiles.concat(includeEntries), {
46
+ base,
47
+ processCwd: serverRootDir,
48
+ resolve: async (id, parent, job, isCjs) => {
49
+ if (id.startsWith("@modern-js/prod-server")) {
50
+ return require.resolve(id, {
51
+ paths: [
52
+ require.resolve("@modern-js/app-tools")
53
+ ]
54
+ });
55
+ } else {
56
+ return resolve(id, parent, job, isCjs);
57
+ }
58
+ }
59
+ });
60
+ const currentProjectModules = path.join(appDir, "node_modules");
61
+ const _resolveTracedPath = (p) => fse.realpath(path.resolve(base, p));
62
+ const tracedFiles = Object.fromEntries(await Promise.all([
63
+ ..._fileTrace.reasons.entries()
64
+ ].map(async ([_path, reasons]) => {
65
+ if (reasons.ignored) {
66
+ return;
67
+ }
68
+ const filePath = await _resolveTracedPath(_path);
69
+ if (filePath.startsWith(serverRootDir) || filePath.startsWith(appDir) && !filePath.startsWith(currentProjectModules)) {
70
+ return;
71
+ }
72
+ if (!await isFile(filePath)) {
73
+ return;
74
+ }
75
+ let baseDir;
76
+ let pkgName;
77
+ let subpath;
78
+ let pkgPath;
79
+ if (filePath.includes("node_modules")) {
80
+ const parsed = parseNodeModulePath(filePath);
81
+ baseDir = parsed.dir;
82
+ pkgName = parsed.name;
83
+ subpath = parsed.subpath;
84
+ pkgPath = path.join(baseDir, pkgName);
85
+ } else {
86
+ const MODERN_UTILS_PATH = "packages/toolkit/utils";
87
+ const MODERN_UTILS_PATH_REGEX = new RegExp(`(.*${MODERN_UTILS_PATH})`);
88
+ const match = filePath.match(MODERN_UTILS_PATH_REGEX);
89
+ const packageJsonPath = match ? path.join(match[0], "package.json") : await pkgUp({
90
+ cwd: path.dirname(filePath)
91
+ });
92
+ if (packageJsonPath) {
93
+ const packageJson = await fse.readJSON(packageJsonPath);
94
+ pkgPath = baseDir = path.dirname(packageJsonPath);
95
+ subpath = path.relative(baseDir, filePath);
96
+ pkgName = packageJson.name;
97
+ }
98
+ }
99
+ if (!baseDir) {
100
+ return;
101
+ }
102
+ const parents = await Promise.all([
103
+ ...reasons.parents
104
+ ].map((p) => _resolveTracedPath(p)));
105
+ const tracedFile = {
106
+ path: filePath,
107
+ parents,
108
+ subpath,
109
+ pkgName,
110
+ pkgPath
111
+ };
112
+ return [
113
+ filePath,
114
+ tracedFile
115
+ ];
116
+ })).then((r) => r.filter(Boolean)));
117
+ const tracedPackages = {};
118
+ for (const tracedFile of Object.values(tracedFiles)) {
119
+ const { pkgName } = tracedFile;
120
+ let tracedPackage = tracedPackages[pkgName];
121
+ let pkgJSON = await readPackageJSON(tracedFile.pkgPath, {
122
+ cache: true
123
+ }).catch(() => {
124
+ });
125
+ if (!pkgJSON) {
126
+ pkgJSON = {
127
+ name: pkgName,
128
+ version: "0.0.0"
129
+ };
130
+ }
131
+ if (!tracedPackage) {
132
+ tracedPackage = {
133
+ name: pkgName,
134
+ versions: {}
135
+ };
136
+ tracedPackages[pkgName] = tracedPackage;
137
+ }
138
+ let tracedPackageVersion = tracedPackage.versions[pkgJSON.version];
139
+ if (!tracedPackageVersion) {
140
+ tracedPackageVersion = {
141
+ path: tracedFile.pkgPath,
142
+ files: [],
143
+ pkgJSON
144
+ };
145
+ tracedPackage.versions[pkgJSON.version] = tracedPackageVersion;
146
+ }
147
+ tracedFile.path.startsWith(tracedFile.pkgPath) && tracedPackageVersion.path === tracedFile.pkgPath && tracedPackageVersion.files.push(tracedFile.path);
148
+ tracedFile.pkgName = pkgName;
149
+ tracedFile.pkgVersion = pkgJSON.version;
150
+ }
151
+ const findPackageParents = (pkg, version) => {
152
+ const versionFiles = pkg.versions[version].files.map((path2) => tracedFiles[path2]);
153
+ const parentPkgs = [
154
+ ...new Set(versionFiles.flatMap((file) => file.parents.map((parentPath) => {
155
+ const parentFile = tracedFiles[parentPath];
156
+ if (parentFile.pkgName === pkg.name) {
157
+ return null;
158
+ }
159
+ return `${parentFile.pkgName}@${parentFile.pkgVersion}`;
160
+ }).filter(Boolean)))
161
+ ];
162
+ return parentPkgs;
163
+ };
164
+ const multiVersionPkgs = {};
165
+ const singleVersionPackages = [];
166
+ for (const tracedPackage of Object.values(tracedPackages)) {
167
+ const versions = Object.keys(tracedPackage.versions);
168
+ if (versions.length === 1) {
169
+ singleVersionPackages.push(tracedPackage.name);
170
+ continue;
171
+ }
172
+ multiVersionPkgs[tracedPackage.name] = {};
173
+ for (const version of versions) {
174
+ multiVersionPkgs[tracedPackage.name][version] = findPackageParents(tracedPackage, version);
175
+ }
176
+ }
177
+ await Promise.all(singleVersionPackages.map((pkgName) => {
178
+ const pkg = tracedPackages[pkgName];
179
+ const version = Object.keys(pkg.versions)[0];
180
+ return writePackage(pkg, version, serverRootDir);
181
+ }));
182
+ console.log("multiVersionPkgs111111111", multiVersionPkgs);
183
+ for (const [pkgName, pkgVersions] of Object.entries(multiVersionPkgs)) {
184
+ const versionEntires = Object.entries(pkgVersions).sort(([v1, p1], [v2, p2]) => {
185
+ if (p1.length === 0) {
186
+ return -1;
187
+ }
188
+ if (p2.length === 0) {
189
+ return 1;
190
+ }
191
+ return semver.lt(v1, v2, {
192
+ loose: true
193
+ }) ? 1 : -1;
194
+ });
195
+ for (const [version, parentPkgs] of versionEntires) {
196
+ const pkg = tracedPackages[pkgName];
197
+ const pkgDestPath = `.modernjs/${pkgName}@${version}/node_modules/${pkgName}`;
198
+ await writePackage(pkg, version, serverRootDir, pkgDestPath);
199
+ await linkPackage(pkgDestPath, `${pkgName}`, serverRootDir);
200
+ for (const parentPkg of parentPkgs) {
201
+ const parentPkgName = parentPkg.replace(/@[^@]+$/, "");
202
+ await (multiVersionPkgs[parentPkgName] ? linkPackage(pkgDestPath, `.modernjs/${parentPkg}/node_modules/${pkgName}`, serverRootDir) : linkPackage(pkgDestPath, `${parentPkgName}/node_modules/${pkgName}`, serverRootDir));
203
+ }
204
+ }
205
+ }
206
+ const projectPkg = await readPackageJSON(serverRootDir).catch(() => ({}));
207
+ const outputPkgPath = path.join(serverRootDir, "package.json");
208
+ await fse.writeJSON(outputPkgPath, {
209
+ name: `${projectPkg.name || "modernjs-project"}-prod`,
210
+ version: projectPkg.version || "0.0.0",
211
+ private: true,
212
+ dependencies: Object.fromEntries([
213
+ ...Object.values(tracedPackages).map((pkg) => [
214
+ pkg.name,
215
+ Object.keys(pkg.versions)[0]
216
+ ])
217
+ ].sort(([a], [b]) => a.localeCompare(b)))
218
+ });
219
+ };
220
+ export {
221
+ handleDependencies
222
+ };
@@ -0,0 +1,71 @@
1
+ import { ROUTE_SPEC_FILE, DEFAULT_SERVER_CONFIG } from "@modern-js/utils";
2
+ import { genPluginImportsCode, getPluginsCode, severAppContextTemplate } from "../utils";
3
+ function genNetlifyEntry({ config, plugins, appContext } = {}) {
4
+ const defaultConfig = {
5
+ server: {
6
+ port: 8080
7
+ },
8
+ output: {
9
+ path: "."
10
+ }
11
+ };
12
+ return `
13
+
14
+ const fs = require('node:fs/promises');
15
+ const path = require('node:path');
16
+ const { createNetlifyFunction } = require('@modern-js/prod-server/netlify');
17
+ ${genPluginImportsCode(plugins || [])}
18
+
19
+ let requestHandler = null;
20
+
21
+ if(!process.env.NODE_ENV){
22
+ process.env.NODE_ENV = 'production';
23
+ }
24
+
25
+ async function createHandler() {
26
+ try {
27
+ let routes = [];
28
+ const routeFilepath = path.join(__dirname, "${ROUTE_SPEC_FILE}");
29
+ try {
30
+ await fs.access(routeFilepath);
31
+ const content = await fs.readFile(routeFilepath, "utf-8");
32
+ const routeSpec = JSON.parse(content);
33
+ routes = routeSpec.routes;
34
+ } catch (error) {
35
+ console.warn('route.json not found, continuing with empty routes.');
36
+ }
37
+
38
+ const prodServerOptions = {
39
+ pwd: __dirname,
40
+ routes,
41
+ config: ${JSON.stringify(config || defaultConfig)},
42
+ serverConfigFile: '${DEFAULT_SERVER_CONFIG}',
43
+ plugins: ${getPluginsCode(plugins || [])},
44
+ appContext: ${appContext ? severAppContextTemplate(appContext) : "undefined"},
45
+ disableCustomHook: true
46
+ }
47
+
48
+ requestHandler = await createNetlifyFunction(prodServerOptions)
49
+
50
+ return requestHandler
51
+ } catch(error) {
52
+ console.error(error);
53
+ process.exit(1);
54
+ }
55
+ }
56
+
57
+ createHandler();
58
+
59
+ const handleRequest = async(request, context) => {
60
+ if(typeof requestHandler !== 'function'){
61
+ await createHandler();
62
+ }
63
+ return requestHandler(request, context);
64
+ }
65
+
66
+ export default handleRequest;
67
+ `;
68
+ }
69
+ export {
70
+ genNetlifyEntry
71
+ };
@@ -0,0 +1,64 @@
1
+ import { ROUTE_SPEC_FILE, DEFAULT_SERVER_CONFIG } from "@modern-js/utils";
2
+ import { genPluginImportsCode, getPluginsCode, severAppContextTemplate } from "../utils";
3
+ function genNodeEntry({ config, plugins, appContext } = {}) {
4
+ const defaultConfig = {
5
+ server: {
6
+ port: 8080
7
+ },
8
+ output: {
9
+ path: "."
10
+ }
11
+ };
12
+ return `
13
+
14
+ const fs = require('node:fs/promises');
15
+ const path = require('node:path');
16
+ const { createProdServer } = require('@modern-js/prod-server');
17
+ ${genPluginImportsCode(plugins || [])}
18
+
19
+ if(!process.env.NODE_ENV){
20
+ process.env.NODE_ENV = 'production';
21
+ }
22
+
23
+ async function main() {
24
+ try {
25
+ let routes = [];
26
+ const routeFilepath = path.join(__dirname, "${ROUTE_SPEC_FILE}");
27
+ try {
28
+ await fs.access(routeFilepath);
29
+ const content = await fs.readFile(routeFilepath, "utf-8");
30
+ const routeSpec = JSON.parse(content);
31
+ routes = routeSpec.routes;
32
+ } catch (error) {
33
+ console.warn('route.json not found, continuing with empty routes.');
34
+ }
35
+
36
+ const prodServerOptions = {
37
+ pwd: __dirname,
38
+ routes,
39
+ config: ${JSON.stringify(config || defaultConfig)},
40
+ serverConfigFile: '${DEFAULT_SERVER_CONFIG}',
41
+ plugins: ${getPluginsCode(plugins || [])},
42
+ appContext: ${appContext ? severAppContextTemplate(appContext) : "undefined"},
43
+ disableCustomHook: true
44
+ }
45
+
46
+ const app = await createProdServer(prodServerOptions)
47
+
48
+ const port = process.env.PORT || 3000;
49
+
50
+ app.listen(port, () => {
51
+ console.log('\\x1b[32mServer is listening on port', port, '\\x1b[0m');
52
+ });
53
+ } catch(error) {
54
+ console.error(error);
55
+ process.exit(1);
56
+ }
57
+ }
58
+
59
+ main();
60
+ `;
61
+ }
62
+ export {
63
+ genNodeEntry
64
+ };
@@ -0,0 +1,70 @@
1
+ import { ROUTE_SPEC_FILE, DEFAULT_SERVER_CONFIG } from "@modern-js/utils";
2
+ import { genPluginImportsCode, getPluginsCode, severAppContextTemplate } from "../utils";
3
+ function genVercelEntry({ config, plugins, appContext } = {}) {
4
+ const defaultConfig = {
5
+ server: {
6
+ port: 8080
7
+ },
8
+ output: {
9
+ path: "."
10
+ }
11
+ };
12
+ return `
13
+
14
+ const fs = require('node:fs/promises');
15
+ const path = require('node:path');
16
+ const { createProdServer } = require('@modern-js/prod-server');
17
+ ${genPluginImportsCode(plugins || [])}
18
+
19
+ if(!process.env.NODE_ENV){
20
+ process.env.NODE_ENV = 'production';
21
+ }
22
+
23
+ let requestHandler = null;
24
+ async function createHandler() {
25
+ try {
26
+ let routes = [];
27
+ const routeFilepath = path.join(__dirname, "${ROUTE_SPEC_FILE}");
28
+ try {
29
+ await fs.access(routeFilepath);
30
+ const content = await fs.readFile(routeFilepath, "utf-8");
31
+ const routeSpec = JSON.parse(content);
32
+ routes = routeSpec.routes;
33
+ } catch (error) {
34
+ console.warn('route.json not found, continuing with empty routes.');
35
+ }
36
+
37
+ const prodServerOptions = {
38
+ pwd: __dirname,
39
+ routes,
40
+ config: ${JSON.stringify(config || defaultConfig)},
41
+ serverConfigFile: '${DEFAULT_SERVER_CONFIG}',
42
+ plugins: ${getPluginsCode(plugins || [])},
43
+ appContext: ${appContext ? severAppContextTemplate(appContext) : "undefined"},
44
+ disableCustomHook: true
45
+ }
46
+
47
+ const app = await createProdServer(prodServerOptions)
48
+
49
+ requestHandler = app.getRequestListener();
50
+
51
+ return requestHandler;
52
+ } catch(error) {
53
+ console.error(error);
54
+ process.exit(1);
55
+ }
56
+ }
57
+
58
+ createHandler();
59
+
60
+ module.exports = async(req, res) => {
61
+ if(typeof requestHandler !== 'function'){
62
+ await createHandler();
63
+ }
64
+ return requestHandler(req, res);
65
+ }
66
+ `;
67
+ }
68
+ export {
69
+ genVercelEntry
70
+ };
@@ -0,0 +1,156 @@
1
+ import path from "path";
2
+ import { fs as fse, getInternalPlugins } from "@modern-js/utils";
3
+ import { provider } from "std-env";
4
+ import { getProjectUsage } from "./utils";
5
+ import { handleDependencies } from "./dependencies";
6
+ var deploy_default = () => ({
7
+ name: "@modern-js/plugin-deploy",
8
+ pre: [
9
+ "@modern-js/plugin-bff",
10
+ "@modern-js/plugin-server"
11
+ ],
12
+ setup: (api) => {
13
+ const deployTarget = process.env.MODERNJS_DEPLOY || provider || "node";
14
+ return {
15
+ async beforeDeploy(options) {
16
+ const { output: outputPath } = options;
17
+ const appContext = api.useAppContext();
18
+ const { appDirectory, distDirectory, serverInternalPlugins, sharedDirectory, apiDirectory, lambdaDirectory, metaName, entrypoints } = appContext;
19
+ const { useSSR, useAPI, useWebServer } = getProjectUsage(appDirectory, distDirectory);
20
+ const needModernServer = useSSR || useAPI || useWebServer;
21
+ const configContext = api.useResolvedConfigContext();
22
+ let outputDirectory = path.resolve(appDirectory, ".output");
23
+ if (outputPath) {
24
+ outputDirectory = path.isAbsolute(outputPath) ? outputPath : path.resolve(outputPath);
25
+ }
26
+ let funcsDirectory = outputDirectory;
27
+ let staticDirectory = path.join(outputDirectory, "static");
28
+ if (deployTarget === "node") {
29
+ await fse.remove(outputDirectory);
30
+ await fse.copy(distDirectory, outputDirectory);
31
+ }
32
+ if (deployTarget === "vercel") {
33
+ const vercelOutput = path.join(appDirectory, ".vercel");
34
+ await fse.remove(vercelOutput);
35
+ outputDirectory = path.join(vercelOutput, "output");
36
+ const config2 = {
37
+ version: 3,
38
+ routes: [
39
+ {
40
+ src: "/static/(.*)",
41
+ headers: {
42
+ "cache-control": "s-maxage=31536000, immutable"
43
+ },
44
+ continue: true
45
+ },
46
+ {
47
+ handle: "filesystem"
48
+ }
49
+ ]
50
+ };
51
+ if (!needModernServer) {
52
+ entrypoints.forEach((entry) => {
53
+ config2.routes.push({
54
+ src: `/${entry.entryName}(?:/.*)?`,
55
+ headers: {
56
+ "cache-control": "s-maxage=0"
57
+ },
58
+ dest: `/html/${entry.entryName}/index.html`
59
+ });
60
+ });
61
+ } else {
62
+ config2.routes.push({
63
+ src: "/(.*)",
64
+ dest: `/index`
65
+ });
66
+ }
67
+ await fse.ensureDir(outputDirectory);
68
+ await fse.writeJSON(path.join(outputDirectory, "config.json"), config2, {
69
+ spaces: 2
70
+ });
71
+ staticDirectory = path.join(outputDirectory, "static/static");
72
+ await fse.copy(path.join(distDirectory, "static"), staticDirectory);
73
+ if (!needModernServer) {
74
+ const destHtmlDirectory = path.join(distDirectory, "html");
75
+ const outputHtmlDirectory = path.join(path.join(outputDirectory, "static"), "html");
76
+ await fse.copy(destHtmlDirectory, outputHtmlDirectory);
77
+ } else {
78
+ funcsDirectory = path.join(outputDirectory, "functions", "index.func");
79
+ await fse.ensureDir(funcsDirectory);
80
+ await fse.copy(distDirectory, funcsDirectory, {
81
+ filter: (src) => {
82
+ const distStaticDirectory = path.join(distDirectory, "static");
83
+ return !src.includes(distStaticDirectory);
84
+ }
85
+ });
86
+ await fse.writeJSON(path.join(funcsDirectory, ".vc-config.json"), {
87
+ runtime: "nodejs16.x",
88
+ handler: "index.js",
89
+ launcherType: "Nodejs",
90
+ shouldAddHelpers: false,
91
+ supportsResponseStreaming: true
92
+ });
93
+ }
94
+ }
95
+ const { bff } = configContext;
96
+ const config = {
97
+ output: {
98
+ path: "."
99
+ },
100
+ bff
101
+ };
102
+ const plugins = getInternalPlugins(appDirectory, serverInternalPlugins);
103
+ const serverAppContext = {
104
+ sharedDirectory: `path.join(__dirname, "${path.relative(appDirectory, sharedDirectory)}")`,
105
+ apiDirectory: `path.join(__dirname, "${path.relative(appDirectory, apiDirectory)}")`,
106
+ lambdaDirectory: `path.join(__dirname, "${path.relative(appDirectory, lambdaDirectory)}")`,
107
+ metaName
108
+ };
109
+ let code = ``;
110
+ console.log("deployTarget111111111", deployTarget);
111
+ switch (deployTarget) {
112
+ case "node": {
113
+ const { genNodeEntry } = await import("./entrys/node");
114
+ code = genNodeEntry({
115
+ plugins,
116
+ config,
117
+ appContext: serverAppContext
118
+ });
119
+ break;
120
+ }
121
+ case "vercel": {
122
+ const { genVercelEntry } = await import("./entrys/vercel");
123
+ code = genVercelEntry({
124
+ plugins,
125
+ config,
126
+ appContext: serverAppContext
127
+ });
128
+ break;
129
+ }
130
+ case "netlify": {
131
+ const { genNetlifyEntry } = await import("./entrys/netlify");
132
+ code = genNetlifyEntry({
133
+ plugins,
134
+ config,
135
+ appContext: serverAppContext
136
+ });
137
+ break;
138
+ }
139
+ default: {
140
+ code = `throw new Error("unknown deploy target, MODERNJS_DEPLOY should be set");`;
141
+ }
142
+ }
143
+ const entryFilePath = path.join(funcsDirectory, "index.js");
144
+ if (needModernServer) {
145
+ await fse.writeFile(entryFilePath, code);
146
+ await handleDependencies(appDirectory, funcsDirectory, [
147
+ "@modern-js/prod-server"
148
+ ]);
149
+ }
150
+ }
151
+ };
152
+ }
153
+ });
154
+ export {
155
+ deploy_default as default
156
+ };