Skip to content

fix(webpack): paths plugin modification to not allow multiple requests more than once #3443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
34 changes: 25 additions & 9 deletions packages/@ngtools/webpack/src/paths-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class PathsPlugin implements Tapable {
aliasPattern = new RegExp(`^${excapedAlias}$`);
} else {
let withStarCapturing = excapedAlias.replace('\\*', '(.*)');
aliasPattern = new RegExp(`^${withStarCapturing}`);
aliasPattern = new RegExp(`^${withStarCapturing}$`);
}

this.mappings.push({
Expand All @@ -122,22 +122,38 @@ export class PathsPlugin implements Tapable {
}

resolve(resolver: ResolverPlugin, mapping: any, request: any, callback: Callback<any>): any {
if (mapping.alias === '*') {
return callback();
}

let innerRequest = getInnerRequest(resolver, request);
if (!innerRequest) {
return callback();
}

let match = innerRequest.match(mapping.aliasPattern);
if (!match) {
let newRequestStr: string;

let moduleNames =
ts.nodeModuleNameResolver(innerRequest, mapping.alias, this._compilerOptions, this._host);
if (!moduleNames.resolvedModule) {
return callback();
} else {
newRequestStr = moduleNames.resolvedModule.resolvedFileName;
}

let newRequestStr = mapping.target;
if (!mapping.onlyModule) {
newRequestStr = newRequestStr.replace('*', match[1]);
let match = innerRequest.match(mapping.aliasPattern);
if (!match && !newRequestStr) {
return callback();
}
if (newRequestStr[0] === '.') {
newRequestStr = path.resolve(this._absoluteBaseUrl, newRequestStr);

if (!newRequestStr) {
newRequestStr = mapping.target;
if (!mapping.onlyModule) {
newRequestStr = newRequestStr.replace('*', match[1]);
}
if (newRequestStr[0] === '.') {
newRequestStr = path.resolve(this._absoluteBaseUrl, newRequestStr);
}
}

let newRequest = Object.assign({}, request, {
Expand All @@ -163,7 +179,7 @@ export class PathsPlugin implements Tapable {
}

createPlugin(resolver: ResolverPlugin, mapping: any): any {
return (request: any, callback: Callback<any>) => {
return (request: Request, callback: Callback<any>) => {
try {
this.resolve(resolver, mapping, request, callback);
} catch (err) {
Expand Down
6 changes: 6 additions & 0 deletions tests/e2e/tests/build/ts-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export default function() {
],
'@shared/*': [
'app/shared/*'
],
'*': [
'*',
'app/shared/*'
]
};
})
Expand All @@ -25,12 +29,14 @@ export default function() {
import { meaning } from 'app/shared/meaning';
import { meaning as meaning2 } from '@shared';
import { meaning as meaning3 } from '@shared/meaning';
import { meaning as meaning4 } from 'meaning';

// need to use imports otherwise they are ignored and
// no error is outputted, even if baseUrl/paths don't work
console.log(meaning)
console.log(meaning2)
console.log(meaning3)
console.log(meaning4)
`))
.then(() => ng('build'));
}