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

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, {
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.13",
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": "9a38b66a84a6880370cac90d737f94f9c5f2f256"
66
66
  }