@modern-js/server-utils 2.25.0 → 2.25.1-alpha.0
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/dist/cjs/common/index.js +4 -4
- package/dist/esm/common/index.js +20 -10
- package/dist/esm-node/common/index.js +2 -2
- package/dist/js/modern/common/index.js +55 -0
- package/dist/js/modern/compilers/babel/index.js +149 -0
- package/dist/js/modern/compilers/typescript/index.js +123 -0
- package/dist/js/modern/compilers/typescript/tsconfig-paths-plugin.js +187 -0
- package/dist/js/modern/compilers/typescript/typescript-loader.js +24 -0
- package/dist/js/modern/index.js +5 -0
- package/dist/js/node/common/index.js +85 -0
- package/dist/js/node/compilers/babel/index.js +178 -0
- package/dist/js/node/compilers/typescript/index.js +150 -0
- package/dist/js/node/compilers/typescript/tsconfig-paths-plugin.js +216 -0
- package/dist/js/node/compilers/typescript/typescript-loader.js +47 -0
- package/dist/js/node/index.js +29 -0
- package/dist/js/treeshaking/common/index.js +204 -0
- package/dist/js/treeshaking/compilers/babel/index.js +368 -0
- package/dist/js/treeshaking/compilers/typescript/index.js +339 -0
- package/dist/js/treeshaking/compilers/typescript/tsconfig-paths-plugin.js +246 -0
- package/dist/js/treeshaking/compilers/typescript/typescript-loader.js +65 -0
- package/dist/js/treeshaking/index.js +3 -0
- package/package.json +3 -3
|
@@ -0,0 +1,339 @@
|
|
|
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";
|
|
156
|
+
import { TypescriptLoader } from "./typescript-loader";
|
|
157
|
+
import { tsconfigPathsBeforeHookFactory } from "./tsconfig-paths-plugin";
|
|
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
|
+
};
|
|
166
|
+
};
|
|
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
|
+
];
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
return function copyFiles(from, to, tsconfigPath) {
|
|
205
|
+
return _ref.apply(this, arguments);
|
|
206
|
+
};
|
|
207
|
+
}();
|
|
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
|
+
];
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
return function compileByTs(appDirectory, config, compileOptions) {
|
|
325
|
+
return _ref.apply(this, arguments);
|
|
326
|
+
};
|
|
327
|
+
}();
|
|
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 };
|
|
@@ -0,0 +1,246 @@
|
|
|
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 _arrayWithHoles(arr) {
|
|
7
|
+
if (Array.isArray(arr)) return arr;
|
|
8
|
+
}
|
|
9
|
+
function _arrayWithoutHoles(arr) {
|
|
10
|
+
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
|
|
11
|
+
}
|
|
12
|
+
function _instanceof(left, right) {
|
|
13
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
14
|
+
return !!right[Symbol.hasInstance](left);
|
|
15
|
+
} else {
|
|
16
|
+
return left instanceof right;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function _iterableToArray(iter) {
|
|
20
|
+
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
21
|
+
}
|
|
22
|
+
function _iterableToArrayLimit(arr, i) {
|
|
23
|
+
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
24
|
+
if (_i == null) return;
|
|
25
|
+
var _arr = [];
|
|
26
|
+
var _n = true;
|
|
27
|
+
var _d = false;
|
|
28
|
+
var _s, _e;
|
|
29
|
+
try {
|
|
30
|
+
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
|
|
31
|
+
_arr.push(_s.value);
|
|
32
|
+
if (i && _arr.length === i) break;
|
|
33
|
+
}
|
|
34
|
+
} catch (err) {
|
|
35
|
+
_d = true;
|
|
36
|
+
_e = err;
|
|
37
|
+
} finally{
|
|
38
|
+
try {
|
|
39
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
|
40
|
+
} finally{
|
|
41
|
+
if (_d) throw _e;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return _arr;
|
|
45
|
+
}
|
|
46
|
+
function _nonIterableRest() {
|
|
47
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
48
|
+
}
|
|
49
|
+
function _nonIterableSpread() {
|
|
50
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
51
|
+
}
|
|
52
|
+
function _slicedToArray(arr, i) {
|
|
53
|
+
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
|
|
54
|
+
}
|
|
55
|
+
function _toConsumableArray(arr) {
|
|
56
|
+
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
|
|
57
|
+
}
|
|
58
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
59
|
+
if (!o) return;
|
|
60
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
61
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
62
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
63
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
64
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
65
|
+
}
|
|
66
|
+
import * as os from "os";
|
|
67
|
+
import path, { dirname, posix } from "path";
|
|
68
|
+
import * as ts from "typescript";
|
|
69
|
+
import { createMatchPath } from "@modern-js/utils/tsconfig-paths";
|
|
70
|
+
var isRegExpKey = function(str) {
|
|
71
|
+
return str.startsWith("^") || str.endsWith("$");
|
|
72
|
+
};
|
|
73
|
+
var resolveAliasPath = function(baseUrl, filePath) {
|
|
74
|
+
if (filePath.startsWith(".") || filePath.startsWith("..")) {
|
|
75
|
+
return path.resolve(baseUrl, filePath);
|
|
76
|
+
}
|
|
77
|
+
return filePath;
|
|
78
|
+
};
|
|
79
|
+
var createAliasMatcher = function(baseUrl, alias) {
|
|
80
|
+
var aliasPairs = Object.keys(alias).reduce(function(o, key) {
|
|
81
|
+
if (isRegExpKey(key)) {
|
|
82
|
+
var regexp = new RegExp(key);
|
|
83
|
+
var aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
84
|
+
o.push([
|
|
85
|
+
regexp,
|
|
86
|
+
aliasPath
|
|
87
|
+
]);
|
|
88
|
+
} else {
|
|
89
|
+
var aliasPath1 = resolveAliasPath(baseUrl, alias[key]);
|
|
90
|
+
o.push([
|
|
91
|
+
key,
|
|
92
|
+
aliasPath1
|
|
93
|
+
]);
|
|
94
|
+
}
|
|
95
|
+
return o;
|
|
96
|
+
}, []);
|
|
97
|
+
var cacheMap = /* @__PURE__ */ new Map();
|
|
98
|
+
return function(requestedModule) {
|
|
99
|
+
if (cacheMap.has(requestedModule)) {
|
|
100
|
+
return cacheMap.get(requestedModule);
|
|
101
|
+
}
|
|
102
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
103
|
+
try {
|
|
104
|
+
for(var _iterator = aliasPairs[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
105
|
+
var _value = _slicedToArray(_step.value, 2), key = _value[0], value = _value[1];
|
|
106
|
+
if (_instanceof(key, RegExp)) {
|
|
107
|
+
if (key.test(requestedModule)) {
|
|
108
|
+
cacheMap.set(requestedModule, value);
|
|
109
|
+
return value;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (requestedModule === key) {
|
|
113
|
+
cacheMap.set(requestedModule, value);
|
|
114
|
+
return value;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
} catch (err) {
|
|
118
|
+
_didIteratorError = true;
|
|
119
|
+
_iteratorError = err;
|
|
120
|
+
} finally{
|
|
121
|
+
try {
|
|
122
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
123
|
+
_iterator.return();
|
|
124
|
+
}
|
|
125
|
+
} finally{
|
|
126
|
+
if (_didIteratorError) {
|
|
127
|
+
throw _iteratorError;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
var isDynamicImport = function(tsBinary, node) {
|
|
134
|
+
return tsBinary.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
|
|
135
|
+
};
|
|
136
|
+
function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
|
|
137
|
+
var tsPaths = {};
|
|
138
|
+
var alias = {};
|
|
139
|
+
Object.keys(paths).forEach(function(key) {
|
|
140
|
+
if (Array.isArray(paths[key])) {
|
|
141
|
+
tsPaths[key] = paths[key];
|
|
142
|
+
} else {
|
|
143
|
+
alias[key] = paths[key];
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
var matchAliasPath = createAliasMatcher(baseUrl, alias);
|
|
147
|
+
var matchTsPath = createMatchPath(baseUrl, tsPaths, [
|
|
148
|
+
"main"
|
|
149
|
+
]);
|
|
150
|
+
var matchPath = function(requestedModule, readJSONSync, fileExists, extensions) {
|
|
151
|
+
var result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
|
|
152
|
+
if (result) {
|
|
153
|
+
return result;
|
|
154
|
+
}
|
|
155
|
+
return matchAliasPath(requestedModule);
|
|
156
|
+
};
|
|
157
|
+
if (Object.keys(paths).length === 0) {
|
|
158
|
+
return void 0;
|
|
159
|
+
}
|
|
160
|
+
return function(ctx) {
|
|
161
|
+
return function(sf) {
|
|
162
|
+
var visitNode = function(node) {
|
|
163
|
+
if (isDynamicImport(tsBinary, node)) {
|
|
164
|
+
var importPathWithQuotes = node.arguments[0].getText(sf);
|
|
165
|
+
var text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
|
|
166
|
+
var result = getNotAliasedPath(sf, matchPath, text);
|
|
167
|
+
if (!result) {
|
|
168
|
+
return node;
|
|
169
|
+
}
|
|
170
|
+
return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([
|
|
171
|
+
tsBinary.factory.createStringLiteral(result)
|
|
172
|
+
]));
|
|
173
|
+
}
|
|
174
|
+
if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
|
|
175
|
+
try {
|
|
176
|
+
var ref;
|
|
177
|
+
var importPathWithQuotes1 = node === null || node === void 0 ? void 0 : (ref = node.moduleSpecifier) === null || ref === void 0 ? void 0 : ref.getText();
|
|
178
|
+
if (!importPathWithQuotes1) {
|
|
179
|
+
return node;
|
|
180
|
+
}
|
|
181
|
+
var text1 = importPathWithQuotes1.substring(1, importPathWithQuotes1.length - 1);
|
|
182
|
+
var result1 = getNotAliasedPath(sf, matchPath, text1);
|
|
183
|
+
if (!result1) {
|
|
184
|
+
return node;
|
|
185
|
+
}
|
|
186
|
+
var moduleSpecifier = tsBinary.factory.createStringLiteral(result1);
|
|
187
|
+
moduleSpecifier.parent = node.moduleSpecifier.parent;
|
|
188
|
+
var newNode;
|
|
189
|
+
if (tsBinary.isImportDeclaration(node)) {
|
|
190
|
+
newNode = tsBinary.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
|
|
191
|
+
} else {
|
|
192
|
+
newNode = tsBinary.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
|
|
193
|
+
}
|
|
194
|
+
newNode.flags = node.flags;
|
|
195
|
+
return newNode;
|
|
196
|
+
} catch (e) {
|
|
197
|
+
return node;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return tsBinary.visitEachChild(node, visitNode, ctx);
|
|
201
|
+
};
|
|
202
|
+
return tsBinary.visitNode(sf, visitNode);
|
|
203
|
+
};
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
function getNotAliasedPath(sf, matcher, text) {
|
|
207
|
+
var result = matcher(text, void 0, void 0, [
|
|
208
|
+
".ts",
|
|
209
|
+
".tsx",
|
|
210
|
+
".js",
|
|
211
|
+
".jsx"
|
|
212
|
+
]);
|
|
213
|
+
if (!result) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
if (os.platform() === "win32") {
|
|
217
|
+
result = result.replace(/\\/g, "/");
|
|
218
|
+
}
|
|
219
|
+
if (!path.isAbsolute(result)) {
|
|
220
|
+
if (!result.startsWith(".") && !result.startsWith("..")) {
|
|
221
|
+
try {
|
|
222
|
+
var packagePath = require.resolve(result, {
|
|
223
|
+
paths: [
|
|
224
|
+
process.cwd()
|
|
225
|
+
].concat(_toConsumableArray(module.paths))
|
|
226
|
+
});
|
|
227
|
+
if (packagePath) {
|
|
228
|
+
return result;
|
|
229
|
+
}
|
|
230
|
+
} catch (e) {}
|
|
231
|
+
}
|
|
232
|
+
try {
|
|
233
|
+
var packagePath1 = require.resolve(text, {
|
|
234
|
+
paths: [
|
|
235
|
+
process.cwd()
|
|
236
|
+
].concat(_toConsumableArray(module.paths))
|
|
237
|
+
});
|
|
238
|
+
if (packagePath1) {
|
|
239
|
+
return text;
|
|
240
|
+
}
|
|
241
|
+
} catch (e1) {}
|
|
242
|
+
}
|
|
243
|
+
var resolvedPath = posix.relative(dirname(sf.fileName), result) || "./";
|
|
244
|
+
return resolvedPath[0] === "." ? resolvedPath : "./".concat(resolvedPath);
|
|
245
|
+
}
|
|
246
|
+
export { tsconfigPathsBeforeHookFactory };
|
|
@@ -0,0 +1,65 @@
|
|
|
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 TypescriptLoader = /*#__PURE__*/ function() {
|
|
34
|
+
"use strict";
|
|
35
|
+
function TypescriptLoader(param) {
|
|
36
|
+
var appDirectory = param.appDirectory;
|
|
37
|
+
_classCallCheck(this, TypescriptLoader);
|
|
38
|
+
_defineProperty(this, "tsBinary", void 0);
|
|
39
|
+
_defineProperty(this, "appDirectory", void 0);
|
|
40
|
+
this.appDirectory = appDirectory;
|
|
41
|
+
}
|
|
42
|
+
_createClass(TypescriptLoader, [
|
|
43
|
+
{
|
|
44
|
+
key: "load",
|
|
45
|
+
value: function load() {
|
|
46
|
+
if (this.tsBinary) {
|
|
47
|
+
return this.tsBinary;
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
var tsPath = require.resolve("typescript", {
|
|
51
|
+
paths: [
|
|
52
|
+
this.appDirectory || process.cwd()
|
|
53
|
+
]
|
|
54
|
+
});
|
|
55
|
+
var ts = require(tsPath);
|
|
56
|
+
return ts;
|
|
57
|
+
} catch (error) {
|
|
58
|
+
throw new Error('TypeScript could not be found! Please, install "typescript" package.');
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
]);
|
|
63
|
+
return TypescriptLoader;
|
|
64
|
+
}();
|
|
65
|
+
export { TypescriptLoader };
|
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "2.25.0",
|
|
18
|
+
"version": "2.25.1-alpha.0",
|
|
19
19
|
"jsnext:source": "./src/index.ts",
|
|
20
20
|
"types": "./dist/types/index.d.ts",
|
|
21
21
|
"main": "./dist/cjs/index.js",
|
|
@@ -51,8 +51,8 @@
|
|
|
51
51
|
"ts-jest": "^29.1.0",
|
|
52
52
|
"typescript": "^5",
|
|
53
53
|
"@modern-js/server-core": "2.25.0",
|
|
54
|
-
"@scripts/
|
|
55
|
-
"@scripts/
|
|
54
|
+
"@scripts/build": "2.25.0",
|
|
55
|
+
"@scripts/jest-config": "2.25.0"
|
|
56
56
|
},
|
|
57
57
|
"sideEffects": false,
|
|
58
58
|
"publishConfig": {
|