@modern-js/app-tools 2.49.3-alpha.10 → 2.49.3-alpha.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. package/dist/cjs/analyze/getServerRoutes.js +3 -4
  2. package/dist/cjs/plugins/deploy/entrys/vercel.js +94 -0
  3. package/dist/cjs/plugins/deploy/index.js +21 -54
  4. package/dist/cjs/utils/routes.js +2 -7
  5. package/dist/esm/analyze/getServerRoutes.js +4 -5
  6. package/dist/esm/plugins/deploy/entrys/vercel.js +40 -0
  7. package/dist/esm/plugins/deploy/index.js +56 -119
  8. package/dist/esm/utils/routes.js +2 -6
  9. package/dist/esm-node/analyze/getServerRoutes.js +4 -5
  10. package/dist/esm-node/plugins/deploy/entrys/vercel.js +70 -0
  11. package/dist/esm-node/plugins/deploy/index.js +21 -54
  12. package/dist/esm-node/utils/routes.js +2 -6
  13. package/dist/types/plugins/deploy/{platforms → entrys}/vercel.d.ts +1 -1
  14. package/dist/types/utils/routes.d.ts +3 -3
  15. package/package.json +7 -7
  16. package/dist/cjs/plugins/deploy/platforms/nodeEntry.js +0 -36
  17. package/dist/cjs/plugins/deploy/platforms/platform.js +0 -16
  18. package/dist/cjs/plugins/deploy/platforms/vercel.js +0 -57
  19. package/dist/cjs/plugins/deploy/platforms/vercelEntry.js +0 -55
  20. package/dist/esm/plugins/deploy/platforms/nodeEntry.js +0 -99
  21. package/dist/esm/plugins/deploy/platforms/platform.js +0 -0
  22. package/dist/esm/plugins/deploy/platforms/vercel.js +0 -47
  23. package/dist/esm/plugins/deploy/platforms/vercelEntry.js +0 -197
  24. package/dist/esm-node/plugins/deploy/platforms/nodeEntry.js +0 -35
  25. package/dist/esm-node/plugins/deploy/platforms/platform.js +0 -0
  26. package/dist/esm-node/plugins/deploy/platforms/vercel.js +0 -23
  27. package/dist/esm-node/plugins/deploy/platforms/vercelEntry.js +0 -63
  28. package/dist/types/plugins/deploy/platforms/nodeEntry.d.ts +0 -1
  29. package/dist/types/plugins/deploy/platforms/platform.d.ts +0 -1
  30. package/dist/types/plugins/deploy/platforms/vercelEntry.d.ts +0 -2
  31. /package/dist/cjs/plugins/deploy/{platforms → entrys}/netlify.js +0 -0
  32. /package/dist/cjs/plugins/deploy/{platforms → entrys}/node.js +0 -0
  33. /package/dist/esm/plugins/deploy/{platforms → entrys}/netlify.js +0 -0
  34. /package/dist/esm/plugins/deploy/{platforms → entrys}/node.js +0 -0
  35. /package/dist/esm-node/plugins/deploy/{platforms → entrys}/netlify.js +0 -0
  36. /package/dist/esm-node/plugins/deploy/{platforms → entrys}/node.js +0 -0
  37. /package/dist/types/plugins/deploy/{platforms → entrys}/netlify.d.ts +0 -0
  38. /package/dist/types/plugins/deploy/{platforms → entrys}/node.d.ts +0 -0
@@ -34,7 +34,6 @@ module.exports = __toCommonJS(getServerRoutes_exports);
34
34
  var import_path = __toESM(require("path"));
35
35
  var import_fs = __toESM(require("fs"));
36
36
  var import_utils = require("@modern-js/utils");
37
- var import_routes = require("../utils/routes");
38
37
  var import_utils2 = require("./utils");
