Skip to content

Commit 567ca72

Browse files
committed
fix(webpack): paths plugin wildcard regex fix
1 parent 901a64f commit 567ca72

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

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

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,12 @@ export class PathsPlugin implements Tapable {
9595
if (onlyModule) {
9696
aliasPattern = new RegExp(`^${excapedAlias}$`);
9797
} else {
98-
let withStarCapturing = excapedAlias.replace('\\*', '(.*)');
99-
aliasPattern = new RegExp(`^${withStarCapturing}`);
98+
if (alias !== '*') {
99+
let withStarCapturing = excapedAlias.replace('\\*', '(.*)');
100+
aliasPattern = new RegExp(`^${withStarCapturing}$`);
101+
} else {
102+
aliasPattern = new RegExp(`${excapedAlias}$`);
103+
}
100104
}
101105

102106
this.mappings.push({
@@ -127,17 +131,31 @@ export class PathsPlugin implements Tapable {
127131
return callback();
128132
}
129133

134+
let newRequestStr: string;
135+
136+
if (mapping.alias === '*') {
137+
let moduleNames =
138+
ts.nodeModuleNameResolver(innerRequest, '*', this._compilerOptions, this._host);
139+
if (!moduleNames.resolvedModule) {
140+
callback();
141+
} else {
142+
newRequestStr = moduleNames.resolvedModule.resolvedFileName;
143+
}
144+
}
145+
130146
let match = innerRequest.match(mapping.aliasPattern);
131-
if (!match) {
147+
if (!match && !newRequestStr) {
132148
return callback();
133149
}
134150

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);
151+
if (!newRequestStr) {
152+
newRequestStr = mapping.target;
153+
if (!mapping.onlyModule) {
154+
newRequestStr = newRequestStr.replace('*', match[1]);
155+
}
156+
if (newRequestStr[0] === '.') {
157+
newRequestStr = path.resolve(this._absoluteBaseUrl, newRequestStr);
158+
}
141159
}
142160

143161
let newRequest = Object.assign({}, request, {
@@ -163,7 +181,7 @@ export class PathsPlugin implements Tapable {
163181
}
164182

165183
createPlugin(resolver: ResolverPlugin, mapping: any): any {
166-
return (request: any, callback: Callback<any>) => {
184+
return (request: Request, callback: Callback<any>) => {
167185
try {
168186
this.resolve(resolver, mapping, request, callback);
169187
} catch (err) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export default function() {
1313
],
1414
'@shared/*': [
1515
'app/shared/*'
16+
],
17+
'*': [
18+
'app/shared'
1619
]
1720
};
1821
})
@@ -25,12 +28,14 @@ export default function() {
2528
import { meaning } from 'app/shared/meaning';
2629
import { meaning as meaning2 } from '@shared';
2730
import { meaning as meaning3 } from '@shared/meaning';
31+
import { meaning as meaning4 } from 'meaning';
2832
2933
// need to use imports otherwise they are ignored and
3034
// no error is outputted, even if baseUrl/paths don't work
3135
console.log(meaning)
3236
console.log(meaning2)
3337
console.log(meaning3)
38+
console.log(meaning4)
3439
`))
3540
.then(() => ng('build'));
3641
}

0 commit comments

Comments
 (0)