Skip to content

Commit ff9e114

Browse files
committed
fix(webpack): paths plugin wildcard regex fix
1 parent 87b93e1 commit ff9e114

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

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

+28-10
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

+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+
'../node_modules/*',
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)