From f5e5d573d3bf87620538601d4e20503a092e689c Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Mon, 26 Feb 2018 13:06:51 -0500 Subject: [PATCH] fix(@ngtools/webpack): prevent relative request path mapping --- packages/@ngtools/webpack/src/paths-plugin.ts | 25 ++++++++++++++----- tests/e2e/tests/misc/module-resolution.ts | 7 ++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/packages/@ngtools/webpack/src/paths-plugin.ts b/packages/@ngtools/webpack/src/paths-plugin.ts index afc321fab3f7..f7e2b339fc68 100644 --- a/packages/@ngtools/webpack/src/paths-plugin.ts +++ b/packages/@ngtools/webpack/src/paths-plugin.ts @@ -31,30 +31,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] }); } @@ -85,7 +98,7 @@ export function resolveWithPaths( } const moduleResolver = ts.resolveModuleName( - request.request, + originalRequest, request.contextInfo.issuer, compilerOptions, host, diff --git a/tests/e2e/tests/misc/module-resolution.ts b/tests/e2e/tests/misc/module-resolution.ts index 1d4e38d245ca..fbb850ae4b2e 100644 --- a/tests/e2e/tests/misc/module-resolution.ts +++ b/tests/e2e/tests/misc/module-resolution.ts @@ -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',