@modern-js/server 2.26.0 → 2.28.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,33 @@
1
1
  # @modern-js/server
2
2
 
3
+ ## 2.28.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 6eae1e7: feat: support worker hmr using dev-server
8
+ feat: 借用 dev-server 支持 wokrer hmr
9
+
10
+ ### Patch Changes
11
+
12
+ - Updated dependencies [4e3ce96]
13
+ - Updated dependencies [6eae1e7]
14
+ - Updated dependencies [00b58a7]
15
+ - @modern-js/types@2.28.0
16
+ - @modern-js/prod-server@2.28.0
17
+ - @modern-js/utils@2.28.0
18
+ - @modern-js/server-utils@2.28.0
19
+
20
+ ## 2.27.0
21
+
22
+ ### Patch Changes
23
+
24
+ - Updated dependencies [91d14b8]
25
+ - Updated dependencies [6d7104d]
26
+ - @modern-js/utils@2.27.0
27
+ - @modern-js/prod-server@2.27.0
28
+ - @modern-js/server-utils@2.27.0
29
+ - @modern-js/types@2.27.0
30
+
3
31
  ## 2.26.0
4
32
 
5
33
  ### Patch Changes
@@ -109,6 +109,7 @@ function canApplyUpdates() {
109
109
  return module.hot.status() === "idle";
110
110
  }
