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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/dist/cjs/analyze/getServerRoutes.js +3 -4
  2. package/dist/cjs/plugins/deploy/entrys/vercel.js +97 -0
  3. package/dist/cjs/plugins/deploy/index.js +24 -53
  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 +43 -0
  7. package/dist/esm/plugins/deploy/index.js +74 -118
  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 +73 -0
  11. package/dist/esm-node/plugins/deploy/index.js +24 -53
  12. package/dist/esm-node/utils/routes.js +2 -6
  13. package/dist/types/plugins/deploy/{platforms → entrys}/vercel.d.ts +3 -2
  14. package/dist/types/utils/routes.d.ts +3 -3
  15. package/package.json +8 -8
  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,97 @@
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
+ if (appContext === null || appContext === void 0 ? void 0 : appContext.apiDirectory) {
36
+ appContext.apiDirectory = appContext === null || appContext === void 0 ? void 0 : appContext.apiDirectory.replace(/\/api$/, "/_api");
37
+ }
38
+ return `
39
+
40
+ const fs = require('node:fs/promises');
41
+ const path = require('node:path');
42
+ const { createProdServer } = require('@modern-js/prod-server');
43
+ ${(0, import_utils2.genPluginImportsCode)(plugins || [])}
44
+
45
+ if(!process.env.NODE_ENV){
46
+ process.env.NODE_ENV = 'production';
47
+ }
48
+
49
+ let requestHandler = null;
50
+ async function createHandler() {
51
+ try {
52
+ let routes = [];
53
+ const routeFilepath = path.join(__dirname, "${import_utils.ROUTE_SPEC_FILE}");
54
+ try {
55
+ await fs.access(routeFilepath);
56
+ const content = await fs.readFile(routeFilepath, "utf-8");
57
+ const routeSpec = JSON.parse(content);
58
+ routes = routeSpec.routes;
59
+ } catch (error) {
60
+ console.warn('route.json not found, continuing with empty routes.');
61
+ }
62
+
63
+ const prodServerOptions = {
64
+ pwd: __dirname,
65
+ routes,
66
+ config: ${JSON.stringify(config || defaultConfig)},
67
+ serverConfigFile: '${import_utils.DEFAULT_SERVER_CONFIG}',
68
+ plugins: ${(0, import_utils2.getPluginsCode)(plugins || [])},
69
+ appContext: ${appContext ? (0, import_utils2.severAppContextTemplate)(appContext) : "undefined"},
70
+ disableCustomHook: true
71
+ }
72
+
73
+ const app = await createProdServer(prodServerOptions)
74
+
75
+ requestHandler = app.getRequestListener();
76
+
77
+ return requestHandler;
78
+ } catch(error) {
79
+ console.error(error);
80
+ process.exit(1);
81
+ }
82
+ }
83
+
84
+ createHandler();
85
+
86
+ module.exports = async(req, res) => {
87
+ if(typeof requestHandler !== 'function'){
88
+ await createHandler();
89
+ }
90
+ return requestHandler(req, res);
91
+ }
92
+ `;
93
+ }
94
+ // Annotate the CommonJS export names for ESM import in node:
95
+ 0 && (module.exports = {
96
+ genVercelEntry
97
+ });
@@ -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,11 +47,10 @@ 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");
@@ -60,48 +58,11 @@ var deploy_default = () => ({
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");
@@ -152,6 +112,10 @@ var deploy_default = () => ({
152
112
  return !src.includes(distStaticDirectory);
153
113
  }
154
114
  });
115
+ const apiDirectory2 = import_path.default.join(funcsDirectory, "api");
116
+ if (await import_utils.fs.pathExists(apiDirectory2)) {
117
+ await import_utils.fs.rename(apiDirectory2, import_path.default.join(funcsDirectory, "_api"));
118
+ }
155
119
  await import_utils.fs.writeJSON(import_path.default.join(funcsDirectory, ".vc-config.json"), {
156
120
  runtime: "nodejs16.x",
157
121
  handler: "index.js",
@@ -161,6 +125,13 @@ var deploy_default = () => ({
161
125
  });
162
126
  }
163
127
  }
128
+ const { bff } = configContext;
129
+ const config = {
130
+ output: {
131
+ path: "."
132
+ },
133
+ bff
134
+ };
164
135
  const plugins = (0, import_utils.getInternalPlugins)(appDirectory, serverInternalPlugins);
165
136
  const serverAppContext = {
166
137
  sharedDirectory: `path.join(__dirname, "${import_path.default.relative(appDirectory, sharedDirectory)}")`,
@@ -172,28 +143,28 @@ var deploy_default = () => ({
172
143
  console.log("deployTarget111111111", deployTarget);
173
144
  switch (deployTarget) {
174
145
  case "node": {
175
- const { genNodeEntry } = await Promise.resolve().then(() => __toESM(require("./platforms/node")));
146
+ const { genNodeEntry } = await Promise.resolve().then(() => __toESM(require("./entrys/node")));
176
147
  code = genNodeEntry({
177
148
  plugins,
178
- config: modernConfig,
149
+ config,
179
150
  appContext: serverAppContext
180
151
  });
181
152
  break;
182
153
  }
183
154
  case "vercel": {
184
- const { genVercelEntry } = await Promise.resolve().then(() => __toESM(require("./platforms/vercel")));
185
- code = await genVercelEntry({
155
+ const { genVercelEntry } = await Promise.resolve().then(() => __toESM(require("./entrys/vercel")));
156
+ code = genVercelEntry({
186
157
  plugins,
187
- config: modernConfig,
158
+ config,
188
159
  appContext: serverAppContext
189
160
  });
190
161
  break;
191
162
  }
192
163
  case "netlify": {
193
- const { genNetlifyEntry } = await Promise.resolve().then(() => __toESM(require("./platforms/netlify")));
164
+ const { genNetlifyEntry } = await Promise.resolve().then(() => __toESM(require("./entrys/netlify")));
194
165
  code = genNetlifyEntry({
195
166
  plugins,
196
- config: modernConfig,
167
+ config,
197
168
  appContext: serverAppContext
198
169
  });
199
170
  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,43 @@
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
+ if (appContext === null || appContext === void 0 ? void 0 : appContext.apiDirectory) {
14
+ appContext.apiDirectory = appContext === null || appContext === void 0 ? void 0 : appContext.apiDirectory.replace(/\/api$/, "/_api");
15
+ }
16
+ 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 || []), `
17
+
18
+ if(!process.env.NODE_ENV){
19
+ process.env.NODE_ENV = 'production';
20
+ }
21
+
22
+ let requestHandler = null;
23
+ async function createHandler() {
24
+ try {
25
+ let routes = [];
26
+ const routeFilepath = path.join(__dirname, "`).concat(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: `).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 ");
40
+ }
41
+ export {
42
+ genVercelEntry
43
+ };