39
38
  const applyBaseUrl = (baseUrl, routes) => {
40
39
  if (baseUrl) {
@@ -107,14 +106,14 @@ const collectHtmlRoutes = (entrypoints, appContext, config) => {
107
106
  const { packageName } = appContext;
108
107
  const workerSSR = deploy === null || deploy === void 0 ? void 0 : (_deploy_worker = deploy.worker) === null || _deploy_worker === void 0 ? void 0 : _deploy_worker.ssr;
109
108
  let htmlRoutes = entrypoints.reduce((previous, { entryName }) => {
110
- const isMain = (0, import_routes.isMainEntry)(entryName, mainEntryName);
111
- const entryOptions = (0, import_utils.getEntryOptions)(entryName, isMain, ssr, ssrByEntries, packageName);
109
+ const isMainEntry = entryName === (mainEntryName || import_utils.MAIN_ENTRY_NAME);
110
+ const entryOptions = (0, import_utils.getEntryOptions)(entryName, isMainEntry, ssr, ssrByEntries, packageName);
112
111
  const isSSR = Boolean(entryOptions);
113
112
  const isWorker = Boolean(workerSSR);
114
113
  const isStream = typeof entryOptions === "object" && (entryOptions.mode === "stream" || Boolean(entryOptions.preload));
115
114
  const { resHeaders } = (routes === null || routes === void 0 ? void 0 : routes[entryName]) || {};
116
115
  let route = {
117
- urlPath: `/${isMain ? "" : entryName}`,
116
+ urlPath: `/${isMainEntry ? "" : entryName}`,
118
117
  entryName,
119
118
  entryPath: (0, import_utils.removeLeadingSlash)(import_path.default.posix.normalize(`${htmlPath}/${entryName}${disableHtmlFolder ? ".html" : "/index.html"}`)),
120
119
  isSPA: true,
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var vercel_exports = {};
20
+ __export(vercel_exports, {
21
+ genVercelEntry: () => genVercelEntry
22
+ });
23
+ module.exports = __toCommonJS(vercel_exports);
24
+ var import_utils = require("@modern-js/utils");
25
+ var import_utils2 = require("../utils");
26
+ function genVercelEntry({ config, plugins, appContext } = {}) {
27
+ const defaultConfig = {
28
+ server: {
29
+ port: 8080
30
+ },
31
+ output: {
32
+ path: "."
33
+ }
34
+ };
35
+ return `
36
+
37
+ const fs = require('node:fs/promises');
38
+ const path = require('node:path');
39
+ const { createProdServer } = require('@modern-js/prod-server');
40
+ ${(0, import_utils2.genPluginImportsCode)(plugins || [])}
41
+
42
+ if(!process.env.NODE_ENV){
43
+ process.env.NODE_ENV = 'production';
44
+ }
45
+
46
+ let requestHandler = null;
47
+ async function createHandler() {
48
+ try {
49
+ let routes = [];
50
+ const routeFilepath = path.join(__dirname, "${import_utils.ROUTE_SPEC_FILE}");
51
+ try {
52
+ await fs.access(routeFilepath);
53
+ const content = await fs.readFile(routeFilepath, "utf-8");
54
+ const routeSpec = JSON.parse(content);
55
+ routes = routeSpec.routes;
56
+ } catch (error) {
57
+ console.warn('route.json not found, continuing with empty routes.');
58
+ }
59
+
60
+ const prodServerOptions = {
61
+ pwd: __dirname,
62
+ routes,
63
+ config: ${JSON.stringify(config || defaultConfig)},
64
+ serverConfigFile: '${import_utils.DEFAULT_SERVER_CONFIG}',
65
+ plugins: ${(0, import_utils2.getPluginsCode)(plugins || [])},
66
+ appContext: ${appContext ? (0, import_utils2.severAppContextTemplate)(appContext) : "undefined"},
67
+ disableCustomHook: true
68
+ }
69
+
70
+ const app = await createProdServer(prodServerOptions)
71
+
72
+ requestHandler = app.getRequestListener();
73
+
74
+ return requestHandler;
75
+ } catch(error) {
76
+ console.error(error);
77
+ process.exit(1);
78
+ }
79
+ }
80
+
81
+ createHandler();
82
+
83
+ module.exports = async(req, res) => {
84
+ if(typeof requestHandler !== 'function'){
85
+ await createHandler();
86
+ }
87
+ return requestHandler(req, res);
88
+ }
89
+ `;
90
+ }
91
+ // Annotate the CommonJS export names for ESM import in node:
92
+ 0 && (module.exports = {
93
+ genVercelEntry
94
+ });
@@ -34,7 +34,6 @@ module.exports = __toCommonJS(deploy_exports);
34
34
  var import_path = __toESM(require("path"));
35
35
  var import_utils = require("@modern-js/utils");
36
36
  var import_std_env = require("std-env");
37
- var import_routes = require("../../utils/routes");
38
37
  var import_utils2 = require("./utils");
39
38
  var import_dependencies = require("./dependencies");
40
39
  var deploy_default = () => ({
@@ -48,60 +47,22 @@ var deploy_default = () => ({
48
47
  return {
49
48
  async beforeDeploy() {
50
49
  const appContext = api.useAppContext();
51
- const modernConfig = api.useResolvedConfigContext();
52
- const { source: { mainEntryName } } = modernConfig;
53
50
  const { appDirectory, distDirectory, serverInternalPlugins, sharedDirectory, apiDirectory, lambdaDirectory, metaName, entrypoints } = appContext;
54
51
  const { useSSR, useAPI, useWebServer } = (0, import_utils2.getProjectUsage)(appDirectory, distDirectory);
55
52
  const needModernServer = useSSR || useAPI || useWebServer;
53
+ const configContext = api.useResolvedConfigContext();
56
54
  let outputDirectory = import_path.default.join(appDirectory, ".output");
57
55
  let funcsDirectory = outputDirectory;
58
56
  let staticDirectory = import_path.default.join(outputDirectory, "static");
59
- if (deployTarget === "node") {
57
+ if (deployTarget === "node" || deployTarget === "netlify") {
60
58
  await import_utils.fs.remove(outputDirectory);
61
59
  await import_utils.fs.copy(distDirectory, outputDirectory);
62
60
  }
63
- if (deployTarget === "netlify") {
64
- const netlifyOutput = import_path.default.join(appDirectory, ".netlify");
65
- funcsDirectory = import_path.default.join(netlifyOutput, "functions");
66
- const routes = [];
67
- if (!needModernServer) {
68
- entrypoints.forEach((entry) => {
69
- const isMain = (0, import_routes.isMainEntry)(entry.entryName, mainEntryName);
70
- routes.push({
71
- src: `/${isMain ? "" : `${entry.entryName}/`}*`,
72
- dest: `/html/${entry.entryName}/index.html`,
73
- status: 200
74
- });
75
- });
76
- } else {
77
- routes.push({
78
- src: `/*`,
79
- dest: `/.netlify/functions/index`,
80
- status: 200
81
- });
82
- throw new Error("Currently on the Netlify platform, only CSR projects are supporte, Support for SSR and BFF projects will be available later");
83
- }
84
- console.log("routes", routes, needModernServer);
85
- const redirectContent = routes.map((route) => {
86
- return `${route.src} ${route.dest} ${route.status}`;
87
- }).join("\n");
88
- console.log("redirectContent", redirectContent);
89
- await import_utils.fs.remove(outputDirectory);
90
- await import_utils.fs.ensureDir(funcsDirectory);
91
- await import_utils.fs.copy(distDirectory, funcsDirectory, {
92
- filter: (src) => {
93
- const distStaticDirectory = import_path.default.join(distDirectory, "static");
94
- return !src.includes(distStaticDirectory);
95
- }
96
- });
97
- const redirectFilePath = import_path.default.join(distDirectory, "_redirects");
98
- await import_utils.fs.writeFile(redirectFilePath, redirectContent);
99
- }
100
61
  if (deployTarget === "vercel") {
101
62
  const vercelOutput = import_path.default.join(appDirectory, ".vercel");
102
63
  await import_utils.fs.remove(vercelOutput);
103
64
  outputDirectory = import_path.default.join(vercelOutput, "output");
104
- const config = {
65
+ const config2 = {
105
66
  version: 3,
106
67
  routes: [
107
68
  {
@@ -118,9 +79,8 @@ var deploy_default = () => ({
118
79
  };
119
80
  if (!needModernServer) {
120
81
  entrypoints.forEach((entry) => {
121
- const isMain = (0, import_routes.isMainEntry)(entry.entryName, mainEntryName);
122
- config.routes.push({
123
- src: `/${isMain ? "" : entry.entryName}(?:/.*)?`,
82
+ config2.routes.push({
83
+ src: `/${entry.entryName}(?:/.*)?`,
124
84
  headers: {
125
85
  "cache-control": "s-maxage=0"
126
86
  },
@@ -128,13 +88,13 @@ var deploy_default = () => ({
128
88
  });
129
89
  });
130
90
  } else {
131
- config.routes.push({
91
+ config2.routes.push({
132
92
  src: "/(.*)",
133
93
  dest: `/index`
134
94
  });
135
95
  }
136
96
  await import_utils.fs.ensureDir(outputDirectory);
137
- await import_utils.fs.writeJSON(import_path.default.join(outputDirectory, "config.json"), config, {
97
+ await import_utils.fs.writeJSON(import_path.default.join(outputDirectory, "config.json"), config2, {
138
98
  spaces: 2
139
99
  });
140
100
  staticDirectory = import_path.default.join(outputDirectory, "static/static");
@@ -161,6 +121,13 @@ var deploy_default = () => ({
161
121
  });
162
122
  }
163
123
  }
124
+ const { bff } = configContext;
125
+ const config = {
126
+ output: {
127
+ path: "."
128
+ },
129
+ bff
130
+ };
164
131
  const plugins = (0, import_utils.getInternalPlugins)(appDirectory, serverInternalPlugins);
165
132
  const serverAppContext = {
166
133
  sharedDirectory: `path.join(__dirname, "${import_path.default.relative(appDirectory, sharedDirectory)}")`,
@@ -172,28 +139,28 @@ var deploy_default = () => ({
172
139
  console.log("deployTarget111111111", deployTarget);
173
140
  switch (deployTarget) {
174
141
  case "node": {
175
- const { genNodeEntry } = await Promise.resolve().then(() => __toESM(require("./platforms/node")));
142
+ const { genNodeEntry } = await Promise.resolve().then(() => __toESM(require("./entrys/node")));
176
143
  code = genNodeEntry({
177
144
  plugins,
178
- config: modernConfig,
145
+ config,
179
146
  appContext: serverAppContext
180
147
  });
181
148
  break;
182
149
  }
183
150
  case "vercel": {
184
- const { genVercelEntry } = await Promise.resolve().then(() => __toESM(require("./platforms/vercel")));
185
- code = await genVercelEntry({
151
+ const { genVercelEntry } = await Promise.resolve().then(() => __toESM(require("./entrys/vercel")));
152
+ code = genVercelEntry({
186
153
  plugins,
187
- config: modernConfig,
154
+ config,
188
155
  appContext: serverAppContext
189
156
  });
190
157
  break;
191
158
  }
192
159
  case "netlify": {
193
- const { genNetlifyEntry } = await Promise.resolve().then(() => __toESM(require("./platforms/netlify")));
160
+ const { genNetlifyEntry } = await Promise.resolve().then(() => __toESM(require("./entrys/netlify")));
194
161
  code = genNetlifyEntry({
195
162
  plugins,
196
- config: modernConfig,
163
+ config,
197
164
  appContext: serverAppContext
198
165
  });
199
166
  break;
@@ -29,8 +29,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
29
  var routes_exports = {};
30
30
  __export(routes_exports, {
31
31
  generateRoutes: () => generateRoutes,
32
- getPathWithoutExt: () => getPathWithoutExt,
33
- isMainEntry: () => isMainEntry
32
+ getPathWithoutExt: () => getPathWithoutExt
34
33
  });
35
34
  module.exports = __toCommonJS(routes_exports);
36
35
  var import_path = __toESM(require("path"));
@@ -46,12 +45,8 @@ const getPathWithoutExt = (filename) => {
46
45
  const extname = import_path.default.extname(filename);
47
46
  return filename.slice(0, -extname.length);
48
47
  };
49
- const isMainEntry = (entryName, mainEntryName) => {
50
- return entryName === (mainEntryName || import_utils.MAIN_ENTRY_NAME);
51
- };
52
48
  // Annotate the CommonJS export names for ESM import in node:
53
49
  0 && (module.exports = {
54
50
  generateRoutes,
55
- getPathWithoutExt,
56
- isMainEntry
51
+ getPathWithoutExt
57
52
  });
@@ -4,8 +4,7 @@ import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_
4
4
  import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
5
5
  import path from "path";
6
6
  import fs from "fs";
7
- import { urlJoin, isPlainObject, removeLeadingSlash, getEntryOptions, SERVER_BUNDLE_DIRECTORY, removeTailSlash, SERVER_WORKER_BUNDLE_DIRECTORY } from "@modern-js/utils";
8
- import { isMainEntry } from "../utils/routes";
7
+ import { urlJoin, isPlainObject, removeLeadingSlash, getEntryOptions, SERVER_BUNDLE_DIRECTORY, MAIN_ENTRY_NAME, removeTailSlash, SERVER_WORKER_BUNDLE_DIRECTORY } from "@modern-js/utils";
9
8
  import { walkDirectory } from "./utils";
10
9
  var applyBaseUrl = function(baseUrl, routes) {
11
10
  if (baseUrl) {
@@ -76,14 +75,14 @@ var collectHtmlRoutes = function(entrypoints, appContext, config) {
76
75
  var workerSSR = deploy === null || deploy === void 0 ? void 0 : (_deploy_worker = deploy.worker) === null || _deploy_worker === void 0 ? void 0 : _deploy_worker.ssr;
77
76
  var htmlRoutes = entrypoints.reduce(function(previous, param) {
78
77
  var entryName = param.entryName;
79
- var isMain = isMainEntry(entryName, mainEntryName);
80
- var entryOptions = getEntryOptions(entryName, isMain, ssr, ssrByEntries, packageName);
78
+ var isMainEntry = entryName === (mainEntryName || MAIN_ENTRY_NAME);
79
+ var entryOptions = getEntryOptions(entryName, isMainEntry, ssr, ssrByEntries, packageName);
81
80
  var isSSR = Boolean(entryOptions);
82
81
  var isWorker = Boolean(workerSSR);
83
82
  var isStream = typeof entryOptions === "object" && (entryOptions.mode === "stream" || Boolean(entryOptions.preload));
84
83
  var resHeaders = ((routes === null || routes === void 0 ? void 0 : routes[entryName]) || {}).resHeaders;
85
84
  var route = {
86
- urlPath: "/".concat(isMain ? "" : entryName),
85
+ urlPath: "/".concat(isMainEntry ? "" : entryName),
87
86
  entryName,
88
87
  entryPath: removeLeadingSlash(path.posix.normalize("".concat(htmlPath, "/").concat(entryName).concat(disableHtmlFolder ? ".html" : "/index.html"))),
89
88
  isSPA: true,
@@ -0,0 +1,40 @@
1
+ import { ROUTE_SPEC_FILE, DEFAULT_SERVER_CONFIG } from "@modern-js/utils";
2
+ import { genPluginImportsCode, getPluginsCode, severAppContextTemplate } from "../utils";
3
+ function genVercelEntry() {
4
+ var _ref = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {}, config = _ref.config, plugins = _ref.plugins, appContext = _ref.appContext;
5
+ var defaultConfig = {
6
+ server: {
7
+ port: 8080
8
+ },
9
+ output: {
10
+ path: "."
11
+ }
12
+ };
13
+ return "\n\n const fs = require('node:fs/promises');\n const path = require('node:path');\n const { createProdServer } = require('@modern-js/prod-server');\n ".concat(genPluginImportsCode(plugins || []), `
14
+
15
+ if(!process.env.NODE_ENV){
16
+ process.env.NODE_ENV = 'production';
17
+ }
18
+
19
+ let requestHandler = null;
20
+ async function createHandler() {
21
+ try {
22
+ let routes = [];
23
+ const routeFilepath = path.join(__dirname, "`).concat(ROUTE_SPEC_FILE, `");
24
+ try {
25
+ await fs.access(routeFilepath);
26
+ const content = await fs.readFile(routeFilepath, "utf-8");
27
+ const routeSpec = JSON.parse(content);
28
+ routes = routeSpec.routes;
29
+ } catch (error) {
30
+ console.warn('route.json not found, continuing with empty routes.');
31
+ }
32
+
33
+ const prodServerOptions = {
34
+ pwd: __dirname,
35
+ routes,
36
+ config: `).concat(JSON.stringify(config || defaultConfig), ",\n serverConfigFile: '").concat(DEFAULT_SERVER_CONFIG, "',\n plugins: ").concat(getPluginsCode(plugins || []), ",\n appContext: ").concat(appContext ? severAppContextTemplate(appContext) : "undefined", ",\n disableCustomHook: true\n }\n\n const app = await createProdServer(prodServerOptions)\n\n requestHandler = app.getRequestListener();\n\n return requestHandler;\n } catch(error) {\n console.error(error);\n process.exit(1);\n }\n }\n\n createHandler();\n\n module.exports = async(req, res) => {\n if(typeof requestHandler !== 'function'){\n await createHandler();\n }\n return requestHandler(req, res);\n }\n ");
37
+ }
38
+ export {
39
+ genVercelEntry
40
+ };