111
111
  function tryApplyUpdates() {
112
+ var _result;
112
113
  if (!module.hot) {
113
114
  window.location.reload();
114
115
  return;
@@ -131,7 +132,7 @@ function tryApplyUpdates() {
131
132
  true,
132
133
  handleApplyUpdates
133
134
  );
134
- if (result === null || result === void 0 ? void 0 : result.then) {
135
+ if ((_result = result) === null || _result === void 0 ? void 0 : _result.then) {
135
136
  result.then((updatedModules) => {
136
137
  handleApplyUpdates(null, updatedModules);
137
138
  }, (err) => {
@@ -10,17 +10,19 @@ Object.defineProperty(exports, "default", {
10
10
  });
11
11
  const _define_property = require("@swc/helpers/_/_define_property");
12
12
  const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
13
+ const _path = require("path");
13
14
  const _events = require("events");
14
15
  const _socketServer = /* @__PURE__ */ _interop_require_default._(require("./socketServer"));
15
16
  const noop = () => {
16
17
  };
17
18
  function getHMRClientPath(client) {
18
- const protocol = (client === null || client === void 0 ? void 0 : client.protocol) ? `&protocol=${client.protocol}` : "";
19
- const host = (client === null || client === void 0 ? void 0 : client.host) ? `&host=${client.host}` : "";
20
- const path = (client === null || client === void 0 ? void 0 : client.path) ? `&path=${client.path}` : "";
21
- const port = (client === null || client === void 0 ? void 0 : client.port) ? `&port=${client.port}` : "";
19
+ var _client, _client1, _client2, _client3;
20
+ const protocol = ((_client = client) === null || _client === void 0 ? void 0 : _client.protocol) ? `&protocol=${client.protocol}` : "";
21
+ const host = ((_client1 = client) === null || _client1 === void 0 ? void 0 : _client1.host) ? `&host=${client.host}` : "";
22
+ const path = ((_client2 = client) === null || _client2 === void 0 ? void 0 : _client2.path) ? `&path=${client.path}` : "";
23
+ const port = ((_client3 = client) === null || _client3 === void 0 ? void 0 : _client3.port) ? `&port=${client.port}` : "";
22
24
  const clientEntry = `${require.resolve("@modern-js/server/hmr-client")}?${host}${path}${port}${protocol}`;
23
- return clientEntry;
25
+ return clientEntry.replace(`${_path.sep}cjs${_path.sep}dev-tools`, `${_path.sep}esm${_path.sep}dev-tools`);
24
26
  }
25
27
  class DevMiddleware extends _events.EventEmitter {
26
28
  init(app) {
@@ -15,6 +15,7 @@ const _utils = require("@modern-js/utils");
15
15
  const _prodserver = require("@modern-js/prod-server");
16
16
  const _getMockData = /* @__PURE__ */ _interop_require_wildcard._(require("./getMockData"));
17
17
  const createMockHandler = ({ pwd }) => {
18
+ var _config;
18
19
  const exts = [
19
20
  ".ts",
20
21
  ".js"
@@ -32,7 +33,7 @@ const createMockHandler = ({ pwd }) => {
32
33
  }
33
34
  const mod = (0, _utils.compatRequire)(filepath, false);
34
35
  const { default: mockModule = mod, config } = mod;
35
- if ((config === null || config === void 0 ? void 0 : config.enable) === false) {
36
+ if (((_config = config) === null || _config === void 0 ? void 0 : _config.enable) === false) {
36
37
  return null;
37
38
  }
38
39
  if (!mockModule) {
@@ -43,7 +44,8 @@ const createMockHandler = ({ pwd }) => {
43
44
  return null;
44
45
  }
45
46
  return async (context, next) => {
46
- if (typeof (config === null || config === void 0 ? void 0 : config.enable) === "function") {
47
+ var _config2;
48
+ if (typeof ((_config2 = config) === null || _config2 === void 0 ? void 0 : _config2.enable) === "function") {
47
49
  const enableMock = config.enable(context.req, context.res);
48
50
  if (!enableMock) {
49
51
  return next();
@@ -22,6 +22,7 @@ const _mock = require("../dev-tools/mock");
22
22
  const _register = require("../dev-tools/register");
23
23
  const _watcher = /* @__PURE__ */ _interop_require_wildcard._(require("../dev-tools/watcher"));
24
24
  const _devmiddleware = /* @__PURE__ */ _interop_require_default._(require("../dev-tools/dev-middleware"));
25
+ const _workerSSRRender = require("./workerSSRRender");
25
26
  class ModernDevServer extends _prodserver.ModernServer {
26
27
  getDevOptions(options) {
27
28
  const devOptions = typeof options.dev === "boolean" ? {} : options.dev;
@@ -79,10 +80,29 @@ class ModernDevServer extends _prodserver.ModernServer {
79
80
  });
80
81
  }
81
82
  }
83
+ // override the ModernServer renderHandler logic
84
+ getRenderHandler() {
85
+ if (this.useWorkerSSR) {
86
+ var _this_conf_server, _conf_security;
87
+ const { distDir, staticGenerate, conf, metaName } = this;
88
+ const ssrConfig = (_this_conf_server = this.conf.server) === null || _this_conf_server === void 0 ? void 0 : _this_conf_server.ssr;
89
+ const forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
90
+ return (0, _prodserver.createRenderHandler)({
91
+ ssrRender: _workerSSRRender.workerSSRRender,
92
+ distDir,
93
+ staticGenerate,
94
+ forceCSR,
95
+ nonce: (_conf_security = conf.security) === null || _conf_security === void 0 ? void 0 : _conf_security.nonce,
96
+ metaName
97
+ });
98
+ }
99
+ return super.getRenderHandler();
100
+ }
82
101
  async applyDefaultMiddlewares(app) {
83
102
  const { pwd, dev, devMiddleware } = this;
84
103
  const isUseStreamingSSR = (routes) => {
85
- return routes === null || routes === void 0 ? void 0 : routes.some((r) => r.isStream === true);
104
+ var _routes;
105
+ return (_routes = routes) === null || _routes === void 0 ? void 0 : _routes.some((r) => r.isStream === true);
86
106
  };
87
107
  if (!isUseStreamingSSR(this.getRoutes()) && dev.compress) {
88
108
  const { default: compression } = await Promise.resolve().then(() => /* @__PURE__ */ _interop_require_wildcard._(require("http-compression")));
@@ -169,7 +189,7 @@ class ModernDevServer extends _prodserver.ModernServer {
169
189
  warmupSSRBundle() {
170
190
  }
171
191
  initReader() {
172
- var _this_dev, _this_dev_devMiddleware;
192
+ var _this_dev_devMiddleware, _this_dev;
173
193
  let isInit = false;
174
194
  if (this.devMiddleware && ((_this_dev = this.dev) === null || _this_dev === void 0 ? void 0 : (_this_dev_devMiddleware = _this_dev.devMiddleware) === null || _this_dev_devMiddleware === void 0 ? void 0 : _this_dev_devMiddleware.writeToDisk) === false) {
175
195
  this.addHandler((ctx, next) => {
@@ -264,11 +284,11 @@ class ModernDevServer extends _prodserver.ModernServer {
264
284
  }
265
285
  }
266
286
  startWatcher() {
267
- var _this_conf_server;
287
+ var _appContext, _appContext1, _this_conf_server;
268
288
  const { pwd, distDir, appContext } = this;
269
289
  const { mock } = _prodserver.AGGRED_DIR;
270
- const apiDir = (appContext === null || appContext === void 0 ? void 0 : appContext.apiDirectory) || _utils.API_DIR;
271
- const sharedDir = (appContext === null || appContext === void 0 ? void 0 : appContext.sharedDirectory) || _utils.SHARED_DIR;
290
+ const apiDir = ((_appContext = appContext) === null || _appContext === void 0 ? void 0 : _appContext.apiDirectory) || _utils.API_DIR;
291
+ const sharedDir = ((_appContext1 = appContext) === null || _appContext1 === void 0 ? void 0 : _appContext1.sharedDirectory) || _utils.SHARED_DIR;
272
292
  const defaultWatched = [
273
293
  `${mock}/**/*`,
274
294
  `${_utils.SERVER_DIR}/**/*`,
@@ -302,11 +322,13 @@ class ModernDevServer extends _prodserver.ModernServer {
302
322
  super(options);
303
323
  _define_property._(this, "mockHandler", null);
304
324
  _define_property._(this, "dev", void 0);
325
+ _define_property._(this, "useWorkerSSR", void 0);
305
326
  _define_property._(this, "appContext", void 0);
306
327
  _define_property._(this, "devMiddleware", void 0);
307
328
  _define_property._(this, "watcher", void 0);
308
329
  this.appContext = options.appContext;
309
330
  this.workDir = this.pwd;
331
+ this.useWorkerSSR = Boolean(options.useWorkerSSR);
310
332
  this.dev = this.getDevOptions(options);
311
333
  this.devMiddleware = new _devmiddleware.default({
312
334
  dev: this.dev,
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "workerSSRRender", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return workerSSRRender;
9
+ }
10
+ });
11
+ const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
12
+ const _axios = /* @__PURE__ */ _interop_require_default._(require("axios"));
13
+ const _utils = require("@modern-js/utils");
14
+ const PORT = 9230;
15
+ async function workerSSRRender(ctx, renderOptions, _runner) {
16
+ const { headers, params } = ctx;
17
+ const { urlPath } = renderOptions;
18
+ const url = `http://0.0.0.0:${PORT}/${urlPath}`;
19
+ const resposne = await _axios.default.get(url, {
20
+ timeout: 5e3,
21
+ responseType: "text",
22
+ headers,
23
+ params
24
+ });
25
+ return {
26
+ content: resposne.data,
27
+ contentType: _utils.mime.contentType("html")
28
+ };
29
+ }
@@ -131,6 +131,7 @@ function tryApplyUpdates() {
131
131
  tryApplyUpdates();
132
132
  }
133
133
  };
134
+ var _result;
134
135
  if (!module.hot) {
135
136
  window.location.reload();
136
137
  return;
@@ -143,7 +144,7 @@ function tryApplyUpdates() {
143
144
  true,
144
145
  handleApplyUpdates
145
146
  );
146
- if (result === null || result === void 0 ? void 0 : result.then) {
147
+ if ((_result = result) === null || _result === void 0 ? void 0 : _result.then) {
147
148
  result.then(function(updatedModules) {
148
149
  handleApplyUpdates(null, updatedModules);
149
150
  }, function(err) {
@@ -7,21 +7,23 @@ import { _ as _inherits } from "@swc/helpers/_/_inherits";
7
7
  import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
8
8
  import { _ as _create_super } from "@swc/helpers/_/_create_super";
9
9
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
10
+ import { sep } from "path";
10
11
  import { EventEmitter } from "events";
11
12
  import SocketServer from "./socketServer";
12
13
  var noop = function() {
13
14
  };
14
15
  function getHMRClientPath(client) {
15
- var protocol = (client === null || client === void 0 ? void 0 : client.protocol) ? "&protocol=".concat(client.protocol) : "";
16
- var host = (client === null || client === void 0 ? void 0 : client.host) ? "&host=".concat(client.host) : "";
17
- var path = (client === null || client === void 0 ? void 0 : client.path) ? "&path=".concat(client.path) : "";
18
- var port = (client === null || client === void 0 ? void 0 : client.port) ? "&port=".concat(client.port) : "";
16
+ var _client, _client1, _client2, _client3;
17
+ var protocol = ((_client = client) === null || _client === void 0 ? void 0 : _client.protocol) ? "&protocol=".concat(client.protocol) : "";
18
+ var host = ((_client1 = client) === null || _client1 === void 0 ? void 0 : _client1.host) ? "&host=".concat(client.host) : "";
19
+ var path = ((_client2 = client) === null || _client2 === void 0 ? void 0 : _client2.path) ? "&path=".concat(client.path) : "";
20
+ var port = ((_client3 = client) === null || _client3 === void 0 ? void 0 : _client3.port) ? "&port=".concat(client.port) : "";
19
21
  var clientEntry = "".concat(require.resolve("@modern-js/server/hmr-client"), "?").concat(host).concat(path).concat(port).concat(protocol);
20
- return clientEntry;
22
+ return clientEntry.replace("".concat(sep, "cjs").concat(sep, "dev-tools"), "".concat(sep, "esm").concat(sep, "dev-tools"));
21
23
  }
22
- var DevMiddleware = /* @__PURE__ */ function(EventEmitter2) {
24
+ var DevMiddleware = /* @__PURE__ */ function(EventEmitter1) {
23
25
  "use strict";
24
- _inherits(DevMiddleware2, EventEmitter2);
26
+ _inherits(DevMiddleware2, EventEmitter1);
25
27
  var _super = _create_super(DevMiddleware2);
26
28
  function DevMiddleware2(param) {
27
29
  var dev = param.dev, devMiddleware = param.devMiddleware;
@@ -6,6 +6,7 @@ import { AGGRED_DIR } from "@modern-js/prod-server";
6
6
  import getMockData, { getMatched } from "./getMockData";
7
7
  export var createMockHandler = function(param) {
8
8
  var pwd = param.pwd;
9
+ var _config;
9
10
  var exts = [
10
11
  ".ts",
11
12
  ".js"
@@ -40,7 +41,7 @@ export var createMockHandler = function(param) {
40
41
  }
41
42
  var mod = compatRequire(filepath, false);
42
43
  var tmp = mod.default, mockModule = tmp === void 0 ? mod : tmp, config = mod.config;
43
- if ((config === null || config === void 0 ? void 0 : config.enable) === false) {
44
+ if (((_config = config) === null || _config === void 0 ? void 0 : _config.enable) === false) {
44
45
  return null;
45
46
  }
46
47
  if (!mockModule) {
@@ -52,9 +53,9 @@ export var createMockHandler = function(param) {
52
53
  }
53
54
  return function() {
54
55
  var _ref = _async_to_generator(function(context, next) {
55
- var enableMock, res, matched;
56
+ var _config2, enableMock, res, matched;
56
57
  return _ts_generator(this, function(_state) {
57
- if (typeof (config === null || config === void 0 ? void 0 : config.enable) === "function") {
58
+ if (typeof ((_config2 = config) === null || _config2 === void 0 ? void 0 : _config2.enable) === "function") {
58
59
  enableMock = config.enable(context.req, context.res);
59
60
  if (!enableMock) {
60
61
  return [
@@ -14,16 +14,17 @@ import { createServer } from "http";
14
14
  import path from "path";
15
15
  import { createServer as createHttpsServer } from "https";
16
16
  import { API_DIR, SERVER_BUNDLE_DIRECTORY, SERVER_DIR, SHARED_DIR, LOADABLE_STATS_FILE } from "@modern-js/utils";
17
- import { createProxyHandler, ModernServer, AGGRED_DIR } from "@modern-js/prod-server";
17
+ import { createProxyHandler, ModernServer, AGGRED_DIR, createRenderHandler } from "@modern-js/prod-server";
18
18
  import { merge as deepMerge } from "@modern-js/utils/lodash";
19
19
  import { getDefaultDevOptions } from "../constants";
20
20
  import { createMockHandler } from "../dev-tools/mock";
21
21
  import { enableRegister } from "../dev-tools/register";
22
22
  import Watcher, { mergeWatchOptions } from "../dev-tools/watcher";
23
23
  import DevMiddleware from "../dev-tools/dev-middleware";
24
- export var ModernDevServer = /* @__PURE__ */ function(ModernServer2) {
24
+ import { workerSSRRender } from "./workerSSRRender";
25
+ export var ModernDevServer = /* @__PURE__ */ function(ModernServer1) {
25
26
  "use strict";
26
- _inherits(ModernDevServer2, ModernServer2);
27
+ _inherits(ModernDevServer2, ModernServer1);
27
28
  var _super = _create_super(ModernDevServer2);
28
29
  function ModernDevServer2(options) {
29
30
  _class_call_check(this, ModernDevServer2);
@@ -31,11 +32,13 @@ export var ModernDevServer = /* @__PURE__ */ function(ModernServer2) {
31
32
  _this = _super.call(this, options);
32
33
  _define_property(_assert_this_initialized(_this), "mockHandler", null);
33
34
  _define_property(_assert_this_initialized(_this), "dev", void 0);
35
+ _define_property(_assert_this_initialized(_this), "useWorkerSSR", void 0);
34
36
  _define_property(_assert_this_initialized(_this), "appContext", void 0);
35
37
  _define_property(_assert_this_initialized(_this), "devMiddleware", void 0);
36
38
  _define_property(_assert_this_initialized(_this), "watcher", void 0);
37
39
  _this.appContext = options.appContext;
38
40
  _this.workDir = _this.pwd;
41
+ _this.useWorkerSSR = Boolean(options.useWorkerSSR);
39
42
  _this.dev = _this.getDevOptions(options);
40
43
  _this.devMiddleware = new DevMiddleware({
41
44
  dev: _this.dev,
@@ -172,6 +175,29 @@ export var ModernDevServer = /* @__PURE__ */ function(ModernServer2) {
172
175
  }
173
176
  )
174
177
  },
178
+ {
179
+ key: "getRenderHandler",
180
+ value: (
181
+ // override the ModernServer renderHandler logic
182
+ function getRenderHandler() {
183
+ if (this.useWorkerSSR) {
184
+ var _this_conf_server, _conf_security;
185
+ var _this = this, distDir = _this.distDir, staticGenerate = _this.staticGenerate, conf = _this.conf, metaName = _this.metaName;
186
+ var ssrConfig = (_this_conf_server = this.conf.server) === null || _this_conf_server === void 0 ? void 0 : _this_conf_server.ssr;
187
+ var forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
188
+ return createRenderHandler({
189
+ ssrRender: workerSSRRender,
190
+ distDir: distDir,
191
+ staticGenerate: staticGenerate,
192
+ forceCSR: forceCSR,
193
+ nonce: (_conf_security = conf.security) === null || _conf_security === void 0 ? void 0 : _conf_security.nonce,
194
+ metaName: metaName
195
+ });
196
+ }
197
+ return _get(_get_prototype_of(ModernDevServer2.prototype), "getRenderHandler", this).call(this);
198
+ }
199
+ )
200
+ },
175
201
  {
176
202
  key: "applyDefaultMiddlewares",
177
203
  value: function applyDefaultMiddlewares(app) {
@@ -183,7 +209,8 @@ export var ModernDevServer = /* @__PURE__ */ function(ModernServer2) {
183
209
  case 0:
184
210
  pwd = _this.pwd, dev = _this.dev, devMiddleware = _this.devMiddleware;
185
211
  isUseStreamingSSR = function(routes) {
186
- return routes === null || routes === void 0 ? void 0 : routes.some(function(r) {
212
+ var _routes;
213
+ return (_routes = routes) === null || _routes === void 0 ? void 0 : _routes.some(function(r) {
187
214
  return r.isStream === true;
188
215
  });
189
216
  };
@@ -358,7 +385,7 @@ export var ModernDevServer = /* @__PURE__ */ function(ModernServer2) {
358
385
  key: "initReader",
359
386
  value: function initReader() {
360
387
  var _this = this;
361
- var _this_dev, _this_dev_devMiddleware;
388
+ var _this_dev_devMiddleware, _this_dev;
362
389
  var isInit = false;
363
390
  if (this.devMiddleware && ((_this_dev = this.dev) === null || _this_dev === void 0 ? void 0 : (_this_dev_devMiddleware = _this_dev.devMiddleware) === null || _this_dev_devMiddleware === void 0 ? void 0 : _this_dev_devMiddleware.writeToDisk) === false) {
364
391
  this.addHandler(function(ctx, next) {
@@ -560,11 +587,11 @@ export var ModernDevServer = /* @__PURE__ */ function(ModernServer2) {
560
587
  key: "startWatcher",
561
588
  value: function startWatcher() {
562
589
  var _this = this;
563
- var _this_conf_server;
590
+ var _appContext, _appContext1, _this_conf_server;
564
591
  var _this1 = this, pwd = _this1.pwd, distDir = _this1.distDir, appContext = _this1.appContext;
565
592
  var mock = AGGRED_DIR.mock;
566
- var apiDir = (appContext === null || appContext === void 0 ? void 0 : appContext.apiDirectory) || API_DIR;
567
- var sharedDir = (appContext === null || appContext === void 0 ? void 0 : appContext.sharedDirectory) || SHARED_DIR;
593
+ var apiDir = ((_appContext = appContext) === null || _appContext === void 0 ? void 0 : _appContext.apiDirectory) || API_DIR;
594
+ var sharedDir = ((_appContext1 = appContext) === null || _appContext1 === void 0 ? void 0 : _appContext1.sharedDirectory) || SHARED_DIR;
568
595
  var defaultWatched = [
569
596
  "".concat(mock, "/**/*"),
570
597
  "".concat(SERVER_DIR, "/**/*"),
@@ -6,9 +6,9 @@ import { ModernDevServer } from "./devServer";
6
6
  var createDevServer = function(options) {
7
7
  return new ModernDevServer(options);
8
8
  };
9
- export var DevServer = /* @__PURE__ */ function(Server2) {
9
+ export var DevServer = /* @__PURE__ */ function(Server1) {
10
10
  "use strict";
11
- _inherits(DevServer2, Server2);
11
+ _inherits(DevServer2, Server1);
12
12
  var _super = _create_super(DevServer2);
13
13
  function DevServer2(options) {
14
14
  _class_call_check(this, DevServer2);
@@ -0,0 +1,40 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
+ import axios from "axios";
4
+ import { mime } from "@modern-js/utils";
5
+ var PORT = 9230;
6
+ export function workerSSRRender(ctx, renderOptions, _runner) {
7
+ return _workerSSRRender.apply(this, arguments);
8
+ }
9
+ function _workerSSRRender() {
10
+ _workerSSRRender = _async_to_generator(function(ctx, renderOptions, _runner) {
11
+ var headers, params, urlPath, url, resposne;
12
+ return _ts_generator(this, function(_state) {
13
+ switch (_state.label) {
14
+ case 0:
15
+ headers = ctx.headers, params = ctx.params;
16
+ urlPath = renderOptions.urlPath;
17
+ url = "http://0.0.0.0:".concat(PORT, "/").concat(urlPath);
18
+ return [
19
+ 4,
20
+ axios.get(url, {
21
+ timeout: 5e3,
22
+ responseType: "text",
23
+ headers: headers,
24
+ params: params
25
+ })
26
+ ];
27
+ case 1:
28
+ resposne = _state.sent();
29
+ return [
30
+ 2,
31
+ {
32
+ content: resposne.data,
33
+ contentType: mime.contentType("html")
34
+ }
35
+ ];
36
+ }
37
+ });
38
+ });
39
+ return _workerSSRRender.apply(this, arguments);
40
+ }
@@ -104,6 +104,7 @@ function canApplyUpdates() {
104
104
  return module.hot.status() === "idle";
105
105
  }
106
106
  function tryApplyUpdates() {
107
+ var _result;
107
108
  if (!module.hot) {
108
109
  window.location.reload();
109
110
  return;
@@ -126,7 +127,7 @@ function tryApplyUpdates() {
126
127
  true,
127
128
  handleApplyUpdates
128
129
  );
129
- if (result === null || result === void 0 ? void 0 : result.then) {
130
+ if ((_result = result) === null || _result === void 0 ? void 0 : _result.then) {
130
131
  result.then((updatedModules) => {
131
132
  handleApplyUpdates(null, updatedModules);
132
133
  }, (err) => {
@@ -1,15 +1,17 @@
1
1
  import { _ as _define_property } from "@swc/helpers/_/_define_property";
2
+ import { sep } from "path";
2
3
  import { EventEmitter } from "events";
3
4
  import SocketServer from "./socketServer";
4
5
  const noop = () => {
5
6
  };
6
7
  function getHMRClientPath(client) {
7
- const protocol = (client === null || client === void 0 ? void 0 : client.protocol) ? `&protocol=${client.protocol}` : "";
8
- const host = (client === null || client === void 0 ? void 0 : client.host) ? `&host=${client.host}` : "";
9
- const path = (client === null || client === void 0 ? void 0 : client.path) ? `&path=${client.path}` : "";
10
- const port = (client === null || client === void 0 ? void 0 : client.port) ? `&port=${client.port}` : "";
8
+ var _client, _client1, _client2, _client3;
9
+ const protocol = ((_client = client) === null || _client === void 0 ? void 0 : _client.protocol) ? `&protocol=${client.protocol}` : "";
10
+ const host = ((_client1 = client) === null || _client1 === void 0 ? void 0 : _client1.host) ? `&host=${client.host}` : "";
11
+ const path = ((_client2 = client) === null || _client2 === void 0 ? void 0 : _client2.path) ? `&path=${client.path}` : "";
12
+ const port = ((_client3 = client) === null || _client3 === void 0 ? void 0 : _client3.port) ? `&port=${client.port}` : "";
11
13
  const clientEntry = `${require.resolve("@modern-js/server/hmr-client")}?${host}${path}${port}${protocol}`;
12
- return clientEntry;
14
+ return clientEntry.replace(`${sep}cjs${sep}dev-tools`, `${sep}esm${sep}dev-tools`);
13
15
  }
14
16
  class DevMiddleware extends EventEmitter {
15
17
  init(app) {
@@ -3,6 +3,7 @@ import { compatRequire, fs } from "@modern-js/utils";
3
3
  import { AGGRED_DIR } from "@modern-js/prod-server";
4
4
  import getMockData, { getMatched } from "./getMockData";
5
5
  export const createMockHandler = ({ pwd }) => {
6
+ var _config;
6
7
  const exts = [
7
8
  ".ts",
8
9
  ".js"
@@ -20,7 +21,7 @@ export const createMockHandler = ({ pwd }) => {
20
21
  }
21
22
  const mod = compatRequire(filepath, false);
22
23
  const { default: mockModule = mod, config } = mod;
23
- if ((config === null || config === void 0 ? void 0 : config.enable) === false) {
24
+ if (((_config = config) === null || _config === void 0 ? void 0 : _config.enable) === false) {
24
25
  return null;
25
26
  }
26
27
  if (!mockModule) {
@@ -31,7 +32,8 @@ export const createMockHandler = ({ pwd }) => {
31
32
  return null;
32
33
  }
33
34
  return async (context, next) => {
34
- if (typeof (config === null || config === void 0 ? void 0 : config.enable) === "function") {
35
+ var _config2;
36
+ if (typeof ((_config2 = config) === null || _config2 === void 0 ? void 0 : _config2.enable) === "function") {
35
37
  const enableMock = config.enable(context.req, context.res);
36
38
  if (!enableMock) {
37
39
  return next();
@@ -3,13 +3,14 @@ import { createServer } from "http";
3
3
  import path from "path";
4
4
  import { createServer as createHttpsServer } from "https";
5
5
  import { API_DIR, SERVER_BUNDLE_DIRECTORY, SERVER_DIR, SHARED_DIR, LOADABLE_STATS_FILE } from "@modern-js/utils";
6
- import { createProxyHandler, ModernServer, AGGRED_DIR } from "@modern-js/prod-server";
6
+ import { createProxyHandler, ModernServer, AGGRED_DIR, createRenderHandler } from "@modern-js/prod-server";
7
7
  import { merge as deepMerge } from "@modern-js/utils/lodash";
8
8
  import { getDefaultDevOptions } from "../constants";
9
9
  import { createMockHandler } from "../dev-tools/mock";
10
10
  import { enableRegister } from "../dev-tools/register";
11
11
  import Watcher, { mergeWatchOptions } from "../dev-tools/watcher";
12
12
  import DevMiddleware from "../dev-tools/dev-middleware";
13
+ import { workerSSRRender } from "./workerSSRRender";
13
14
  export class ModernDevServer extends ModernServer {
14
15
  getDevOptions(options) {
15
16
  const devOptions = typeof options.dev === "boolean" ? {} : options.dev;
@@ -67,10 +68,29 @@ export class ModernDevServer extends ModernServer {
67
68
  });
68
69
  }
69
70
  }
71
+ // override the ModernServer renderHandler logic
72
+ getRenderHandler() {
73
+ if (this.useWorkerSSR) {
74
+ var _this_conf_server, _conf_security;
75
+ const { distDir, staticGenerate, conf, metaName } = this;
76
+ const ssrConfig = (_this_conf_server = this.conf.server) === null || _this_conf_server === void 0 ? void 0 : _this_conf_server.ssr;
77
+ const forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
78
+ return createRenderHandler({
79
+ ssrRender: workerSSRRender,
80
+ distDir,
81
+ staticGenerate,
82
+ forceCSR,
83
+ nonce: (_conf_security = conf.security) === null || _conf_security === void 0 ? void 0 : _conf_security.nonce,
84
+ metaName
85
+ });
86
+ }
87
+ return super.getRenderHandler();
88
+ }
70
89
  async applyDefaultMiddlewares(app) {
71
90
  const { pwd, dev, devMiddleware } = this;
72
91
  const isUseStreamingSSR = (routes) => {
73
- return routes === null || routes === void 0 ? void 0 : routes.some((r) => r.isStream === true);
92
+ var _routes;
93
+ return (_routes = routes) === null || _routes === void 0 ? void 0 : _routes.some((r) => r.isStream === true);
74
94
  };
75
95
  if (!isUseStreamingSSR(this.getRoutes()) && dev.compress) {
76
96
  const { default: compression } = await import("http-compression");
@@ -157,7 +177,7 @@ export class ModernDevServer extends ModernServer {
157
177
  warmupSSRBundle() {
158
178
  }
159
179
  initReader() {
160
- var _this_dev, _this_dev_devMiddleware;
180
+ var _this_dev_devMiddleware, _this_dev;
161
181
  let isInit = false;
162
182
  if (this.devMiddleware && ((_this_dev = this.dev) === null || _this_dev === void 0 ? void 0 : (_this_dev_devMiddleware = _this_dev.devMiddleware) === null || _this_dev_devMiddleware === void 0 ? void 0 : _this_dev_devMiddleware.writeToDisk) === false) {
163
183
  this.addHandler((ctx, next) => {
@@ -252,11 +272,11 @@ export class ModernDevServer extends ModernServer {
252
272
  }
253
273
  }
254
274
  startWatcher() {
255
- var _this_conf_server;
275
+ var _appContext, _appContext1, _this_conf_server;
256
276
  const { pwd, distDir, appContext } = this;
257
277
  const { mock } = AGGRED_DIR;
258
- const apiDir = (appContext === null || appContext === void 0 ? void 0 : appContext.apiDirectory) || API_DIR;
259
- const sharedDir = (appContext === null || appContext === void 0 ? void 0 : appContext.sharedDirectory) || SHARED_DIR;
278
+ const apiDir = ((_appContext = appContext) === null || _appContext === void 0 ? void 0 : _appContext.apiDirectory) || API_DIR;
279
+ const sharedDir = ((_appContext1 = appContext) === null || _appContext1 === void 0 ? void 0 : _appContext1.sharedDirectory) || SHARED_DIR;
260
280
  const defaultWatched = [
261
281
  `${mock}/**/*`,
262
282
  `${SERVER_DIR}/**/*`,
@@ -290,11 +310,13 @@ export class ModernDevServer extends ModernServer {
290
310
  super(options);
291
311
  _define_property(this, "mockHandler", null);
292
312
  _define_property(this, "dev", void 0);
313
+ _define_property(this, "useWorkerSSR", void 0);
293
314
  _define_property(this, "appContext", void 0);
294
315
  _define_property(this, "devMiddleware", void 0);
295
316
  _define_property(this, "watcher", void 0);
296
317
  this.appContext = options.appContext;
297
318
  this.workDir = this.pwd;
319
+ this.useWorkerSSR = Boolean(options.useWorkerSSR);
298
320
  this.dev = this.getDevOptions(options);
299
321
  this.devMiddleware = new DevMiddleware({
300
322
  dev: this.dev,
@@ -0,0 +1,18 @@
1
+ import axios from "axios";
2
+ import { mime } from "@modern-js/utils";
3
+ const PORT = 9230;
4
+ export async function workerSSRRender(ctx, renderOptions, _runner) {
5
+ const { headers, params } = ctx;
6
+ const { urlPath } = renderOptions;
7
+ const url = `http://0.0.0.0:${PORT}/${urlPath}`;
8
+ const resposne = await axios.get(url, {
9
+ timeout: 5e3,
10
+ responseType: "text",
11
+ headers,
12
+ params
13
+ });
14
+ return {
15
+ content: resposne.data,
16
+ contentType: mime.contentType("html")
17
+ };
18
+ }
@@ -3,11 +3,13 @@
3
3
  import { IncomingMessage, Server, ServerResponse } from 'http';
4
4
  import { NextFunction, ServerHookRunner, ModernServer, BuildOptions } from '@modern-js/prod-server';
5
5
  import type { ModernServerContext } from '@modern-js/types';
6
+ import { RenderHandler } from '@modern-js/prod-server/src/libs/render';
6
7
  import { WatchEvent } from '../dev-tools/watcher';
7
8
  import type { ModernDevServerOptions } from '../types';
8
9
  export declare class ModernDevServer extends ModernServer {
9
10
  private mockHandler;
10
11
  private readonly dev;
12
+ private readonly useWorkerSSR;
11
13
  private readonly appContext;
12
14
  private readonly devMiddleware;
13
15
  private watcher?;
@@ -16,6 +18,7 @@ export declare class ModernDevServer extends ModernServer {
16
18
  private addMiddlewareHandler;
17
19
  private applySetupMiddlewares;
18
20
  onInit(runner: ServerHookRunner, app: Server): Promise<void>;
21
+ getRenderHandler(): RenderHandler;
19
22
  private applyDefaultMiddlewares;
20
23
  onRepack(options?: BuildOptions): void;
21
24
  createHTTPServer(handler: (req: IncomingMessage, res: ServerResponse, next?: () => void) => void): Promise<Server<typeof IncomingMessage, typeof ServerResponse> | import("https").Server<typeof IncomingMessage, typeof ServerResponse>>;
@@ -0,0 +1,9 @@
1
+ import { ServerHookRunner } from '@modern-js/prod-server';
2
+ import { ModernServerContext } from '@modern-js/types/server';
3
+ export declare function workerSSRRender(ctx: ModernServerContext, renderOptions: {
4
+ urlPath: string;
5
+ [props: string]: any;
6
+ }, _runner: ServerHookRunner): Promise<{
7
+ content: any;
8
+ contentType: string;
9
+ }>;
@@ -33,5 +33,6 @@ export type DevMiddleware = (options: DevMiddlewareOptions) => DevMiddlewareAPI;
33
33
  export type ExtraOptions = {
34
34
  dev: boolean | Partial<DevServerOptions>;
35
35
  devMiddleware?: DevMiddleware;
36
+ useWorkerSSR?: boolean;
36
37
  };
37
38
  export type ModernDevServerOptions = ModernServerOptions & ExtraOptions;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.26.0",
18
+ "version": "2.28.0",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -47,10 +47,11 @@
47
47
  "minimatch": "^3.0.4",
48
48
  "path-to-regexp": "^6.2.0",
49
49
  "ws": "^8.2.0",
50
- "@modern-js/prod-server": "2.26.0",
51
- "@modern-js/server-utils": "2.26.0",
52
- "@modern-js/types": "2.26.0",
53
- "@modern-js/utils": "2.26.0"
50
+ "axios": "^1.2.1",
51
+ "@modern-js/prod-server": "2.28.0",
52
+ "@modern-js/server-utils": "2.28.0",
53
+ "@modern-js/types": "2.28.0",
54
+ "@modern-js/utils": "2.28.0"
54
55
  },
55
56
  "devDependencies": {
56
57
  "@types/connect-history-api-fallback": "^1.3.5",
@@ -65,9 +66,9 @@
65
66
  "typescript": "^5",
66
67
  "webpack": "^5.88.1",
67
68
  "websocket": "^1",
68
- "@modern-js/server-core": "2.26.0",
69
- "@scripts/build": "2.26.0",
70
- "@scripts/jest-config": "2.26.0"
69
+ "@modern-js/server-core": "2.28.0",
70
+ "@scripts/build": "2.28.0",
71
+ "@scripts/jest-config": "2.28.0"
71
72
  },
72
73
  "peerDependencies": {
73
74
  "devcert": "^1.0.0",