Skip to content

Commit 595168e

Browse files
clydinhansl
authored andcommitted
fix(@ngtools/webpack): directly use single path mappings
1 parent a4f9c08 commit 595168e

File tree

2 files changed

+61
-13
lines changed

2 files changed

+61
-13
lines changed

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

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function resolveWithPaths(
1414
host: ts.CompilerHost,
1515
cache?: ts.ModuleResolutionCache,
1616
) {
17-
if (!request || !request.request) {
17+
if (!request || !request.request || !compilerOptions.paths) {
1818
callback(null, request);
1919
return;
2020
}
@@ -26,21 +26,54 @@ export function resolveWithPaths(
2626
}
2727

2828
// check if any path mapping rules are relevant
29-
const isPathMapped = compilerOptions.paths && Object.keys(compilerOptions.paths)
30-
.some(pattern => {
29+
const pathMapOptions = [];
30+
for (const pattern in compilerOptions.paths) {
3131
// can only contain zero or one
3232
const starIndex = pattern.indexOf('*');
3333
if (starIndex === -1) {
34-
return pattern === request.request;
34+
if (pattern === request.request) {
35+
pathMapOptions.push({
36+
partial: '',
37+
potentials: compilerOptions.paths[pattern]
38+
});
39+
}
3540
} else if (starIndex === pattern.length - 1) {
36-
return request.request.startsWith(pattern.slice(0, -1));
41+
if (request.request.startsWith(pattern.slice(0, -1))) {
42+
pathMapOptions.push({
43+
partial: request.request.slice(pattern.length - 1),
44+
potentials: compilerOptions.paths[pattern]
45+
});
46+
}
3747
} else {
3848
const [prefix, suffix] = pattern.split('*');
39-
return request.request.startsWith(prefix) && request.request.endsWith(suffix);
49+
if (request.request.startsWith(prefix) && request.request.endsWith(suffix)) {
50+
pathMapOptions.push({
51+
partial: request.request.slice(prefix.length).slice(0, -suffix.length),
52+
potentials: compilerOptions.paths[pattern]
53+
});
54+
}
4055
}
41-
});
56+
}
57+
58+
if (pathMapOptions.length === 0) {
59+
callback(null, request);
60+
return;
61+
}
62+
63+
if (pathMapOptions.length === 1 && pathMapOptions[0].potentials.length === 1) {
64+
const onlyPotential = pathMapOptions[0].potentials[0];
65+
let replacement;
66+
const starIndex = onlyPotential.indexOf('*');
67+
if (starIndex === -1) {
68+
replacement = onlyPotential;
69+
} else if (starIndex === onlyPotential.length - 1) {
70+
replacement = onlyPotential.slice(0, -1) + pathMapOptions[0].partial;
71+
} else {
72+
const [prefix, suffix] = onlyPotential.split('*');
73+
replacement = prefix + pathMapOptions[0].partial + suffix;
74+
}
4275

43-
if (!isPathMapped) {
76+
request.request = path.resolve(compilerOptions.baseUrl, replacement);
4477
callback(null, request);
4578
return;
4679
}

tests/e2e/tests/misc/module-resolution.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
1-
import { appendToFile, prependToFile } from '../../utils/fs';
1+
import { appendToFile, createDir, moveFile, prependToFile } from '../../utils/fs';
22
import { ng, silentNpm } from '../../utils/process';
33
import { updateJsonFile } from '../../utils/project';
44
import { expectToFail } from '../../utils/utils';
55

66

77
export default async function () {
8+
await createDir('xyz');
9+
await moveFile(
10+
'node_modules/@angular/common',
11+
'xyz/common'
12+
);
13+
14+
await expectToFail(() => ng('build'));
15+
16+
await updateJsonFile('src/tsconfig.app.json', tsconfig => {
17+
tsconfig.compilerOptions.paths = {
18+
'@angular/common': [ '../xyz/common' ],
19+
};
20+
});
21+
await ng('build');
22+
23+
await updateJsonFile('src/tsconfig.app.json', tsconfig => {
24+
delete tsconfig.compilerOptions.paths;
25+
});
26+
827
await prependToFile('src/app/app.module.ts', 'import * as firebase from \'firebase\';');
928
await appendToFile('src/app/app.module.ts', 'firebase.initializeApp({});');
1029

@@ -16,10 +35,6 @@ export default async function () {
1635
await ng('build', '--aot');
1736
await ng('test', '--single-run');
1837

19-
// await prependToFile('src/app/app.module.ts', 'import * as firebase from \'firebase\';');
20-
// await appendToFile('src/app/app.module.ts', 'firebase.initializeApp({});');
21-
// await ng('build');
22-
2338
await updateJsonFile('src/tsconfig.app.json', tsconfig => {
2439
tsconfig.compilerOptions.paths = {};
2540
});

0 commit comments

Comments
 (0)