Skip to content

Commit d5a9895

Browse files
clydinhansl
authored andcommitted
fix(@ngtools/webpack): directly use single path mappings
1 parent 5b66371 commit d5a9895

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
@@ -20,7 +20,7 @@ export function resolveWithPaths(
2020
host: ts.CompilerHost,
2121
cache?: ts.ModuleResolutionCache,
2222
) {
23-
if (!request || !request.request) {
23+
if (!request || !request.request || !compilerOptions.paths) {
2424
callback(null, request);
2525
return;
2626
}
@@ -32,21 +32,54 @@ export function resolveWithPaths(
3232
}
3333

3434
// check if any path mapping rules are relevant
35-
const isPathMapped = compilerOptions.paths && Object.keys(compilerOptions.paths)
36-
.some(pattern => {
35+
const pathMapOptions = [];
36+
for (const pattern in compilerOptions.paths) {
3737
// can only contain zero or one
3838
const starIndex = pattern.indexOf('*');
3939
if (starIndex === -1) {
40-
return pattern === request.request;
40+
if (pattern === request.request) {
41+
pathMapOptions.push({
42+
partial: '',
43+
potentials: compilerOptions.paths[pattern]
44+
});
45+
}
4146
} else if (starIndex === pattern.length - 1) {
42-
return request.request.startsWith(pattern.slice(0, -1));
47+
if (request.request.startsWith(pattern.slice(0, -1))) {
48+
pathMapOptions.push({
49+
partial: request.request.slice(pattern.length - 1),
50+
potentials: compilerOptions.paths[pattern]
51+
});
52+
}
4353
} else {
4454
const [prefix, suffix] = pattern.split('*');
45-
return request.request.startsWith(prefix) && request.request.endsWith(suffix);
55+
if (request.request.startsWith(prefix) && request.request.endsWith(suffix)) {
56+
pathMapOptions.push({
57+
partial: request.request.slice(prefix.length).slice(0, -suffix.length),
58+
potentials: compilerOptions.paths[pattern]
59+
});
60+
}
4661
}
47-
});
62+
}
63+
64+
if (pathMapOptions.length === 0) {
65+
callback(null, request);
66+
return;
67+
}
68+
69+
if (pathMapOptions.length === 1 && pathMapOptions[0].potentials.length === 1) {
70+
const onlyPotential = pathMapOptions[0].potentials[0];
71+
let replacement;
72+
const starIndex = onlyPotential.indexOf('*');
73+
if (starIndex === -1) {
74+
replacement = onlyPotential;
75+
} else if (starIndex === onlyPotential.length - 1) {
76+
replacement = onlyPotential.slice(0, -1) + pathMapOptions[0].partial;
77+
} else {
78+
const [prefix, suffix] = onlyPotential.split('*');
79+
replacement = prefix + pathMapOptions[0].partial + suffix;
80+
}
4881

49-
if (!isPathMapped) {
82+
request.request = path.resolve(compilerOptions.baseUrl, replacement);
5083
callback(null, request);
5184
return;
5285
}

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)