Skip to content

Commit 5dee617

Browse files
committed
fix(@ngtools/webpack): prevent relative request path mapping
1 parent 9f77c86 commit 5dee617

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

packages/@ngtools/webpack/src/paths-plugin.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,43 @@ export function resolveWithPaths(
2525
return;
2626
}
2727

28+
const originalRequest = request.request.trim();
29+
30+
// Relative requests are not mapped
31+
if (originalRequest.startsWith('.') || originalRequest.startsWith('/')) {
32+
callback(null, request);
33+
return;
34+
}
35+
2836
// check if any path mapping rules are relevant
2937
const pathMapOptions = [];
3038
for (const pattern in compilerOptions.paths) {
3139
// can only contain zero or one
3240
const starIndex = pattern.indexOf('*');
3341
if (starIndex === -1) {
34-
if (pattern === request.request) {
42+
if (pattern === originalRequest) {
3543
pathMapOptions.push({
3644
partial: '',
3745
potentials: compilerOptions.paths[pattern]
3846
});
3947
}
48+
} else if (starIndex === 0 && pattern.length === 1) {
49+
pathMapOptions.push({
50+
partial: originalRequest,
51+
potentials: compilerOptions.paths[pattern],
52+
});
4053
} else if (starIndex === pattern.length - 1) {
41-
if (request.request.startsWith(pattern.slice(0, -1))) {
54+
if (originalRequest.startsWith(pattern.slice(0, -1))) {
4255
pathMapOptions.push({
43-
partial: request.request.slice(pattern.length - 1),
56+
partial: originalRequest.slice(pattern.length - 1),
4457
potentials: compilerOptions.paths[pattern]
4558
});
4659
}
4760
} else {
4861
const [prefix, suffix] = pattern.split('*');
49-
if (request.request.startsWith(prefix) && request.request.endsWith(suffix)) {
62+
if (originalRequest.startsWith(prefix) && originalRequest.endsWith(suffix)) {
5063
pathMapOptions.push({
51-
partial: request.request.slice(prefix.length).slice(0, -suffix.length),
64+
partial: originalRequest.slice(prefix.length).slice(0, -suffix.length),
5265
potentials: compilerOptions.paths[pattern]
5366
});
5467
}
@@ -79,7 +92,7 @@ export function resolveWithPaths(
7992
}
8093

8194
const moduleResolver = ts.resolveModuleName(
82-
request.request,
95+
originalRequest,
8396
request.contextInfo.issuer,
8497
compilerOptions,
8598
host,

tests/e2e/tests/misc/module-resolution.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import { expectToFail } from '../../utils/utils';
55

66

77
export default async function () {
8+
await updateJsonFile('src/tsconfig.app.json', tsconfig => {
9+
tsconfig.compilerOptions.paths = {
10+
'*': [ '../node_modules/*' ],
11+
};
12+
});
13+
await ng('build');
14+
815
await createDir('xyz');
916
await moveFile(
1017
'node_modules/@angular/common',

0 commit comments

Comments
 (0)