@modern-js/prod-server 2.6.1-alpha.0 → 2.8.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.
Files changed (133) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/README.md +2 -2
  3. package/dist/cjs/libs/context/context.js +8 -9
  4. package/dist/cjs/libs/serveFile.js +3 -4
  5. package/dist/cjs/server/modernServer.js +25 -15
  6. package/dist/cjs/utils.js +4 -13
  7. package/dist/esm/libs/context/context.js +11 -12
  8. package/dist/esm/libs/serveFile.js +4 -5
  9. package/dist/esm/server/modernServer.js +50 -28
  10. package/dist/esm/utils.js +4 -8
  11. package/dist/esm-node/libs/context/context.js +8 -9
  12. package/dist/esm-node/libs/serveFile.js +3 -4
  13. package/dist/esm-node/server/modernServer.js +32 -16
  14. package/dist/esm-node/utils.js +4 -12
  15. package/dist/types/libs/context/context.d.ts +2 -4
  16. package/dist/types/libs/serveFile.d.ts +1 -2
  17. package/dist/types/server/modernServer.d.ts +1 -1
  18. package/dist/types/utils.d.ts +2 -3
  19. package/package.json +11 -11
  20. package/dist/js/modern/constants.js +0 -35
  21. package/dist/js/modern/index.js +0 -18
  22. package/dist/js/modern/libs/context/context.js +0 -160
  23. package/dist/js/modern/libs/context/index.js +0 -6
  24. package/dist/js/modern/libs/hook-api/index.js +0 -134
  25. package/dist/js/modern/libs/hook-api/route.js +0 -20
  26. package/dist/js/modern/libs/hook-api/template.js +0 -73
  27. package/dist/js/modern/libs/loadConfig.js +0 -62
  28. package/dist/js/modern/libs/logger.js +0 -111
  29. package/dist/js/modern/libs/metrics.js +0 -11
  30. package/dist/js/modern/libs/proxy.js +0 -92
  31. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +0 -114
  32. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +0 -254
  33. package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +0 -47
  34. package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +0 -37
  35. package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +0 -91
  36. package/dist/js/modern/libs/render/cache/index.js +0 -115
  37. package/dist/js/modern/libs/render/cache/page-caches/index.js +0 -32
  38. package/dist/js/modern/libs/render/cache/page-caches/lru.js +0 -29
  39. package/dist/js/modern/libs/render/cache/spr.js +0 -248
  40. package/dist/js/modern/libs/render/cache/type.js +0 -0
  41. package/dist/js/modern/libs/render/cache/util.js +0 -102
  42. package/dist/js/modern/libs/render/index.js +0 -86
  43. package/dist/js/modern/libs/render/measure.js +0 -68
  44. package/dist/js/modern/libs/render/reader.js +0 -107
  45. package/dist/js/modern/libs/render/ssr.js +0 -100
  46. package/dist/js/modern/libs/render/static.js +0 -60
  47. package/dist/js/modern/libs/render/type.js +0 -9
  48. package/dist/js/modern/libs/route/index.js +0 -54
  49. package/dist/js/modern/libs/route/matcher.js +0 -87
  50. package/dist/js/modern/libs/route/route.js +0 -16
  51. package/dist/js/modern/libs/serve-file.js +0 -67
  52. package/dist/js/modern/server/index.js +0 -208
  53. package/dist/js/modern/server/modern-server-split.js +0 -74
  54. package/dist/js/modern/server/modern-server.js +0 -554
  55. package/dist/js/modern/type.js +0 -0
  56. package/dist/js/modern/utils.js +0 -136
  57. package/dist/js/modern/worker-server.js +0 -89
  58. package/dist/js/node/constants.js +0 -62
  59. package/dist/js/node/index.js +0 -44
  60. package/dist/js/node/libs/context/context.js +0 -189
  61. package/dist/js/node/libs/context/index.js +0 -30
  62. package/dist/js/node/libs/hook-api/index.js +0 -164
  63. package/dist/js/node/libs/hook-api/route.js +0 -43
  64. package/dist/js/node/libs/hook-api/template.js +0 -97
  65. package/dist/js/node/libs/loadConfig.js +0 -91
  66. package/dist/js/node/libs/logger.js +0 -133
  67. package/dist/js/node/libs/metrics.js +0 -34
  68. package/dist/js/node/libs/proxy.js +0 -114
  69. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +0 -115
  70. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +0 -245
  71. package/dist/js/node/libs/render/cache/__tests__/cacheable.js +0 -70
  72. package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +0 -60
  73. package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +0 -114
  74. package/dist/js/node/libs/render/cache/index.js +0 -134
  75. package/dist/js/node/libs/render/cache/page-caches/index.js +0 -55
  76. package/dist/js/node/libs/render/cache/page-caches/lru.js +0 -58
  77. package/dist/js/node/libs/render/cache/spr.js +0 -270
  78. package/dist/js/node/libs/render/cache/type.js +0 -15
  79. package/dist/js/node/libs/render/cache/util.js +0 -138
  80. package/dist/js/node/libs/render/index.js +0 -115
  81. package/dist/js/node/libs/render/measure.js +0 -90
  82. package/dist/js/node/libs/render/reader.js +0 -140
  83. package/dist/js/node/libs/render/ssr.js +0 -123
  84. package/dist/js/node/libs/render/static.js +0 -89
  85. package/dist/js/node/libs/render/type.js +0 -32
  86. package/dist/js/node/libs/route/index.js +0 -78
  87. package/dist/js/node/libs/route/matcher.js +0 -106
  88. package/dist/js/node/libs/route/route.js +0 -39
  89. package/dist/js/node/libs/serve-file.js +0 -97
  90. package/dist/js/node/server/index.js +0 -219
  91. package/dist/js/node/server/modern-server-split.js +0 -97
  92. package/dist/js/node/server/modern-server.js +0 -559
  93. package/dist/js/node/type.js +0 -15
  94. package/dist/js/node/utils.js +0 -166
  95. package/dist/js/node/worker-server.js +0 -113
  96. package/dist/js/treeshaking/constants.js +0 -29
  97. package/dist/js/treeshaking/index.js +0 -13
  98. package/dist/js/treeshaking/libs/context/context.js +0 -274
  99. package/dist/js/treeshaking/libs/context/index.js +0 -5
  100. package/dist/js/treeshaking/libs/hook-api/index.js +0 -281
  101. package/dist/js/treeshaking/libs/hook-api/route.js +0 -68
  102. package/dist/js/treeshaking/libs/hook-api/template.js +0 -127
  103. package/dist/js/treeshaking/libs/loadConfig.js +0 -82
  104. package/dist/js/treeshaking/libs/logger.js +0 -205
  105. package/dist/js/treeshaking/libs/metrics.js +0 -6
  106. package/dist/js/treeshaking/libs/proxy.js +0 -244
  107. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +0 -291
  108. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +0 -781
  109. package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +0 -67
  110. package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +0 -45
  111. package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +0 -147
  112. package/dist/js/treeshaking/libs/render/cache/index.js +0 -346
  113. package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +0 -154
  114. package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +0 -84
  115. package/dist/js/treeshaking/libs/render/cache/spr.js +0 -492
  116. package/dist/js/treeshaking/libs/render/cache/type.js +0 -1
  117. package/dist/js/treeshaking/libs/render/cache/util.js +0 -280
  118. package/dist/js/treeshaking/libs/render/index.js +0 -234
  119. package/dist/js/treeshaking/libs/render/measure.js +0 -146
  120. package/dist/js/treeshaking/libs/render/reader.js +0 -339
  121. package/dist/js/treeshaking/libs/render/ssr.js +0 -223
  122. package/dist/js/treeshaking/libs/render/static.js +0 -216
  123. package/dist/js/treeshaking/libs/render/type.js +0 -7
  124. package/dist/js/treeshaking/libs/route/index.js +0 -130
  125. package/dist/js/treeshaking/libs/route/matcher.js +0 -143
  126. package/dist/js/treeshaking/libs/route/route.js +0 -40
  127. package/dist/js/treeshaking/libs/serve-file.js +0 -184
  128. package/dist/js/treeshaking/server/index.js +0 -505
  129. package/dist/js/treeshaking/server/modern-server-split.js +0 -360
  130. package/dist/js/treeshaking/server/modern-server.js +0 -1089
  131. package/dist/js/treeshaking/type.js +0 -1
  132. package/dist/js/treeshaking/utils.js +0 -147
  133. package/dist/js/treeshaking/worker-server.js +0 -233
