Skip to content

Commit b8dde3d

Browse files
committed
fix(webpack): paths plugin wildcard regex fix
1 parent 20bb864 commit b8dde3d

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

packages/@ngtools/webpack/src/paths-plugin.ts

+25-9
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export class PathsPlugin implements Tapable {
9696
aliasPattern = new RegExp(`^${excapedAlias}$`);
9797
} else {
9898
let withStarCapturing = excapedAlias.replace('\\*', '(.*)');
99-
aliasPattern = new RegExp(`^${withStarCapturing}`);
99+
aliasPattern = new RegExp(`^${withStarCapturing}$`);
100100
}
101101

102102
this.mappings.push({
@@ -122,22 +122,38 @@ export class PathsPlugin implements Tapable {
122122
}
123123

124124
resolve(resolver: ResolverPlugin, mapping: any, request: any, callback: Callback<any>): any {
125+
if (mapping.alias === '*') {
126+
return callback();
127+
}
128+
125129
let innerRequest = getInnerRequest(resolver, request);
126130
if (!innerRequest) {
127131
return callback();
128132
}
129133

134+
let newRequestStr: string;
135+
136+
let moduleNames =
137+
ts.nodeModuleNameResolver(innerRequest, mapping.alias, this._compilerOptions, this._host);
138+
if (!moduleNames.resolvedModule) {
139+
callback();
140+
} else {
141+
newRequestStr = moduleNames.resolvedModule.resolvedFileName;
142+
}
143+
130144
let match = innerRequest.match(mapping.aliasPattern);
131-
if (!match) {
145+
if (!match && !newRequestStr) {
132146
return callback();
133147
}
134148

135-
let newRequestStr = mapping.target;
136-
if (!mapping.onlyModule) {
137-
newRequestStr = newRequestStr.replace('*', match[1]);
138-
}
139-
if (newRequestStr[0] === '.') {
140-
newRequestStr = path.resolve(this._absoluteBaseUrl, newRequestStr);
149+
if (!newRequestStr) {
150+
newRequestStr = mapping.target;
151+
if (!mapping.onlyModule) {
152+
newRequestStr = newRequestStr.replace('*', match[1]);
153+
}
154+
if (newRequestStr[0] === '.') {
155+
newRequestStr = path.resolve(this._absoluteBaseUrl, newRequestStr);
156+
}
141157
}
142158

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

165181
createPlugin(resolver: ResolverPlugin, mapping: any): any {
166-
return (request: any, callback: Callback<any>) => {
182+
return (request: Request, callback: Callback<any>) => {
167183
try {
168184
this.resolve(resolver, mapping, request, callback);
169185
} catch (err) {

tests/e2e/tests/build/ts-paths.ts

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export default function() {
1313
],
1414
'@shared/*': [
1515
'app/shared/*'
16+
],
17+
'*': [
18+
'*',
19+
'app/shared/*'
1620
]
1721
};
1822
})
@@ -25,12 +29,14 @@ export default function() {
2529
import { meaning } from 'app/shared/meaning';
2630
import { meaning as meaning2 } from '@shared';
2731
import { meaning as meaning3 } from '@shared/meaning';
32+
import { meaning as meaning4 } from 'meaning';
2833
2934
// need to use imports otherwise they are ignored and
3035
// no error is outputted, even if baseUrl/paths don't work
3136
console.log(meaning)
3237
console.log(meaning2)
3338
console.log(meaning3)
39+
console.log(meaning4)
3440
`))
3541
.then(() => ng('build'));
3642
}

0 commit comments

Comments
 (0)