UNPKG

4.17 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getRootTsConfigPath = exports.getRootTsConfigFileName = exports.getRelativePathToRootTsConfig = exports.getRootTsConfigPathInTree = exports.resolveModuleByImport = exports.readTsConfig = exports.getSourceNodes = exports.findNodes = exports.compileTypeScript = void 0;
4const devkit_1 = require("@nrwl/devkit");
5const devkit_2 = require("@nrwl/devkit");
6const fs_1 = require("fs");
7const path_1 = require("path");
8var compilation_1 = require("./typescript/compilation");
9Object.defineProperty(exports, "compileTypeScript", { enumerable: true, get: function () { return compilation_1.compileTypeScript; } });
10var find_nodes_1 = require("./typescript/find-nodes");
11Object.defineProperty(exports, "findNodes", { enumerable: true, get: function () { return find_nodes_1.findNodes; } });
12var get_source_nodes_1 = require("./typescript/get-source-nodes");
13Object.defineProperty(exports, "getSourceNodes", { enumerable: true, get: function () { return get_source_nodes_1.getSourceNodes; } });
14const normalizedAppRoot = devkit_2.workspaceRoot.replace(/\\/g, '/');
15let tsModule;
16function 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}
23exports.readTsConfig = readTsConfig;
24function readTsConfigOptions(tsConfigPath) {
25 if (!tsModule) {
26 tsModule = require('typescript');
27 }
28 const readResult = tsModule.readConfigFile(tsConfigPath, tsModule.sys.readFile);
29 // we don't need to scan the files, we only care about options
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}
36let compilerHost;
37/**
38 * Find a module based on it's import
39 *
40 * @param importExpr Import used to resolve to a module
41 * @param filePath
42 * @param tsConfigPath
43 */
44function 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}
55exports.resolveModuleByImport = resolveModuleByImport;
56function 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}
62function 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}
70exports.getRootTsConfigPathInTree = getRootTsConfigPathInTree;
71function getRelativePathToRootTsConfig(tree, targetPath) {
72 return (0, devkit_1.offsetFromRoot)(targetPath) + getRootTsConfigPathInTree(tree);
73}
74exports.getRelativePathToRootTsConfig = getRelativePathToRootTsConfig;
75function 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}
84exports.getRootTsConfigFileName = getRootTsConfigFileName;
85function getRootTsConfigPath() {
86 const tsConfigFileName = getRootTsConfigFileName();
87 return tsConfigFileName ? (0, path_1.join)(devkit_2.workspaceRoot, tsConfigFileName) : null;
88}
89exports.getRootTsConfigPath = getRootTsConfigPath;
90//# sourceMappingURL=typescript.js.map
\No newline at end of file