@modern-js/server-utils 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.
@@ -1,156 +1,339 @@
1
- import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
2
- import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
3
- import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
4
- import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
5
- import path from 'path';
6
- import { logger, getAliasConfig, fs } from '@modern-js/utils';
7
- import ts from 'typescript';
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
+ function _defineProperty(obj, key, value) {
31
+ if (key in obj) {
32
+ Object.defineProperty(obj, key, {
33
+ value: value,
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true
37
+ });
38
+ } else {
39
+ obj[key] = value;
40
+ }
41
+ return obj;
42
+ }
43
+ function _objectSpread(target) {
44
+ for(var i = 1; i < arguments.length; i++){
45
+ var source = arguments[i] != null ? arguments[i] : {};
46
+ var ownKeys = Object.keys(source);
47
+ if (typeof Object.getOwnPropertySymbols === "function") {
48
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
49
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
50
+ }));
51
+ }
52
+ ownKeys.forEach(function(key) {
53
+ _defineProperty(target, key, source[key]);
54
+ });
55
+ }
56
+ return target;
57
+ }
58
+ var __generator = this && this.__generator || function(thisArg, body) {
59
+ var f, y, t, g, _ = {
60
+ label: 0,
61
+ sent: function() {
62
+ if (t[0] & 1) throw t[1];
63
+ return t[1];
64
+ },
65
+ trys: [],
66
+ ops: []
67
+ };
68
+ return(g = {
69
+ next: verb(0),
70
+ "throw": verb(1),
71
+ "return": verb(2)
72
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
73
+ return this;
74
+ }), g);
75
+ function verb(n) {
76
+ return function(v) {
77
+ return step([
78
+ n,
79
+ v
80
+ ]);
81
+ };
82
+ }
83
+ function step(op) {
84
+ if (f) throw new TypeError("Generator is already executing.");
85
+ while(_)try {
86
+ 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;
87
+ if (y = 0, t) op = [
88
+ op[0] & 2,
89
+ t.value
90
+ ];
91
+ switch(op[0]){
92
+ case 0:
93
+ case 1:
94
+ t = op;
95
+ break;
96
+ case 4:
97
+ _.label++;
98
+ return {
99
+ value: op[1],
100
+ done: false
101
+ };
102
+ case 5:
103
+ _.label++;
104
+ y = op[1];
105
+ op = [
106
+ 0
107
+ ];
108
+ continue;
109
+ case 7:
110
+ op = _.ops.pop();
111
+ _.trys.pop();
112
+ continue;
113
+ default:
114
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
115
+ _ = 0;
116
+ continue;
117
+ }
118
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
119
+ _.label = op[1];
120
+ break;
121
+ }
122
+ if (op[0] === 6 && _.label < t[1]) {
123
+ _.label = t[1];
124
+ t = op;
125
+ break;
126
+ }
127
+ if (t && _.label < t[2]) {
128
+ _.label = t[2];
129
+ _.ops.push(op);
130
+ break;
131
+ }
132
+ if (t[2]) _.ops.pop();
133
+ _.trys.pop();
134
+ continue;
135
+ }
136
+ op = body.call(thisArg, _);
137
+ } catch (e) {
138
+ op = [
139
+ 6,
140
+ e
141
+ ];
142
+ y = 0;
143
+ } finally{
144
+ f = t = 0;
145
+ }
146
+ if (op[0] & 5) throw op[1];
147
+ return {
148
+ value: op[0] ? op[1] : void 0,
149
+ done: true
150
+ };
151
+ }
152
+ };
153
+ import path from "path";
154
+ import { logger, getAliasConfig, fs } from "@modern-js/utils";
155
+ import ts from "typescript";
8
156
  import { TypescriptLoader } from "./typescript-loader";
9
157
  import { tsconfigPathsBeforeHookFactory } from "./tsconfig-paths-plugin";
