@modern-js/server 2.69.5 → 3.0.0-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.
- package/dist/cjs/createDevServer.js +98 -100
- package/dist/cjs/dev-tools/https/index.js +57 -49
- package/dist/cjs/dev-tools/watcher/dependencyTree.js +94 -92
- package/dist/cjs/dev-tools/watcher/index.js +129 -124
- package/dist/cjs/dev-tools/watcher/statsCache.js +84 -81
- package/dist/cjs/dev.js +104 -110
- package/dist/cjs/helpers/devOptions.js +55 -54
- package/dist/cjs/helpers/fileReader.js +48 -44
- package/dist/cjs/helpers/index.js +168 -107
- package/dist/cjs/helpers/mock.js +121 -124
- package/dist/cjs/helpers/repack.js +45 -39
- package/dist/cjs/helpers/utils.js +34 -26
- package/dist/cjs/index.js +33 -25
- package/dist/cjs/types.js +17 -15
- package/dist/esm/createDevServer.mjs +59 -0
- package/dist/esm/dev-tools/https/index.mjs +28 -0
- package/dist/esm/dev-tools/watcher/dependencyTree.mjs +57 -0
- package/dist/esm/dev-tools/watcher/index.mjs +88 -0
- package/dist/esm/dev-tools/watcher/statsCache.mjs +50 -0
- package/dist/esm/dev.mjs +77 -0
- package/dist/esm/helpers/devOptions.mjs +24 -0
- package/dist/esm/helpers/fileReader.mjs +18 -0
- package/dist/esm/helpers/index.mjs +65 -0
- package/dist/esm/helpers/mock.mjs +87 -0
- package/dist/esm/helpers/repack.mjs +16 -0
- package/dist/esm/helpers/utils.mjs +3 -0
- package/dist/{esm-node/index.js → esm/index.mjs} +1 -3
- package/dist/esm-node/createDevServer.mjs +59 -0
- package/dist/esm-node/dev-tools/https/index.mjs +28 -0
- package/dist/esm-node/dev-tools/watcher/dependencyTree.mjs +57 -0
- package/dist/esm-node/dev-tools/watcher/index.mjs +88 -0
- package/dist/esm-node/dev-tools/watcher/statsCache.mjs +50 -0
- package/dist/esm-node/dev.mjs +77 -0
- package/dist/esm-node/helpers/devOptions.mjs +24 -0
- package/dist/esm-node/helpers/fileReader.mjs +18 -0
- package/dist/esm-node/helpers/index.mjs +65 -0
- package/dist/esm-node/helpers/mock.mjs +87 -0
- package/dist/esm-node/helpers/repack.mjs +16 -0
- package/dist/esm-node/helpers/utils.mjs +3 -0
- package/dist/esm-node/index.mjs +2 -0
- package/dist/types/createDevServer.d.ts +1 -1
- package/dist/types/dev.d.ts +4 -4
- package/dist/types/helpers/devOptions.d.ts +10 -6
- package/dist/types/helpers/fileReader.d.ts +1 -1
- package/dist/types/index.d.ts +3 -1
- package/dist/types/types.d.ts +48 -12
- package/package.json +42 -31
- package/rslib.config.mts +4 -0
- package/dist/cjs/helpers/constants.js +0 -49
- package/dist/esm/createDevServer.js +0 -154
- package/dist/esm/dev-tools/https/index.js +0 -74
- package/dist/esm/dev-tools/watcher/dependencyTree.js +0 -88
- package/dist/esm/dev-tools/watcher/index.js +0 -120
- package/dist/esm/dev-tools/watcher/statsCache.js +0 -80
- package/dist/esm/dev.js +0 -126
- package/dist/esm/helpers/constants.js +0 -25
- package/dist/esm/helpers/devOptions.js +0 -35
- package/dist/esm/helpers/fileReader.js +0 -43
- package/dist/esm/helpers/index.js +0 -123
- package/dist/esm/helpers/mock.js +0 -266
- package/dist/esm/helpers/repack.js +0 -20
- package/dist/esm/helpers/utils.js +0 -5
- package/dist/esm/index.js +0 -4
- package/dist/esm-node/createDevServer.js +0 -71
- package/dist/esm-node/dev-tools/https/index.js +0 -30
- package/dist/esm-node/dev-tools/watcher/dependencyTree.js +0 -67
- package/dist/esm-node/dev-tools/watcher/index.js +0 -100
- package/dist/esm-node/dev-tools/watcher/statsCache.js +0 -58
- package/dist/esm-node/dev.js +0 -93
- package/dist/esm-node/helpers/constants.js +0 -25
- package/dist/esm-node/helpers/devOptions.js +0 -35
- package/dist/esm-node/helpers/fileReader.js +0 -24
- package/dist/esm-node/helpers/index.js +0 -73
- package/dist/esm-node/helpers/mock.js +0 -104
- package/dist/esm-node/helpers/repack.js +0 -20
- package/dist/esm-node/helpers/utils.js +0 -5
- package/dist/types/helpers/constants.d.ts +0 -2
- /package/dist/esm/{types.js → types.mjs} +0 -0
- /package/dist/esm-node/{types.js → types.mjs} +0 -0
package/dist/esm/helpers/mock.js
DELETED
|
@@ -1,266 +0,0 @@
|
|
|
1
|
-
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
-
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
|
|
3
|
-
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
4
|
-
import path from "node:path";
|
|
5
|
-
import { AGGRED_DIR } from "@modern-js/server-core";
|
|
6
|
-
import { connectMockMid2HonoMid } from "@modern-js/server-core/node";
|
|
7
|
-
import { fs } from "@modern-js/utils";
|
|
8
|
-
import { match } from "path-to-regexp";
|
|
9
|
-
var mockAPIs = [];
|
|
10
|
-
var mockConfig;
|
|
11
|
-
var parseKey = function(key) {
|
|
12
|
-
var _blank = " ";
|
|
13
|
-
var splitted = key.split(_blank).filter(Boolean);
|
|
14
|
-
if (splitted.length > 1) {
|
|
15
|
-
var _splitted = _sliced_to_array(splitted, 2), method = _splitted[0], pathname = _splitted[1];
|
|
16
|
-
return {
|
|
17
|
-
method: method.toLowerCase(),
|
|
18
|
-
path: pathname
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
return {
|
|
22
|
-
method: "get",
|
|
23
|
-
path: key
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
var getMockModule = function() {
|
|
27
|
-
var _ref = _async_to_generator(function(pwd) {
|
|
28
|
-
var exts, mockFilePath, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, ext, maybeMatch, err, _ref2, mockHandlers, config, enable;
|
|
29
|
-
return _ts_generator(this, function(_state) {
|
|
30
|
-
switch (_state.label) {
|
|
31
|
-
case 0:
|
|
32
|
-
exts = [
|
|
33
|
-
".ts",
|
|
34
|
-
".js"
|
|
35
|
-
];
|
|
36
|
-
mockFilePath = "";
|
|
37
|
-
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
38
|
-
_state.label = 1;
|
|
39
|
-
case 1:
|
|
40
|
-
_state.trys.push([
|
|
41
|
-
1,
|
|
42
|
-
6,
|
|
43
|
-
7,
|
|
44
|
-
8
|
|
45
|
-
]);
|
|
46
|
-
_iterator = exts[Symbol.iterator]();
|
|
47
|
-
_state.label = 2;
|
|
48
|
-
case 2:
|
|
49
|
-
if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done))
|
|
50
|
-
return [
|
|
51
|
-
3,
|
|
52
|
-
5
|
|
53
|
-
];
|
|
54
|
-
ext = _step.value;
|
|
55
|
-
maybeMatch = path.join(pwd, "".concat(AGGRED_DIR.mock, "/index").concat(ext));
|
|
56
|
-
return [
|
|
57
|
-
4,
|
|
58
|
-
fs.pathExists(maybeMatch)
|
|
59
|
-
];
|
|
60
|
-
case 3:
|
|
61
|
-
if (_state.sent()) {
|
|
62
|
-
mockFilePath = maybeMatch;
|
|
63
|
-
return [
|
|
64
|
-
3,
|
|
65
|
-
5
|
|
66
|
-
];
|
|
67
|
-
}
|
|
68
|
-
_state.label = 4;
|
|
69
|
-
case 4:
|
|
70
|
-
_iteratorNormalCompletion = true;
|
|
71
|
-
return [
|
|
72
|
-
3,
|
|
73
|
-
2
|
|
74
|
-
];
|
|
75
|
-
case 5:
|
|
76
|
-
return [
|
|
77
|
-
3,
|
|
78
|
-
8
|
|
79
|
-
];
|
|
80
|
-
case 6:
|
|
81
|
-
err = _state.sent();
|
|
82
|
-
_didIteratorError = true;
|
|
83
|
-
_iteratorError = err;
|
|
84
|
-
return [
|
|
85
|
-
3,
|
|
86
|
-
8
|
|
87
|
-
];
|
|
88
|
-
case 7:
|
|
89
|
-
try {
|
|
90
|
-
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
91
|
-
_iterator.return();
|
|
92
|
-
}
|
|
93
|
-
} finally {
|
|
94
|
-
if (_didIteratorError) {
|
|
95
|
-
throw _iteratorError;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return [
|
|
99
|
-
7
|
|
100
|
-
];
|
|
101
|
-
case 8:
|
|
102
|
-
if (!mockFilePath) {
|
|
103
|
-
return [
|
|
104
|
-
2,
|
|
105
|
-
void 0
|
|
106
|
-
];
|
|
107
|
-
}
|
|
108
|
-
return [
|
|
109
|
-
4,
|
|
110
|
-
import(mockFilePath)
|
|
111
|
-
];
|
|
112
|
-
case 9:
|
|
113
|
-
_ref2 = _state.sent(), mockHandlers = _ref2.default, config = _ref2.config;
|
|
114
|
-
enable = config === null || config === void 0 ? void 0 : config.enable;
|
|
115
|
-
if (enable === false) {
|
|
116
|
-
return [
|
|
117
|
-
2,
|
|
118
|
-
void 0
|
|
119
|
-
];
|
|
120
|
-
}
|
|
121
|
-
if (!mockHandlers) {
|
|
122
|
-
throw new Error("Mock file ".concat(mockFilePath, " parsed failed!"));
|
|
123
|
-
}
|
|
124
|
-
return [
|
|
125
|
-
2,
|
|
126
|
-
{
|
|
127
|
-
mockHandlers,
|
|
128
|
-
config
|
|
129
|
-
}
|
|
130
|
-
];
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
return function getMockModule2(pwd) {
|
|
135
|
-
return _ref.apply(this, arguments);
|
|
136
|
-
};
|
|
137
|
-
}();
|
|
138
|
-
var getMatched = function(request, mockApis) {
|
|
139
|
-
var targetPathname = request.path, targetMethod = request.method;
|
|
140
|
-
var matched = mockApis.find(function(mockApi) {
|
|
141
|
-
var method = mockApi.method, pathname = mockApi.path;
|
|
142
|
-
if (method.toLowerCase() === targetMethod.toLowerCase()) {
|
|
143
|
-
return match(pathname, {
|
|
144
|
-
encode: encodeURI,
|
|
145
|
-
decode: decodeURIComponent
|
|
146
|
-
})(targetPathname);
|
|
147
|
-
}
|
|
148
|
-
return false;
|
|
149
|
-
});
|
|
150
|
-
return matched;
|
|
151
|
-
};
|
|
152
|
-
function initOrUpdateMockMiddlewares(pwd) {
|
|
153
|
-
return _initOrUpdateMockMiddlewares.apply(this, arguments);
|
|
154
|
-
}
|
|
155
|
-
function _initOrUpdateMockMiddlewares() {
|
|
156
|
-
_initOrUpdateMockMiddlewares = _async_to_generator(function(pwd) {
|
|
157
|
-
var mockModule;
|
|
158
|
-
return _ts_generator(this, function(_state) {
|
|
159
|
-
switch (_state.label) {
|
|
160
|
-
case 0:
|
|
161
|
-
return [
|
|
162
|
-
4,
|
|
163
|
-
getMockModule(pwd)
|
|
164
|
-
];
|
|
165
|
-
case 1:
|
|
166
|
-
mockModule = _state.sent();
|
|
167
|
-
mockConfig = mockModule === null || mockModule === void 0 ? void 0 : mockModule.config;
|
|
168
|
-
mockAPIs = Object.entries((mockModule === null || mockModule === void 0 ? void 0 : mockModule.mockHandlers) || {}).map(function(param) {
|
|
169
|
-
var _param = _sliced_to_array(param, 2), key = _param[0], handler = _param[1];
|
|
170
|
-
var _parseKey = parseKey(key), method = _parseKey.method, path2 = _parseKey.path;
|
|
171
|
-
return {
|
|
172
|
-
method,
|
|
173
|
-
path: path2,
|
|
174
|
-
handler
|
|
175
|
-
};
|
|
176
|
-
});
|
|
177
|
-
return [
|
|
178
|
-
2
|
|
179
|
-
];
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
return _initOrUpdateMockMiddlewares.apply(this, arguments);
|
|
184
|
-
}
|
|
185
|
-
function getMockMiddleware(pwd) {
|
|
186
|
-
return _getMockMiddleware.apply(this, arguments);
|
|
187
|
-
}
|
|
188
|
-
function _getMockMiddleware() {
|
|
189
|
-
_getMockMiddleware = _async_to_generator(function(pwd) {
|
|
190
|
-
var mockMiddleware;
|
|
191
|
-
return _ts_generator(this, function(_state) {
|
|
192
|
-
switch (_state.label) {
|
|
193
|
-
case 0:
|
|
194
|
-
return [
|
|
195
|
-
4,
|
|
196
|
-
initOrUpdateMockMiddlewares(pwd)
|
|
197
|
-
];
|
|
198
|
-
case 1:
|
|
199
|
-
_state.sent();
|
|
200
|
-
mockMiddleware = function() {
|
|
201
|
-
var _ref = _async_to_generator(function(c, next) {
|
|
202
|
-
var isEnabled, matchedMockAPI, handler;
|
|
203
|
-
return _ts_generator(this, function(_state2) {
|
|
204
|
-
switch (_state2.label) {
|
|
205
|
-
case 0:
|
|
206
|
-
if (typeof (mockConfig === null || mockConfig === void 0 ? void 0 : mockConfig.enable) === "function") {
|
|
207
|
-
isEnabled = mockConfig.enable(c.env.node.req, c.env.node.res);
|
|
208
|
-
if (!isEnabled) {
|
|
209
|
-
return [
|
|
210
|
-
2,
|
|
211
|
-
next()
|
|
212
|
-
];
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
matchedMockAPI = getMatched(c.req, mockAPIs);
|
|
216
|
-
if (!matchedMockAPI)
|
|
217
|
-
return [
|
|
218
|
-
3,
|
|
219
|
-
3
|
|
220
|
-
];
|
|
221
|
-
handler = matchedMockAPI.handler;
|
|
222
|
-
if (!(typeof handler === "function"))
|
|
223
|
-
return [
|
|
224
|
-
3,
|
|
225
|
-
2
|
|
226
|
-
];
|
|
227
|
-
return [
|
|
228
|
-
4,
|
|
229
|
-
connectMockMid2HonoMid(handler)(c, next)
|
|
230
|
-
];
|
|
231
|
-
case 1:
|
|
232
|
-
return [
|
|
233
|
-
2,
|
|
234
|
-
_state2.sent()
|
|
235
|
-
];
|
|
236
|
-
case 2:
|
|
237
|
-
return [
|
|
238
|
-
2,
|
|
239
|
-
c.json(handler)
|
|
240
|
-
];
|
|
241
|
-
case 3:
|
|
242
|
-
return [
|
|
243
|
-
2,
|
|
244
|
-
next()
|
|
245
|
-
];
|
|
246
|
-
}
|
|
247
|
-
});
|
|
248
|
-
});
|
|
249
|
-
return function mockMiddleware2(c, next) {
|
|
250
|
-
return _ref.apply(this, arguments);
|
|
251
|
-
};
|
|
252
|
-
}();
|
|
253
|
-
return [
|
|
254
|
-
2,
|
|
255
|
-
mockMiddleware
|
|
256
|
-
];
|
|
257
|
-
}
|
|
258
|
-
});
|
|
259
|
-
});
|
|
260
|
-
return _getMockMiddleware.apply(this, arguments);
|
|
261
|
-
}
|
|
262
|
-
export {
|
|
263
|
-
getMatched,
|
|
264
|
-
getMockMiddleware,
|
|
265
|
-
initOrUpdateMockMiddlewares
|
|
266
|
-
};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { fileReader } from "@modern-js/runtime-utils/fileReader";
|
|
2
|
-
var cleanSSRCache = function(distDir) {
|
|
3
|
-
Object.keys(require.cache).forEach(function(key) {
|
|
4
|
-
if (key.startsWith(distDir)) {
|
|
5
|
-
delete require.cache[key];
|
|
6
|
-
}
|
|
7
|
-
});
|
|
8
|
-
};
|
|
9
|
-
var onRepack = function(distDir, hooks) {
|
|
10
|
-
cleanSSRCache(distDir);
|
|
11
|
-
fileReader.reset();
|
|
12
|
-
hooks.onReset.call({
|
|
13
|
-
event: {
|
|
14
|
-
type: "repack"
|
|
15
|
-
}
|
|
16
|
-
});
|
|
17
|
-
};
|
|
18
|
-
export {
|
|
19
|
-
onRepack
|
|
20
|
-
};
|
package/dist/esm/index.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import { createServerBase } from "@modern-js/server-core";
|
|
3
|
-
import { createNodeServer, loadServerRuntimeConfig } from "@modern-js/server-core/node";
|
|
4
|
-
import { devPlugin } from "./dev";
|
|
5
|
-
import { getDevAssetPrefix, getDevOptions } from "./helpers";
|
|
6
|
-
async function createDevServer(options, applyPlugins) {
|
|
7
|
-
var _config_output_distPath;
|
|
8
|
-
const { config, pwd, serverConfigFile, serverConfigPath, builder, metaName } = options;
|
|
9
|
-
const dev = getDevOptions(options);
|
|
10
|
-
const distDir = path.resolve(pwd, ((_config_output_distPath = config.output.distPath) === null || _config_output_distPath === void 0 ? void 0 : _config_output_distPath.root) || "dist");
|
|
11
|
-
const serverConfig = await loadServerRuntimeConfig(distDir, serverConfigFile, serverConfigPath, metaName) || {};
|
|
12
|
-
const prodServerOptions = {
|
|
13
|
-
...options,
|
|
14
|
-
pwd: distDir,
|
|
15
|
-
serverConfig: {
|
|
16
|
-
...serverConfig,
|
|
17
|
-
...options.serverConfig
|
|
18
|
-
},
|
|
19
|
-
/**
|
|
20
|
-
* 1. server plugins from modern.server.ts
|
|
21
|
-
* 2. server plugins register by cli use _internalServerPlugins
|
|
22
|
-
* Merge plugins, the plugins from modern.server.ts will run first
|
|
23
|
-
*/
|
|
24
|
-
plugins: [
|
|
25
|
-
...serverConfig.plugins || [],
|
|
26
|
-
...options.plugins || []
|
|
27
|
-
]
|
|
28
|
-
};
|
|
29
|
-
const server = createServerBase(prodServerOptions);
|
|
30
|
-
const devHttpsOption = typeof dev === "object" && dev.https;
|
|
31
|
-
const isHttp2 = devHttpsOption && typeof dev.proxy === "undefined";
|
|
32
|
-
let nodeServer;
|
|
33
|
-
if (devHttpsOption) {
|
|
34
|
-
const { genHttpsOptions } = await import("./dev-tools/https");
|
|
35
|
-
const httpsOptions = await genHttpsOptions(devHttpsOption, pwd);
|
|
36
|
-
nodeServer = await createNodeServer(server.handle.bind(server), httpsOptions, isHttp2);
|
|
37
|
-
} else {
|
|
38
|
-
nodeServer = await createNodeServer(server.handle.bind(server));
|
|
39
|
-
}
|
|
40
|
-
const promise = getDevAssetPrefix(builder);
|
|
41
|
-
let compiler = null;
|
|
42
|
-
builder === null || builder === void 0 ? void 0 : builder.onAfterCreateCompiler((context) => {
|
|
43
|
-
compiler = context.compiler;
|
|
44
|
-
});
|
|
45
|
-
const builderDevServer = await (builder === null || builder === void 0 ? void 0 : builder.createDevServer({
|
|
46
|
-
runCompile: options.runCompile,
|
|
47
|
-
compiler: options.compiler
|
|
48
|
-
}));
|
|
49
|
-
server.addPlugins([
|
|
50
|
-
devPlugin({
|
|
51
|
-
...options,
|
|
52
|
-
builderDevServer
|
|
53
|
-
}, compiler)
|
|
54
|
-
]);
|
|
55
|
-
const assetPrefix = await promise;
|
|
56
|
-
if (assetPrefix) {
|
|
57
|
-
prodServerOptions.config.output.assetPrefix = assetPrefix;
|
|
58
|
-
}
|
|
59
|
-
await applyPlugins(server, prodServerOptions, nodeServer);
|
|
60
|
-
await server.init();
|
|
61
|
-
const afterListen = async () => {
|
|
62
|
-
await (builderDevServer === null || builderDevServer === void 0 ? void 0 : builderDevServer.afterListen());
|
|
63
|
-
};
|
|
64
|
-
return {
|
|
65
|
-
server: nodeServer,
|
|
66
|
-
afterListen
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
export {
|
|
70
|
-
createDevServer
|
|
71
|
-
};
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { chalk, getPackageManager, logger } from "@modern-js/utils";
|
|
2
|
-
const genHttpsOptions = async (userOptions, pwd) => {
|
|
3
|
-
const httpsOptions = typeof userOptions === "boolean" ? {} : userOptions;
|
|
4
|
-
if (!httpsOptions.key || !httpsOptions.cert) {
|
|
5
|
-
let devcertPath;
|
|
6
|
-
try {
|
|
7
|
-
devcertPath = require.resolve("devcert", {
|
|
8
|
-
paths: [
|
|
9
|
-
pwd,
|
|
10
|
-
__dirname
|
|
11
|
-
]
|
|
12
|
-
});
|
|
13
|
-
} catch (err) {
|
|
14
|
-
const packageManager = await getPackageManager(pwd);
|
|
15
|
-
const command = chalk.yellow.bold(`${packageManager} add devcert@1.2.2 -D`);
|
|
16
|
-
logger.error(`You have enabled "dev.https" option, but the "devcert" package is not installed.`);
|
|
17
|
-
logger.error(`Please run ${command} to install manually, otherwise the https can not work.`);
|
|
18
|
-
throw new Error('[https] "devcert" is not found.');
|
|
19
|
-
}
|
|
20
|
-
const devcert = require(devcertPath);
|
|
21
|
-
const selfsign = await devcert.certificateFor([
|
|
22
|
-
"localhost"
|
|
23
|
-
]);
|
|
24
|
-
return selfsign;
|
|
25
|
-
}
|
|
26
|
-
return httpsOptions;
|
|
27
|
-
};
|
|
28
|
-
export {
|
|
29
|
-
genHttpsOptions
|
|
30
|
-
};
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import minimatch from "minimatch";
|
|
2
|
-
const defaultIgnores = [
|
|
3
|
-
"**/coverage/**",
|
|
4
|
-
"**/node_modules/**",
|
|
5
|
-
"**/.*/**",
|
|
6
|
-
"**/*.d.ts",
|
|
7
|
-
"**/*.log"
|
|
8
|
-
];
|
|
9
|
-
class DependencyTree {
|
|
10
|
-
getNode(path) {
|
|
11
|
-
return this.tree.get(path);
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* update dependency tree
|
|
15
|
-
*
|
|
16
|
-
* @param cache
|
|
17
|
-
*/
|
|
18
|
-
update(cache) {
|
|
19
|
-
this.tree.clear();
|
|
20
|
-
Object.keys(cache).forEach((path) => {
|
|
21
|
-
if (!this.shouldIgnore(path)) {
|
|
22
|
-
const module = cache[path];
|
|
23
|
-
this.tree.set(module.filename, {
|
|
24
|
-
module,
|
|
25
|
-
parent: /* @__PURE__ */ new Set(),
|
|
26
|
-
children: /* @__PURE__ */ new Set()
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
for (const treeNode of this.tree.values()) {
|
|
31
|
-
const { parent } = treeNode.module;
|
|
32
|
-
const { children } = treeNode.module;
|
|
33
|
-
if (parent && !this.shouldIgnore(parent.filename)) {
|
|
34
|
-
const parentTreeNode = this.tree.get(parent.filename);
|
|
35
|
-
if (parentTreeNode) {
|
|
36
|
-
treeNode.parent.add(parentTreeNode);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
children === null || children === void 0 ? void 0 : children.forEach((child) => {
|
|
40
|
-
if (!this.shouldIgnore(child.filename)) {
|
|
41
|
-
const childTreeNode = this.tree.get(child.filename);
|
|
42
|
-
if (childTreeNode) {
|
|
43
|
-
treeNode.children.add(childTreeNode);
|
|
44
|
-
childTreeNode.parent.add(treeNode);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
shouldIgnore(path) {
|
|
51
|
-
return !path || Boolean(this.ignore.find((rule) => minimatch.match([
|
|
52
|
-
path
|
|
53
|
-
], rule, {
|
|
54
|
-
dot: true
|
|
55
|
-
}).length > 0));
|
|
56
|
-
}
|
|
57
|
-
constructor() {
|
|
58
|
-
this.tree = /* @__PURE__ */ new Map();
|
|
59
|
-
this.ignore = [
|
|
60
|
-
...defaultIgnores
|
|
61
|
-
];
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
export {
|
|
65
|
-
DependencyTree,
|
|
66
|
-
defaultIgnores
|
|
67
|
-
};
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import { fs, chokidar } from "@modern-js/utils";
|
|
3
|
-
import { DependencyTree } from "./dependencyTree";
|
|
4
|
-
import { StatsCache } from "./statsCache";
|
|
5
|
-
const defaultWatchOptions = {
|
|
6
|
-
// 初始化的时候不触发 add、addDir 事件
|
|
7
|
-
ignoreInitial: true,
|
|
8
|
-
ignored: /api\/typings\/.*/
|
|
9
|
-
};
|
|
10
|
-
const getWatchedFiles = (watcher) => {
|
|
11
|
-
const watched = watcher.getWatched();
|
|
12
|
-
const files = [];
|
|
13
|
-
Object.keys(watched).forEach((dir) => {
|
|
14
|
-
watched[dir].forEach((fileName) => {
|
|
15
|
-
files.push(path.join(dir, fileName));
|
|
16
|
-
});
|
|
17
|
-
});
|
|
18
|
-
return files;
|
|
19
|
-
};
|
|
20
|
-
const mergeWatchOptions = (options) => {
|
|
21
|
-
const watchOptions = {
|
|
22
|
-
...options
|
|
23
|
-
};
|
|
24
|
-
if (watchOptions) {
|
|
25
|
-
const { ignored } = watchOptions;
|
|
26
|
-
const finalIgnored = ignored ? [
|
|
27
|
-
defaultWatchOptions.ignored,
|
|
28
|
-
...Array.isArray(ignored) ? ignored : [
|
|
29
|
-
ignored
|
|
30
|
-
]
|
|
31
|
-
] : ignored;
|
|
32
|
-
if (finalIgnored) {
|
|
33
|
-
watchOptions.ignored = finalIgnored;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
const finalWatchOptions = {
|
|
37
|
-
...defaultWatchOptions,
|
|
38
|
-
...watchOptions
|
|
39
|
-
};
|
|
40
|
-
return finalWatchOptions;
|
|
41
|
-
};
|
|
42
|
-
class Watcher {
|
|
43
|
-
listen(files, options, callback) {
|
|
44
|
-
const watched = files.filter(Boolean);
|
|
45
|
-
const filenames = watched.map((filename) => filename.replace(/\\/g, "/"));
|
|
46
|
-
const cache = new StatsCache();
|
|
47
|
-
const watcher = chokidar.watch(filenames, options);
|
|
48
|
-
watcher.on("ready", () => {
|
|
49
|
-
cache.add(getWatchedFiles(watcher));
|
|
50
|
-
});
|
|
51
|
-
watcher.on("change", (changed) => {
|
|
52
|
-
if (!fs.existsSync(changed) || cache.isDiff(changed)) {
|
|
53
|
-
cache.refresh(changed);
|
|
54
|
-
callback(changed, "change");
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
watcher.on("add", (changed) => {
|
|
58
|
-
if (!cache.has(changed)) {
|
|
59
|
-
cache.add([
|
|
60
|
-
changed
|
|
61
|
-
]);
|
|
62
|
-
callback(changed, "add");
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
watcher.on("unlink", (changed) => {
|
|
66
|
-
cache.del(changed);
|
|
67
|
-
callback(changed, "unlink");
|
|
68
|
-
});
|
|
69
|
-
this.watcher = watcher;
|
|
70
|
-
}
|
|
71
|
-
createDepTree() {
|
|
72
|
-
this.dependencyTree = new DependencyTree();
|
|
73
|
-
}
|
|
74
|
-
updateDepTree() {
|
|
75
|
-
var _this_dependencyTree;
|
|
76
|
-
(_this_dependencyTree = this.dependencyTree) === null || _this_dependencyTree === void 0 ? void 0 : _this_dependencyTree.update(require.cache);
|
|
77
|
-
}
|
|
78
|
-
cleanDepCache(filepath) {
|
|
79
|
-
var _this_dependencyTree;
|
|
80
|
-
const node = (_this_dependencyTree = this.dependencyTree) === null || _this_dependencyTree === void 0 ? void 0 : _this_dependencyTree.getNode(filepath);
|
|
81
|
-
if (node && require.cache[filepath]) {
|
|
82
|
-
delete require.cache[filepath];
|
|
83
|
-
for (const parentNode of node.parent.values()) {
|
|
84
|
-
this.cleanDepCache(parentNode.module.filename);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
close() {
|
|
89
|
-
return this.watcher.close();
|
|
90
|
-
}
|
|
91
|
-
constructor() {
|
|
92
|
-
this.dependencyTree = null;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
export {
|
|
96
|
-
Watcher as default,
|
|
97
|
-
defaultWatchOptions,
|
|
98
|
-
getWatchedFiles,
|
|
99
|
-
mergeWatchOptions
|
|
100
|
-
};
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import crypto from "crypto";
|
|
2
|
-
import fs from "fs";
|
|
3
|
-
class StatsCache {
|
|
4
|
-
add(files) {
|
|
5
|
-
const { cachedHash, cachedSize } = this;
|
|
6
|
-
for (const filename of files) {
|
|
7
|
-
if (fs.existsSync(filename)) {
|
|
8
|
-
const stats = fs.statSync(filename);
|
|
9
|
-
if (stats.isFile() && !cachedHash[filename]) {
|
|
10
|
-
cachedHash[filename] = this.hash(stats, filename);
|
|
11
|
-
cachedSize[filename] = stats.size;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
refresh(filename) {
|
|
17
|
-
const { cachedHash, cachedSize } = this;
|
|
18
|
-
if (fs.existsSync(filename)) {
|
|
19
|
-
const stats = fs.statSync(filename);
|
|
20
|
-
if (stats.isFile()) {
|
|
21
|
-
cachedHash[filename] = this.hash(stats, filename);
|
|
22
|
-
cachedSize[filename] = stats.size;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
del(filename) {
|
|
27
|
-
if (this.cachedHash[filename]) {
|
|
28
|
-
delete this.cachedHash[filename];
|
|
29
|
-
delete this.cachedSize[filename];
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
isDiff(filename) {
|
|
33
|
-
const { cachedHash, cachedSize } = this;
|
|
34
|
-
const stats = fs.statSync(filename);
|
|
35
|
-
const hash = cachedHash[filename];
|
|
36
|
-
const size = cachedSize[filename];
|
|
37
|
-
if (stats.size !== size) {
|
|
38
|
-
return true;
|
|
39
|
-
}
|
|
40
|
-
if (this.hash(stats, filename) !== hash) {
|
|
41
|
-
return true;
|
|
42
|
-
}
|
|
43
|
-
return false;
|
|
44
|
-
}
|
|
45
|
-
has(filename) {
|
|
46
|
-
return Boolean(this.cachedHash[filename]);
|
|
47
|
-
}
|
|
48
|
-
hash(stats, filename) {
|
|
49
|
-
return crypto.createHash("md5").update(fs.readFileSync(filename)).digest("hex");
|
|
50
|
-
}
|
|
51
|
-
constructor() {
|
|
52
|
-
this.cachedHash = {};
|
|
53
|
-
this.cachedSize = {};
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
export {
|
|
57
|
-
StatsCache
|
|
58
|
-
};
|