@modern-js/babel-compiler 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.
@@ -0,0 +1,224 @@
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 { fs, logger } from "@modern-js/utils";
126
+ import { defaultDistFileExtMap } from "./constants";
127
+ import { compiler } from "./compiler";
128
+ var build = function() {
129
+ var _ref = _asyncToGenerator(function(option) {
130
+ var babelConfig, rootDir, enableVirtualDist, filenames, clean, distDir, _distFileExtMap, distFileExtMap, _verbose, verbose, _quiet, quiet, virtualDists, messageDetails, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, filename, dist, happenError;
131
+ var _arguments = arguments;
132
+ return __generator(this, function(_state) {
133
+ switch(_state.label){
134
+ case 0:
135
+ babelConfig = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : {};
136
+ rootDir = option.rootDir, enableVirtualDist = option.enableVirtualDist, filenames = option.filenames, clean = option.clean, distDir = option.distDir, _distFileExtMap = option.distFileExtMap, distFileExtMap = _distFileExtMap === void 0 ? defaultDistFileExtMap : _distFileExtMap, _verbose = option.verbose, verbose = _verbose === void 0 ? false : _verbose, _quiet = option.quiet, quiet = _quiet === void 0 ? false : _quiet;
137
+ virtualDists = [];
138
+ if (!clean) return [
139
+ 3,
140
+ 2
141
+ ];
142
+ return [
143
+ 4,
144
+ fs.remove(distDir)
145
+ ];
146
+ case 1:
147
+ _state.sent();
148
+ _state.label = 2;
149
+ case 2:
150
+ fs.ensureDir(distDir);
151
+ messageDetails = [];
152
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
153
+ try {
154
+ for(_iterator = filenames[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
155
+ filename = _step.value;
156
+ try {
157
+ dist = compiler({
158
+ rootDir: rootDir,
159
+ enableVirtualDist: enableVirtualDist,
160
+ filepath: filename,
161
+ distDir: distDir,
162
+ verbose: verbose,
163
+ quiet: quiet,
164
+ babelConfig: babelConfig,
165
+ distFileExtMap: distFileExtMap
166
+ });
167
+ if (enableVirtualDist && dist) {
168
+ virtualDists.push(dist);
169
+ }
170
+ } catch (e) {
171
+ messageDetails.push({
172
+ filename: filename,
173
+ content: e.toString()
174
+ });
175
+ }
176
+ }
177
+ } catch (err) {
178
+ _didIteratorError = true;
179
+ _iteratorError = err;
180
+ } finally{
181
+ try {
182
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
183
+ _iterator.return();
184
+ }
185
+ } finally{
186
+ if (_didIteratorError) {
187
+ throw _iteratorError;
188
+ }
189
+ }
190
+ }
191
+ happenError = messageDetails.length > 0;
192
+ if (!quiet) {
193
+ if (happenError) {
194
+ logger.error("Compilation failure ".concat(messageDetails.length, " ").concat(messageDetails.length !== 1 ? "files" : "file", " with Babel."));
195
+ } else {
196
+ logger.info("Successfully compiled ".concat(filenames.length, " ").concat(filenames.length !== 1 ? "files" : "file", " with Babel."));
197
+ }
198
+ }
199
+ if (happenError) {
200
+ return [
201
+ 2,
202
+ {
203
+ code: 1,
204
+ message: "Compilation failure ".concat(messageDetails.length, " ").concat(messageDetails.length !== 1 ? "files" : "file", " with Babel."),
205
+ messageDetails: messageDetails
206
+ }
207
+ ];
208
+ }
209
+ return [
210
+ 2,
211
+ {
212
+ code: 0,
213
+ message: "Successfully compiled ".concat(filenames.length, " ").concat(filenames.length !== 1 ? "files" : "file", " with Babel."),
214
+ virtualDists: virtualDists
215
+ }
216
+ ];
217
+ }
218
+ });
219
+ });
220
+ return function build(option) {
221
+ return _ref.apply(this, arguments);
222
+ };
223
+ }();
224
+ export { build };
@@ -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 };