Skip to content

Commit

Permalink
fix(@ngtools/webpack): fix paths mapping recursive support
Browse files Browse the repository at this point in the history
Some path mappings (see issue) were not resolving properly when they were
used in deep paths.

Fixes #7341
  • Loading branch information
hansl committed Aug 22, 2017
1 parent 29988d5 commit d1d2aa0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
38 changes: 23 additions & 15 deletions packages/@ngtools/webpack/src/paths-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,29 +122,37 @@ export class PathsPlugin implements Tapable {

this._nmf.plugin('before-resolve', (request: NormalModuleFactoryRequest,
callback: Callback<any>) => {
// Only work on TypeScript issuers.
if (!request.contextInfo.issuer || !request.contextInfo.issuer.endsWith('.ts')) {
return callback(null, request);
}

for (let mapping of this._mappings) {
const match = request.request.match(mapping.aliasPattern);
if (!match) { continue; }

let newRequestStr = mapping.target;
if (!mapping.onlyModule) {
newRequestStr = newRequestStr.replace('*', match[1]);
}

const moduleResolver: ts.ResolvedModuleWithFailedLookupLocations =
ts.nodeModuleNameResolver(
newRequestStr,
this._absoluteBaseUrl,
this._compilerOptions,
this._host
);
const moduleFilePath = moduleResolver.resolvedModule ?
moduleResolver.resolvedModule.resolvedFileName : '';

const moduleResolver = ts.resolveModuleName(
request.request,

This comment has been minimized.

Copy link
@jonrimmer

jonrimmer Aug 30, 2017

Didn't you mean to use newRequestStr here? Using the original request will cause TypeScript to resolve it anywhere in the path, including outside of the mapped path.

request.contextInfo.issuer,
this._compilerOptions,
this._host
);
let moduleFilePath = moduleResolver.resolvedModule
&& moduleResolver.resolvedModule.resolvedFileName;

// If TypeScript gives us a .d.ts it's probably a node module and we need to let webpack
// do the resolution.
if (moduleFilePath && moduleFilePath.endsWith('.d.ts')) {
moduleFilePath = moduleFilePath.replace(/\.d\.ts$/, '.js');
if (!this._host.fileExists(moduleFilePath)) {
continue;
}
}
if (moduleFilePath) {
return callback(null, Object.assign({}, request, {
request: moduleFilePath.includes('.d.ts') ? newRequestStr : moduleFilePath
}));
return callback(null, Object.assign({}, request, { request: moduleFilePath }));
}
}

Expand Down
19 changes: 13 additions & 6 deletions tests/e2e/tests/build/ts-paths.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
import {updateTsConfig} from '../../utils/project';
import {writeMultipleFiles, appendToFile, createDir} from '../../utils/fs';
import {writeMultipleFiles, appendToFile, createDir, replaceInFile} from '../../utils/fs';
import {ng} from '../../utils/process';
import {stripIndents} from 'common-tags';


export default function() {
return updateTsConfig(json => {
json['compilerOptions']['baseUrl'] = '.';
json['compilerOptions']['baseUrl'] = './';
json['compilerOptions']['paths'] = {
'@shared': [
'app/shared'
],
'@shared/*': [
'app/shared/*'
],
'*': [
'*',
'app/shared/*'
'@root/*': [
'./*'
]
};
})
.then(() => createDir('src/app/shared'))
.then(() => writeMultipleFiles({
'src/meaning-too.ts': 'export var meaning = 42;',
'src/app/shared/meaning.ts': 'export var meaning = 42;',
'src/app/shared/index.ts': `export * from './meaning'`
'src/app/shared/index.ts': `export * from './meaning'`,
}))
.then(() => replaceInFile('src/app/app.module.ts', './app.component', '@root/app/app.component'))
.then(() => ng('build'))
.then(() => updateTsConfig(json => {
json['compilerOptions']['paths']['*'] = [
'*',
'app/shared/*'
];
}))
.then(() => appendToFile('src/app/app.component.ts', stripIndents`
import { meaning } from 'app/shared/meaning';
Expand Down

0 comments on commit d1d2aa0

Please sign in to comment.