@midwayjs/core 4.0.0-beta.12 → 4.0.0-beta.14

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.
@@ -13,6 +13,7 @@ export declare class CommonJSFileDetector extends AbstractFileDetector<{
13
13
  pattern?: string | string[];
14
14
  ignore?: string | string[];
15
15
  conflictCheck?: boolean;
16
+ importQuery?: boolean | string | (() => string);
16
17
  }> {
17
18
  private duplicateModuleCheckSet;
18
19
  run(container: IMidwayGlobalContainer, namespace: string): Promise<void>;
@@ -8,6 +8,7 @@ const constants_1 = require("../constants");
8
8
  const util_1 = require("../util");
9
9
  const decorator_1 = require("../decorator");
10
10
  const util_2 = require("util");
11
+ const path_1 = require("path");
11
12
  const debug = (0, util_2.debuglog)('midway:debug');
12
13
  class AbstractFileDetector {
13
14
  options;
@@ -82,7 +83,21 @@ class CommonJSFileDetector extends AbstractFileDetector {
82
83
  async loadAsync(container, namespace) {
83
84
  this.options = this.options || {};
84
85
  const loadDirs = [].concat(this.options.loadDir ?? container.get('baseDir'));
85
- const importQuery = `${Date.now()}_${Math.random()}`;
86
+ let importQuery;
87
+ const envImportQueryEnabled = process.env.MIDWAY_HMR_IMPORT_QUERY === '1';
88
+ const envImportQueryFile = process.env.MIDWAY_HMR_IMPORT_QUERY_FILE;
89
+ if (typeof this.options.importQuery === 'function') {
90
+ importQuery = this.options.importQuery();
91
+ }
92
+ else if (typeof this.options.importQuery === 'string') {
93
+ importQuery = this.options.importQuery;
94
+ }
95
+ else if (this.options.importQuery) {
96
+ importQuery = `${Date.now()}_${Math.random()}`;
97
+ }
98
+ else if (envImportQueryEnabled && !envImportQueryFile) {
99
+ importQuery = `${Date.now()}_${Math.random()}`;
100
+ }
86
101
  for (const dir of loadDirs) {
87
102
  const fileResults = (0, glob_1.run)(DEFAULT_GLOB_PATTERN.concat(this.options.pattern || []), {
88
103
  cwd: dir,
@@ -104,9 +119,18 @@ class CommonJSFileDetector extends AbstractFileDetector {
104
119
  }
105
120
  };
106
121
  for (const file of fileResults) {
122
+ let currentImportQuery = importQuery;
123
+ if (!this.options.importQuery &&
124
+ envImportQueryEnabled &&
125
+ envImportQueryFile) {
126
+ currentImportQuery =
127
+ (0, path_1.resolve)(file) === (0, path_1.resolve)(envImportQueryFile)
128
+ ? `${Date.now()}_${Math.random()}`
129
+ : undefined;
130
+ }
107
131
  const exports = await (0, util_1.loadModule)(file, {
108
132
  loadMode: 'esm',
109
- importQuery,
133
+ importQuery: currentImportQuery,
110
134
  });
111
135
  // add module to set
112
136
  container.bindClass(exports, {
@@ -108,7 +108,7 @@ let MidwayMiddlewareService = class MidwayMiddlewareService {
108
108
  * 2、如果 result 有值(非 undefined),则不管什么情况,都会覆盖当前 body,注意,这里有可能赋值 null,导致 status 为 204,会在中间件处进行修正
109
109
  * 3、如果 result 没值,且 ctx.body 已经赋值,则向 result 赋值
110
110
  */
111
- if (result !== undefined) {
111
+ if (result !== undefined && result !== context['body']) {
112
112
  context['body'] = result;
113
113
  }
114
114
  else if (context['body'] !== undefined) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/core",
3
- "version": "4.0.0-beta.12",
3
+ "version": "4.0.0-beta.14",
4
4
  "description": "midway core",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -62,5 +62,5 @@
62
62
  "types": "./dist/functional/index.d.ts"
63
63
  }
64
64
  },
65
- "gitHead": "1c48179b7c827ba8ec9351e9b6c36ec1726d3e11"
65
+ "gitHead": "52335cb1baa00754fffd48487f6153a3b2f1f50a"
66
66
  }