Skip to content

Commit

Permalink
fix(@ngtools/webpack): prevent relative request path mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
clydin committed Mar 8, 2018
1 parent 9f77c86 commit 5dee617
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
25 changes: 19 additions & 6 deletions packages/@ngtools/webpack/src/paths-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,43 @@ export function resolveWithPaths(
return;
}

const originalRequest = request.request.trim();

// Relative requests are not mapped
if (originalRequest.startsWith('.') || originalRequest.startsWith('/')) {
callback(null, request);
return;
}

// check if any path mapping rules are relevant
const pathMapOptions = [];
for (const pattern in compilerOptions.paths) {
// can only contain zero or one
const starIndex = pattern.indexOf('*');
if (starIndex === -1) {
if (pattern === request.request) {
if (pattern === originalRequest) {
pathMapOptions.push({
partial: '',
potentials: compilerOptions.paths[pattern]
});
}
} else if (starIndex === 0 && pattern.length === 1) {
pathMapOptions.push({
partial: originalRequest,
potentials: compilerOptions.paths[pattern],
});
} else if (starIndex === pattern.length - 1) {
if (request.request.startsWith(pattern.slice(0, -1))) {
if (originalRequest.startsWith(pattern.slice(0, -1))) {
pathMapOptions.push({
partial: request.request.slice(pattern.length - 1),
partial: originalRequest.slice(pattern.length - 1),
potentials: compilerOptions.paths[pattern]
});
}
} else {
const [prefix, suffix] = pattern.split('*');
if (request.request.startsWith(prefix) && request.request.endsWith(suffix)) {
if (originalRequest.startsWith(prefix) && originalRequest.endsWith(suffix)) {
pathMapOptions.push({
partial: request.request.slice(prefix.length).slice(0, -suffix.length),
partial: originalRequest.slice(prefix.length).slice(0, -suffix.length),
potentials: compilerOptions.paths[pattern]
});
}
Expand Down Expand Up @@ -79,7 +92,7 @@ export function resolveWithPaths(
}

const moduleResolver = ts.resolveModuleName(
request.request,
originalRequest,
request.contextInfo.issuer,
compilerOptions,
host,
Expand Down
7 changes: 7 additions & 0 deletions tests/e2e/tests/misc/module-resolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import { expectToFail } from '../../utils/utils';


export default async function () {
await updateJsonFile('src/tsconfig.app.json', tsconfig => {
tsconfig.compilerOptions.paths = {
'*': [ '../node_modules/*' ],
};
});
await ng('build');

await createDir('xyz');
await moveFile(
'node_modules/@angular/common',
Expand Down

0 comments on commit 5dee617

Please sign in to comment.