Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit e2714f2

Browse files
author
Dimitar Tachev
authored
fix: add a typescript module resolution when searching for the main Angular module location (#800)
1 parent 25bfdcb commit e2714f2

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

Diff for: templates/webpack.angular.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module.exports = env => {
5151
const externals = nsWebpack.getConvertedExternals(env.externals);
5252
const appFullPath = resolve(projectRoot, appPath);
5353
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
54-
54+
const tsConfigName = "tsconfig.tns.json";
5555
const entryModule = `${nsWebpack.getEntryModule(appFullPath)}.ts`;
5656
const entryPath = `.${sep}${entryModule}`;
5757
const ngCompilerTransformers = [];
@@ -68,7 +68,7 @@ module.exports = env => {
6868
// directly from node_modules and the Angular modules loader won't be able to resolve the lazy routes
6969
// fixes https://github.com/NativeScript/nativescript-cli/issues/4024
7070
if (env.externals && env.externals.indexOf("@angular/core") > -1) {
71-
const appModuleRelativePath = getMainModulePath(resolve(appFullPath, entryModule));
71+
const appModuleRelativePath = getMainModulePath(resolve(appFullPath, entryModule), tsConfigName);
7272
if (appModuleRelativePath) {
7373
const appModuleFolderPath = dirname(resolve(appFullPath, appModuleRelativePath));
7474
// include the lazy loader inside app module
@@ -82,7 +82,7 @@ module.exports = env => {
8282
hostReplacementPaths: nsWebpack.getResolver([platform, "tns"]),
8383
platformTransformers: ngCompilerTransformers.map(t => t(() => ngCompilerPlugin, resolve(appFullPath, entryModule))),
8484
mainPath: resolve(appPath, entryModule),
85-
tsConfigPath: join(__dirname, "tsconfig.tns.json"),
85+
tsConfigPath: join(__dirname, tsConfigName),
8686
skipCodeGeneration: !aot,
8787
sourceMap: !!sourceMap,
8888
additionalLazyModuleResources: additionalLazyModuleResources

Diff for: utils/ast-utils.ts

+46-3
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,62 @@
1414
// example of a working workaround by searching for content in each parent.
1515
// 4) Always test your transformer both single and in combinations with the other ones.
1616

17-
import { dirname, join } from "path";
17+
import { dirname, join, relative } from "path";
1818
import * as ts from "typescript";
1919
import { readFileSync, existsSync } from "fs";
2020
import { collectDeepNodes } from "@ngtools/webpack/src/transformers";
2121

22-
export function getMainModulePath(entryFilePath) {
22+
export function getMainModulePath(entryFilePath: string, tsConfigName: string) {
2323
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;
2531
} catch (e) {
2632
return null;
2733
}
2834
}
2935

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+
3073
export function findBootstrapModuleCall(mainPath: string): ts.CallExpression | null {
3174
const source = getSourceFile(mainPath);
3275

0 commit comments

Comments
 (0)