@modern-js/server-core 2.58.0 → 2.58.2-alpha.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.
@@ -34,9 +34,9 @@ module.exports = __toCommonJS(loadCache_exports);
34
34
  var import_path = __toESM(require("path"));
35
35
  var import_utils = require("@modern-js/utils");
36
36
  const CACHE_FILENAME = "cache";
37
- function loadCacheConfig(pwd) {
37
+ async function loadCacheConfig(pwd) {
38
38
  const serverCacheFilepath = import_path.default.resolve(pwd, import_utils.SERVER_DIR, CACHE_FILENAME);
39
- const mod = (0, import_utils.requireExistModule)(serverCacheFilepath, {
39
+ const mod = await (0, import_utils.requireExistModule)(serverCacheFilepath, {
40
40
  interop: false
41
41
  });
42
42
  if (mod === null || mod === void 0 ? void 0 : mod.cacheOption) {
@@ -34,30 +34,31 @@ __export(loadConfig_exports, {
34
34
  module.exports = __toCommonJS(loadConfig_exports);
35
35
  var import_path = __toESM(require("path"));
36
36
  var import_utils = require("@modern-js/utils");
37
- const requireConfig = (serverConfigPath) => {
37
+ var import_flatted = require("flatted");
38
+ const requireConfig = async (serverConfigPath) => {
38
39
  if (import_utils.fs.pathExistsSync(serverConfigPath)) {
39
- return (0, import_utils.compatRequire)(serverConfigPath);
40
+ return (0, import_utils.compatibleRequire)(serverConfigPath);
40
41
  }
41
42
  return void 0;
42
43
  };
43
- function loadServerConfigNew(serverConfigPath) {
44
- const mod = (0, import_utils.requireExistModule)(serverConfigPath);
44
+ async function loadServerConfigNew(serverConfigPath) {
45
+ const mod = await (0, import_utils.requireExistModule)(serverConfigPath);
45
46
  if (mod) {
46
47
  return mod;
47
48
  }
48
49
  return void 0;
49
50
  }
50
- function loadServerConfigOld(pwd, configFile) {
51
- const serverConfigPath = import_path.default.join(pwd, `${configFile}.js`);
52
- const serverConfig = requireConfig(serverConfigPath);
51
+ async function loadServerConfigOld(pwd, configFile) {
52
+ const serverConfigPath = import_path.default.join(pwd, `${configFile}.cjs`);
53
+ const serverConfig = await requireConfig(serverConfigPath);
53
54
  return serverConfig;
54
55
  }
55
- function loadServerRuntimeConfig(pwd, oldServerFile = import_utils.DEFAULT_SERVER_CONFIG, newServerConfigPath) {
56
- const newServerConfig = newServerConfigPath && loadServerConfigNew(newServerConfigPath);
56
+ async function loadServerRuntimeConfig(pwd, oldServerFile = import_utils.DEFAULT_SERVER_CONFIG, newServerConfigPath) {
57
+ const newServerConfig = newServerConfigPath && await loadServerConfigNew(newServerConfigPath);
57
58
  if (newServerConfig) {
58
59
  return newServerConfig;
59
60
  }
60
- const oldServerConfig = loadServerConfigOld(pwd, oldServerFile);
61
+ const oldServerConfig = await loadServerConfigOld(pwd, oldServerFile);
61
62
  return oldServerConfig;
62
63
  }
63
64
  function loadServerCliConfig(pwd, defaultConfig = {}) {
@@ -75,7 +76,8 @@ function loadServerCliConfig(pwd, defaultConfig = {}) {
75
76
  dev: {}
76
77
  };
77
78
  try {
78
- cliConfig = require(cliConfigPath);
79
+ const content = import_utils.fs.readFileSync(cliConfigPath, "utf-8");
80
+ cliConfig = (0, import_flatted.parse)(content);
79
81
  } catch (_2) {
80
82
  }
81
83
  const mergedCliConfig = import_utils.lodash.merge(defaultConfig, cliConfig);
@@ -22,15 +22,15 @@ __export(loadPlugin_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(loadPlugin_exports);
24
24
  var import_utils = require("@modern-js/utils");
25
- function resolveServerPlugin(plugin, appDirectory) {
25
+ async function resolveServerPlugin(plugin, appDirectory) {
26
26
  const { name, options } = plugin;
27
27
  const pluginPath = (0, import_utils.tryResolve)(name, appDirectory);
28
- const module2 = (0, import_utils.compatRequire)(pluginPath);
28
+ const module2 = await (0, import_utils.compatibleRequire)(pluginPath);
29
29
  const pluginInstance = module2(options);
30
30
  return pluginInstance;
31
31
  }
32
- function loadServerPlugins(serverPlugins, appDirectory) {
33
- const instances = serverPlugins.map((plugin) => resolveServerPlugin(plugin, appDirectory));
32
+ async function loadServerPlugins(serverPlugins, appDirectory) {
33
+ const instances = await Promise.all(serverPlugins.map((plugin) => resolveServerPlugin(plugin, appDirectory)));
34
34
  return instances;
35
35
  }
36
36
  // Annotate the CommonJS export names for ESM import in node:
@@ -128,10 +128,10 @@ const createNodeServer = async (requestHandler, httpsOptions) => {
128
128
  const requestListener = getRequestListener(requestHandler);
129
129
  let nodeServer;
130
130
  if (httpsOptions) {
131
- const { createServer } = await Promise.resolve().then(() => __toESM(require("node:https")));
131
+ const { createServer } = await import("node:https");
132
132
  nodeServer = createServer(httpsOptions, requestListener);
133
133
  } else {
134
- const { createServer } = await Promise.resolve().then(() => __toESM(require("node:http")));
134
+ const { createServer } = await import("node:http");
135
135
  nodeServer = createServer(requestListener);
136
136
  }
137
137
  nodeServer.getRequestListener = () => requestListener;
@@ -64,22 +64,17 @@ function injectTemplates(pwd, routes) {
64
64
  await next();
65
65
  };
66
66
  }
67
- const dynamicImport = (filePath) => {
68
- try {
69
- const module2 = require(filePath);
70
- return Promise.resolve(module2);
71
- } catch (e) {
72
- return Promise.reject(e);
73
- }
74
- };
75
67
  const loadBundle = async (filepath, logger) => {
76
68
  if (!await import_utils.fs.pathExists(filepath)) {
77
69
  return void 0;
78
70
  }
79
- return dynamicImport(filepath).catch((e) => {
80
- logger === null || logger === void 0 ? void 0 : logger.error(`Load ${filepath} bundle failed, error = %s`, e instanceof Error ? e.stack || e.message : e);
71
+ try {
72
+ const module2 = await (0, import_utils.compatibleRequire)(filepath, false);
73
+ return module2;
74
+ } catch (e) {
75
+ logger.error(`Load ${filepath} bundle failed, error = %s`, e instanceof Error ? e.stack || e.message : e);
81
76
  return void 0;
82
- });
77
+ }
83
78
  };
84
79
  async function getServerManifest(pwd, routes, logger) {
85
80
  const loaderBundles = {};
@@ -94,11 +89,11 @@ async function getServerManifest(pwd, routes, logger) {
94
89
  loaderBundle && (loaderBundles[entryName] = loaderBundle);
95
90
  }));
96
91
  const loadableUri = import_path.default.join(pwd, import_utils.LOADABLE_STATS_FILE);
97
- const loadableStats = await Promise.resolve().then(() => __toESM(require(loadableUri))).catch((_) => ({}));
92
+ const loadableStats = await (0, import_utils.compatibleRequire)(loadableUri).catch((_) => ({}));
98
93
  const routesManifestUri = import_path.default.join(pwd, import_utils.ROUTE_MANIFEST_FILE);
99
- const routeManifest = await Promise.resolve().then(() => __toESM(require(routesManifestUri))).catch((_) => ({}));
94
+ const routeManifest = await (0, import_utils.compatibleRequire)(routesManifestUri).catch((_) => ({}));
100
95
  const nestedRoutesJsonPath = import_path.default.join(pwd, import_utils.NESTED_ROUTE_SPEC_FILE);
101
- const nestedRoutesJson = await Promise.resolve().then(() => __toESM(require(nestedRoutesJsonPath))).catch((_) => ({}));
96
+ const nestedRoutesJson = await import(nestedRoutesJsonPath).catch((_) => ({}));
102
97
  return {
103
98
  loaderBundles,
104
99
  renderBundles,
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
  var customServer_exports = {};
30
20
  __export(customServer_exports, {
@@ -169,14 +159,14 @@ function isRedirect(headers, code) {
169
159
  }
170
160
  async function createMiddlewareContextFromHono(c) {
171
161
  const loaderContext = (0, import_helper.getLoaderCtx)(c);
172
- let rawRequest = c.req.raw;
162
+ const rawRequest = c.req.raw;
173
163
  const method = rawRequest.method.toUpperCase();
174
164
  if (![
175
165
  "GET",
176
166
  "HEAD"
177
167
  ].includes(method) && !rawRequest.body && c.env.node.req) {
178
168
  const streamPath = "../../adapters/node/polyfills/stream";
179
- const { createReadableStreamFromReadable } = await Promise.resolve().then(() => __toESM(require(streamPath)));
169
+ const { createReadableStreamFromReadable } = await import(streamPath);
180
170
  const init = {
181
171
  body: createReadableStreamFromReadable(c.env.node.req),
182
172
  headers: rawRequest.headers,
@@ -184,11 +174,14 @@ async function createMiddlewareContextFromHono(c) {
184
174
  method: rawRequest.method
185
175
  };
186
176
  init.duplex = "half";
187
- rawRequest = new Request(rawRequest.url, init);
177
+ c.req.raw = new Request(rawRequest.url, init);
188
178
  }
189
179
  return {
190
180
  get request() {
191
- return rawRequest;
181
+ return c.req.raw;
182
+ },
183
+ set request(request) {
184
+ c.req.raw = request;
192
185
  },
193
186
  get response() {
194
187
  return c.res;
@@ -164,7 +164,7 @@ async function renderHandler(request, options, mode, onError) {
164
164
  const { nestedRoutesJson } = serverManifest;
165
165
  const routes = nestedRoutesJson === null || nestedRoutesJson === void 0 ? void 0 : nestedRoutesJson[options.routeInfo.entryName];
166
166
  if (routes) {
167
- const { matchRoutes } = await Promise.resolve().then(() => __toESM(require("@modern-js/runtime-utils/remix-router")));
167
+ const { matchRoutes } = await import("@modern-js/runtime-utils/remix-router");
168
168
  const url = new URL(request.url);
169
169
  const matchedRoutes = matchRoutes(routes, url.pathname, options.routeInfo.urlPath);
170
170
  if (!matchedRoutes) {
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
  var warmup_exports = {};
30
20
  __export(warmup_exports, {
@@ -33,7 +23,7 @@ __export(warmup_exports, {
33
23
  module.exports = __toCommonJS(warmup_exports);
34
24
  function warmup(bundles) {
35
25
  bundles.forEach((bundle) => {
36
- bundle && Promise.resolve().then(() => __toESM(require(bundle))).catch((_) => {
26
+ bundle && import(bundle).catch((_) => {
37
27
  });
38
28
  });
39
29
  }
@@ -1,18 +1,43 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
1
3
  import path from "path";
2
4
  import { SERVER_DIR, requireExistModule } from "@modern-js/utils";
3
5
  var CACHE_FILENAME = "cache";
4
6
  function loadCacheConfig(pwd) {
5
- var serverCacheFilepath = path.resolve(pwd, SERVER_DIR, CACHE_FILENAME);
6
- var mod = requireExistModule(serverCacheFilepath, {
7
- interop: false
7
+ return _loadCacheConfig.apply(this, arguments);
8
+ }
9
+ function _loadCacheConfig() {
10
+ _loadCacheConfig = _async_to_generator(function(pwd) {
11
+ var serverCacheFilepath, mod;
12
+ return _ts_generator(this, function(_state) {
13
+ switch (_state.label) {
14
+ case 0:
15
+ serverCacheFilepath = path.resolve(pwd, SERVER_DIR, CACHE_FILENAME);
16
+ return [
17
+ 4,
18
+ requireExistModule(serverCacheFilepath, {
19
+ interop: false
20
+ })
21
+ ];
22
+ case 1:
23
+ mod = _state.sent();
24
+ if (mod === null || mod === void 0 ? void 0 : mod.cacheOption) {
25
+ return [
26
+ 2,
27
+ {
28
+ strategy: mod.cacheOption,
29
+ container: mod.customContainer
30
+ }
31
+ ];
32
+ }
33
+ return [
34
+ 2,
35
+ void 0
36
+ ];
37
+ }
38
+ });
8
39
  });
9
- if (mod === null || mod === void 0 ? void 0 : mod.cacheOption) {
10
- return {
11
- strategy: mod.cacheOption,
12
- container: mod.customContainer
13
- };
14
- }
15
- return void 0;
40
+ return _loadCacheConfig.apply(this, arguments);
16
41
  }
17
42
  export {
18
43
  loadCacheConfig
@@ -1,31 +1,128 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
1
3
  import path from "path";
2
- import { compatRequire, fs, DEFAULT_SERVER_CONFIG, requireExistModule, ensureAbsolutePath, OUTPUT_CONFIG_FILE, lodash as _ } from "@modern-js/utils";
3
- var requireConfig = function(serverConfigPath) {
4
- if (fs.pathExistsSync(serverConfigPath)) {
5
- return compatRequire(serverConfigPath);
6
- }
7
- return void 0;
8
- };
4
+ import { fs, DEFAULT_SERVER_CONFIG, requireExistModule, ensureAbsolutePath, OUTPUT_CONFIG_FILE, lodash as _, compatibleRequire } from "@modern-js/utils";
5
+ import { parse } from "flatted";
6
+ var requireConfig = function() {
7
+ var _ref = _async_to_generator(function(serverConfigPath) {
8
+ return _ts_generator(this, function(_state) {
9
+ if (fs.pathExistsSync(serverConfigPath)) {
10
+ return [
11
+ 2,
12
+ compatibleRequire(serverConfigPath)
13
+ ];
14
+ }
15
+ return [
16
+ 2,
17
+ void 0
18
+ ];
19
+ });
20
+ });
21
+ return function requireConfig2(serverConfigPath) {
22
+ return _ref.apply(this, arguments);
23
+ };
24
+ }();
9
25
  function loadServerConfigNew(serverConfigPath) {
10
- var mod = requireExistModule(serverConfigPath);
11
- if (mod) {
12
- return mod;
13
- }
14
- return void 0;
26
+ return _loadServerConfigNew.apply(this, arguments);
27
+ }
28
+ function _loadServerConfigNew() {
29
+ _loadServerConfigNew = _async_to_generator(function(serverConfigPath) {
30
+ var mod;
31
+ return _ts_generator(this, function(_state) {
32
+ switch (_state.label) {
33
+ case 0:
34
+ return [
35
+ 4,
36
+ requireExistModule(serverConfigPath)
37
+ ];
38
+ case 1:
39
+ mod = _state.sent();
40
+ if (mod) {
41
+ return [
42
+ 2,
43
+ mod
44
+ ];
45
+ }
46
+ return [
47
+ 2,
48
+ void 0
49
+ ];
50
+ }
51
+ });
52
+ });
53
+ return _loadServerConfigNew.apply(this, arguments);
15
54
  }
16
55
  function loadServerConfigOld(pwd, configFile) {
17
- var serverConfigPath = path.join(pwd, "".concat(configFile, ".js"));
18
- var serverConfig = requireConfig(serverConfigPath);
19
- return serverConfig;
56
+ return _loadServerConfigOld.apply(this, arguments);
57
+ }
58
+ function _loadServerConfigOld() {
59
+ _loadServerConfigOld = _async_to_generator(function(pwd, configFile) {
60
+ var serverConfigPath, serverConfig;
61
+ return _ts_generator(this, function(_state) {
62
+ switch (_state.label) {
63
+ case 0:
64
+ serverConfigPath = path.join(pwd, "".concat(configFile, ".cjs"));
65
+ return [
66
+ 4,
67
+ requireConfig(serverConfigPath)
68
+ ];
69
+ case 1:
70
+ serverConfig = _state.sent();
71
+ return [
72
+ 2,
73
+ serverConfig
74
+ ];
75
+ }
76
+ });
77
+ });
78
+ return _loadServerConfigOld.apply(this, arguments);
20
79
  }
21
80
  function loadServerRuntimeConfig(pwd) {
22
- var oldServerFile = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : DEFAULT_SERVER_CONFIG, newServerConfigPath = arguments.length > 2 ? arguments[2] : void 0;
23
- var newServerConfig = newServerConfigPath && loadServerConfigNew(newServerConfigPath);
24
- if (newServerConfig) {
25
- return newServerConfig;
26
- }
27
- var oldServerConfig = loadServerConfigOld(pwd, oldServerFile);
28
- return oldServerConfig;
81
+ return _loadServerRuntimeConfig.apply(this, arguments);
82
+ }
83
+ function _loadServerRuntimeConfig() {
84
+ _loadServerRuntimeConfig = _async_to_generator(function(pwd) {
85
+ var oldServerFile, newServerConfigPath, newServerConfig, _tmp, oldServerConfig;
86
+ var _arguments = arguments;
87
+ return _ts_generator(this, function(_state) {
88
+ switch (_state.label) {
89
+ case 0:
90
+ oldServerFile = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : DEFAULT_SERVER_CONFIG, newServerConfigPath = _arguments.length > 2 ? _arguments[2] : void 0;
91
+ _tmp = newServerConfigPath;
92
+ if (!_tmp)
93
+ return [
94
+ 3,
95
+ 2
96
+ ];
97
+ return [
98
+ 4,
99
+ loadServerConfigNew(newServerConfigPath)
100
+ ];
101
+ case 1:
102
+ _tmp = _state.sent();
103
+ _state.label = 2;
104
+ case 2:
105
+ newServerConfig = _tmp;
106
+ if (newServerConfig) {
107
+ return [
108
+ 2,
109
+ newServerConfig
110
+ ];
111
+ }
112
+ return [
113
+ 4,
114
+ loadServerConfigOld(pwd, oldServerFile)
115
+ ];
116
+ case 3:
117
+ oldServerConfig = _state.sent();
118
+ return [
119
+ 2,
120
+ oldServerConfig
121
+ ];
122
+ }
123
+ });
124
+ });
125
+ return _loadServerRuntimeConfig.apply(this, arguments);
29
126
  }
30
127
  function loadServerCliConfig(pwd) {
31
128
  var defaultConfig = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
@@ -43,7 +140,8 @@ function loadServerCliConfig(pwd) {
43
140
  dev: {}
44
141
  };
45
142
  try {
46
- cliConfig = require(cliConfigPath);
143
+ var content = fs.readFileSync(cliConfigPath, "utf-8");
144
+ cliConfig = parse(content);
47
145
  } catch (_2) {
48
146
  }
49
147
  var mergedCliConfig = _.merge(defaultConfig, cliConfig);
@@ -1,16 +1,58 @@
1
- import { compatRequire, tryResolve } from "@modern-js/utils";
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
+ import { compatibleRequire, tryResolve } from "@modern-js/utils";
2
4
  function resolveServerPlugin(plugin, appDirectory) {
3
- var name = plugin.name, options = plugin.options;
4
- var pluginPath = tryResolve(name, appDirectory);
5
- var module = compatRequire(pluginPath);
6
- var pluginInstance = module(options);
7
- return pluginInstance;
5
+ return _resolveServerPlugin.apply(this, arguments);
6
+ }
7
+ function _resolveServerPlugin() {
8
+ _resolveServerPlugin = _async_to_generator(function(plugin, appDirectory) {
9
+ var name, options, pluginPath, module, pluginInstance;
10
+ return _ts_generator(this, function(_state) {
11
+ switch (_state.label) {
12
+ case 0:
13
+ name = plugin.name, options = plugin.options;
14
+ pluginPath = tryResolve(name, appDirectory);
15
+ return [
16
+ 4,
17
+ compatibleRequire(pluginPath)
18
+ ];
19
+ case 1:
20
+ module = _state.sent();
21
+ pluginInstance = module(options);
22
+ return [
23
+ 2,
24
+ pluginInstance
25
+ ];
26
+ }
27
+ });
28
+ });
29
+ return _resolveServerPlugin.apply(this, arguments);
8
30
  }
9
31
  function loadServerPlugins(serverPlugins, appDirectory) {
10
- var instances = serverPlugins.map(function(plugin) {
11
- return resolveServerPlugin(plugin, appDirectory);
32
+ return _loadServerPlugins.apply(this, arguments);
33
+ }
34
+ function _loadServerPlugins() {
35
+ _loadServerPlugins = _async_to_generator(function(serverPlugins, appDirectory) {
36
+ var instances;
37
+ return _ts_generator(this, function(_state) {
38
+ switch (_state.label) {
39
+ case 0:
40
+ return [
41
+ 4,
42
+ Promise.all(serverPlugins.map(function(plugin) {
43
+ return resolveServerPlugin(plugin, appDirectory);
44
+ }))
45
+ ];
46
+ case 1:
47
+ instances = _state.sent();
48
+ return [
49
+ 2,
50
+ instances
51
+ ];
52
+ }
53
+ });
12
54
  });
13
- return instances;
55
+ return _loadServerPlugins.apply(this, arguments);
14
56
  }
15
57
  export {
16
58
  loadServerPlugins
@@ -3,7 +3,7 @@ import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
3
3
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
4
  import path from "path";
5
5
  import { fileReader } from "@modern-js/runtime-utils/fileReader";
6
- import { LOADABLE_STATS_FILE, MAIN_ENTRY_NAME, NESTED_ROUTE_SPEC_FILE, ROUTE_MANIFEST_FILE, SERVER_BUNDLE_DIRECTORY, fs } from "@modern-js/utils";
6
+ import { LOADABLE_STATS_FILE, MAIN_ENTRY_NAME, NESTED_ROUTE_SPEC_FILE, ROUTE_MANIFEST_FILE, SERVER_BUNDLE_DIRECTORY, fs, compatibleRequire } from "@modern-js/utils";
7
7
  function getHtmlTemplates(pwd, routes) {
8
8
  return _getHtmlTemplates.apply(this, arguments);
9
9
  }
@@ -110,16 +110,9 @@ function injectTemplates(pwd, routes) {
110
110
  };
111
111
  }();
112
112
  }
113
- var dynamicImport = function(filePath) {
114
- try {
115
- var module = require(filePath);
116
- return Promise.resolve(module);
117
- } catch (e) {
118
- return Promise.reject(e);
119
- }
120
- };
121
113
  var loadBundle = function() {
122
114
  var _ref = _async_to_generator(function(filepath, logger) {
115
+ var module, e;
123
116
  return _ts_generator(this, function(_state) {
124
117
  switch (_state.label) {
125
118
  case 0:
@@ -134,12 +127,34 @@ var loadBundle = function() {
134
127
  void 0
135
128
  ];
136
129
  }
130
+ _state.label = 2;
131
+ case 2:
132
+ _state.trys.push([
133
+ 2,
134
+ 4,
135
+ ,
136
+ 5
137
+ ]);
138
+ return [
139
+ 4,
140
+ compatibleRequire(filepath, false)
141
+ ];
142
+ case 3:
143
+ module = _state.sent();
137
144
  return [
138
145
  2,
139
- dynamicImport(filepath).catch(function(e) {
140
- logger === null || logger === void 0 ? void 0 : logger.error("Load ".concat(filepath, " bundle failed, error = %s"), _instanceof(e, Error) ? e.stack || e.message : e);
141
- return void 0;
142
- })
146
+ module
147
+ ];
148
+ case 4:
149
+ e = _state.sent();
150
+ logger.error("Load ".concat(filepath, " bundle failed, error = %s"), _instanceof(e, Error) ? e.stack || e.message : e);
151
+ return [
152
+ 2,
153
+ void 0
154
+ ];
155
+ case 5:
156
+ return [
157
+ 2
143
158
  ];
144
159
  }
145
160
  });
@@ -202,7 +217,7 @@ function _getServerManifest() {
202
217
  loadableUri = path.join(pwd, LOADABLE_STATS_FILE);
203
218
  return [
204
219
  4,
205
- import(loadableUri).catch(function(_) {
220
+ compatibleRequire(loadableUri).catch(function(_) {
206
221
  return {};
207
222
  })
208
223
  ];
@@ -211,7 +226,7 @@ function _getServerManifest() {
211
226
  routesManifestUri = path.join(pwd, ROUTE_MANIFEST_FILE);
212
227
  return [
213
228
  4,
214
- import(routesManifestUri).catch(function(_) {
229
+ compatibleRequire(routesManifestUri).catch(function(_) {
215
230
  return {};
216
231
  })
217
232
  ];
@@ -310,14 +310,17 @@ function _createMiddlewareContextFromHono() {
310
310
  method: rawRequest.method
311
311
  };
312
312
  init.duplex = "half";
313
- rawRequest = new Request(rawRequest.url, init);
313
+ c.req.raw = new Request(rawRequest.url, init);
314
314
  _state.label = 2;
315
315
  case 2:
316
316
  return [
317
317
  2,
318
318
  {
319
319
  get request() {
320
- return rawRequest;
320
+ return c.req.raw;
321
+ },
322
+ set request(request) {
323
+ c.req.raw = request;
321
324
  },
322
325
  get response() {
323
326
  return c.res;
@@ -1,9 +1,9 @@
1
1
  import path from "path";
2
2
  import { SERVER_DIR, requireExistModule } from "@modern-js/utils";
3
3
  const CACHE_FILENAME = "cache";
4
- function loadCacheConfig(pwd) {
4
+ async function loadCacheConfig(pwd) {
5
5
  const serverCacheFilepath = path.resolve(pwd, SERVER_DIR, CACHE_FILENAME);
6
- const mod = requireExistModule(serverCacheFilepath, {
6
+ const mod = await requireExistModule(serverCacheFilepath, {
7
7
  interop: false
8
8
  });
9
9
  if (mod === null || mod === void 0 ? void 0 : mod.cacheOption) {
@@ -1,29 +1,30 @@
1
1
  import path from "path";
2
- import { compatRequire, fs, DEFAULT_SERVER_CONFIG, requireExistModule, ensureAbsolutePath, OUTPUT_CONFIG_FILE, lodash as _ } from "@modern-js/utils";
3
- const requireConfig = (serverConfigPath) => {
2
+ import { fs, DEFAULT_SERVER_CONFIG, requireExistModule, ensureAbsolutePath, OUTPUT_CONFIG_FILE, lodash as _, compatibleRequire } from "@modern-js/utils";
3
+ import { parse } from "flatted";
4
+ const requireConfig = async (serverConfigPath) => {
4
5
  if (fs.pathExistsSync(serverConfigPath)) {
5
- return compatRequire(serverConfigPath);
6
+ return compatibleRequire(serverConfigPath);
6
7
  }
7
8
  return void 0;
8
9
  };
9
- function loadServerConfigNew(serverConfigPath) {
10
- const mod = requireExistModule(serverConfigPath);
10
+ async function loadServerConfigNew(serverConfigPath) {
11
+ const mod = await requireExistModule(serverConfigPath);
11
12
  if (mod) {
12
13
  return mod;
13
14
  }
14
15
  return void 0;
15
16
  }
16
- function loadServerConfigOld(pwd, configFile) {
17
- const serverConfigPath = path.join(pwd, `${configFile}.js`);
18
- const serverConfig = requireConfig(serverConfigPath);
17
+ async function loadServerConfigOld(pwd, configFile) {
18
+ const serverConfigPath = path.join(pwd, `${configFile}.cjs`);
19
+ const serverConfig = await requireConfig(serverConfigPath);
19
20
  return serverConfig;
20
21
  }
21
- function loadServerRuntimeConfig(pwd, oldServerFile = DEFAULT_SERVER_CONFIG, newServerConfigPath) {
22
- const newServerConfig = newServerConfigPath && loadServerConfigNew(newServerConfigPath);
22
+ async function loadServerRuntimeConfig(pwd, oldServerFile = DEFAULT_SERVER_CONFIG, newServerConfigPath) {
23
+ const newServerConfig = newServerConfigPath && await loadServerConfigNew(newServerConfigPath);
23
24
  if (newServerConfig) {
24
25
  return newServerConfig;
25
26
  }
26
- const oldServerConfig = loadServerConfigOld(pwd, oldServerFile);
27
+ const oldServerConfig = await loadServerConfigOld(pwd, oldServerFile);
27
28
  return oldServerConfig;
28
29
  }
29
30
  function loadServerCliConfig(pwd, defaultConfig = {}) {
@@ -41,7 +42,8 @@ function loadServerCliConfig(pwd, defaultConfig = {}) {
41
42
  dev: {}
42
43
  };
43
44
  try {
44
- cliConfig = require(cliConfigPath);
45
+ const content = fs.readFileSync(cliConfigPath, "utf-8");
46
+ cliConfig = parse(content);
45
47
  } catch (_2) {
46
48
  }
47
49
  const mergedCliConfig = _.merge(defaultConfig, cliConfig);
@@ -1,13 +1,13 @@
1
- import { compatRequire, tryResolve } from "@modern-js/utils";
2
- function resolveServerPlugin(plugin, appDirectory) {
1
+ import { compatibleRequire, tryResolve } from "@modern-js/utils";
2
+ async function resolveServerPlugin(plugin, appDirectory) {
3
3
  const { name, options } = plugin;
4
4
  const pluginPath = tryResolve(name, appDirectory);
5
- const module = compatRequire(pluginPath);
5
+ const module = await compatibleRequire(pluginPath);
6
6
  const pluginInstance = module(options);
7
7
  return pluginInstance;
8
8
  }
9
- function loadServerPlugins(serverPlugins, appDirectory) {
10
- const instances = serverPlugins.map((plugin) => resolveServerPlugin(plugin, appDirectory));
9
+ async function loadServerPlugins(serverPlugins, appDirectory) {
10
+ const instances = await Promise.all(serverPlugins.map((plugin) => resolveServerPlugin(plugin, appDirectory)));
11
11
  return instances;
12
12
  }
13
13
  export {
@@ -1,6 +1,6 @@
1
1
  import path from "path";
2
2
  import { fileReader } from "@modern-js/runtime-utils/fileReader";
3
- import { LOADABLE_STATS_FILE, MAIN_ENTRY_NAME, NESTED_ROUTE_SPEC_FILE, ROUTE_MANIFEST_FILE, SERVER_BUNDLE_DIRECTORY, fs } from "@modern-js/utils";
3
+ import { LOADABLE_STATS_FILE, MAIN_ENTRY_NAME, NESTED_ROUTE_SPEC_FILE, ROUTE_MANIFEST_FILE, SERVER_BUNDLE_DIRECTORY, fs, compatibleRequire } from "@modern-js/utils";
4
4
  async function getHtmlTemplates(pwd, routes) {
5
5
  const htmls = await Promise.all(routes.map(async (route) => {
6
6
  let html;
@@ -27,22 +27,17 @@ function injectTemplates(pwd, routes) {
27
27
  await next();
28
28
  };
29
29
  }
30
- const dynamicImport = (filePath) => {
31
- try {
32
- const module = require(filePath);
33
- return Promise.resolve(module);
34
- } catch (e) {
35
- return Promise.reject(e);
36
- }
37
- };
38
30
  const loadBundle = async (filepath, logger) => {
39
31
  if (!await fs.pathExists(filepath)) {
40
32
  return void 0;
41
33
  }
42
- return dynamicImport(filepath).catch((e) => {
43
- logger === null || logger === void 0 ? void 0 : logger.error(`Load ${filepath} bundle failed, error = %s`, e instanceof Error ? e.stack || e.message : e);
34
+ try {
35
+ const module = await compatibleRequire(filepath, false);
36
+ return module;
37
+ } catch (e) {
38
+ logger.error(`Load ${filepath} bundle failed, error = %s`, e instanceof Error ? e.stack || e.message : e);
44
39
  return void 0;
45
- });
40
+ }
46
41
  };
47
42
  async function getServerManifest(pwd, routes, logger) {
48
43
  const loaderBundles = {};
@@ -57,9 +52,9 @@ async function getServerManifest(pwd, routes, logger) {
57
52
  loaderBundle && (loaderBundles[entryName] = loaderBundle);
58
53
  }));
59
54
  const loadableUri = path.join(pwd, LOADABLE_STATS_FILE);
60
- const loadableStats = await import(loadableUri).catch((_) => ({}));
55
+ const loadableStats = await compatibleRequire(loadableUri).catch((_) => ({}));
61
56
  const routesManifestUri = path.join(pwd, ROUTE_MANIFEST_FILE);
62
- const routeManifest = await import(routesManifestUri).catch((_) => ({}));
57
+ const routeManifest = await compatibleRequire(routesManifestUri).catch((_) => ({}));
63
58
  const nestedRoutesJsonPath = path.join(pwd, NESTED_ROUTE_SPEC_FILE);
64
59
  const nestedRoutesJson = await import(nestedRoutesJsonPath).catch((_) => ({}));
65
60
  return {
@@ -135,7 +135,7 @@ function isRedirect(headers, code) {
135
135
  }
136
136
  async function createMiddlewareContextFromHono(c) {
137
137
  const loaderContext = getLoaderCtx(c);
138
- let rawRequest = c.req.raw;
138
+ const rawRequest = c.req.raw;
139
139
  const method = rawRequest.method.toUpperCase();
140
140
  if (![
141
141
  "GET",
@@ -150,11 +150,14 @@ async function createMiddlewareContextFromHono(c) {
150
150
  method: rawRequest.method
151
151
  };
152
152
  init.duplex = "half";
153
- rawRequest = new Request(rawRequest.url, init);
153
+ c.req.raw = new Request(rawRequest.url, init);
154
154
  }
155
155
  return {
156
156
  get request() {
157
- return rawRequest;
157
+ return c.req.raw;
158
+ },
159
+ set request(request) {
160
+ c.req.raw = request;
158
161
  },
159
162
  get response() {
160
163
  return c.res;
@@ -1,2 +1,2 @@
1
1
  import { CacheConfig } from '../../../types';
2
- export declare function loadCacheConfig(pwd: string): CacheConfig | undefined;
2
+ export declare function loadCacheConfig(pwd: string): Promise<CacheConfig | undefined>;
@@ -1,3 +1,3 @@
1
1
  import { CliConfig, ServerConfig, UserConfig } from '../../../types';
2
- export declare function loadServerRuntimeConfig(pwd: string, oldServerFile?: string, newServerConfigPath?: string): ServerConfig | undefined;
2
+ export declare function loadServerRuntimeConfig(pwd: string, oldServerFile?: string, newServerConfigPath?: string): Promise<ServerConfig | undefined>;
3
3
  export declare function loadServerCliConfig(pwd: string, defaultConfig?: UserConfig): CliConfig;
@@ -1,3 +1,3 @@
1
1
  import { ServerPlugin } from '@modern-js/types';
2
2
  import { ServerPlugin as ServerPluginInstance } from '../../../types';
3
- export declare function loadServerPlugins(serverPlugins: ServerPlugin[], appDirectory: string): ServerPluginInstance[];
3
+ export declare function loadServerPlugins(serverPlugins: ServerPlugin[], appDirectory: string): Promise<ServerPluginInstance[]>;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.58.0",
18
+ "version": "2.58.2-alpha.0",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -48,13 +48,14 @@
48
48
  "dependencies": {
49
49
  "@swc/helpers": "0.5.3",
50
50
  "@web-std/fetch": "^4.2.1",
51
- "@web-std/stream": "^1.0.3",
52
51
  "@web-std/file": "^3.0.3",
52
+ "@web-std/stream": "^1.0.3",
53
+ "flatted": "^3.2.9",
53
54
  "hono": "^3.12.2",
54
55
  "ts-deepmerge": "7.0.0",
55
- "@modern-js/plugin": "2.58.0",
56
- "@modern-js/runtime-utils": "2.58.0",
57
- "@modern-js/utils": "2.58.0"
56
+ "@modern-js/plugin": "2.58.1",
57
+ "@modern-js/utils": "2.58.1",
58
+ "@modern-js/runtime-utils": "2.58.1"
58
59
  },
59
60
  "devDependencies": {
60
61
  "@types/jest": "^29",
@@ -64,9 +65,9 @@
64
65
  "jest": "^29",
65
66
  "ts-jest": "^29.1.0",
66
67
  "typescript": "^5",
67
- "@modern-js/types": "2.58.0",
68
- "@scripts/build": "2.58.0",
69
- "@scripts/jest-config": "2.58.0"
68
+ "@modern-js/types": "2.58.1",
69
+ "@scripts/build": "2.58.1",
70
+ "@scripts/jest-config": "2.58.1"
70
71
  },
71
72
  "sideEffects": false,
72
73
  "publishConfig": {