package/CHANGELOG.md CHANGED
@@ -1,5 +1,44 @@
1
1
  # @modern-js/prod-server
2
2
 
3
+ ## 2.8.0
4
+
5
+ ### Patch Changes
6
+
7
+ - 1104a9f18b: feat: support start web service only
8
+ feat: 支持只启动 web 服务
9
+ - 70d82e1408: fix: use mock host instead real url host for url parse, for new URL not support ipv6, and we only need parse url path & search
10
+ fix: 使用虚拟的域名代替真实的主机名,因为 new URL 不支持解析 ipv6 的域名,并且我们只需要解析 url 的路径和查询字符串
11
+ - Updated dependencies [1104a9f18b]
12
+ - Updated dependencies [1f6ca2c7fb]
13
+ - @modern-js/utils@2.8.0
14
+ - @modern-js/server-core@2.8.0
15
+
16
+ ## 2.7.0
17
+
18
+ ### Minor Changes
19
+
20
+ - 84bfb439b8: feat: support custom apiDir, lambdaDir and style of writing for bff
21
+ feat: 支持定制 api 目录,lambda 目录,bff 的写法
22
+
23
+ ### Patch Changes
24
+
25
+ - 7fff9020e1: chore: make file naming consistent
26
+
27
+ chore: 统一文件命名为小驼峰格式
28
+
29
+ - 1eea234fdd: chore: make test files naming consistent
30
+
31
+ chore: 统一测试文件命名为小驼峰格式
32
+
33
+ - Updated dependencies [0f15fc597c]
34
+ - Updated dependencies [dcad887024]
35
+ - Updated dependencies [a4672f7c16]
36
+ - Updated dependencies [7fff9020e1]
37
+ - Updated dependencies [1eea234fdd]
38
+ - Updated dependencies [84bfb439b8]
39
+ - @modern-js/utils@2.7.0
40
+ - @modern-js/server-core@2.7.0
41
+
3
42
  ## 2.6.0
