@modern-js/plugin-ssg 2.48.5 → 2.49.0

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.
@@ -51,8 +51,13 @@ const createServer = (api, ssgRoutes, pageRoutes, apiRoutes, options, appDirecto
51
51
  options,
52
52
  renderRoutes: ssgRoutes,
53
53
  routes: total,
54
- appDirectory,
55
- plugins
54
+ appContext: {
55
+ apiDirectory: appContext.apiDirectory,
56
+ lambdaDirectory: appContext.lambdaDirectory,
57
+ appDirectory: appContext.appDirectory
58
+ },
59
+ plugins,
60
+ distDirectory: appContext.distDirectory
56
61
  }));
57
62
  const htmlChunks = [];
58
63
  const htmlAry = [];
@@ -21,48 +21,68 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
21
21
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
22
  mod
23
23
  ));
24
- var import_prod_server = __toESM(require("@modern-js/prod-server"));
24
+ var import_http = require("http");
25
25
  var import_portfinder = __toESM(require("portfinder"));
26
- var import_make = require("../libs/make");
27
- var import_prerender = require("./prerender");
26
+ var import_prod_server = require("@modern-js/prod-server");
28
27
  var import_consts = require("./consts");
29
28
  process.on("message", async (chunk) => {
30
29
  if (chunk === import_consts.CLOSE_SIGN) {
31
30
  process.exit();
32
31
  }
33
32
  const context = JSON.parse(chunk);
34
- const { routes, renderRoutes, options, appDirectory, plugins } = context;
35
- let modernServer = null;
33
+ const { routes, renderRoutes, options, appContext, plugins, distDirectory } = context;
34
+ let nodeServer = null;
36
35
  try {
37
- const { server: serverOptions } = options;
38
- const defaultPort = Number(process.env.PORT) || serverOptions.port;
36
+ const { server: serverConfig } = options;
37
+ const defaultPort = Number(process.env.PORT) || serverConfig.port;
39
38
  import_portfinder.default.basePort = defaultPort;
40
39
  const port = await import_portfinder.default.getPortPromise();
41
- modernServer = await (0, import_prod_server.default)({
42
- pwd: appDirectory,
40
+ const serverOptions = {
41
+ pwd: distDirectory,
43
42
  config: options,
43
+ appContext,
44
44
  routes,
45
45
  staticGenerate: true,
46
46
  internalPlugins: plugins
47
- });
48
- modernServer.listen(port, async (err) => {
49
- if (err) {
50
- throw err;
51
- }
52
- if (!modernServer) {
47
+ };
48
+ nodeServer = await (0, import_prod_server.createProdServer)(serverOptions);
49
+ nodeServer.listen(port, async () => {
50
+ if (!nodeServer) {
53
51
  return;
54
52
  }
55
- const render = (0, import_prerender.compile)(modernServer.getRequestHandler());
56
- const renderPromiseAry = (0, import_make.makeRender)(renderRoutes, render, port);
57
- const htmlAry = await Promise.all(renderPromiseAry);
53
+ const htmlAry = await Promise.all(renderRoutes.map((route) => {
54
+ const url = `http://localhost:${port}${route.urlPath}`;
55
+ return getHtml(url, port);
56
+ }));
58
57
  htmlAry.forEach((html) => {
59
58
  process.send(html);
60
59
  process.send(null);
61
60
  });
62
- modernServer.close();
61
+ nodeServer.close();
63
62
  });
64
63
  } catch (e) {
65
- modernServer === null || modernServer === void 0 ? void 0 : modernServer.close();
64
+ nodeServer === null || nodeServer === void 0 ? void 0 : nodeServer.close();
66
65
  process.stderr.write(e instanceof Error ? e.stack : e.toString());
67
66
  }
68
67
  });
68
+ function getHtml(url, port) {
69
+ const headers = {
70
+ host: `localhost:${port}`
71
+ };
72
+ return new Promise((resolve, reject) => {
73
+ (0, import_http.request)(url, {
74
+ headers
75
+ }, (res) => {
76
+ let html = "";
77
+ res.on("error", (error) => {
78
+ reject(error);
79
+ });
80
+ res.on("data", (chunk) => {
81
+ html += chunk.toString();
82
+ });
83
+ res.on("end", () => {
84
+ resolve(html);
85
+ });
86
+ }).end();
87
+ });
88
+ }
@@ -21,8 +21,13 @@ var createServer = function(api, ssgRoutes, pageRoutes, apiRoutes, options, appD
21
21
  options,
22
22
  renderRoutes: ssgRoutes,
23
23
  routes: total,
24
- appDirectory,
25
- plugins
24
+ appContext: {
25
+ apiDirectory: appContext.apiDirectory,
26
+ lambdaDirectory: appContext.lambdaDirectory,
27
+ appDirectory: appContext.appDirectory
28
+ },
29
+ plugins,
30
+ distDirectory: appContext.distDirectory
26
31
  }));
27
32
  var htmlChunks = [];
28
33
  var htmlAry = [];
@@ -1,14 +1,13 @@
1
1
  import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
2
  import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
3
3
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
- import server from "@modern-js/prod-server";
4
+ import { request } from "http";
5
5
  import portfinder from "portfinder";
6
- import { makeRender } from "../libs/make";
7
- import { compile as createRender } from "./prerender";
6
+ import { createProdServer } from "@modern-js/prod-server";
8
7
  import { CLOSE_SIGN } from "./consts";
9
8
  process.on("message", function() {
10
9
  var _ref = _async_to_generator(function(chunk) {
11
- var context, routes, renderRoutes, options, appDirectory, plugins, modernServer, serverOptions, defaultPort, port, e;
10
+ var context, routes, renderRoutes, options, appContext, plugins, distDirectory, nodeServer, serverConfig, defaultPort, port, serverOptions, e;
12
11
  return _ts_generator(this, function(_state) {
13
12
  switch (_state.label) {
14
13
  case 0:
@@ -16,8 +15,8 @@ process.on("message", function() {
16
15
  process.exit();
17
16
  }
18
17
  context = JSON.parse(chunk);
19
- routes = context.routes, renderRoutes = context.renderRoutes, options = context.options, appDirectory = context.appDirectory, plugins = context.plugins;
20
- modernServer = null;
18
+ routes = context.routes, renderRoutes = context.renderRoutes, options = context.options, appContext = context.appContext, plugins = context.plugins, distDirectory = context.distDirectory;
19
+ nodeServer = null;
21
20
  _state.label = 1;
22
21
  case 1:
23
22
  _state.trys.push([
@@ -26,8 +25,8 @@ process.on("message", function() {
26
25
  ,
27
26
  5
28
27
  ]);
29
- serverOptions = options.server;
30
- defaultPort = Number(process.env.PORT) || serverOptions.port;
28
+ serverConfig = options.server;
29
+ defaultPort = Number(process.env.PORT) || serverConfig.port;
31
30
  portfinder.basePort = defaultPort;
32
31
  return [
33
32
  4,
@@ -35,62 +34,57 @@ process.on("message", function() {
35
34
  ];
36
35
  case 2:
37
36
  port = _state.sent();
37
+ serverOptions = {
38
+ pwd: distDirectory,
39
+ config: options,
40
+ appContext,
41
+ routes,
42
+ staticGenerate: true,
43
+ internalPlugins: plugins
44
+ };
38
45
  return [
39
46
  4,
40
- server({
41
- pwd: appDirectory,
42
- config: options,
43
- routes,
44
- staticGenerate: true,
45
- internalPlugins: plugins
46
- })
47
+ createProdServer(serverOptions)
47
48
  ];
48
49
  case 3:
49
- modernServer = _state.sent();
50
- modernServer.listen(port, function() {
51
- var _ref2 = _async_to_generator(function(err) {
52
- var render, renderPromiseAry, htmlAry;
53
- return _ts_generator(this, function(_state2) {
54
- switch (_state2.label) {
55
- case 0:
56
- if (err) {
57
- throw err;
58
- }
59
- if (!modernServer) {
60
- return [
61
- 2
62
- ];
63
- }
64
- render = createRender(modernServer.getRequestHandler());
65
- renderPromiseAry = makeRender(renderRoutes, render, port);
66
- return [
67
- 4,
68
- Promise.all(renderPromiseAry)
69
- ];
70
- case 1:
71
- htmlAry = _state2.sent();
72
- htmlAry.forEach(function(html) {
73
- process.send(html);
74
- process.send(null);
75
- });
76
- modernServer.close();
50
+ nodeServer = _state.sent();
51
+ nodeServer.listen(port, /* @__PURE__ */ _async_to_generator(function() {
52
+ var htmlAry;
53
+ return _ts_generator(this, function(_state2) {
54
+ switch (_state2.label) {
55
+ case 0:
56
+ if (!nodeServer) {
77
57
  return [
78
58
  2
79
59
  ];
80
- }
81
- });
60
+ }
61
+ return [
62
+ 4,
63
+ Promise.all(renderRoutes.map(function(route) {
64
+ var url = "http://localhost:".concat(port).concat(route.urlPath);
65
+ return getHtml(url, port);
66
+ }))
67
+ ];
68
+ case 1:
69
+ htmlAry = _state2.sent();
70
+ htmlAry.forEach(function(html) {
71
+ process.send(html);
72
+ process.send(null);
73
+ });
74
+ nodeServer.close();
75
+ return [
76
+ 2
77
+ ];
78
+ }
82
79
  });
83
- return function(err) {
84
- return _ref2.apply(this, arguments);
85
- };
86
- }());
80
+ }));
87
81
  return [
88
82
  3,
89
83
  5
90
84
  ];
91
85
  case 4:
92
86
  e = _state.sent();
93
- modernServer === null || modernServer === void 0 ? void 0 : modernServer.close();
87
+ nodeServer === null || nodeServer === void 0 ? void 0 : nodeServer.close();
94
88
  process.stderr.write(_instanceof(e, Error) ? e.stack : e.toString());
95
89
  return [
96
90
  3,
@@ -107,3 +101,24 @@ process.on("message", function() {
107
101
  return _ref.apply(this, arguments);
108
102
  };
109
103
  }());
104
+ function getHtml(url, port) {
105
+ var headers = {
106
+ host: "localhost:".concat(port)
107
+ };
108
+ return new Promise(function(resolve, reject) {
109
+ request(url, {
110
+ headers
111
+ }, function(res) {
112
+ var html = "";
113
+ res.on("error", function(error) {
114
+ reject(error);
115
+ });
116
+ res.on("data", function(chunk) {
117
+ html += chunk.toString();
118
+ });
119
+ res.on("end", function() {
120
+ resolve(html);
121
+ });
122
+ }).end();
123
+ });
124
+ }
@@ -18,8 +18,13 @@ const createServer = (api, ssgRoutes, pageRoutes, apiRoutes, options, appDirecto
18
18
  options,
19
19
  renderRoutes: ssgRoutes,
20
20
  routes: total,
21
- appDirectory,
22
- plugins
21
+ appContext: {
22
+ apiDirectory: appContext.apiDirectory,
23
+ lambdaDirectory: appContext.lambdaDirectory,
24
+ appDirectory: appContext.appDirectory
25
+ },
26
+ plugins,
27
+ distDirectory: appContext.distDirectory
23
28
  }));
24
29
  const htmlChunks = [];
25
30
  const htmlAry = [];
@@ -1,45 +1,65 @@
1
- import server from "@modern-js/prod-server";
1
+ import { request } from "http";
2
2
  import portfinder from "portfinder";
3
- import { makeRender } from "../libs/make";
4
- import { compile as createRender } from "./prerender";
3
+ import { createProdServer } from "@modern-js/prod-server";
5
4
  import { CLOSE_SIGN } from "./consts";
6
5
  process.on("message", async (chunk) => {
7
6
  if (chunk === CLOSE_SIGN) {
8
7
  process.exit();
9
8
  }
10
9
  const context = JSON.parse(chunk);
11
- const { routes, renderRoutes, options, appDirectory, plugins } = context;
12
- let modernServer = null;
10
+ const { routes, renderRoutes, options, appContext, plugins, distDirectory } = context;
11
+ let nodeServer = null;
13
12
  try {
14
- const { server: serverOptions } = options;
15
- const defaultPort = Number(process.env.PORT) || serverOptions.port;
13
+ const { server: serverConfig } = options;
14
+ const defaultPort = Number(process.env.PORT) || serverConfig.port;
16
15
  portfinder.basePort = defaultPort;
17
16
  const port = await portfinder.getPortPromise();
18
- modernServer = await server({
19
- pwd: appDirectory,
17
+ const serverOptions = {
18
+ pwd: distDirectory,
20
19
  config: options,
20
+ appContext,
21
21
  routes,
22
22
  staticGenerate: true,
23
23
  internalPlugins: plugins
24
- });
25
- modernServer.listen(port, async (err) => {
26
- if (err) {
27
- throw err;
28
- }
29
- if (!modernServer) {
24
+ };
25
+ nodeServer = await createProdServer(serverOptions);
26
+ nodeServer.listen(port, async () => {
27
+ if (!nodeServer) {
30
28
  return;
31
29
  }
32
- const render = createRender(modernServer.getRequestHandler());
33
- const renderPromiseAry = makeRender(renderRoutes, render, port);
34
- const htmlAry = await Promise.all(renderPromiseAry);
30
+ const htmlAry = await Promise.all(renderRoutes.map((route) => {
31
+ const url = `http://localhost:${port}${route.urlPath}`;
32
+ return getHtml(url, port);
33
+ }));
35
34
  htmlAry.forEach((html) => {
36
35
  process.send(html);
37
36
  process.send(null);
38
37
  });
39
- modernServer.close();
38
+ nodeServer.close();
40
39
  });
41
40
  } catch (e) {
42
- modernServer === null || modernServer === void 0 ? void 0 : modernServer.close();
41
+ nodeServer === null || nodeServer === void 0 ? void 0 : nodeServer.close();
43
42
  process.stderr.write(e instanceof Error ? e.stack : e.toString());
44
43
  }
45
44
  });
45
+ function getHtml(url, port) {
46
+ const headers = {
47
+ host: `localhost:${port}`
48
+ };
49
+ return new Promise((resolve, reject) => {
50
+ request(url, {
51
+ headers
52
+ }, (res) => {
53
+ let html = "";
54
+ res.on("error", (error) => {
55
+ reject(error);
56
+ });
57
+ res.on("data", (chunk) => {
58
+ html += chunk.toString();
59
+ });
60
+ res.on("end", () => {
61
+ resolve(html);
62
+ });
63
+ }).end();
64
+ });
65
+ }
@@ -1,5 +1,4 @@
1
- /// <reference types="node/http" />
2
- /// <reference types="@modern-js/prod-server/src/type" />
1
+ /// <reference types="node" />
3
2
  import { IncomingMessage, ServerResponse } from 'http';
4
3
  export type Options = {
5
4
  url: string;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.48.5",
18
+ "version": "2.49.0",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -63,7 +63,7 @@
63
63
  "normalize-path": "3.0.0",
64
64
  "portfinder": "^1.0.28",
65
65
  "@swc/helpers": "0.5.3",
66
- "@modern-js/utils": "2.48.5"
66
+ "@modern-js/utils": "2.49.0"
67
67
  },
68
68
  "peerDependencies": {
69
69
  "react-router-dom": ">=5.1.2"
@@ -81,11 +81,11 @@
81
81
  "react-dom": "^18",
82
82
  "react-router-dom": "6.22.0",
83
83
  "typescript": "^5",
84
- "@modern-js/app-tools": "2.48.5",
85
- "@modern-js/prod-server": "2.48.5",
86
- "@modern-js/types": "2.48.5",
87
- "@scripts/build": "2.48.5",
88
- "@scripts/jest-config": "2.48.5"
84
+ "@modern-js/app-tools": "2.49.0",
85
+ "@modern-js/prod-server": "2.49.0",
86
+ "@modern-js/types": "2.49.0",
87
+ "@scripts/jest-config": "2.49.0",
88
+ "@scripts/build": "2.49.0"
89
89
  },
90
90
  "sideEffects": false,
91
91
  "publishConfig": {