1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getRootTsConfigPath = exports.getRootTsConfigFileName = exports.getRelativePathToRootTsConfig = exports.getRootTsConfigPathInTree = exports.resolveModuleByImport = exports.readTsConfig = exports.getSourceNodes = exports.findNodes = exports.compileTypeScript = void 0;
|
4 | const devkit_1 = require("@nrwl/devkit");
|
5 | const devkit_2 = require("@nrwl/devkit");
|
6 | const fs_1 = require("fs");
|
7 | const path_1 = require("path");
|
8 | var compilation_1 = require("./typescript/compilation");
|
9 | Object.defineProperty(exports, "compileTypeScript", { enumerable: true, get: function () { return compilation_1.compileTypeScript; } });
|
10 | var find_nodes_1 = require("./typescript/find-nodes");
|
11 | Object.defineProperty(exports, "findNodes", { enumerable: true, get: function () { return find_nodes_1.findNodes; } });
|
12 | var get_source_nodes_1 = require("./typescript/get-source-nodes");
|
13 | Object.defineProperty(exports, "getSourceNodes", { enumerable: true, get: function () { return get_source_nodes_1.getSourceNodes; } });
|
14 | const normalizedAppRoot = devkit_2.workspaceRoot.replace(/\\/g, '/');
|
15 | let tsModule;
|
16 | function readTsConfig(tsConfigPath) {
|
17 | if (!tsModule) {
|
18 | tsModule = require('typescript');
|
19 | }
|
20 | const readResult = tsModule.readConfigFile(tsConfigPath, tsModule.sys.readFile);
|
21 | return tsModule.parseJsonConfigFileContent(readResult.config, tsModule.sys, (0, path_1.dirname)(tsConfigPath));
|
22 | }
|
23 | exports.readTsConfig = readTsConfig;
|
24 | function readTsConfigOptions(tsConfigPath) {
|
25 | if (!tsModule) {
|
26 | tsModule = require('typescript');
|
27 | }
|
28 | const readResult = tsModule.readConfigFile(tsConfigPath, tsModule.sys.readFile);
|
29 |
|
30 | const host = {
|
31 | readDirectory: () => [],
|
32 | fileExists: tsModule.sys.fileExists,
|
33 | };
|
34 | return tsModule.parseJsonConfigFileContent(readResult.config, host, (0, path_1.dirname)(tsConfigPath)).options;
|
35 | }
|
36 | let compilerHost;
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | function resolveModuleByImport(importExpr, filePath, tsConfigPath) {
|
45 | compilerHost = compilerHost || getCompilerHost(tsConfigPath);
|
46 | const { options, host, moduleResolutionCache } = compilerHost;
|
47 | const { resolvedModule } = tsModule.resolveModuleName(importExpr, filePath, options, host, moduleResolutionCache);
|
48 | if (!resolvedModule) {
|
49 | return;
|
50 | }
|
51 | else {
|
52 | return resolvedModule.resolvedFileName.replace(`${normalizedAppRoot}/`, '');
|
53 | }
|
54 | }
|
55 | exports.resolveModuleByImport = resolveModuleByImport;
|
56 | function getCompilerHost(tsConfigPath) {
|
57 | const options = readTsConfigOptions(tsConfigPath);
|
58 | const host = tsModule.createCompilerHost(options, true);
|
59 | const moduleResolutionCache = tsModule.createModuleResolutionCache(devkit_2.workspaceRoot, host.getCanonicalFileName);
|
60 | return { options, host, moduleResolutionCache };
|
61 | }
|
62 | function getRootTsConfigPathInTree(tree) {
|
63 | for (const path of ['tsconfig.base.json', 'tsconfig.json']) {
|
64 | if (tree.exists(path)) {
|
65 | return path;
|
66 | }
|
67 | }
|
68 | return 'tsconfig.base.json';
|
69 | }
|
70 | exports.getRootTsConfigPathInTree = getRootTsConfigPathInTree;
|
71 | function getRelativePathToRootTsConfig(tree, targetPath) {
|
72 | return (0, devkit_1.offsetFromRoot)(targetPath) + getRootTsConfigPathInTree(tree);
|
73 | }
|
74 | exports.getRelativePathToRootTsConfig = getRelativePathToRootTsConfig;
|
75 | function getRootTsConfigFileName() {
|
76 | for (const tsConfigName of ['tsconfig.base.json', 'tsconfig.json']) {
|
77 | const tsConfigPath = (0, path_1.join)(devkit_2.workspaceRoot, tsConfigName);
|
78 | if ((0, fs_1.existsSync)(tsConfigPath)) {
|
79 | return tsConfigName;
|
80 | }
|
81 | }
|
82 | return null;
|
83 | }
|
84 | exports.getRootTsConfigFileName = getRootTsConfigFileName;
|
85 | function getRootTsConfigPath() {
|
86 | const tsConfigFileName = getRootTsConfigFileName();
|
87 | return tsConfigFileName ? (0, path_1.join)(devkit_2.workspaceRoot, tsConfigFileName) : null;
|
88 | }
|
89 | exports.getRootTsConfigPath = getRootTsConfigPath;
|
90 |
|
\ | No newline at end of file |