10
- var readTsConfigByFile = function readTsConfigByFile(tsConfigFile) {
11
- var parsedCmd = ts.getParsedCommandLineOfConfigFile(tsConfigFile, undefined, ts.sys);
12
- var _ref = parsedCmd,
13
- options = _ref.options,
14
- fileNames = _ref.fileNames,
15
- projectReferences = _ref.projectReferences;
16
- return {
17
- options: options,
18
- fileNames: fileNames,
19
- projectReferences: projectReferences
20
- };
158
+ var readTsConfigByFile = function(tsConfigFile) {
159
+ var parsedCmd = ts.getParsedCommandLineOfConfigFile(tsConfigFile, void 0, ts.sys);
160
+ var options = parsedCmd.options, fileNames = parsedCmd.fileNames, projectReferences = parsedCmd.projectReferences;
161
+ return {
162
+ options: options,
163
+ fileNames: fileNames,
164
+ projectReferences: projectReferences
165
+ };
21
166
  };
22
- var copyFiles = /*#__PURE__*/function () {
23
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(from, to, tsconfigPath) {
24
- var basename, targetDir;
25
- return _regeneratorRuntime().wrap(function _callee$(_context) {
26
- while (1) {
27
- switch (_context.prev = _context.next) {
28
- case 0:
29
- _context.next = 2;
30
- return fs.pathExists(from);
31
- case 2:
32
- if (!_context.sent) {
33
- _context.next = 7;
34
- break;
167
+ var copyFiles = function() {
168
+ var _ref = _asyncToGenerator(function(from, to, tsconfigPath) {
169
+ var basename, targetDir;
170
+ return __generator(this, function(_state) {
171
+ switch(_state.label){
172
+ case 0:
173
+ return [
174
+ 4,
175
+ fs.pathExists(from)
176
+ ];
177
+ case 1:
178
+ if (!_state.sent()) return [
179
+ 3,
180
+ 3
181
+ ];
182
+ basename = path.basename(from);
183
+ targetDir = path.join(to, basename);
184
+ return [
185
+ 4,
186
+ fs.copy(from, targetDir, {
187
+ filter: function(src) {
188
+ return ![
189
+ ".ts"
190
+ ].includes(path.extname(src)) && src !== tsconfigPath;
191
+ }
192
+ })
193
+ ];
194
+ case 2:
195
+ _state.sent();
196
+ _state.label = 3;
197
+ case 3:
198
+ return [
199
+ 2
200
+ ];
35
201
  }
36
- basename = path.basename(from);
37
- targetDir = path.join(to, basename);
38
- _context.next = 7;
39
- return fs.copy(from, targetDir, {
40
- filter: function filter(src) {
41
- return !['.ts'].includes(path.extname(src)) && src !== tsconfigPath;
42
- }
43
- });
44
- case 7:
45
- case "end":
46
- return _context.stop();
47
- }
48
- }
49
- }, _callee);
50
- }));
51
- return function copyFiles(_x, _x2, _x3) {
52
- return _ref2.apply(this, arguments);
53
- };
202
+ });
203
+ });
204
+ return function copyFiles(from, to, tsconfigPath) {
205
+ return _ref.apply(this, arguments);
206
+ };
54
207
  }();
55
- export var compileByTs = /*#__PURE__*/function () {
56
- var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(appDirectory, config, compileOptions) {
57
- var sourceDirs, distDir, tsconfigPath, ts, createProgram, formatHost, alias, aliasOption, _aliasOption$paths, paths, _aliasOption$absolute, absoluteBaseUrl, _readTsConfigByFile, options, fileNames, projectReferences, sourcePosixPaths, rootNames, program, tsconfigPathsPlugin, emitResult, allDiagnostics, _iterator, _step, source;
58
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
59
- while (1) {
60
- switch (_context2.prev = _context2.next) {
61
- case 0:
62
- logger.info("Running ts compile...");
63
- sourceDirs = compileOptions.sourceDirs, distDir = compileOptions.distDir, tsconfigPath = compileOptions.tsconfigPath;
64
- if (tsconfigPath) {
65
- _context2.next = 4;
66
- break;
67
- }
68
- return _context2.abrupt("return");
69
- case 4:
70
- ts = new TypescriptLoader({
71
- appDirectory: appDirectory
72
- }).load();
73
- createProgram = ts.createIncrementalProgram || ts.createProgram;
74
- formatHost = getFormatHost(ts);
75
- alias = config.alias;
76
- aliasOption = getAliasConfig(alias, {
77
- appDirectory: appDirectory,
78
- tsconfigPath: tsconfigPath
79
- });
80
- _aliasOption$paths = aliasOption.paths, paths = _aliasOption$paths === void 0 ? {} : _aliasOption$paths, _aliasOption$absolute = aliasOption.absoluteBaseUrl, absoluteBaseUrl = _aliasOption$absolute === void 0 ? './' : _aliasOption$absolute;
81
- _readTsConfigByFile = readTsConfigByFile(tsconfigPath), options = _readTsConfigByFile.options, fileNames = _readTsConfigByFile.fileNames, projectReferences = _readTsConfigByFile.projectReferences;
82
- sourcePosixPaths = sourceDirs.map(function (sourceDir) {
83
- return sourceDir.split(path.sep).join(path.posix.sep);
84
- });
85
- rootNames = fileNames.filter(function (fileName) {
86
- return fileName.endsWith('.d.ts') || sourcePosixPaths.some(function (sourceDir) {
87
- return fileName.includes(sourceDir);
88
- });
89
- });
90
- program = createProgram.call(ts, {
91
- rootNames: rootNames,
92
- projectReferences: projectReferences,
93
- options: _objectSpread({
94
- rootDir: appDirectory,
95
- outDir: distDir
96
- }, options)
97
- });
98
- tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths);
99
- emitResult = program.emit(undefined, undefined, undefined, undefined, {
100
- before: [tsconfigPathsPlugin]
101
- });
102
- allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
103
- if (allDiagnostics.length > 0) {
104
- logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
105
- // eslint-disable-next-line no-process-exit
106
- process.exit(1);
107
- }
108
- _iterator = _createForOfIteratorHelper(sourceDirs);
109
- _context2.prev = 19;
110
- _iterator.s();
111
- case 21:
112
- if ((_step = _iterator.n()).done) {
113
- _context2.next = 27;
114
- break;
208
+ var compileByTs = function() {
209
+ var _ref = _asyncToGenerator(function(appDirectory, config, compileOptions) {
210
+ var sourceDirs, distDir, tsconfigPath, ts2, createProgram, formatHost, alias, aliasOption, _paths, paths, _absoluteBaseUrl, absoluteBaseUrl, ref, options, fileNames, projectReferences, sourcePosixPaths, rootNames, program, tsconfigPathsPlugin, emitResult, allDiagnostics, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, source, err;
211
+ return __generator(this, function(_state) {
212
+ switch(_state.label){
213
+ case 0:
214
+ logger.info("Running ts compile...");
215
+ sourceDirs = compileOptions.sourceDirs, distDir = compileOptions.distDir, tsconfigPath = compileOptions.tsconfigPath;
216
+ if (!tsconfigPath) {
217
+ return [
218
+ 2
219
+ ];
220
+ }
221
+ ts2 = new TypescriptLoader({
222
+ appDirectory: appDirectory
223
+ }).load();
224
+ createProgram = ts2.createIncrementalProgram || ts2.createProgram;
225
+ formatHost = getFormatHost(ts2);
226
+ alias = config.alias;
227
+ aliasOption = getAliasConfig(alias, {
228
+ appDirectory: appDirectory,
229
+ tsconfigPath: tsconfigPath
230
+ });
231
+ _paths = aliasOption.paths, paths = _paths === void 0 ? {} : _paths, _absoluteBaseUrl = aliasOption.absoluteBaseUrl, absoluteBaseUrl = _absoluteBaseUrl === void 0 ? "./" : _absoluteBaseUrl;
232
+ ref = readTsConfigByFile(tsconfigPath), options = ref.options, fileNames = ref.fileNames, projectReferences = ref.projectReferences;
233
+ sourcePosixPaths = sourceDirs.map(function(sourceDir) {
234
+ return sourceDir.split(path.sep).join(path.posix.sep);
235
+ });
236
+ rootNames = fileNames.filter(function(fileName) {
237
+ return fileName.endsWith(".d.ts") || sourcePosixPaths.some(function(sourceDir) {
238
+ return fileName.includes(sourceDir);
239
+ });
240
+ });
241
+ program = createProgram.call(ts2, {
242
+ rootNames: rootNames,
243
+ projectReferences: projectReferences,
244
+ options: _objectSpread({
245
+ rootDir: appDirectory,
246
+ outDir: distDir
247
+ }, options)
248
+ });
249
+ tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts2, absoluteBaseUrl, paths);
250
+ emitResult = program.emit(void 0, void 0, void 0, void 0, {
251
+ before: [
252
+ tsconfigPathsPlugin
253
+ ]
254
+ });
255
+ allDiagnostics = ts2.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
256
+ if (allDiagnostics.length > 0) {
257
+ logger.error(ts2.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
258
+ process.exit(1);
259
+ }
260
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
261
+ _state.label = 1;
262
+ case 1:
263
+ _state.trys.push([
264
+ 1,
265
+ 6,
266
+ 7,
267
+ 8
268
+ ]);
269
+ _iterator = sourceDirs[Symbol.iterator]();
270
+ _state.label = 2;
271
+ case 2:
272
+ if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
273
+ 3,
274
+ 5
275
+ ];
276
+ source = _step.value;
277
+ return [
278
+ 4,
279
+ copyFiles(source, distDir, tsconfigPath)
280
+ ];
281
+ case 3:
282
+ _state.sent();
283
+ _state.label = 4;
284
+ case 4:
285
+ _iteratorNormalCompletion = true;
286
+ return [
287
+ 3,
288
+ 2
289
+ ];
290
+ case 5:
291
+ return [
292
+ 3,
293
+ 8
294
+ ];
295
+ case 6:
296
+ err = _state.sent();
297
+ _didIteratorError = true;
298
+ _iteratorError = err;
299
+ return [
300
+ 3,
301
+ 8
302
+ ];
303
+ case 7:
304
+ try {
305
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
306
+ _iterator.return();
307
+ }
308
+ } finally{
309
+ if (_didIteratorError) {
310
+ throw _iteratorError;
311
+ }
312
+ }
313
+ return [
314
+ 7
315
+ ];
316
+ case 8:
317
+ logger.info("Ts compile succeed");
318
+ return [
319
+ 2
320
+ ];
115
321
  }
116
- source = _step.value;
117
- _context2.next = 25;
118
- return copyFiles(source, distDir, tsconfigPath);
119
- case 25:
120
- _context2.next = 21;
121
- break;
122
- case 27:
123
- _context2.next = 32;
124
- break;
125
- case 29:
126
- _context2.prev = 29;
127
- _context2.t0 = _context2["catch"](19);
128
- _iterator.e(_context2.t0);
129
- case 32:
130
- _context2.prev = 32;
131
- _iterator.f();
132
- return _context2.finish(32);
133
- case 35:
134
- logger.info("Ts compile succeed");
135
- case 36:
136
- case "end":
137
- return _context2.stop();
138
- }
139
- }
140
- }, _callee2, null, [[19, 29, 32, 35]]);
141
- }));
142
- return function compileByTs(_x4, _x5, _x6) {
143
- return _ref3.apply(this, arguments);
144
- };
322
+ });
323
+ });
324
+ return function compileByTs(appDirectory, config, compileOptions) {
325
+ return _ref.apply(this, arguments);
326
+ };
145
327
  }();
146
- var getFormatHost = function getFormatHost(ts) {
147
- return {
148
- getCanonicalFileName: function getCanonicalFileName(path) {
149
- return path;
150
- },
151
- getCurrentDirectory: ts.sys.getCurrentDirectory,
152
- getNewLine: function getNewLine() {
153
- return ts.sys.newLine;
154
- }
155
- };
156
- };
328
+ var getFormatHost = function(ts2) {
329
+ return {
330
+ getCanonicalFileName: function(path2) {
331
+ return path2;
332
+ },
333
+ getCurrentDirectory: ts2.sys.getCurrentDirectory,
334
+ getNewLine: function() {
335
+ return ts2.sys.newLine;
336
+ }
337
+ };
338
+ };
339
+ export { compileByTs };