Skip to content

Commit 47f937e

Browse files
alan-agius4clydin
authored andcommitted
fix(@ngtools/webpack): improve fallbacking for path mapping
TypeScript resolution works as follows; ``` // /node_modules/moduleB.ts // /node_modules/moduleB.tsx // /node_modules/moduleB.d.ts // /node_modules/moduleB/package.json (if it specifies a "types" property) // /node_modules/moduleB/index.ts // /node_modules/moduleB/index.tsx // /node_modules/moduleB/index.d.ts ```
1 parent c0bc9bb commit 47f937e

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

Diff for: packages/ngtools/webpack/src/paths-plugin.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,21 @@ export function resolveWithPaths(
154154

155155
// If TypeScript gives us a `.d.ts`, it is probably a node module
156156
if (moduleFilePath.endsWith('.d.ts')) {
157-
// If in a package, let webpack resolve the package
158-
const packageRootPath = path.join(path.dirname(moduleFilePath), 'package.json');
159-
if (!host.fileExists(packageRootPath)) {
160-
// Otherwise, if there is a file with a .js extension use that
161-
const jsFilePath = moduleFilePath.slice(0, -5) + '.js';
162-
if (host.fileExists(jsFilePath)) {
157+
const pathNoExtension = moduleFilePath.slice(0, -5);
158+
const pathDirName = path.dirname(moduleFilePath);
159+
const packageRootPath = path.join(pathDirName, 'package.json');
160+
const jsFilePath = `${pathNoExtension}.js`;
161+
162+
if (host.fileExists(pathNoExtension)) {
163+
// This is mainly for secondary entry points
164+
// ex: 'node_modules/@angular/core/testing.d.ts' -> 'node_modules/@angular/core/testing'
165+
request.request = pathNoExtension;
166+
} else if (host.fileExists(packageRootPath)) {
167+
// Let webpack resolve the correct module format
168+
request.request = pathDirName;
169+
} else if (host.fileExists(jsFilePath)) {
170+
// Otherwise, if there is a file with a .js extension use that
163171
request.request = jsFilePath;
164-
}
165172
}
166173

167174
callback(null, request);

0 commit comments

Comments
 (0)