4
43
 
5
44
  ### Patch Changes
package/README.md CHANGED
@@ -19,8 +19,8 @@ Please follow [Quick Start](https://modernjs.dev/en/guides/get-started/quick-sta
19
19
 
20
20
  ## Contributing
21
21
 
22
- Please read the [Contributing Guide](https://github.com/modern-js-dev/modern.js/blob/main/CONTRIBUTING.md).
22
+ Please read the [Contributing Guide](https://github.com/web-infra-dev/modern.js/blob/main/CONTRIBUTING.md).
23
23
 
24
24
  ## License
25
25
 
26
- Modern.js is [MIT licensed](https://github.com/modern-js-dev/modern.js/blob/main/LICENSE).
26
+ Modern.js is [MIT licensed](https://github.com/web-infra-dev/modern.js/blob/main/LICENSE).
@@ -36,6 +36,7 @@ var import_buffer = require("buffer");
36
36
  var import_etag = __toESM(require("etag"));
37
37
  var import_fresh = __toESM(require("fresh"));
38
38
  var import_utils = require("../../utils");
39
+ const MOCK_URL_BASE = "https://modernjs.dev/";
39
40
  class ModernServerContext {
40
41
  constructor(req, res, options) {
41
42
  /**
@@ -55,6 +56,10 @@ class ModernServerContext {
55
56
  get metrics() {
56
57
  return this.req.metrics;
57
58
  }
59
+ get parsedURL() {
60
+ const url = new import_url.URL(this.req.url, MOCK_URL_BASE);
61
+ return url;
62
+ }
58
63
  bind() {
59
64
  const { req, res } = this;
60
65
  req.get = (key) => this.getReqHeader(key);
@@ -146,23 +151,17 @@ class ModernServerContext {
146
151
  get href() {
147
152
  return this.origin + this.url;
148
153
  }
149
- get parsedURL() {
150
- const url = new import_url.URL(this.req.url, this.origin);
151
- return url;
152
- }
153
154
  get path() {
154
155
  return this.parsedURL.pathname;
155
156
  }
156
157
  set path(p) {
157
- const url = new import_url.URL(this.req.url, this.origin);
158
- if (!url || !p) {
158
+ if (!p) {
159
159
  return;
160
160
  }
161
- if (url.pathname === p) {
161
+ if (this.path === p) {
162
162
  return;
163
163
  }
164
- url.pathname = p;
165
- this.url = url.toString();
164
+ this.url = p + this.parsedURL.search;
166
165
  }
167
166
  get querystring() {
168
167
  if (!this.req) {
@@ -33,9 +33,9 @@ __export(serveFile_exports, {
33
33
  module.exports = __toCommonJS(serveFile_exports);
34
34
  var import_serve_static = __toESM(require("serve-static"));
35
35
  var import_utils = require("@modern-js/utils");
36
- var import_utils2 = require("../utils");
37
36
  const removedPrefix = (req, prefix) => {
38
- if ((0, import_utils2.useLocalPrefix)(prefix)) {
37
+ const useLocalPrefix = !prefix.includes(".");
38
+ if (useLocalPrefix) {
39
39
  req.url = req.url.slice(prefix.length);
40
40
  return () => {
41
41
  req.url = prefix + req.url;
@@ -53,11 +53,10 @@ const faviconFallbackHandler = (context, next) => {
53
53
  next();
54
54
  }
55
55
  };
56
- const createStaticFileHandler = (rules, output = {}) => (
56
+ const createStaticFileHandler = (rules, assetPrefix = "/") => (
57
57
  // eslint-disable-next-line consistent-return
58
58
  async (context, next) => {
59
59
  const { url: requestUrl, req, res } = context;
60
- const { assetPrefix = "/" } = output;
61
60
  const hitRule = rules.find((item) => {
62
61
  if ((0, import_utils.isString)(item.path) && requestUrl.startsWith(item.path)) {
63
62
  return true;
@@ -77,7 +77,7 @@ class ModernServer {
77
77
  }
78
78
  // server prepare
79
79
  async onInit(runner, app) {
80
- var _a, _b;
80
+ var _a, _b, _c;
81
81
  this.runner = runner;
82
82
  const { distDir, staticGenerate, conf } = this;
83
83
  (0, import_utils2.debug)("final server conf", this.conf);
@@ -96,19 +96,6 @@ class ModernServer {
96
96
  this.warmupSSRBundle();
97
97
  await this.prepareFrameHandler();
98
98
  await this.prepareBeforeRouteHandler(usageRoutes, distDir);
99
- const staticPathRegExp = (0, import_utils2.getStaticReg)(
100
- this.conf.output || {},
101
- this.conf.html
102
- );
103
- this.staticFileHandler = (0, import_serveFile.createStaticFileHandler)(
104
- [
105
- {
106
- path: staticPathRegExp,
107
- target: distDir
108
- }
109
- ],
110
- this.conf.output
111
- );
112
99
  const ssrConfig = (_b = this.conf.server) == null ? void 0 : _b.ssr;
113
100
  const forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
114
101
  this.routeRenderHandler = (0, import_render.createRenderHandler)({
@@ -117,7 +104,7 @@ class ModernServer {
117
104
  forceCSR
118
105
  });
119
106
  await this.setupBeforeProdMiddleware();
120
- this.addHandler(this.staticFileHandler);
107
+ this.addHandler(this.setupStaticMiddleware((_c = this.conf.output) == null ? void 0 : _c.assetPrefix));
121
108
  this.addHandler(import_serveFile.faviconFallbackHandler);
122
109
  this.addBeforeRouteHandler();
123
110
  this.addHandler(this.routeHandler.bind(this));
@@ -236,6 +223,13 @@ class ModernServer {
236
223
  const { workDir, runner, conf } = this;
237
224
  const { bff } = conf;
238
225
  const prefix = (bff == null ? void 0 : bff.prefix) || "/api";
226
+ const webOnly = await (0, import_utils.isWebOnly)();
227
+ if (webOnly && process.env.NODE_ENV === "development") {
228
+ return (req, res) => {
229
+ res.setHeader("Content-Type", "text/plain");
230
+ res.end(JSON.stringify(""));
231
+ };
232
+ }
239
233
  return runner.prepareApiServer(
240
234
  {
241
235
  pwd: workDir,
@@ -257,6 +251,22 @@ class ModernServer {
257
251
  this.addHandler(mid);
258
252
  });
259
253
  }
254
+ setupStaticMiddleware(prefix) {
255
+ const staticPathRegExp = (0, import_utils2.getStaticReg)(
256
+ this.conf.output,
257
+ this.conf.html,
258
+ prefix
259
+ );
260
+ return (0, import_serveFile.createStaticFileHandler)(
261
+ [
262
+ {
263
+ path: staticPathRegExp,
264
+ target: this.distDir
265
+ }
266
+ ],
267
+ prefix
268
+ );
269
+ }
260
270
  async handleAPI(context) {
261
271
  const { req, res } = context;
262
272
  if (!this.frameAPIHandler) {
package/dist/cjs/utils.js CHANGED
@@ -25,8 +25,7 @@ __export(utils_exports, {
25
25
  isRedirect: () => isRedirect,
26
26
  mergeExtension: () => mergeExtension,
27
27
  noop: () => noop,
28
- prepareFavicons: () => prepareFavicons,
29
- useLocalPrefix: () => useLocalPrefix
28
+ prepareFavicons: () => prepareFavicons
30
29
  });
31
30
  module.exports = __toCommonJS(utils_exports);
32
31
  var import_utils = require("@modern-js/utils");
@@ -89,16 +88,9 @@ const createMiddlewareCollecter = () => {
89
88
  addAPIMiddleware
90
89
  };
91
90
  };
92
- const useLocalPrefix = (url) => {
93
- return (0, import_utils.isProd)() && !url.includes(".");
94
- };
95
- const getStaticReg = (output = {}, html = {}) => {
96
- const {
97
- distPath: { css: cssPath, js: jsPath, media: mediaPath } = {},
98
- assetPrefix = "/"
99
- } = output;
91
+ const getStaticReg = (output = {}, html = {}, prefix = "/") => {
92
+ const { distPath: { css: cssPath, js: jsPath, media: mediaPath } = {} } = output;
100
93
  const { favicon, faviconByEntries } = html;
101
- const prefix = useLocalPrefix(assetPrefix) ? assetPrefix : "";
102
94
  const favicons = prepareFavicons(favicon, faviconByEntries);
103
95
  const staticFiles = [cssPath, jsPath, mediaPath].filter((v) => Boolean(v));
104
96
  const staticReg = ["static/", "upload/", ...staticFiles];
@@ -147,6 +139,5 @@ const isRedirect = (code) => {
147
139
  isRedirect,
148
140
  mergeExtension,
149
141
  noop,
150
- prepareFavicons,
151
- useLocalPrefix
142
+ prepareFavicons
152
143
  });
@@ -43,6 +43,7 @@ import { Buffer } from "buffer";
43
43
  import createEtag from "etag";
44
44
  import fresh from "fresh";
45
45
  import { headersWithoutCookie } from "../../utils";
46
+ var MOCK_URL_BASE = "https://modernjs.dev/";
46
47
  var ModernServerContext = /*#__PURE__*/ function() {
47
48
  "use strict";
48
49
  function ModernServerContext(req, res, options) {
@@ -77,6 +78,13 @@ var ModernServerContext = /*#__PURE__*/ function() {
77
78
  return this.req.metrics;
78
79
  }
79
80
  },
81
+ {
82
+ key: "parsedURL",
83
+ get: function get() {
84
+ var url = new URL(this.req.url, MOCK_URL_BASE);
85
+ return url;
86
+ }
87
+ },
80
88
  {
81
89
  key: "bind",
82
90
  value: function bind() {
@@ -213,28 +221,19 @@ var ModernServerContext = /*#__PURE__*/ function() {
213
221
  return this.origin + this.url;
214
222
  }
215
223
  },
216
- {
217
- key: "parsedURL",
218
- get: function get() {
219
- var url = new URL(this.req.url, this.origin);
220
- return url;
221
- }
222
- },
223
224
  {
224
225
  key: "path",
225
226
  get: function get() {
226
227
  return this.parsedURL.pathname;
227
228
  },
228
229
  set: function set(p) {
229
- var url = new URL(this.req.url, this.origin);
230
- if (!url || !p) {
230
+ if (!p) {
231
231
  return;
232
232
  }
233
- if (url.pathname === p) {
233
+ if (this.path === p) {
234
234
  return;
235
235
  }
236
- url.pathname = p;
237
- this.url = url.toString();
236
+ this.url = p + this.parsedURL.search;
238
237
  }
239
238
  },
240
239
  {
@@ -124,9 +124,9 @@ var __generator = this && this.__generator || function(thisArg, body) {
124
124
  };
125
125
  import serve from "serve-static";
126
126
  import { isString, isRegExp } from "@modern-js/utils";
127
- import { useLocalPrefix } from "../utils";
128
127
  var removedPrefix = function(req, prefix) {
129
- if (useLocalPrefix(prefix)) {
128
+ var useLocalPrefix = !prefix.includes(".");
129
+ if (useLocalPrefix) {
130
130
  req.url = req.url.slice(prefix.length);
131
131
  return function() {
132
132
  req.url = prefix + req.url;
@@ -144,14 +144,13 @@ var faviconFallbackHandler = function(context, next) {
144
144
  }
145
145
  };
146
146
  var createStaticFileHandler = function(rules) {
147
- var output = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
147
+ var assetPrefix = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "/";
148
148
  return function() {
149
149
  var _ref = // eslint-disable-next-line consistent-return
150
150
  _asyncToGenerator(function(context, next) {
151
- var requestUrl, req, res, _output_assetPrefix, assetPrefix, hitRule, resume;
151
+ var requestUrl, req, res, hitRule, resume;
152
152
  return __generator(this, function(_state) {
153
153
  requestUrl = context.url, req = context.req, res = context.res;
154
- _output_assetPrefix = output.assetPrefix, assetPrefix = _output_assetPrefix === void 0 ? "/" : _output_assetPrefix;
155
154
  hitRule = rules.find(function(item) {
156
155
  if (isString(item.path) && requestUrl.startsWith(item.path)) {
157
156
  return true;
@@ -198,7 +198,7 @@ var __generator = this && this.__generator || function(thisArg, body) {
198
198
  };
199
199
  import { createServer } from "http";
200
200
  import path from "path";
201
- import { fs, isPromise, mime, ROUTE_SPEC_FILE } from "@modern-js/utils";
201
+ import { fs, isPromise, isWebOnly, mime, ROUTE_SPEC_FILE } from "@modern-js/utils";
202
202
  import { RouteMatchManager } from "../libs/route";
203
203
  import { createRenderHandler } from "../libs/render";
204
204
  import { createStaticFileHandler, faviconFallbackHandler } from "../libs/serveFile";
@@ -231,7 +231,6 @@ var ModernServer = /*#__PURE__*/ function() {
231
231
  _defineProperty(this, "runMode", void 0);
232
232
  _defineProperty(this, "reader", reader);
233
233
  _defineProperty(this, "proxyTarget", void 0);
234
- _defineProperty(this, "staticFileHandler", void 0);
235
234
  _defineProperty(this, "routeRenderHandler", void 0);
236
235
  _defineProperty(this, "beforeRouteHandler", null);
237
236
  _defineProperty(this, "frameWebHandler", null);
@@ -260,7 +259,7 @@ var ModernServer = /*#__PURE__*/ function() {
260
259
  function onInit(runner, app) {
261
260
  var _this = this;
262
261
  return _asyncToGenerator(function() {
263
- var _conf_bff, _this_conf_server, distDir, staticGenerate, conf, usageRoutes, staticPathRegExp, ssrConfig, forceCSR;
262
+ var _conf_bff, _this_conf_server, _this_conf_output, distDir, staticGenerate, conf, usageRoutes, ssrConfig, forceCSR;
264
263
  return __generator(this, function(_state) {
265
264
  switch(_state.label){
266
265
  case 0:
@@ -292,13 +291,6 @@ var ModernServer = /*#__PURE__*/ function() {
292
291
  ];
293
292
  case 2:
294
293
  _state.sent();
295
- staticPathRegExp = getStaticReg(_this.conf.output || {}, _this.conf.html);
296
- _this.staticFileHandler = createStaticFileHandler([
297
- {
298
- path: staticPathRegExp,
299
- target: distDir
300
- }
301
- ], _this.conf.output);
302
294
  ssrConfig = (_this_conf_server = _this.conf.server) === null || _this_conf_server === void 0 ? void 0 : _this_conf_server.ssr;
303
295
  forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
304
296
  _this.routeRenderHandler = createRenderHandler({
@@ -312,7 +304,7 @@ var ModernServer = /*#__PURE__*/ function() {
312
304
  ];
313
305
  case 3:
314
306
  _state.sent();
315
- _this.addHandler(_this.staticFileHandler);
307
+ _this.addHandler(_this.setupStaticMiddleware((_this_conf_output = _this.conf.output) === null || _this_conf_output === void 0 ? void 0 : _this_conf_output.assetPrefix));
316
308
  _this.addHandler(faviconFallbackHandler);
317
309
  _this.addBeforeRouteHandler();
318
310
  _this.addHandler(_this.routeHandler.bind(_this));
@@ -608,25 +600,43 @@ var ModernServer = /*#__PURE__*/ function() {
608
600
  value: function prepareAPIHandler(extension) {
609
601
  var _this = this;
610
602
  return _asyncToGenerator(function() {
611
- var workDir, runner, conf, bff, prefix;
603
+ var workDir, runner, conf, bff, prefix, webOnly;
612
604
  return __generator(this, function(_state) {
613
- workDir = _this.workDir, runner = _this.runner, conf = _this.conf;
614
- bff = conf.bff;
615
- prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || "/api";
616
- return [
617
- 2,
618
- runner.prepareApiServer({
619
- pwd: workDir,
620
- config: extension,
621
- prefix: Array.isArray(prefix) ? prefix[0] : prefix,
622
- httpMethodDecider: bff === null || bff === void 0 ? void 0 : bff.httpMethodDecider,
623
- render: _this.render.bind(_this)
624
- }, {
625
- onLast: function() {
626
- return null;
605
+ switch(_state.label){
606
+ case 0:
607
+ workDir = _this.workDir, runner = _this.runner, conf = _this.conf;
608
+ bff = conf.bff;
609
+ prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || "/api";
610
+ return [
611
+ 4,
612
+ isWebOnly()
613
+ ];
614
+ case 1:
615
+ webOnly = _state.sent();
616
+ if (webOnly && process.env.NODE_ENV === "development") {
617
+ return [
618
+ 2,
619
+ function(req, res) {
620
+ res.setHeader("Content-Type", "text/plain");
621
+ res.end(JSON.stringify(""));
622
+ }
623
+ ];
627
624
  }
628
- })
629
- ];
625
+ return [
626
+ 2,
627
+ runner.prepareApiServer({
628
+ pwd: workDir,
629
+ config: extension,
630
+ prefix: Array.isArray(prefix) ? prefix[0] : prefix,
631
+ httpMethodDecider: bff === null || bff === void 0 ? void 0 : bff.httpMethodDecider,
632
+ render: _this.render.bind(_this)
633
+ }, {
634
+ onLast: function() {
635
+ return null;
636
+ }
637
+ })
638
+ ];
639
+ }
630
640
  });
631
641
  })();
632
642
  }
@@ -664,6 +674,18 @@ var ModernServer = /*#__PURE__*/ function() {
664
674
  })();
665
675
  }
666
676
  },
677
+ {
678
+ key: "setupStaticMiddleware",
679
+ value: function setupStaticMiddleware(prefix) {
680
+ var staticPathRegExp = getStaticReg(this.conf.output, this.conf.html, prefix);
681
+ return createStaticFileHandler([
682
+ {
683
+ path: staticPathRegExp,
684
+ target: this.distDir
685
+ }
686
+ ], prefix);
687
+ }
688
+ },
667
689
  {
668
690
  key: "handleAPI",
669
691
  value: function handleAPI(context) {
package/dist/esm/utils.js CHANGED
@@ -51,7 +51,7 @@ function _unsupportedIterableToArray(o, minLen) {
51
51
  if (n === "Map" || n === "Set") return Array.from(n);
52
52
  if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
53
53
  }
54
- import { createDebugger, isProd } from "@modern-js/utils";
54
+ import { createDebugger } from "@modern-js/utils";
55
55
  var debug = createDebugger("prod-server");
56
56
  var mergeExtension = function(users) {
57
57
  var output = [];
@@ -85,14 +85,10 @@ var createMiddlewareCollecter = function() {
85
85
  addAPIMiddleware: addAPIMiddleware
86
86
  };
87
87
  };
88
- var useLocalPrefix = function(url) {
89
- return isProd() && !url.includes(".");
90
- };
91
88
  var getStaticReg = function() {
92
- var output = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {}, html = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
93
- var tmp = output.distPath, _ref = tmp === void 0 ? {} : tmp, cssPath = _ref.css, jsPath = _ref.js, mediaPath = _ref.media, _output_assetPrefix = output.assetPrefix, assetPrefix = _output_assetPrefix === void 0 ? "/" : _output_assetPrefix;
89
+ var output = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {}, html = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}, prefix = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : "/";
90
+ var tmp = output.distPath, _ref = tmp === void 0 ? {} : tmp, cssPath = _ref.css, jsPath = _ref.js, mediaPath = _ref.media;
94
91
  var favicon = html.favicon, faviconByEntries = html.faviconByEntries;
95
- var prefix = useLocalPrefix(assetPrefix) ? assetPrefix : "";
96
92
  var favicons = prepareFavicons(favicon, faviconByEntries);
97
93
  var staticFiles = [
98
94
  cssPath,
@@ -144,4 +140,4 @@ var isRedirect = function(code) {
144
140
  308
145
141
  ].includes(code);
146
142
  };
147
- export { createErrorDocument, createMiddlewareCollecter, debug, getStaticReg, headersWithoutCookie, isRedirect, mergeExtension, noop, prepareFavicons, useLocalPrefix };
143
+ export { createErrorDocument, createMiddlewareCollecter, debug, getStaticReg, headersWithoutCookie, isRedirect, mergeExtension, noop, prepareFavicons };
@@ -4,6 +4,7 @@ import { Buffer } from "buffer";
4
4
  import createEtag from "etag";
5
5
  import fresh from "fresh";
6
6
  import { headersWithoutCookie } from "../../utils";
7
+ const MOCK_URL_BASE = "https://modernjs.dev/";
7
8
  class ModernServerContext {
8
9
  constructor(req, res, options) {
9
10
  /**
@@ -23,6 +24,10 @@ class ModernServerContext {
23
24
  get metrics() {
24
25
  return this.req.metrics;
25
26
  }
27
+ get parsedURL() {
28
+ const url = new URL(this.req.url, MOCK_URL_BASE);
29
+ return url;
30
+ }
26
31
  bind() {
27
32
  const { req, res } = this;
28
33
  req.get = (key) => this.getReqHeader(key);
@@ -114,23 +119,17 @@ class ModernServerContext {
114
119
  get href() {
115
120
  return this.origin + this.url;
116
121
  }
117
- get parsedURL() {
118
- const url = new URL(this.req.url, this.origin);
119
- return url;
120
- }
121
122
  get path() {
122
123
  return this.parsedURL.pathname;
123
124
  }
124
125
  set path(p) {
125
- const url = new URL(this.req.url, this.origin);
126
- if (!url || !p) {
126
+ if (!p) {
127
127
  return;
128
128
  }
129
- if (url.pathname === p) {
129
+ if (this.path === p) {
130
130
  return;
131
131
  }
132
- url.pathname = p;
133
- this.url = url.toString();
132
+ this.url = p + this.parsedURL.search;
134
133
  }
135
134
  get querystring() {
136
135
  if (!this.req) {
@@ -1,8 +1,8 @@
1
1
  import serve from "serve-static";
2
2
  import { isString, isRegExp } from "@modern-js/utils";
3
- import { useLocalPrefix } from "../utils";
4
3
  const removedPrefix = (req, prefix) => {
5
- if (useLocalPrefix(prefix)) {
4
+ const useLocalPrefix = !prefix.includes(".");
5
+ if (useLocalPrefix) {
6
6
  req.url = req.url.slice(prefix.length);
7
7
  return () => {
8
8
  req.url = prefix + req.url;
@@ -20,11 +20,10 @@ const faviconFallbackHandler = (context, next) => {
20
20
  next();
21
21
  }
22
22
  };
23
- const createStaticFileHandler = (rules, output = {}) => (
23
+ const createStaticFileHandler = (rules, assetPrefix = "/") => (
24
24
  // eslint-disable-next-line consistent-return
25
25
  async (context, next) => {
26
26
  const { url: requestUrl, req, res } = context;
27
- const { assetPrefix = "/" } = output;
28
27
  const hitRule = rules.find((item) => {
29
28
  if (isString(item.path) && requestUrl.startsWith(item.path)) {
30
29
  return true;
@@ -1,6 +1,12 @@
1
1
  import { createServer } from "http";
2
2
  import path from "path";
3
- import { fs, isPromise, mime, ROUTE_SPEC_FILE } from "@modern-js/utils";
3
+ import {
4
+ fs,
5
+ isPromise,
6
+ isWebOnly,
7
+ mime,
8
+ ROUTE_SPEC_FILE
9
+ } from "@modern-js/utils";
4
10
  import {
5
11
  RouteMatchManager
6
12
  } from "../libs/route";
@@ -67,7 +73,7 @@ class ModernServer {
67
73
  }
68
74
  // server prepare
69
75
  async onInit(runner, app) {
70
- var _a, _b;
76
+ var _a, _b, _c;
71
77
  this.runner = runner;
72
78
  const { distDir, staticGenerate, conf } = this;
73
79
  debug("final server conf", this.conf);
@@ -86,19 +92,6 @@ class ModernServer {
86
92
  this.warmupSSRBundle();
87
93
  await this.prepareFrameHandler();
88
94
  await this.prepareBeforeRouteHandler(usageRoutes, distDir);
89
- const staticPathRegExp = getStaticReg(
90
- this.conf.output || {},
91
- this.conf.html
92
- );
93
- this.staticFileHandler = createStaticFileHandler(
94
- [
95
- {
96
- path: staticPathRegExp,
97
- target: distDir
98
- }
99
- ],
100
- this.conf.output
101
- );
102
95
  const ssrConfig = (_b = this.conf.server) == null ? void 0 : _b.ssr;
103
96
  const forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
104
97
  this.routeRenderHandler = createRenderHandler({
@@ -107,7 +100,7 @@ class ModernServer {
107
100
  forceCSR
108
101
  });
109
102
  await this.setupBeforeProdMiddleware();
110
- this.addHandler(this.staticFileHandler);
103
+ this.addHandler(this.setupStaticMiddleware((_c = this.conf.output) == null ? void 0 : _c.assetPrefix));
111
104
  this.addHandler(faviconFallbackHandler);
112
105
  this.addBeforeRouteHandler();
113
106
  this.addHandler(this.routeHandler.bind(this));
@@ -226,6 +219,13 @@ class ModernServer {
226
219
  const { workDir, runner, conf } = this;
227
220
  const { bff } = conf;
228
221
  const prefix = (bff == null ? void 0 : bff.prefix) || "/api";
222
+ const webOnly = await isWebOnly();
223
+ if (webOnly && process.env.NODE_ENV === "development") {
224
+ return (req, res) => {
225
+ res.setHeader("Content-Type", "text/plain");
226
+ res.end(JSON.stringify(""));
227
+ };
228
+ }
229
229
  return runner.prepareApiServer(
230
230
  {
231
231
  pwd: workDir,
@@ -247,6 +247,22 @@ class ModernServer {
247
247
  this.addHandler(mid);
248
248
  });
249
249
  }
250
+ setupStaticMiddleware(prefix) {
251
+ const staticPathRegExp = getStaticReg(
252
+ this.conf.output,
253
+ this.conf.html,
254
+ prefix
255
+ );
256
+ return createStaticFileHandler(
257
+ [
258
+ {
259
+ path: staticPathRegExp,
260
+ target: this.distDir
261
+ }
262
+ ],
263
+ prefix
264
+ );
265
+ }
250
266
  async handleAPI(context) {
251
267
  const { req, res } = context;
252
268
  if (!this.frameAPIHandler) {
@@ -1,4 +1,4 @@
1
- import { createDebugger, isProd } from "@modern-js/utils";
1
+ import { createDebugger } from "@modern-js/utils";
2
2
  const debug = createDebugger("prod-server");
3
3
  const mergeExtension = (users) => {
4
4
  const output = [];
@@ -58,16 +58,9 @@ const createMiddlewareCollecter = () => {
58
58
  addAPIMiddleware
59
59
  };
60
60
  };
61
- const useLocalPrefix = (url) => {
62
- return isProd() && !url.includes(".");
63
- };
64
- const getStaticReg = (output = {}, html = {}) => {
65
- const {
66
- distPath: { css: cssPath, js: jsPath, media: mediaPath } = {},
67
- assetPrefix = "/"
68
- } = output;
61
+ const getStaticReg = (output = {}, html = {}, prefix = "/") => {
62
+ const { distPath: { css: cssPath, js: jsPath, media: mediaPath } = {} } = output;
69
63
  const { favicon, faviconByEntries } = html;
70
- const prefix = useLocalPrefix(assetPrefix) ? assetPrefix : "";
71
64
  const favicons = prepareFavicons(favicon, faviconByEntries);
72
65
  const staticFiles = [cssPath, jsPath, mediaPath].filter((v) => Boolean(v));
73
66
  const staticReg = ["static/", "upload/", ...staticFiles];
@@ -115,6 +108,5 @@ export {
115
108
  isRedirect,
116
109
  mergeExtension,
117
110
  noop,
118
- prepareFavicons,
119
- useLocalPrefix
111
+ prepareFavicons
120
112
  };