@modern-js/server 2.0.0-beta.3 → 2.0.0-beta.4
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/CHANGELOG.md +79 -0
- package/dist/js/modern/constants.js +10 -9
- package/dist/js/modern/dev-tools/dev-middleware/dev-server-plugin.js +14 -20
- package/dist/js/modern/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +14 -19
- package/dist/js/modern/dev-tools/dev-middleware/hmr-client/index.js +134 -184
- package/dist/js/modern/dev-tools/dev-middleware/hmr-client/types.d.js +0 -0
- package/dist/js/modern/dev-tools/dev-middleware/index.js +65 -60
- package/dist/js/modern/dev-tools/dev-middleware/socket-server.js +33 -59
- package/dist/js/modern/dev-tools/https/global.d.js +0 -0
- package/dist/js/modern/dev-tools/https/index.js +28 -5
- package/dist/js/modern/dev-tools/mock/getMockData.js +71 -45
- package/dist/js/modern/dev-tools/mock/index.js +34 -15
- package/dist/js/modern/dev-tools/register/index.js +71 -54
- package/dist/js/modern/dev-tools/watcher/dependency-tree.js +25 -34
- package/dist/js/modern/dev-tools/watcher/index.js +51 -33
- package/dist/js/modern/dev-tools/watcher/stats-cache.js +13 -20
- package/dist/js/modern/index.js +7 -4
- package/dist/js/modern/server/dev-server.js +191 -215
- package/dist/js/modern/server/index.js +7 -4
- package/dist/js/node/constants.js +27 -14
- package/dist/js/node/dev-tools/dev-middleware/dev-server-plugin.js +36 -24
- package/dist/js/node/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +31 -24
- package/dist/js/node/dev-tools/dev-middleware/hmr-client/index.js +56 -98
- package/dist/js/node/dev-tools/dev-middleware/hmr-client/types.d.js +0 -0
- package/dist/js/node/dev-tools/dev-middleware/index.js +94 -72
- package/dist/js/node/dev-tools/dev-middleware/socket-server.js +57 -66
- package/dist/js/node/dev-tools/https/global.d.js +0 -0
- package/dist/js/node/dev-tools/https/index.js +52 -13
- package/dist/js/node/dev-tools/mock/getMockData.js +88 -51
- package/dist/js/node/dev-tools/mock/index.js +62 -28
- package/dist/js/node/dev-tools/register/index.js +98 -64
- package/dist/js/node/dev-tools/watcher/dependency-tree.js +50 -43
- package/dist/js/node/dev-tools/watcher/index.js +79 -47
- package/dist/js/node/dev-tools/watcher/stats-cache.js +40 -30
- package/dist/js/node/index.js +25 -15
- package/dist/js/node/server/dev-server.js +234 -237
- package/dist/js/node/server/index.js +26 -11
- package/dist/js/treeshaking/constants.js +21 -0
- package/dist/js/treeshaking/dev-tools/dev-middleware/dev-server-plugin.js +74 -0
- package/dist/js/treeshaking/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +51 -0
- package/dist/js/treeshaking/dev-tools/dev-middleware/hmr-client/index.js +157 -0
- package/dist/js/treeshaking/dev-tools/dev-middleware/hmr-client/types.d.js +1 -0
- package/dist/js/treeshaking/dev-tools/dev-middleware/index.js +363 -0
- package/dist/js/treeshaking/dev-tools/dev-middleware/socket-server.js +209 -0
- package/dist/js/treeshaking/dev-tools/https/global.d.js +1 -0
- package/dist/js/treeshaking/dev-tools/https/index.js +161 -0
- package/dist/js/treeshaking/dev-tools/mock/getMockData.js +327 -0
- package/dist/js/treeshaking/dev-tools/mock/index.js +191 -0
- package/dist/js/treeshaking/dev-tools/register/index.js +153 -0
- package/dist/js/treeshaking/dev-tools/watcher/dependency-tree.js +150 -0
- package/dist/js/treeshaking/dev-tools/watcher/index.js +200 -0
- package/dist/js/treeshaking/dev-tools/watcher/stats-cache.js +128 -0
- package/dist/js/treeshaking/index.js +9 -0
- package/dist/js/treeshaking/server/dev-server.js +800 -0
- package/dist/js/treeshaking/server/index.js +92 -0
- package/dist/js/treeshaking/types.js +1 -0
- package/dist/types/dev-tools/mock/getMockData.d.ts +2 -0
- package/dist/types/dev-tools/watcher/dependency-tree.d.ts +2 -0
- package/dist/types/dev-tools/watcher/index.d.ts +1 -1
- package/dist/types/index.d.ts +2 -0
- package/package.json +16 -13
- package/temp-fix-hmr.js +483 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
2
|
+
try {
|
|
3
|
+
var info = gen[key](arg);
|
|
4
|
+
var value = info.value;
|
|
5
|
+
} catch (error) {
|
|
6
|
+
reject(error);
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
if (info.done) {
|
|
10
|
+
resolve(value);
|
|
11
|
+
} else {
|
|
12
|
+
Promise.resolve(value).then(_next, _throw);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function _asyncToGenerator(fn) {
|
|
16
|
+
return function() {
|
|
17
|
+
var self = this, args = arguments;
|
|
18
|
+
return new Promise(function(resolve, reject) {
|
|
19
|
+
var gen = fn.apply(self, args);
|
|
20
|
+
function _next(value) {
|
|
21
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
22
|
+
}
|
|
23
|
+
function _throw(err) {
|
|
24
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
25
|
+
}
|
|
26
|
+
_next(undefined);
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
var __generator = this && this.__generator || function(thisArg, body) {
|
|
31
|
+
var f, y, t, g, _ = {
|
|
32
|
+
label: 0,
|
|
33
|
+
sent: function() {
|
|
34
|
+
if (t[0] & 1) throw t[1];
|
|
35
|
+
return t[1];
|
|
36
|
+
},
|
|
37
|
+
trys: [],
|
|
38
|
+
ops: []
|
|
39
|
+
};
|
|
40
|
+
return(g = {
|
|
41
|
+
next: verb(0),
|
|
42
|
+
"throw": verb(1),
|
|
43
|
+
"return": verb(2)
|
|
44
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
45
|
+
return this;
|
|
46
|
+
}), g);
|
|
47
|
+
function verb(n) {
|
|
48
|
+
return function(v) {
|
|
49
|
+
return step([
|
|
50
|
+
n,
|
|
51
|
+
v
|
|
52
|
+
]);
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function step(op) {
|
|
56
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
57
|
+
while(_)try {
|
|
58
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
59
|
+
if (y = 0, t) op = [
|
|
60
|
+
op[0] & 2,
|
|
61
|
+
t.value
|
|
62
|
+
];
|
|
63
|
+
switch(op[0]){
|
|
64
|
+
case 0:
|
|
65
|
+
case 1:
|
|
66
|
+
t = op;
|
|
67
|
+
break;
|
|
68
|
+
case 4:
|
|
69
|
+
_.label++;
|
|
70
|
+
return {
|
|
71
|
+
value: op[1],
|
|
72
|
+
done: false
|
|
73
|
+
};
|
|
74
|
+
case 5:
|
|
75
|
+
_.label++;
|
|
76
|
+
y = op[1];
|
|
77
|
+
op = [
|
|
78
|
+
0
|
|
79
|
+
];
|
|
80
|
+
continue;
|
|
81
|
+
case 7:
|
|
82
|
+
op = _.ops.pop();
|
|
83
|
+
_.trys.pop();
|
|
84
|
+
continue;
|
|
85
|
+
default:
|
|
86
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
87
|
+
_ = 0;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
91
|
+
_.label = op[1];
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
95
|
+
_.label = t[1];
|
|
96
|
+
t = op;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
if (t && _.label < t[2]) {
|
|
100
|
+
_.label = t[2];
|
|
101
|
+
_.ops.push(op);
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
if (t[2]) _.ops.pop();
|
|
105
|
+
_.trys.pop();
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
op = body.call(thisArg, _);
|
|
109
|
+
} catch (e) {
|
|
110
|
+
op = [
|
|
111
|
+
6,
|
|
112
|
+
e
|
|
113
|
+
];
|
|
114
|
+
y = 0;
|
|
115
|
+
} finally{
|
|
116
|
+
f = t = 0;
|
|
117
|
+
}
|
|
118
|
+
if (op[0] & 5) throw op[1];
|
|
119
|
+
return {
|
|
120
|
+
value: op[0] ? op[1] : void 0,
|
|
121
|
+
done: true
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
import path from "path";
|
|
126
|
+
import { fs } from "@modern-js/utils";
|
|
127
|
+
import { AGGRED_DIR } from "@modern-js/prod-server";
|
|
128
|
+
import getMockData, { getMatched } from "./getMockData";
|
|
129
|
+
var createMockHandler = function(param) {
|
|
130
|
+
var pwd = param.pwd;
|
|
131
|
+
var exts = [
|
|
132
|
+
".ts",
|
|
133
|
+
".js"
|
|
134
|
+
];
|
|
135
|
+
var filepath = "";
|
|
136
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
137
|
+
try {
|
|
138
|
+
for(var _iterator = exts[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
139
|
+
var ext = _step.value;
|
|
140
|
+
var maybeMatch = path.join(pwd, "".concat(AGGRED_DIR.mock, "/index").concat(ext));
|
|
141
|
+
if (fs.existsSync(maybeMatch)) {
|
|
142
|
+
filepath = maybeMatch;
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
} catch (err) {
|
|
147
|
+
_didIteratorError = true;
|
|
148
|
+
_iteratorError = err;
|
|
149
|
+
} finally{
|
|
150
|
+
try {
|
|
151
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
152
|
+
_iterator.return();
|
|
153
|
+
}
|
|
154
|
+
} finally{
|
|
155
|
+
if (_didIteratorError) {
|
|
156
|
+
throw _iteratorError;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (!filepath) {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
var apiList = getMockData(filepath);
|
|
164
|
+
if (!apiList || apiList.length === 0) {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
return function() {
|
|
168
|
+
var _ref = _asyncToGenerator(function(context, next) {
|
|
169
|
+
var res, matched;
|
|
170
|
+
return __generator(this, function(_state) {
|
|
171
|
+
res = context.res;
|
|
172
|
+
matched = getMatched(context, apiList);
|
|
173
|
+
if (!matched) {
|
|
174
|
+
return [
|
|
175
|
+
2,
|
|
176
|
+
next()
|
|
177
|
+
];
|
|
178
|
+
}
|
|
179
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
180
|
+
return [
|
|
181
|
+
2,
|
|
182
|
+
matched.handler(context, next)
|
|
183
|
+
];
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
return function(context, next) {
|
|
187
|
+
return _ref.apply(this, arguments);
|
|
188
|
+
};
|
|
189
|
+
}();
|
|
190
|
+
};
|
|
191
|
+
export { createMockHandler };
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
function _defineProperty(obj, key, value) {
|
|
2
|
+
if (key in obj) {
|
|
3
|
+
Object.defineProperty(obj, key, {
|
|
4
|
+
value: value,
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true
|
|
8
|
+
});
|
|
9
|
+
} else {
|
|
10
|
+
obj[key] = value;
|
|
11
|
+
}
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
14
|
+
function _objectSpread(target) {
|
|
15
|
+
for(var i = 1; i < arguments.length; i++){
|
|
16
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
17
|
+
var ownKeys = Object.keys(source);
|
|
18
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
19
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
20
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
21
|
+
}));
|
|
22
|
+
}
|
|
23
|
+
ownKeys.forEach(function(key) {
|
|
24
|
+
_defineProperty(target, key, source[key]);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
return target;
|
|
28
|
+
}
|
|
29
|
+
function ownKeys(object, enumerableOnly) {
|
|
30
|
+
var keys = Object.keys(object);
|
|
31
|
+
if (Object.getOwnPropertySymbols) {
|
|
32
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
33
|
+
if (enumerableOnly) {
|
|
34
|
+
symbols = symbols.filter(function(sym) {
|
|
35
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
keys.push.apply(keys, symbols);
|
|
39
|
+
}
|
|
40
|
+
return keys;
|
|
41
|
+
}
|
|
42
|
+
function _objectSpreadProps(target, source) {
|
|
43
|
+
source = source != null ? source : {};
|
|
44
|
+
if (Object.getOwnPropertyDescriptors) {
|
|
45
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
46
|
+
} else {
|
|
47
|
+
ownKeys(Object(source)).forEach(function(key) {
|
|
48
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return target;
|
|
52
|
+
}
|
|
53
|
+
import path from "path";
|
|
54
|
+
import { resolveBabelConfig } from "@modern-js/server-utils";
|
|
55
|
+
import { fs, getAliasConfig, createDebugger } from "@modern-js/utils";
|
|
56
|
+
var debug = createDebugger("server");
|
|
57
|
+
var checkDep = function(depName, paths) {
|
|
58
|
+
var packagePath = "";
|
|
59
|
+
try {
|
|
60
|
+
packagePath = require.resolve(depName, {
|
|
61
|
+
paths: paths
|
|
62
|
+
});
|
|
63
|
+
} catch (error) {}
|
|
64
|
+
return Boolean(packagePath);
|
|
65
|
+
};
|
|
66
|
+
var enableRegister = function(projectRoot, config) {
|
|
67
|
+
var registerDirs = [
|
|
68
|
+
"./api",
|
|
69
|
+
"./server",
|
|
70
|
+
"./config/mock",
|
|
71
|
+
"./shared"
|
|
72
|
+
];
|
|
73
|
+
var TS_CONFIG_FILENAME = "tsconfig.json";
|
|
74
|
+
var tsconfigPath = path.resolve(projectRoot, TS_CONFIG_FILENAME);
|
|
75
|
+
var isTsProject = fs.existsSync(tsconfigPath);
|
|
76
|
+
var existTsNode = checkDep("ts-node", [
|
|
77
|
+
projectRoot
|
|
78
|
+
]);
|
|
79
|
+
var existTsConfigPaths = checkDep("tsconfig-paths", [
|
|
80
|
+
projectRoot
|
|
81
|
+
]);
|
|
82
|
+
if (isTsProject && existTsNode && existTsConfigPaths) {
|
|
83
|
+
var ref;
|
|
84
|
+
debug("use ts-node");
|
|
85
|
+
var distPath = ((ref = config.output.distPath) === null || ref === void 0 ? void 0 : ref.root) || "dist";
|
|
86
|
+
var tsNode = require("ts-node");
|
|
87
|
+
var tsConfigPaths = require("tsconfig-paths");
|
|
88
|
+
var alias = config.source.alias;
|
|
89
|
+
var aliasConfig = getAliasConfig(alias, {
|
|
90
|
+
appDirectory: projectRoot,
|
|
91
|
+
tsconfigPath: tsconfigPath
|
|
92
|
+
});
|
|
93
|
+
var _paths = aliasConfig.paths, paths = _paths === void 0 ? {} : _paths, _absoluteBaseUrl = aliasConfig.absoluteBaseUrl, absoluteBaseUrl = _absoluteBaseUrl === void 0 ? "./" : _absoluteBaseUrl;
|
|
94
|
+
var tsPaths = Object.keys(paths).reduce(function(o, key) {
|
|
95
|
+
var tsPath = paths[key];
|
|
96
|
+
if (typeof tsPath === "string" && path.isAbsolute(tsPath)) {
|
|
97
|
+
tsPath = path.relative(absoluteBaseUrl, tsPath);
|
|
98
|
+
}
|
|
99
|
+
if (typeof tsPath === "string") {
|
|
100
|
+
tsPath = [
|
|
101
|
+
tsPath
|
|
102
|
+
];
|
|
103
|
+
}
|
|
104
|
+
return _objectSpreadProps(_objectSpread({}, o), _defineProperty({}, "".concat(key), tsPath));
|
|
105
|
+
}, {});
|
|
106
|
+
tsConfigPaths.register({
|
|
107
|
+
baseUrl: absoluteBaseUrl || "./",
|
|
108
|
+
paths: tsPaths
|
|
109
|
+
});
|
|
110
|
+
tsNode.register({
|
|
111
|
+
project: tsconfigPath,
|
|
112
|
+
files: true,
|
|
113
|
+
transpileOnly: true,
|
|
114
|
+
ignore: [
|
|
115
|
+
"(?:^|/)node_modules/",
|
|
116
|
+
"(?:^|/)".concat(distPath, "/")
|
|
117
|
+
]
|
|
118
|
+
});
|
|
119
|
+
} else {
|
|
120
|
+
var ref1, ref2;
|
|
121
|
+
debug("use @babel/register");
|
|
122
|
+
var babelConfig = resolveBabelConfig(projectRoot, _objectSpreadProps(_objectSpread({}, config.source), {
|
|
123
|
+
babelConfig: (ref1 = config.tools) === null || ref1 === void 0 ? void 0 : ref1.babel,
|
|
124
|
+
server: {
|
|
125
|
+
compiler: (ref2 = config.server) === null || ref2 === void 0 ? void 0 : ref2.compiler
|
|
126
|
+
}
|
|
127
|
+
}), {
|
|
128
|
+
tsconfigPath: tsconfigPath,
|
|
129
|
+
syntax: "es6+",
|
|
130
|
+
type: "commonjs"
|
|
131
|
+
});
|
|
132
|
+
return require("@babel/register")(_objectSpreadProps(_objectSpread({}, babelConfig), {
|
|
133
|
+
only: [
|
|
134
|
+
function(filePath) {
|
|
135
|
+
if (filePath.includes("node_modules".concat(path.sep, ".modern-js"))) {
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
return registerDirs.some(function(registerDir) {
|
|
139
|
+
return filePath.startsWith(path.join(projectRoot, registerDir));
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
],
|
|
143
|
+
extensions: [
|
|
144
|
+
".js",
|
|
145
|
+
".ts"
|
|
146
|
+
],
|
|
147
|
+
babelrc: false,
|
|
148
|
+
configFile: false,
|
|
149
|
+
root: projectRoot
|
|
150
|
+
}));
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
export { enableRegister };
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
function _arrayLikeToArray(arr, len) {
|
|
2
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
3
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
4
|
+
return arr2;
|
|
5
|
+
}
|
|
6
|
+
function _arrayWithoutHoles(arr) {
|
|
7
|
+
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
|
|
8
|
+
}
|
|
9
|
+
function _classCallCheck(instance, Constructor) {
|
|
10
|
+
if (!(instance instanceof Constructor)) {
|
|
11
|
+
throw new TypeError("Cannot call a class as a function");
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function _defineProperties(target, props) {
|
|
15
|
+
for(var i = 0; i < props.length; i++){
|
|
16
|
+
var descriptor = props[i];
|
|
17
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
18
|
+
descriptor.configurable = true;
|
|
19
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
20
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function _createClass(Constructor, protoProps, staticProps) {
|
|
24
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
25
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
26
|
+
return Constructor;
|
|
27
|
+
}
|
|
28
|
+
function _defineProperty(obj, key, value) {
|
|
29
|
+
if (key in obj) {
|
|
30
|
+
Object.defineProperty(obj, key, {
|
|
31
|
+
value: value,
|
|
32
|
+
enumerable: true,
|
|
33
|
+
configurable: true,
|
|
34
|
+
writable: true
|
|
35
|
+
});
|
|
36
|
+
} else {
|
|
37
|
+
obj[key] = value;
|
|
38
|
+
}
|
|
39
|
+
return obj;
|
|
40
|
+
}
|
|
41
|
+
function _iterableToArray(iter) {
|
|
42
|
+
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
43
|
+
}
|
|
44
|
+
function _nonIterableSpread() {
|
|
45
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
46
|
+
}
|
|
47
|
+
function _toConsumableArray(arr) {
|
|
48
|
+
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
|
|
49
|
+
}
|
|
50
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
51
|
+
if (!o) return;
|
|
52
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
53
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
54
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
55
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
56
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
57
|
+
}
|
|
58
|
+
import minimatch from "minimatch";
|
|
59
|
+
var defaultIgnores = [
|
|
60
|
+
"**/bower_components/**",
|
|
61
|
+
"**/coverage/**",
|
|
62
|
+
"**/node_modules/**",
|
|
63
|
+
"**/.*/**",
|
|
64
|
+
"**/*.d.ts",
|
|
65
|
+
"**/*.log"
|
|
66
|
+
];
|
|
67
|
+
var DependencyTree = /*#__PURE__*/ function() {
|
|
68
|
+
"use strict";
|
|
69
|
+
function DependencyTree() {
|
|
70
|
+
_classCallCheck(this, DependencyTree);
|
|
71
|
+
_defineProperty(this, "tree", void 0);
|
|
72
|
+
_defineProperty(this, "ignore", void 0);
|
|
73
|
+
this.tree = /* @__PURE__ */ new Map();
|
|
74
|
+
this.ignore = _toConsumableArray(defaultIgnores);
|
|
75
|
+
}
|
|
76
|
+
_createClass(DependencyTree, [
|
|
77
|
+
{
|
|
78
|
+
key: "getNode",
|
|
79
|
+
value: function getNode(path) {
|
|
80
|
+
return this.tree.get(path);
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
key: "update",
|
|
85
|
+
value: function update(cache) {
|
|
86
|
+
var _this = this;
|
|
87
|
+
this.tree.clear();
|
|
88
|
+
Object.keys(cache).forEach(function(path) {
|
|
89
|
+
if (!_this.shouldIgnore(path)) {
|
|
90
|
+
var module = cache[path];
|
|
91
|
+
_this.tree.set(module.filename, {
|
|
92
|
+
module: module,
|
|
93
|
+
parent: /* @__PURE__ */ new Set(),
|
|
94
|
+
children: /* @__PURE__ */ new Set()
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
99
|
+
try {
|
|
100
|
+
var _this1 = this, _loop = function() {
|
|
101
|
+
var treeNode = _step.value;
|
|
102
|
+
var parent = treeNode.module.parent;
|
|
103
|
+
var children = treeNode.module.children;
|
|
104
|
+
if (parent && !_this1.shouldIgnore(parent.filename)) {
|
|
105
|
+
var parentTreeNode = _this1.tree.get(parent.filename);
|
|
106
|
+
if (parentTreeNode) {
|
|
107
|
+
treeNode.parent.add(parentTreeNode);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
children.forEach(function(child) {
|
|
111
|
+
if (!_this.shouldIgnore(child.filename)) {
|
|
112
|
+
var childTreeNode = _this.tree.get(child.filename);
|
|
113
|
+
if (childTreeNode) {
|
|
114
|
+
treeNode.children.add(childTreeNode);
|
|
115
|
+
childTreeNode.parent.add(treeNode);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
for(var _iterator = this.tree.values()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
|
|
121
|
+
} catch (err) {
|
|
122
|
+
_didIteratorError = true;
|
|
123
|
+
_iteratorError = err;
|
|
124
|
+
} finally{
|
|
125
|
+
try {
|
|
126
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
127
|
+
_iterator.return();
|
|
128
|
+
}
|
|
129
|
+
} finally{
|
|
130
|
+
if (_didIteratorError) {
|
|
131
|
+
throw _iteratorError;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
key: "shouldIgnore",
|
|
139
|
+
value: function shouldIgnore(path) {
|
|
140
|
+
return !path || Boolean(this.ignore.find(function(rule) {
|
|
141
|
+
return minimatch.match([
|
|
142
|
+
path
|
|
143
|
+
], rule).length > 0;
|
|
144
|
+
}));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
]);
|
|
148
|
+
return DependencyTree;
|
|
149
|
+
}();
|
|
150
|
+
export { DependencyTree, defaultIgnores };
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
function _arrayLikeToArray(arr, len) {
|
|
2
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
3
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
4
|
+
return arr2;
|
|
5
|
+
}
|
|
6
|
+
function _arrayWithoutHoles(arr) {
|
|
7
|
+
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
|
|
8
|
+
}
|
|
9
|
+
function _classCallCheck(instance, Constructor) {
|
|
10
|
+
if (!(instance instanceof Constructor)) {
|
|
11
|
+
throw new TypeError("Cannot call a class as a function");
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function _defineProperties(target, props) {
|
|
15
|
+
for(var i = 0; i < props.length; i++){
|
|
16
|
+
var descriptor = props[i];
|
|
17
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
18
|
+
descriptor.configurable = true;
|
|
19
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
20
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function _createClass(Constructor, protoProps, staticProps) {
|
|
24
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
25
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
26
|
+
return Constructor;
|
|
27
|
+
}
|
|
28
|
+
function _defineProperty(obj, key, value) {
|
|
29
|
+
if (key in obj) {
|
|
30
|
+
Object.defineProperty(obj, key, {
|
|
31
|
+
value: value,
|
|
32
|
+
enumerable: true,
|
|
33
|
+
configurable: true,
|
|
34
|
+
writable: true
|
|
35
|
+
});
|
|
36
|
+
} else {
|
|
37
|
+
obj[key] = value;
|
|
38
|
+
}
|
|
39
|
+
return obj;
|
|
40
|
+
}
|
|
41
|
+
function _iterableToArray(iter) {
|
|
42
|
+
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
43
|
+
}
|
|
44
|
+
function _nonIterableSpread() {
|
|
45
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
46
|
+
}
|
|
47
|
+
function _objectSpread(target) {
|
|
48
|
+
for(var i = 1; i < arguments.length; i++){
|
|
49
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
50
|
+
var ownKeys = Object.keys(source);
|
|
51
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
52
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
53
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
54
|
+
}));
|
|
55
|
+
}
|
|
56
|
+
ownKeys.forEach(function(key) {
|
|
57
|
+
_defineProperty(target, key, source[key]);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return target;
|
|
61
|
+
}
|
|
62
|
+
function _toConsumableArray(arr) {
|
|
63
|
+
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
|
|
64
|
+
}
|
|
65
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
66
|
+
if (!o) return;
|
|
67
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
68
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
69
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
70
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
71
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
72
|
+
}
|
|
73
|
+
import path from "path";
|
|
74
|
+
import { fs, chokidar } from "@modern-js/utils";
|
|
75
|
+
import { DependencyTree } from "./dependency-tree";
|
|
76
|
+
import { StatsCache } from "./stats-cache";
|
|
77
|
+
var defaultWatchOptions = {
|
|
78
|
+
ignoreInitial: true,
|
|
79
|
+
ignored: /api\/typings\/.*/
|
|
80
|
+
};
|
|
81
|
+
var getWatchedFiles = function(watcher) {
|
|
82
|
+
var watched = watcher.getWatched();
|
|
83
|
+
var files = [];
|
|
84
|
+
Object.keys(watched).forEach(function(dir) {
|
|
85
|
+
watched[dir].forEach(function(fileName) {
|
|
86
|
+
files.push(path.join(dir, fileName));
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
return files;
|
|
90
|
+
};
|
|
91
|
+
var mergeWatchOptions = function(options) {
|
|
92
|
+
var watchOptions = _objectSpread({}, options);
|
|
93
|
+
if (watchOptions) {
|
|
94
|
+
var ignored = watchOptions.ignored;
|
|
95
|
+
var finalIgnored = ignored ? [
|
|
96
|
+
defaultWatchOptions.ignored
|
|
97
|
+
].concat(_toConsumableArray(Array.isArray(ignored) ? ignored : [
|
|
98
|
+
ignored
|
|
99
|
+
])) : ignored;
|
|
100
|
+
if (finalIgnored) {
|
|
101
|
+
watchOptions.ignored = finalIgnored;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
var finalWatchOptions = _objectSpread({}, defaultWatchOptions, watchOptions);
|
|
105
|
+
return finalWatchOptions;
|
|
106
|
+
};
|
|
107
|
+
var Watcher = /*#__PURE__*/ function() {
|
|
108
|
+
"use strict";
|
|
109
|
+
function Watcher() {
|
|
110
|
+
_classCallCheck(this, Watcher);
|
|
111
|
+
_defineProperty(this, "dependencyTree", null);
|
|
112
|
+
_defineProperty(this, "watcher", void 0);
|
|
113
|
+
}
|
|
114
|
+
_createClass(Watcher, [
|
|
115
|
+
{
|
|
116
|
+
key: "listen",
|
|
117
|
+
value: function listen(files, options, callback) {
|
|
118
|
+
var watched = files.filter(Boolean);
|
|
119
|
+
var filenames = watched.map(function(filename) {
|
|
120
|
+
return filename.replace(/\\/g, "/");
|
|
121
|
+
});
|
|
122
|
+
var cache = new StatsCache();
|
|
123
|
+
var watcher = chokidar.watch(filenames, options);
|
|
124
|
+
watcher.on("ready", function() {
|
|
125
|
+
cache.add(getWatchedFiles(watcher));
|
|
126
|
+
});
|
|
127
|
+
watcher.on("change", function(changed) {
|
|
128
|
+
if (!fs.existsSync(changed) || cache.isDiff(changed)) {
|
|
129
|
+
cache.refresh(changed);
|
|
130
|
+
callback(changed, "change");
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
watcher.on("add", function(changed) {
|
|
134
|
+
if (!cache.has(changed)) {
|
|
135
|
+
cache.add([
|
|
136
|
+
changed
|
|
137
|
+
]);
|
|
138
|
+
callback(changed, "add");
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
watcher.on("unlink", function(changed) {
|
|
142
|
+
cache.del(changed);
|
|
143
|
+
callback(changed, "unlink");
|
|
144
|
+
});
|
|
145
|
+
this.watcher = watcher;
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
key: "createDepTree",
|
|
150
|
+
value: function createDepTree() {
|
|
151
|
+
this.dependencyTree = new DependencyTree();
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
key: "updateDepTree",
|
|
156
|
+
value: function updateDepTree() {
|
|
157
|
+
var ref;
|
|
158
|
+
(ref = this.dependencyTree) === null || ref === void 0 ? void 0 : ref.update(require.cache);
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
key: "cleanDepCache",
|
|
163
|
+
value: function cleanDepCache(filepath) {
|
|
164
|
+
var ref;
|
|
165
|
+
var node = (ref = this.dependencyTree) === null || ref === void 0 ? void 0 : ref.getNode(filepath);
|
|
166
|
+
if (node && require.cache[filepath]) {
|
|
167
|
+
delete require.cache[filepath];
|
|
168
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
169
|
+
try {
|
|
170
|
+
for(var _iterator = node.parent.values()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
171
|
+
var parentNode = _step.value;
|
|
172
|
+
this.cleanDepCache(parentNode.module.filename);
|
|
173
|
+
}
|
|
174
|
+
} catch (err) {
|
|
175
|
+
_didIteratorError = true;
|
|
176
|
+
_iteratorError = err;
|
|
177
|
+
} finally{
|
|
178
|
+
try {
|
|
179
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
180
|
+
_iterator.return();
|
|
181
|
+
}
|
|
182
|
+
} finally{
|
|
183
|
+
if (_didIteratorError) {
|
|
184
|
+
throw _iteratorError;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
key: "close",
|
|
193
|
+
value: function close() {
|
|
194
|
+
return this.watcher.close();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
]);
|
|
198
|
+
return Watcher;
|
|
199
|
+
}();
|
|
200
|
+
export { Watcher as default, defaultWatchOptions, getWatchedFiles, mergeWatchOptions };
|