@modern-js/server-core 3.9.3 → 3.10.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.
@@ -77,28 +77,51 @@ function injectTemplates(pwd, routes, htmlTemplatePromise) {
77
77
  await next();
78
78
  };
79
79
  }
80
- const loadBundle = async (filepath, monitors)=>{
81
- if (!await utils_namespaceObject.fs.pathExists(filepath)) return;
80
+ function reportBundleError(filepath, error, monitors) {
81
+ const detail = error instanceof Error ? error.stack || error.message : error;
82
+ if (monitors) monitors.error(`Load ${filepath} bundle failed, error = %s`, detail);
83
+ else console.error(`Load ${filepath} bundle failed, error = ${detail}`);
84
+ }
85
+ const loadBundle = async (filepath, monitors, { required = false, failOnError = false } = {})=>{
86
+ if (!required && !failOnError && !await utils_namespaceObject.fs.pathExists(filepath)) return;
82
87
  try {
88
+ if (!required && failOnError) try {
89
+ await utils_namespaceObject.fs.access(filepath);
90
+ } catch (e) {
91
+ const code = e?.code;
92
+ if ('ENOENT' === code || 'ENOTDIR' === code) return;
93
+ throw e;
94
+ }
83
95
  const module = await (0, utils_namespaceObject.compatibleRequire)(filepath, false);
84
96
  return module;
85
97
  } catch (e) {
86
- if (monitors) monitors.error(`Load ${filepath} bundle failed, error = %s`, e instanceof Error ? e.stack || e.message : e);
87
- else console.error(`Load ${filepath} bundle failed, error = ${e instanceof Error ? e.stack || e.message : e}`);
98
+ reportBundleError(filepath, e, monitors);
99
+ if (failOnError) throw e;
88
100
  return;
89
101
  }
90
102
  };
91
- async function getServerManifest(pwd, routes, monitors) {
103
+ async function getServerManifest(pwd, routes, monitors, { failOnError = false } = {}) {
92
104
  const loaderBundles = {};
93
105
  const renderBundles = {};
94
106
  await Promise.all(routes.filter((route)=>Boolean(route.bundle)).map(async (route)=>{
107
+ const failOnBundleError = failOnError && Boolean(route.isSSR);
95
108
  const entryName = route.entryName || utils_namespaceObject.MAIN_ENTRY_NAME;
96
109
  const renderBundlePath = external_path_default().join(pwd, route.bundle || '');
97
110
  const loaderBundlePath = external_path_default().join(pwd, utils_namespaceObject.SERVER_BUNDLE_DIRECTORY, `${entryName}-server-loaders.js`);
98
- const renderBundle = await loadBundle(renderBundlePath, monitors);
99
- const loaderBundle = await loadBundle(loaderBundlePath, monitors);
111
+ const renderBundle = await loadBundle(renderBundlePath, monitors, {
112
+ required: failOnBundleError,
113
+ failOnError: failOnBundleError
114
+ });
115
+ const loaderBundle = await loadBundle(loaderBundlePath, monitors, {
116
+ failOnError: failOnBundleError
117
+ });
100
118
  renderBundle && (renderBundles[entryName] = renderBundle);
101
- loaderBundle && (loaderBundles[entryName] = loaderBundle?.loadModules ? await loaderBundle?.loadModules() : loaderBundle);
119
+ if (loaderBundle) try {
120
+ loaderBundles[entryName] = loaderBundle.loadModules ? await loaderBundle.loadModules() : loaderBundle;
121
+ } catch (e) {
122
+ if (failOnBundleError) reportBundleError(loaderBundlePath, e, monitors);
123
+ throw e;
124
+ }
102
125
  }));
103
126
  const loadableUri = external_path_default().join(pwd, utils_namespaceObject.LOADABLE_STATS_FILE);
104
127
  const loadableStats = await (0, utils_namespaceObject.compatibleRequire)(loadableUri).catch((_)=>({}));
@@ -166,13 +189,18 @@ const injectRscManifestPlugin = (enableRsc)=>({
166
189
  const injectResourcePlugin = ()=>({
167
190
  name: '@modern-js/plugin-inject-resource',
168
191
  setup (api) {
169
- api.onPrepare(()=>{
192
+ api.onPrepare(async ()=>{
170
193
  const { middlewares, routes, distDirectory: pwd } = api.getServerContext();
171
194
  let htmlTemplatePromise;
172
195
  let manifestPromise;
173
196
  if ((0, utils_namespaceObject.isProd)()) {
174
- manifestPromise = getServerManifest(pwd, routes || [], void 0);
197
+ const warmupRoutes = routes || [];
198
+ const failOnError = warmupRoutes.some((route)=>route.isSSR && route.bundle) && !warmupRoutes.some((route)=>route.isRSC);
199
+ manifestPromise = getServerManifest(pwd, warmupRoutes, void 0, {
200
+ failOnError
201
+ });
175
202
  htmlTemplatePromise = getHtmlTemplates(pwd, routes || []);
203
+ if (failOnError) await manifestPromise;
176
204
  }
177
205
  middlewares.push({
178
206
  name: 'inject-server-manifest',
@@ -115,7 +115,9 @@ class ServerBase {
115
115
  this.serverContext = null;
116
116
  this.serverOptions = options;
117
117
  this.app = new external_hono_namespaceObject.Hono();
118
- this.app.use('*', external_context_js_namespaceObject.run);
118
+ this.app.use('*', async (context, next)=>{
119
+ await (0, external_context_js_namespaceObject.run)(context, next);
120
+ });
119
121
  }
120
122
  }
121
123
  function applyMiddlewares() {
@@ -27,28 +27,51 @@ function injectTemplates(pwd, routes, htmlTemplatePromise) {
27
27
  await next();
28
28
  };
29
29
  }
30
- const loadBundle = async (filepath, monitors)=>{
31
- if (!await fs.pathExists(filepath)) return;
30
+ function reportBundleError(filepath, error, monitors) {
31
+ const detail = error instanceof Error ? error.stack || error.message : error;
32
+ if (monitors) monitors.error(`Load ${filepath} bundle failed, error = %s`, detail);
33
+ else console.error(`Load ${filepath} bundle failed, error = ${detail}`);
34
+ }
35
+ const loadBundle = async (filepath, monitors, { required = false, failOnError = false } = {})=>{
36
+ if (!required && !failOnError && !await fs.pathExists(filepath)) return;
32
37
  try {
38
+ if (!required && failOnError) try {
39
+ await fs.access(filepath);
40
+ } catch (e) {
41
+ const code = e?.code;
42
+ if ('ENOENT' === code || 'ENOTDIR' === code) return;
43
+ throw e;
44
+ }
33
45
  const module = await compatibleRequire(filepath, false);
34
46
  return module;
35
47
  } catch (e) {
36
- if (monitors) monitors.error(`Load ${filepath} bundle failed, error = %s`, e instanceof Error ? e.stack || e.message : e);
37
- else console.error(`Load ${filepath} bundle failed, error = ${e instanceof Error ? e.stack || e.message : e}`);
48
+ reportBundleError(filepath, e, monitors);
49
+ if (failOnError) throw e;
38
50
  return;
39
51
  }
40
52
  };
41
- async function getServerManifest(pwd, routes, monitors) {
53
+ async function getServerManifest(pwd, routes, monitors, { failOnError = false } = {}) {
42
54
  const loaderBundles = {};
43
55
  const renderBundles = {};
44
56
  await Promise.all(routes.filter((route)=>Boolean(route.bundle)).map(async (route)=>{
57
+ const failOnBundleError = failOnError && Boolean(route.isSSR);
45
58
  const entryName = route.entryName || MAIN_ENTRY_NAME;
46
59
  const renderBundlePath = path.join(pwd, route.bundle || '');
47
60
  const loaderBundlePath = path.join(pwd, SERVER_BUNDLE_DIRECTORY, `${entryName}-server-loaders.js`);
48
- const renderBundle = await loadBundle(renderBundlePath, monitors);
49
- const loaderBundle = await loadBundle(loaderBundlePath, monitors);
61
+ const renderBundle = await loadBundle(renderBundlePath, monitors, {
62
+ required: failOnBundleError,
63
+ failOnError: failOnBundleError
64
+ });
65
+ const loaderBundle = await loadBundle(loaderBundlePath, monitors, {
66
+ failOnError: failOnBundleError
67
+ });
50
68
  renderBundle && (renderBundles[entryName] = renderBundle);
51
- loaderBundle && (loaderBundles[entryName] = loaderBundle?.loadModules ? await loaderBundle?.loadModules() : loaderBundle);
69
+ if (loaderBundle) try {
70
+ loaderBundles[entryName] = loaderBundle.loadModules ? await loaderBundle.loadModules() : loaderBundle;
71
+ } catch (e) {
72
+ if (failOnBundleError) reportBundleError(loaderBundlePath, e, monitors);
73
+ throw e;
74
+ }
52
75
  }));
53
76
  const loadableUri = path.join(pwd, LOADABLE_STATS_FILE);
54
77
  const loadableStats = await compatibleRequire(loadableUri).catch((_)=>({}));
@@ -116,13 +139,18 @@ const injectRscManifestPlugin = (enableRsc)=>({
116
139
  const injectResourcePlugin = ()=>({
117
140
  name: '@modern-js/plugin-inject-resource',
118
141
  setup (api) {
119
- api.onPrepare(()=>{
142
+ api.onPrepare(async ()=>{
120
143
  const { middlewares, routes, distDirectory: pwd } = api.getServerContext();
121
144
  let htmlTemplatePromise;
122
145
  let manifestPromise;
123
146
  if (isProd()) {
124
- manifestPromise = getServerManifest(pwd, routes || [], void 0);
147
+ const warmupRoutes = routes || [];
148
+ const failOnError = warmupRoutes.some((route)=>route.isSSR && route.bundle) && !warmupRoutes.some((route)=>route.isRSC);
149
+ manifestPromise = getServerManifest(pwd, warmupRoutes, void 0, {
150
+ failOnError
151
+ });
125
152
  htmlTemplatePromise = getHtmlTemplates(pwd, routes || []);
153
+ if (failOnError) await manifestPromise;
126
154
  }
127
155
  middlewares.push({
128
156
  name: 'inject-server-manifest',
@@ -82,7 +82,9 @@ class ServerBase {
82
82
  this.serverContext = null;
83
83
  this.serverOptions = options;
84
84
  this.app = new Hono();
85
- this.app.use('*', run);
85
+ this.app.use('*', async (context, next)=>{
86
+ await run(context, next);
87
+ });
86
88
  }
87
89
  }
88
90
  function applyMiddlewares() {
@@ -28,28 +28,51 @@ function injectTemplates(pwd, routes, htmlTemplatePromise) {
28
28
  await next();
29
29
  };
30
30
  }
31
- const loadBundle = async (filepath, monitors)=>{
32
- if (!await fs.pathExists(filepath)) return;
31
+ function reportBundleError(filepath, error, monitors) {
32
+ const detail = error instanceof Error ? error.stack || error.message : error;
33
+ if (monitors) monitors.error(`Load ${filepath} bundle failed, error = %s`, detail);
34
+ else console.error(`Load ${filepath} bundle failed, error = ${detail}`);
35
+ }
36
+ const loadBundle = async (filepath, monitors, { required = false, failOnError = false } = {})=>{
37
+ if (!required && !failOnError && !await fs.pathExists(filepath)) return;
33
38
  try {
39
+ if (!required && failOnError) try {
40
+ await fs.access(filepath);
41
+ } catch (e) {
42
+ const code = e?.code;
43
+ if ('ENOENT' === code || 'ENOTDIR' === code) return;
44
+ throw e;
45
+ }
34
46
  const module = await compatibleRequire(filepath, false);
35
47
  return module;
36
48
  } catch (e) {
37
- if (monitors) monitors.error(`Load ${filepath} bundle failed, error = %s`, e instanceof Error ? e.stack || e.message : e);
38
- else console.error(`Load ${filepath} bundle failed, error = ${e instanceof Error ? e.stack || e.message : e}`);
49
+ reportBundleError(filepath, e, monitors);
50
+ if (failOnError) throw e;
39
51
  return;
40
52
  }
41
53
  };
42
- async function getServerManifest(pwd, routes, monitors) {
54
+ async function getServerManifest(pwd, routes, monitors, { failOnError = false } = {}) {
43
55
  const loaderBundles = {};
44
56
  const renderBundles = {};
45
57
  await Promise.all(routes.filter((route)=>Boolean(route.bundle)).map(async (route)=>{
58
+ const failOnBundleError = failOnError && Boolean(route.isSSR);
46
59
  const entryName = route.entryName || MAIN_ENTRY_NAME;
47
60
  const renderBundlePath = path.join(pwd, route.bundle || '');
48
61
  const loaderBundlePath = path.join(pwd, SERVER_BUNDLE_DIRECTORY, `${entryName}-server-loaders.js`);
49
- const renderBundle = await loadBundle(renderBundlePath, monitors);
50
- const loaderBundle = await loadBundle(loaderBundlePath, monitors);
62
+ const renderBundle = await loadBundle(renderBundlePath, monitors, {
63
+ required: failOnBundleError,
64
+ failOnError: failOnBundleError
65
+ });
66
+ const loaderBundle = await loadBundle(loaderBundlePath, monitors, {
67
+ failOnError: failOnBundleError
68
+ });
51
69
  renderBundle && (renderBundles[entryName] = renderBundle);
52
- loaderBundle && (loaderBundles[entryName] = loaderBundle?.loadModules ? await loaderBundle?.loadModules() : loaderBundle);
70
+ if (loaderBundle) try {
71
+ loaderBundles[entryName] = loaderBundle.loadModules ? await loaderBundle.loadModules() : loaderBundle;
72
+ } catch (e) {
73
+ if (failOnBundleError) reportBundleError(loaderBundlePath, e, monitors);
74
+ throw e;
75
+ }
53
76
  }));
54
77
  const loadableUri = path.join(pwd, LOADABLE_STATS_FILE);
55
78
  const loadableStats = await compatibleRequire(loadableUri).catch((_)=>({}));
@@ -117,13 +140,18 @@ const injectRscManifestPlugin = (enableRsc)=>({
117
140
  const injectResourcePlugin = ()=>({
118
141
  name: '@modern-js/plugin-inject-resource',
119
142
  setup (api) {
120
- api.onPrepare(()=>{
143
+ api.onPrepare(async ()=>{
121
144
  const { middlewares, routes, distDirectory: pwd } = api.getServerContext();
122
145
  let htmlTemplatePromise;
123
146
  let manifestPromise;
124
147
  if (isProd()) {
125
- manifestPromise = getServerManifest(pwd, routes || [], void 0);
148
+ const warmupRoutes = routes || [];
149
+ const failOnError = warmupRoutes.some((route)=>route.isSSR && route.bundle) && !warmupRoutes.some((route)=>route.isRSC);
150
+ manifestPromise = getServerManifest(pwd, warmupRoutes, void 0, {
151
+ failOnError
152
+ });
126
153
  htmlTemplatePromise = getHtmlTemplates(pwd, routes || []);
154
+ if (failOnError) await manifestPromise;
127
155
  }
128
156
  middlewares.push({
129
157
  name: 'inject-server-manifest',
@@ -83,7 +83,9 @@ class ServerBase {
83
83
  this.serverContext = null;
84
84
  this.serverOptions = options;
85
85
  this.app = new Hono();
86
- this.app.use('*', run);
86
+ this.app.use('*', async (context, next)=>{
87
+ await run(context, next);
88
+ });
87
89
  }
88
90
  }
89
91
  function applyMiddlewares() {
@@ -1,5 +1,5 @@
1
1
  export { httpCallBack2HonoMid, connectMid2HonoMid, connectMockMid2HonoMid, } from './hono.js';
2
2
  export type { ServerNodeContext, ServerNodeMiddleware } from './hono.js';
3
- export { createNodeServer, sendResponse, createWebRequest, } from './node.js';
3
+ export { createNodeServer, sendResponse, createWebRequest } from './node.js';
4
4
  export { serverStaticPlugin, injectResourcePlugin, getHtmlTemplates, getServerManifest, injectNodeSeverPlugin, injectRscManifestPlugin, } from './plugins/index.js';
5
5
  export { loadServerPlugins, loadServerEnv, loadServerRuntimeConfig, loadServerCliConfig, loadCacheConfig, } from './helper/index.js';
@@ -2,7 +2,9 @@ import type { Monitors, ServerRoute } from '@modern-js/types';
2
2
  import type { Middleware, ServerEnv, ServerManifest, ServerPlugin } from '../../../types/index.js';
3
3
  export declare function getHtmlTemplates(pwd: string, routes: ServerRoute[]): Promise<Record<string, string>>;
4
4
  export declare function injectTemplates(pwd: string, routes?: ServerRoute[], htmlTemplatePromise?: ReturnType<typeof getHtmlTemplates>): Middleware<ServerEnv>;
5
- export declare function getServerManifest(pwd: string, routes: ServerRoute[], monitors?: Monitors): Promise<ServerManifest>;
5
+ export declare function getServerManifest(pwd: string, routes: ServerRoute[], monitors?: Monitors, { failOnError }?: {
6
+ failOnError?: boolean | undefined;
7
+ }): Promise<ServerManifest>;
6
8
  export declare function injectServerManifest(pwd: string, routes?: ServerRoute[], manifestPromise?: Promise<ServerManifest>): Middleware<ServerEnv>;
7
9
  export declare function getRscServerManifest(pwd: string): Promise<any>;
8
10
  export declare function getClientManifest(pwd: string): Promise<any>;
@@ -1,3 +1,3 @@
1
1
  export type { Context, Next, MiddlewareHandler, MiddlewareHandler as Middleware, HonoRequest, } from 'hono';
2
- export { setCookie, getCookie, deleteCookie, } from 'hono/cookie';
2
+ export { setCookie, getCookie, deleteCookie } from 'hono/cookie';
3
3
  export { languageDetector } from 'hono/language';
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "3.9.3",
18
+ "version": "3.10.0",
19
19
  "types": "./dist/types/index.d.ts",
20
20
  "main": "./dist/cjs/index.js",
21
21
  "exports": {
@@ -71,19 +71,18 @@
71
71
  "flatted": "^3.4.4",
72
72
  "hono": "^4.11.7",
73
73
  "ts-deepmerge": "8.0.0",
74
- "@modern-js/plugin": "3.9.3",
75
- "@modern-js/runtime-utils": "3.9.3",
76
- "@modern-js/utils": "3.9.3"
74
+ "@modern-js/plugin": "3.10.0",
75
+ "@modern-js/runtime-utils": "3.10.0",
76
+ "@modern-js/utils": "3.10.0"
77
77
  },
78
78
  "devDependencies": {
79
- "@rslib/core": "1.0.1",
80
79
  "@types/cloneable-readable": "^2.0.3",
81
80
  "@types/merge-deep": "^3.0.3",
82
81
  "@types/node": "^20",
83
82
  "http-proxy-middleware": "^2.0.10",
84
83
  "typescript": "^5",
85
84
  "@modern-js/rslib": "2.68.10",
86
- "@modern-js/types": "3.9.3",
85
+ "@modern-js/types": "3.10.0",
87
86
  "@scripts/rstest-config": "2.66.0"
88
87
  },
89
88
  "sideEffects": false,
@@ -92,8 +91,8 @@
92
91
  "access": "public"
93
92
  },
94
93
  "scripts": {
95
- "dev": "rslib build --watch",
96
- "build": "rslib build",
94
+ "dev": "rs lib --watch",
95
+ "build": "rs lib",
97
96
  "test": "rstest --passWithNoTests"
98
97
  }
99
98
  }
@@ -0,0 +1,4 @@
1
+ import { rslibConfig } from '@modern-js/rslib';
2
+ import { define } from 'rstack';
3
+
4
+ define.lib(rslibConfig);
package/rslib.config.mts DELETED
@@ -1,4 +0,0 @@
1
- import { rslibConfig } from '@modern-js/rslib';
2
- import { defineConfig } from '@rslib/core';
3
-
4
- export default defineConfig(rslibConfig);