Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
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