DepInfo.js
6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.BUILD_STATUS = void 0;
function _react() {
const data = _interopRequireDefault(require("react"));
_react = function _react() {
return data;
};
return data;
}
function _utils() {
const data = require("@umijs/utils");
_utils = function _utils() {
return data;
};
return data;
}
function _assert() {
const data = _interopRequireDefault(require("assert"));
_assert = function _assert() {
return data;
};
return data;
}
function _fs() {
const data = require("fs");
_fs = function _fs() {
return data;
};
return data;
}
function _path() {
const data = require("path");
_path = function _path() {
return data;
};
return data;
}
var _constants = require("./constants");
var _getDepVersion = require("./getDepVersion");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const debug = (0, _utils().createDebug)('umi:mfsu:DepInfo');
let BUILD_STATUS;
exports.BUILD_STATUS = BUILD_STATUS;
(function (BUILD_STATUS) {
BUILD_STATUS[BUILD_STATUS["IDLE"] = 0] = "IDLE";
BUILD_STATUS[BUILD_STATUS["BUILDING"] = 1] = "BUILDING";
BUILD_STATUS[BUILD_STATUS["SUCCESS"] = 2] = "SUCCESS";
BUILD_STATUS[BUILD_STATUS["FAIL"] = 3] = "FAIL";
})(BUILD_STATUS || (exports.BUILD_STATUS = BUILD_STATUS = {}));
class DepInfo {
constructor(opts) {
this.data = void 0;
this.cacheDir = void 0;
this.cwd = void 0;
this.mode = void 0;
this.tmpDeps = void 0;
this.cachePath = void 0;
this.webpackAlias = void 0;
this.api = void 0;
this.api = opts.api;
this.cwd = opts.cwd;
this.cacheDir = opts.tmpDir;
this.mode = opts.mode;
this.tmpDeps = {};
this.webpackAlias = opts.webpackAlias || {};
this.cachePath = (0, _path().join)(this.cacheDir, _constants.DEP_INFO_CACHE_FILE);
(0, _assert().default)(['development', 'production'].includes(this.mode), `[MFSU] Unsupported mode ${this.mode}`);
this.data = {
deps: {},
config: {}
};
}
loadCache() {
const path = this.cachePath;
try {
if ((0, _fs().existsSync)(path)) {
debug('cache exists');
const data = JSON.parse((0, _fs().readFileSync)(path, 'utf-8'));
(0, _assert().default)(data.deps, `[MFSU] cache parse failed, deps not found.`);
const normalizedDeps = {};
Object.keys(data.deps).forEach(key => {
const normalizeKey = key.replace(_constants.CWD, (0, _utils().winPath)(this.api.cwd));
normalizedDeps[normalizeKey] = data.deps[key];
});
data.deps = normalizedDeps;
this.data = data;
}
} catch (e) {
throw new Error(`${e.message}, try to remove cache file and retry.`);
}
}
addTmpDep(dep) {
debug(`add tmp dep ${dep}`);
if (typeof dep === 'object' && dep.key && dep.version) {
this.setTmpDep(dep);
} else if (typeof dep === 'string') {
const version = (0, _getDepVersion.getDepVersion)({
dep,
cwd: this.cwd,
webpackAlias: this.webpackAlias
});
this.setTmpDep({
key: dep,
version
});
}
}
setTmpDep(opts) {
if (this.tmpDeps[opts.key] && this.tmpDeps[opts.key] !== opts.version) {
throw new Error(`[MFSU] dep ${opts.key} conflicts of ${opts.version} and ${this.tmpDeps[opts.key]}`);
}
this.tmpDeps[opts.key] = opts.version;
}
loadTmpDeps() {
const shouldBuild = this.shouldBuild();
if (shouldBuild) {
Object.assign(this.data.deps, this.tmpDeps);
this.data.config = this.getConfig(); // clear tmp deps
this.tmpDeps = {};
}
return {
shouldBuild
};
}
shouldBuild() {
debug('tmpDeps', this.tmpDeps); // 没有变更,不 build
if (!Object.keys(this.tmpDeps).length) {
return false;
} // 配置变更后,强制 build
if (!_utils().lodash.isEqual(this.getConfig(), this.data.config)) {
debug(`config changed, new: ${JSON.stringify(this.getConfig())}, origin: ${JSON.stringify(this.data.config)}`);
return true;
}
debug('this.data.deps', this.data.deps);
if (this.mode === 'production') {
return !_utils().lodash.isEqual(this.tmpDeps, this.data.deps);
} else {
for (var _i = 0, _Object$keys = Object.keys(this.tmpDeps); _i < _Object$keys.length; _i++) {
const key = _Object$keys[_i];
// 新增或修改
if (this.data.deps[key] !== this.tmpDeps[key]) {
return true;
}
}
return false;
}
}
getConfig() {
return {
// 目前只有 theme 会触发依赖重新编译
theme: this.api.config.theme || {}
};
}
writeCache() {
const noAbsDeps = {};
Object.keys(this.data.deps).forEach(depName => {
const noAbsDepName = depName.replace((0, _utils().winPath)(this.api.cwd), _constants.CWD);
noAbsDeps[noAbsDepName] = this.data.deps[depName];
});
const content = JSON.stringify(_objectSpread(_objectSpread({}, this.data), {}, {
deps: noAbsDeps
}), null, 2);
(0, _fs().writeFileSync)(this.cachePath, content, 'utf-8');
}
}
exports.default = DepInfo;