@modern-js/babel-compiler 2.0.0-beta.3 → 2.0.0-beta.6
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 +61 -0
- package/dist/js/modern/build.js +35 -9
- package/dist/js/modern/buildWatch.js +107 -68
- package/dist/js/modern/compiler.js +48 -26
- package/dist/js/modern/compilerErrorResult.js +12 -11
- package/dist/js/modern/constants.js +9 -6
- package/dist/js/modern/defaults.js +20 -4
- package/dist/js/modern/getFinalOption.js +40 -21
- package/dist/js/modern/index.js +37 -12
- package/dist/js/modern/utils.js +8 -4
- package/dist/js/modern/validate.js +17 -22
- package/dist/js/node/build.js +62 -20
- package/dist/js/node/buildWatch.js +136 -79
- package/dist/js/node/compiler.js +84 -44
- package/dist/js/node/compilerErrorResult.js +32 -15
- package/dist/js/node/constants.js +29 -10
- package/dist/js/node/defaults.js +42 -12
- package/dist/js/node/getFinalOption.js +66 -33
- package/dist/js/node/index.js +61 -42
- package/dist/js/node/type.js +15 -0
- package/dist/js/node/utils.js +35 -10
- package/dist/js/node/validate.js +44 -33
- package/dist/js/treeshaking/build.js +224 -0
- package/dist/js/treeshaking/buildWatch.js +417 -0
- package/dist/js/treeshaking/compiler.js +140 -0
- package/dist/js/treeshaking/compilerErrorResult.js +110 -0
- package/dist/js/treeshaking/constants.js +7 -0
- package/dist/js/treeshaking/defaults.js +44 -0
- package/dist/js/treeshaking/getFinalOption.js +137 -0
- package/dist/js/treeshaking/index.js +166 -0
- package/dist/js/treeshaking/type.js +1 -0
- package/dist/js/treeshaking/utils.js +5 -0
- package/dist/js/treeshaking/validate.js +38 -0
- package/package.json +4 -4
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
function _assertThisInitialized(self) {
|
|
2
|
+
if (self === void 0) {
|
|
3
|
+
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
4
|
+
}
|
|
5
|
+
return self;
|
|
6
|
+
}
|
|
7
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
8
|
+
try {
|
|
9
|
+
var info = gen[key](arg);
|
|
10
|
+
var value = info.value;
|
|
11
|
+
} catch (error) {
|
|
12
|
+
reject(error);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (info.done) {
|
|
16
|
+
resolve(value);
|
|
17
|
+
} else {
|
|
18
|
+
Promise.resolve(value).then(_next, _throw);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function _asyncToGenerator(fn) {
|
|
22
|
+
return function() {
|
|
23
|
+
var self = this, args = arguments;
|
|
24
|
+
return new Promise(function(resolve, reject) {
|
|
25
|
+
var gen = fn.apply(self, args);
|
|
26
|
+
function _next(value) {
|
|
27
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
28
|
+
}
|
|
29
|
+
function _throw(err) {
|
|
30
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
31
|
+
}
|
|
32
|
+
_next(undefined);
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function _classCallCheck(instance, Constructor) {
|
|
37
|
+
if (!(instance instanceof Constructor)) {
|
|
38
|
+
throw new TypeError("Cannot call a class as a function");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function _defineProperties(target, props) {
|
|
42
|
+
for(var i = 0; i < props.length; i++){
|
|
43
|
+
var descriptor = props[i];
|
|
44
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
45
|
+
descriptor.configurable = true;
|
|
46
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
47
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function _createClass(Constructor, protoProps, staticProps) {
|
|
51
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
52
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
53
|
+
return Constructor;
|
|
54
|
+
}
|
|
55
|
+
function _defineProperty(obj, key, value) {
|
|
56
|
+
if (key in obj) {
|
|
57
|
+
Object.defineProperty(obj, key, {
|
|
58
|
+
value: value,
|
|
59
|
+
enumerable: true,
|
|
60
|
+
configurable: true,
|
|
61
|
+
writable: true
|
|
62
|
+
});
|
|
63
|
+
} else {
|
|
64
|
+
obj[key] = value;
|
|
65
|
+
}
|
|
66
|
+
return obj;
|
|
67
|
+
}
|
|
68
|
+
function _getPrototypeOf(o) {
|
|
69
|
+
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
|
|
70
|
+
return o.__proto__ || Object.getPrototypeOf(o);
|
|
71
|
+
};
|
|
72
|
+
return _getPrototypeOf(o);
|
|
73
|
+
}
|
|
74
|
+
function _inherits(subClass, superClass) {
|
|
75
|
+
if (typeof superClass !== "function" && superClass !== null) {
|
|
76
|
+
throw new TypeError("Super expression must either be null or a function");
|
|
77
|
+
}
|
|
78
|
+
subClass.prototype = Object.create(superClass && superClass.prototype, {
|
|
79
|
+
constructor: {
|
|
80
|
+
value: subClass,
|
|
81
|
+
writable: true,
|
|
82
|
+
configurable: true
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
if (superClass) _setPrototypeOf(subClass, superClass);
|
|
86
|
+
}
|
|
87
|
+
function _objectSpread(target) {
|
|
88
|
+
for(var i = 1; i < arguments.length; i++){
|
|
89
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
90
|
+
var ownKeys = Object.keys(source);
|
|
91
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
92
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
93
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
94
|
+
}));
|
|
95
|
+
}
|
|
96
|
+
ownKeys.forEach(function(key) {
|
|
97
|
+
_defineProperty(target, key, source[key]);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return target;
|
|
101
|
+
}
|
|
102
|
+
function ownKeys(object, enumerableOnly) {
|
|
103
|
+
var keys = Object.keys(object);
|
|
104
|
+
if (Object.getOwnPropertySymbols) {
|
|
105
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
106
|
+
if (enumerableOnly) {
|
|
107
|
+
symbols = symbols.filter(function(sym) {
|
|
108
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
keys.push.apply(keys, symbols);
|
|
112
|
+
}
|
|
113
|
+
return keys;
|
|
114
|
+
}
|
|
115
|
+
function _objectSpreadProps(target, source) {
|
|
116
|
+
source = source != null ? source : {};
|
|
117
|
+
if (Object.getOwnPropertyDescriptors) {
|
|
118
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
119
|
+
} else {
|
|
120
|
+
ownKeys(Object(source)).forEach(function(key) {
|
|
121
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
return target;
|
|
125
|
+
}
|
|
126
|
+
function _possibleConstructorReturn(self, call) {
|
|
127
|
+
if (call && (_typeof(call) === "object" || typeof call === "function")) {
|
|
128
|
+
return call;
|
|
129
|
+
}
|
|
130
|
+
return _assertThisInitialized(self);
|
|
131
|
+
}
|
|
132
|
+
function _setPrototypeOf(o, p) {
|
|
133
|
+
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
|
|
134
|
+
o.__proto__ = p;
|
|
135
|
+
return o;
|
|
136
|
+
};
|
|
137
|
+
return _setPrototypeOf(o, p);
|
|
138
|
+
}
|
|
139
|
+
var _typeof = function(obj) {
|
|
140
|
+
"@swc/helpers - typeof";
|
|
141
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
142
|
+
};
|
|
143
|
+
function _isNativeReflectConstruct() {
|
|
144
|
+
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
|
|
145
|
+
if (Reflect.construct.sham) return false;
|
|
146
|
+
if (typeof Proxy === "function") return true;
|
|
147
|
+
try {
|
|
148
|
+
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
|
|
149
|
+
return true;
|
|
150
|
+
} catch (e) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function _createSuper(Derived) {
|
|
155
|
+
var hasNativeReflectConstruct = _isNativeReflectConstruct();
|
|
156
|
+
return function _createSuperInternal() {
|
|
157
|
+
var Super = _getPrototypeOf(Derived), result;
|
|
158
|
+
if (hasNativeReflectConstruct) {
|
|
159
|
+
var NewTarget = _getPrototypeOf(this).constructor;
|
|
160
|
+
result = Reflect.construct(Super, arguments, NewTarget);
|
|
161
|
+
} else {
|
|
162
|
+
result = Super.apply(this, arguments);
|
|
163
|
+
}
|
|
164
|
+
return _possibleConstructorReturn(this, result);
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
var __generator = this && this.__generator || function(thisArg, body) {
|
|
168
|
+
var f, y, t, g, _ = {
|
|
169
|
+
label: 0,
|
|
170
|
+
sent: function() {
|
|
171
|
+
if (t[0] & 1) throw t[1];
|
|
172
|
+
return t[1];
|
|
173
|
+
},
|
|
174
|
+
trys: [],
|
|
175
|
+
ops: []
|
|
176
|
+
};
|
|
177
|
+
return(g = {
|
|
178
|
+
next: verb(0),
|
|
179
|
+
"throw": verb(1),
|
|
180
|
+
"return": verb(2)
|
|
181
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
182
|
+
return this;
|
|
183
|
+
}), g);
|
|
184
|
+
function verb(n) {
|
|
185
|
+
return function(v) {
|
|
186
|
+
return step([
|
|
187
|
+
n,
|
|
188
|
+
v
|
|
189
|
+
]);
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
function step(op) {
|
|
193
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
194
|
+
while(_)try {
|
|
195
|
+
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;
|
|
196
|
+
if (y = 0, t) op = [
|
|
197
|
+
op[0] & 2,
|
|
198
|
+
t.value
|
|
199
|
+
];
|
|
200
|
+
switch(op[0]){
|
|
201
|
+
case 0:
|
|
202
|
+
case 1:
|
|
203
|
+
t = op;
|
|
204
|
+
break;
|
|
205
|
+
case 4:
|
|
206
|
+
_.label++;
|
|
207
|
+
return {
|
|
208
|
+
value: op[1],
|
|
209
|
+
done: false
|
|
210
|
+
};
|
|
211
|
+
case 5:
|
|
212
|
+
_.label++;
|
|
213
|
+
y = op[1];
|
|
214
|
+
op = [
|
|
215
|
+
0
|
|
216
|
+
];
|
|
217
|
+
continue;
|
|
218
|
+
case 7:
|
|
219
|
+
op = _.ops.pop();
|
|
220
|
+
_.trys.pop();
|
|
221
|
+
continue;
|
|
222
|
+
default:
|
|
223
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
224
|
+
_ = 0;
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
228
|
+
_.label = op[1];
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
232
|
+
_.label = t[1];
|
|
233
|
+
t = op;
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
if (t && _.label < t[2]) {
|
|
237
|
+
_.label = t[2];
|
|
238
|
+
_.ops.push(op);
|
|
239
|
+
break;
|
|
240
|
+
}
|
|
241
|
+
if (t[2]) _.ops.pop();
|
|
242
|
+
_.trys.pop();
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
op = body.call(thisArg, _);
|
|
246
|
+
} catch (e) {
|
|
247
|
+
op = [
|
|
248
|
+
6,
|
|
249
|
+
e
|
|
250
|
+
];
|
|
251
|
+
y = 0;
|
|
252
|
+
} finally{
|
|
253
|
+
f = t = 0;
|
|
254
|
+
}
|
|
255
|
+
if (op[0] & 5) throw op[1];
|
|
256
|
+
return {
|
|
257
|
+
value: op[0] ? op[1] : void 0,
|
|
258
|
+
done: true
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
import * as path from "path";
|
|
263
|
+
import * as Event from "events";
|
|
264
|
+
import { logger, watch, WatchChangeType } from "@modern-js/utils";
|
|
265
|
+
import { build } from "./build";
|
|
266
|
+
import { CompilerErrorResult } from "./compilerErrorResult";
|
|
267
|
+
var BuildWatchEvent = {
|
|
268
|
+
firstCompiler: "first-compiler",
|
|
269
|
+
compiling: "compiling",
|
|
270
|
+
watchingCompiler: "watching-compiler"
|
|
271
|
+
};
|
|
272
|
+
var BuildWatchEmitter = /*#__PURE__*/ function(_EventEmitter) {
|
|
273
|
+
"use strict";
|
|
274
|
+
_inherits(BuildWatchEmitter, _EventEmitter);
|
|
275
|
+
var _super = _createSuper(BuildWatchEmitter);
|
|
276
|
+
function BuildWatchEmitter() {
|
|
277
|
+
_classCallCheck(this, BuildWatchEmitter);
|
|
278
|
+
var _this;
|
|
279
|
+
_this = _super.apply(this, arguments);
|
|
280
|
+
_defineProperty(_assertThisInitialized(_this), "_initFn", void 0);
|
|
281
|
+
return _this;
|
|
282
|
+
}
|
|
283
|
+
_createClass(BuildWatchEmitter, [
|
|
284
|
+
{
|
|
285
|
+
key: "setInitFn",
|
|
286
|
+
value: function setInitFn(fn) {
|
|
287
|
+
this._initFn = fn;
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
key: "watch",
|
|
292
|
+
value: function watch() {
|
|
293
|
+
var _this = this;
|
|
294
|
+
return _asyncToGenerator(function() {
|
|
295
|
+
return __generator(this, function(_state) {
|
|
296
|
+
if (typeof _this._initFn === "function") {
|
|
297
|
+
return [
|
|
298
|
+
2,
|
|
299
|
+
_this._initFn(_this)
|
|
300
|
+
];
|
|
301
|
+
}
|
|
302
|
+
return [
|
|
303
|
+
2,
|
|
304
|
+
null
|
|
305
|
+
];
|
|
306
|
+
});
|
|
307
|
+
})();
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
]);
|
|
311
|
+
return BuildWatchEmitter;
|
|
312
|
+
}(Event.EventEmitter);
|
|
313
|
+
var runBuildWatch = function() {
|
|
314
|
+
var _ref = _asyncToGenerator(function(option) {
|
|
315
|
+
var babelConfig, emitter, errorResult, watchDir, distDir, quiet, firstBuildResult, code;
|
|
316
|
+
var _arguments = arguments;
|
|
317
|
+
return __generator(this, function(_state) {
|
|
318
|
+
switch(_state.label){
|
|
319
|
+
case 0:
|
|
320
|
+
babelConfig = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : {}, emitter = _arguments.length > 2 ? _arguments[2] : void 0;
|
|
321
|
+
emitter.emit(BuildWatchEvent.compiling);
|
|
322
|
+
errorResult = new CompilerErrorResult();
|
|
323
|
+
watchDir = option.watchDir;
|
|
324
|
+
distDir = option.distDir, quiet = option.quiet;
|
|
325
|
+
return [
|
|
326
|
+
4,
|
|
327
|
+
build(option, babelConfig)
|
|
328
|
+
];
|
|
329
|
+
case 1:
|
|
330
|
+
firstBuildResult = _state.sent();
|
|
331
|
+
code = firstBuildResult.code;
|
|
332
|
+
if (code === 1) {
|
|
333
|
+
errorResult.init(firstBuildResult);
|
|
334
|
+
emitter.emit(BuildWatchEvent.firstCompiler, errorResult.value);
|
|
335
|
+
} else {
|
|
336
|
+
emitter.emit(BuildWatchEvent.firstCompiler, firstBuildResult);
|
|
337
|
+
}
|
|
338
|
+
return [
|
|
339
|
+
2,
|
|
340
|
+
watch("".concat(watchDir, "/**/*.{js,jsx,ts,tsx}"), function() {
|
|
341
|
+
var _ref = _asyncToGenerator(function(param) {
|
|
342
|
+
var changeType, changedFilePath, removeFiles, result2, result;
|
|
343
|
+
return __generator(this, function(_state) {
|
|
344
|
+
switch(_state.label){
|
|
345
|
+
case 0:
|
|
346
|
+
changeType = param.changeType, changedFilePath = param.changedFilePath;
|
|
347
|
+
emitter.emit(BuildWatchEvent.compiling);
|
|
348
|
+
if (changeType === WatchChangeType.UNLINK) {
|
|
349
|
+
removeFiles = [
|
|
350
|
+
path.normalize("./".concat(distDir, "/").concat(path.relative(watchDir, changedFilePath)))
|
|
351
|
+
];
|
|
352
|
+
if (!quiet) {
|
|
353
|
+
logger.info("remove file: ".concat(removeFiles.join(",")));
|
|
354
|
+
}
|
|
355
|
+
result2 = {
|
|
356
|
+
code: 0,
|
|
357
|
+
message: "remove file: ".concat(removeFiles.join(",")),
|
|
358
|
+
removeFiles: removeFiles
|
|
359
|
+
};
|
|
360
|
+
emitter.emit(BuildWatchEvent.watchingCompiler, result2);
|
|
361
|
+
return [
|
|
362
|
+
2
|
|
363
|
+
];
|
|
364
|
+
}
|
|
365
|
+
return [
|
|
366
|
+
4,
|
|
367
|
+
build(_objectSpreadProps(_objectSpread({}, option), {
|
|
368
|
+
filenames: [
|
|
369
|
+
changedFilePath
|
|
370
|
+
]
|
|
371
|
+
}), babelConfig)
|
|
372
|
+
];
|
|
373
|
+
case 1:
|
|
374
|
+
result = _state.sent();
|
|
375
|
+
if (result.code === 1) {
|
|
376
|
+
errorResult.update(result.messageDetails || []);
|
|
377
|
+
emitter.emit(BuildWatchEvent.watchingCompiler, errorResult.value);
|
|
378
|
+
!quiet && logger.info(errorResult.value.message);
|
|
379
|
+
} else {
|
|
380
|
+
errorResult.removeByFileName(changedFilePath);
|
|
381
|
+
if (errorResult.checkExistError()) {
|
|
382
|
+
emitter.emit(BuildWatchEvent.watchingCompiler, _objectSpreadProps(_objectSpread({}, errorResult.value), {
|
|
383
|
+
virtualDists: result.virtualDists
|
|
384
|
+
}));
|
|
385
|
+
!quiet && logger.info(errorResult.value.message);
|
|
386
|
+
} else {
|
|
387
|
+
emitter.emit(BuildWatchEvent.watchingCompiler, result);
|
|
388
|
+
!quiet && logger.info(result.message);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return [
|
|
392
|
+
2
|
|
393
|
+
];
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
});
|
|
397
|
+
return function(_) {
|
|
398
|
+
return _ref.apply(this, arguments);
|
|
399
|
+
};
|
|
400
|
+
}(), [
|
|
401
|
+
"".concat(watchDir, "/**/*.d.ts")
|
|
402
|
+
])
|
|
403
|
+
];
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
return function runBuildWatch(option) {
|
|
408
|
+
return _ref.apply(this, arguments);
|
|
409
|
+
};
|
|
410
|
+
}();
|
|
411
|
+
var buildWatch = function(option) {
|
|
412
|
+
var babelConfig = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
413
|
+
var buildWatchEmitter = new BuildWatchEmitter();
|
|
414
|
+
buildWatchEmitter.setInitFn(runBuildWatch.bind(null, option, babelConfig));
|
|
415
|
+
return buildWatchEmitter;
|
|
416
|
+
};
|
|
417
|
+
export { BuildWatchEmitter, BuildWatchEvent, buildWatch, runBuildWatch };
|
|
@@ -0,0 +1,140 @@
|
|
|
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 * as path from "path";
|
|
54
|
+
import * as babel from "@babel/core";
|
|
55
|
+
import { logger, fs } from "@modern-js/utils";
|
|
56
|
+
import * as utils from "./utils";
|
|
57
|
+
import { defaultDistFileExtMap } from "./constants";
|
|
58
|
+
var defaultDistDir = "dist";
|
|
59
|
+
var isRes = function(r) {
|
|
60
|
+
return Boolean(r);
|
|
61
|
+
};
|
|
62
|
+
var getDistFilePath = function(option) {
|
|
63
|
+
var filepath = option.filepath, rootDir = option.rootDir, distDir = option.distDir, extMap = option.extMap;
|
|
64
|
+
var ext = path.extname(filepath);
|
|
65
|
+
return path.join(distDir, path.relative(rootDir, filepath).replace(ext, extMap[ext]));
|
|
66
|
+
};
|
|
67
|
+
var resolveSourceMap = function(option) {
|
|
68
|
+
var babelRes = option.babelRes, sourceFilePath = option.sourceFilePath, distFilePath = option.distFilePath, _enableVirtualDist = option.enableVirtualDist, enableVirtualDist = _enableVirtualDist === void 0 ? false : _enableVirtualDist;
|
|
69
|
+
var mapLoc = "".concat(distFilePath, ".map");
|
|
70
|
+
babelRes.code = utils.addSourceMappingUrl(babelRes.code, mapLoc);
|
|
71
|
+
if (babelRes.map) {
|
|
72
|
+
babelRes.map.file = path.basename(distFilePath);
|
|
73
|
+
babelRes.map.sources = [
|
|
74
|
+
path.relative(path.dirname(distFilePath), sourceFilePath)
|
|
75
|
+
];
|
|
76
|
+
}
|
|
77
|
+
var sourceMapVirtualDist = {
|
|
78
|
+
sourcemap: JSON.stringify(babelRes.map),
|
|
79
|
+
sourceMapPath: mapLoc
|
|
80
|
+
};
|
|
81
|
+
if (enableVirtualDist) {
|
|
82
|
+
return sourceMapVirtualDist;
|
|
83
|
+
}
|
|
84
|
+
fs.ensureDirSync(path.dirname(mapLoc));
|
|
85
|
+
fs.writeFileSync(mapLoc, JSON.stringify(babelRes.map));
|
|
86
|
+
return sourceMapVirtualDist;
|
|
87
|
+
};
|
|
88
|
+
var compiler = function(option) {
|
|
89
|
+
var filepath = option.filepath, rootDir = option.rootDir, _enableVirtualDist = option.enableVirtualDist, enableVirtualDist = _enableVirtualDist === void 0 ? false : _enableVirtualDist, _distDir = option.distDir, distDir = _distDir === void 0 ? path.join(path.dirname(rootDir), defaultDistDir) : _distDir, _verbose = option.verbose, verbose = _verbose === void 0 ? false : _verbose, _babelConfig = option.babelConfig, babelConfig = _babelConfig === void 0 ? {} : _babelConfig, _distFileExtMap = option.distFileExtMap, distFileExtMap = _distFileExtMap === void 0 ? defaultDistFileExtMap : _distFileExtMap, _quiet = option.quiet, quiet = _quiet === void 0 ? false : _quiet;
|
|
90
|
+
var babelRes = babel.transformFileSync(filepath, babelConfig);
|
|
91
|
+
var virtualDist = null;
|
|
92
|
+
if (!isRes(babelRes)) {
|
|
93
|
+
throw new Error("".concat(filepath, " happen error"));
|
|
94
|
+
}
|
|
95
|
+
var distFilePath = getDistFilePath({
|
|
96
|
+
filepath: filepath,
|
|
97
|
+
rootDir: rootDir,
|
|
98
|
+
distDir: distDir,
|
|
99
|
+
extMap: distFileExtMap
|
|
100
|
+
});
|
|
101
|
+
if (enableVirtualDist) {
|
|
102
|
+
virtualDist = {
|
|
103
|
+
distPath: distFilePath,
|
|
104
|
+
sourceMapPath: "",
|
|
105
|
+
code: "",
|
|
106
|
+
sourcemap: ""
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
if ((babelRes === null || babelRes === void 0 ? void 0 : babelRes.map) && babelConfig.sourceMaps && babelConfig.sourceMaps !== "inline") {
|
|
110
|
+
if (virtualDist) {
|
|
111
|
+
virtualDist = _objectSpread({}, virtualDist, resolveSourceMap({
|
|
112
|
+
babelRes: babelRes,
|
|
113
|
+
sourceFilePath: filepath,
|
|
114
|
+
distFilePath: distFilePath,
|
|
115
|
+
enableVirtualDist: enableVirtualDist
|
|
116
|
+
}));
|
|
117
|
+
} else {
|
|
118
|
+
resolveSourceMap({
|
|
119
|
+
babelRes: babelRes,
|
|
120
|
+
sourceFilePath: filepath,
|
|
121
|
+
distFilePath: distFilePath,
|
|
122
|
+
enableVirtualDist: enableVirtualDist
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (virtualDist) {
|
|
127
|
+
virtualDist = _objectSpreadProps(_objectSpread({}, virtualDist), {
|
|
128
|
+
distPath: distFilePath,
|
|
129
|
+
code: babelRes.code
|
|
130
|
+
});
|
|
131
|
+
} else {
|
|
132
|
+
fs.ensureDirSync(path.dirname(distFilePath));
|
|
133
|
+
fs.writeFileSync(distFilePath, babelRes.code);
|
|
134
|
+
}
|
|
135
|
+
if (verbose && !quiet) {
|
|
136
|
+
logger.info("".concat(filepath, " => ").concat(distFilePath));
|
|
137
|
+
}
|
|
138
|
+
return virtualDist;
|
|
139
|
+
};
|
|
140
|
+
export { compiler, getDistFilePath, isRes, resolveSourceMap };
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
function _classCallCheck(instance, Constructor) {
|
|
2
|
+
if (!(instance instanceof Constructor)) {
|
|
3
|
+
throw new TypeError("Cannot call a class as a function");
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
function _defineProperties(target, props) {
|
|
7
|
+
for(var i = 0; i < props.length; i++){
|
|
8
|
+
var descriptor = props[i];
|
|
9
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
10
|
+
descriptor.configurable = true;
|
|
11
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
12
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function _createClass(Constructor, protoProps, staticProps) {
|
|
16
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
17
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
18
|
+
return Constructor;
|
|
19
|
+
}
|
|
20
|
+
function _defineProperty(obj, key, value) {
|
|
21
|
+
if (key in obj) {
|
|
22
|
+
Object.defineProperty(obj, key, {
|
|
23
|
+
value: value,
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true
|
|
27
|
+
});
|
|
28
|
+
} else {
|
|
29
|
+
obj[key] = value;
|
|
30
|
+
}
|
|
31
|
+
return obj;
|
|
32
|
+
}
|
|
33
|
+
var CompilerErrorResult = /*#__PURE__*/ function() {
|
|
34
|
+
"use strict";
|
|
35
|
+
function CompilerErrorResult(initErrorResult) {
|
|
36
|
+
_classCallCheck(this, CompilerErrorResult);
|
|
37
|
+
_defineProperty(this, "_messageDetails", void 0);
|
|
38
|
+
this.init(initErrorResult);
|
|
39
|
+
}
|
|
40
|
+
_createClass(CompilerErrorResult, [
|
|
41
|
+
{
|
|
42
|
+
key: "init",
|
|
43
|
+
value: function init(initErrorResult) {
|
|
44
|
+
this._messageDetails = (initErrorResult === null || initErrorResult === void 0 ? void 0 : initErrorResult.messageDetails) || [];
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
key: "update",
|
|
49
|
+
value: function update(messageDetails) {
|
|
50
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
51
|
+
try {
|
|
52
|
+
var _this = this, _loop = function() {
|
|
53
|
+
var messageDetail = _step.value;
|
|
54
|
+
var addError = !_this._messageDetails.some(function(detail) {
|
|
55
|
+
if (detail.filename === messageDetail.filename) {
|
|
56
|
+
detail.content = messageDetail.content;
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
return false;
|
|
60
|
+
});
|
|
61
|
+
if (addError) {
|
|
62
|
+
_this._messageDetails.push(messageDetail);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
for(var _iterator = messageDetails[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
|
|
66
|
+
} catch (err) {
|
|
67
|
+
_didIteratorError = true;
|
|
68
|
+
_iteratorError = err;
|
|
69
|
+
} finally{
|
|
70
|
+
try {
|
|
71
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
72
|
+
_iterator.return();
|
|
73
|
+
}
|
|
74
|
+
} finally{
|
|
75
|
+
if (_didIteratorError) {
|
|
76
|
+
throw _iteratorError;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
key: "removeByFileName",
|
|
84
|
+
value: function removeByFileName(filename) {
|
|
85
|
+
this._messageDetails = this._messageDetails.filter(function(detail) {
|
|
86
|
+
return detail.filename !== filename;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
key: "value",
|
|
92
|
+
get: function get() {
|
|
93
|
+
var ref;
|
|
94
|
+
return {
|
|
95
|
+
code: 1,
|
|
96
|
+
message: "Compilation failure ".concat((ref = this._messageDetails) === null || ref === void 0 ? void 0 : ref.length, " files with Babel."),
|
|
97
|
+
messageDetails: this._messageDetails
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
key: "checkExistError",
|
|
103
|
+
value: function checkExistError() {
|
|
104
|
+
return this._messageDetails.length > 0;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
]);
|
|
108
|
+
return CompilerErrorResult;
|
|
109
|
+
}();
|
|
110
|
+
export { CompilerErrorResult };
|