|
14 | 14 | // example of a working workaround by searching for content in each parent.
|
15 | 15 | // 4) Always test your transformer both single and in combinations with the other ones.
|
16 | 16 |
|
17 |
| -import { dirname, join } from "path"; |
| 17 | +import { dirname, join, relative } from "path"; |
18 | 18 | import * as ts from "typescript";
|
19 | 19 | import { readFileSync, existsSync } from "fs";
|
20 | 20 | import { collectDeepNodes } from "@ngtools/webpack/src/transformers";
|
21 | 21 |
|
22 |
| -export function getMainModulePath(entryFilePath) { |
| 22 | +export function getMainModulePath(entryFilePath: string, tsConfigName: string) { |
23 | 23 | try {
|
24 |
| - return findBootstrappedModulePath(entryFilePath); |
| 24 | + // backwards compatibility |
| 25 | + tsConfigName = tsConfigName || "tsconfig.tns.json"; |
| 26 | + |
| 27 | + const tsModuleName = findBootstrappedModulePath(entryFilePath); |
| 28 | + const result = tsResolve(tsModuleName, entryFilePath, tsConfigName); |
| 29 | + |
| 30 | + return result; |
25 | 31 | } catch (e) {
|
26 | 32 | return null;
|
27 | 33 | }
|
28 | 34 | }
|
29 | 35 |
|
| 36 | +/** |
| 37 | + * Returns the real path to the ts/d.ts of the specified `moduleName` relative to the specified `containingFilePath`. (e.g. `~/app/file` -> `./app/file.ts`) |
| 38 | + * @param moduleName The name of the module to be resolved (e.g. `~/config.js`, `lodash`, `./already-relative.js`, `@custom-path/file`). |
| 39 | + * @param containingFilePath An absolute path to the file where the `moduleName` is imported. The relative result will be based on this file. |
| 40 | + * @param tsConfigName The name of the tsconfig which will be used during the module resolution (e.g. `tsconfig.json`). |
| 41 | + * We need this config in order to get its compiler options into account (e.g. resolve any custom `paths` like `~` or `@src`). |
| 42 | + */ |
| 43 | +function tsResolve(moduleName: string, containingFilePath: string, tsConfigName: string) { |
| 44 | + let result = moduleName; |
| 45 | + try { |
| 46 | + const parseConfigFileHost: ts.ParseConfigFileHost = { |
| 47 | + getCurrentDirectory: ts.sys.getCurrentDirectory, |
| 48 | + useCaseSensitiveFileNames: false, |
| 49 | + readDirectory: ts.sys.readDirectory, |
| 50 | + fileExists: ts.sys.fileExists, |
| 51 | + readFile: ts.sys.readFile, |
| 52 | + onUnRecoverableConfigFileDiagnostic: undefined |
| 53 | + }; |
| 54 | + |
| 55 | + const tsConfig = ts.getParsedCommandLineOfConfigFile(tsConfigName, ts.getDefaultCompilerOptions(), parseConfigFileHost); |
| 56 | + |
| 57 | + const compilerOptions: ts.CompilerOptions = tsConfig.options || ts.getDefaultCompilerOptions(); |
| 58 | + const moduleResolutionHost: ts.ModuleResolutionHost = { |
| 59 | + fileExists: ts.sys.fileExists, |
| 60 | + readFile: ts.sys.readFile |
| 61 | + }; |
| 62 | + |
| 63 | + const resolutionResult = ts.resolveModuleName(moduleName, containingFilePath, compilerOptions, moduleResolutionHost); |
| 64 | + |
| 65 | + if (resolutionResult && resolutionResult.resolvedModule && resolutionResult.resolvedModule.resolvedFileName) { |
| 66 | + result = relative(dirname(containingFilePath), resolutionResult.resolvedModule.resolvedFileName); |
| 67 | + } |
| 68 | + } catch (err) { } |
| 69 | + |
| 70 | + return result; |
| 71 | +} |
| 72 | + |
30 | 73 | export function findBootstrapModuleCall(mainPath: string): ts.CallExpression | null {
|
31 | 74 | const source = getSourceFile(mainPath);
|
32 | 75 |
|
|
0 commit comments