1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.tsconfigPathsBeforeHookFactory = void 0;
|
4 | const os = require("os");
|
5 | const path_1 = require("path");
|
6 | const typescript_loader_1 = require("../typescript-loader");
|
7 | const tsPaths = require("tsconfig-paths");
|
8 | function tsconfigPathsBeforeHookFactory(compilerOptions) {
|
9 | var _a;
|
10 | const tsBinary = new typescript_loader_1.TypeScriptBinaryLoader().load();
|
11 | const [tsVersionMajor, tsVersionMinor] = (_a = tsBinary.versionMajorMinor) === null || _a === void 0 ? void 0 : _a.split('.').map((x) => +x);
|
12 | const isInUpdatedAstContext = tsVersionMinor >= 8 || tsVersionMajor > 4;
|
13 | const { paths = {}, baseUrl = './' } = compilerOptions;
|
14 | const matcher = tsPaths.createMatchPath(baseUrl, paths, ['main']);
|
15 | return (ctx) => {
|
16 | return (sf) => {
|
17 | const visitNode = (node) => {
|
18 | if (tsBinary.isImportDeclaration(node) ||
|
19 | (tsBinary.isExportDeclaration(node) && node.moduleSpecifier)) {
|
20 | try {
|
21 | const importPathWithQuotes = node.moduleSpecifier && node.moduleSpecifier.getText();
|
22 | if (!importPathWithQuotes) {
|
23 | return node;
|
24 | }
|
25 | const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
|
26 | const result = getNotAliasedPath(sf, matcher, text);
|
27 | if (!result) {
|
28 | return node;
|
29 | }
|
30 | const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
|
31 | moduleSpecifier.parent = node.moduleSpecifier.parent;
|
32 | if (tsBinary.isImportDeclaration(node)) {
|
33 | const updatedNode = isInUpdatedAstContext
|
34 | ? tsBinary.factory.updateImportDeclaration(node, node.modifiers, node.importClause, moduleSpecifier, node.assertClause)
|
35 | : tsBinary.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
|
36 | updatedNode.flags = node.flags;
|
37 | return updatedNode;
|
38 | }
|
39 | else {
|
40 | const updatedNode = isInUpdatedAstContext
|
41 | ? tsBinary.factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause)
|
42 | : tsBinary.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
|
43 | updatedNode.flags = node.flags;
|
44 | return updatedNode;
|
45 | }
|
46 | }
|
47 | catch (_a) {
|
48 | return node;
|
49 | }
|
50 | }
|
51 | return tsBinary.visitEachChild(node, visitNode, ctx);
|
52 | };
|
53 | return tsBinary.visitNode(sf, visitNode);
|
54 | };
|
55 | };
|
56 | }
|
57 | exports.tsconfigPathsBeforeHookFactory = tsconfigPathsBeforeHookFactory;
|
58 | function getNotAliasedPath(sf, matcher, text) {
|
59 | let result = matcher(text, undefined, undefined, [
|
60 | '.ts',
|
61 | '.tsx',
|
62 | '.js',
|
63 | '.jsx',
|
64 | ]);
|
65 | if (!result) {
|
66 | return;
|
67 | }
|
68 | if (os.platform() === 'win32') {
|
69 | result = result.replace(/\\/g, '/');
|
70 | }
|
71 | try {
|
72 |
|
73 |
|
74 | const packagePath = require.resolve(text, {
|
75 | paths: [process.cwd(), ...module.paths],
|
76 | });
|
77 | if (packagePath) {
|
78 | return text;
|
79 | }
|
80 | }
|
81 | catch (_a) { }
|
82 | const resolvedPath = path_1.posix.relative((0, path_1.dirname)(sf.fileName), result) || './';
|
83 | return resolvedPath[0] === '.' ? resolvedPath : './' + resolvedPath;
|
84 | }
|