Skip to content

Commit 29aa274

Browse files
clydinBrocco
authored andcommitted
refactor(@ngtools/webpack): simplify paths module resolution
1 parent 33ff37a commit 29aa274

File tree

3 files changed

+25
-36
lines changed

3 files changed

+25
-36
lines changed

packages/@angular/cli/models/webpack-configs/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
203203
resolve: {
204204
extensions: ['.ts', '.js'],
205205
symlinks: !buildOptions.preserveSymlinks,
206+
modules: [appRoot, 'node_modules'],
206207
alias
207208
},
208209
resolveLoader: {

packages/@angular/cli/tasks/eject.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,9 @@ class JsonWebpackSerializer {
288288
private _resolveReplacer(value: any) {
289289
this.variableImports['rxjs/_esm5/path-mapping'] = 'rxPaths';
290290
return Object.assign({}, value, {
291-
alias: this._escape('rxPaths()')
291+
alias: this._escape('rxPaths()'),
292+
modules: value.modules
293+
&& value.modules.map((x: string) => './' + path.relative(this._root, x)),
292294
});
293295
}
294296

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

Lines changed: 21 additions & 35 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) {
23+
if (!request || !request.request) {
2424
callback(null, request);
2525
return;
2626
}
@@ -31,6 +31,26 @@ export function resolveWithPaths(
3131
return;
3232
}
3333

34+
// check if any path mapping rules are relevant
35+
const isPathMapped = compilerOptions.paths && Object.keys(compilerOptions.paths)
36+
.some(pattern => {
37+
// can only contain zero or one
38+
const starIndex = pattern.indexOf('*');
39+
if (starIndex === -1) {
40+
return pattern === request.request;
41+
} else if (starIndex === pattern.length - 1) {
42+
return request.request.startsWith(pattern.slice(0, -1));
43+
} else {
44+
const [prefix, suffix] = pattern.split('*');
45+
return request.request.startsWith(prefix) && request.request.endsWith(suffix);
46+
}
47+
});
48+
49+
if (!isPathMapped) {
50+
callback(null, request);
51+
return;
52+
}
53+
3454
const moduleResolver = ts.resolveModuleName(
3555
request.request,
3656
request.contextInfo.issuer,
@@ -64,40 +84,6 @@ export function resolveWithPaths(
6484
return;
6585
}
6686

67-
// TypeScript gives `index.ts` and the request is not for the specific file,
68-
// check if it is a module
69-
const requestFilePath = path.basename(request.request);
70-
if (path.basename(moduleFilePath) === 'index.ts'
71-
&& requestFilePath !== 'index' && requestFilePath !== 'index.ts') {
72-
const packageRootPath = path.join(path.dirname(moduleFilePath), 'package.json');
73-
if (host.fileExists(packageRootPath)) {
74-
// potential module request
75-
let isPathMapped = false;
76-
if (compilerOptions.paths) {
77-
// check if any path mapping rules are relevant
78-
isPathMapped = Object.keys(compilerOptions.paths)
79-
.some(pattern => {
80-
// can only contain zero or one
81-
const starIndex = pattern.indexOf('*');
82-
if (starIndex === -1) {
83-
return pattern === request.request;
84-
} else if (starIndex === pattern.length - 1) {
85-
return request.request.startsWith(pattern.slice(0, -1));
86-
} else {
87-
const [prefix, suffix] = pattern.split('*');
88-
return request.request.startsWith(prefix) && request.request.endsWith(suffix);
89-
}
90-
});
91-
}
92-
if (!isPathMapped) {
93-
// path mapping not involved, let webpack handle the module request
94-
request.request = path.dirname(moduleFilePath);
95-
callback(null, request);
96-
return;
97-
}
98-
}
99-
}
100-
10187
request.request = moduleFilePath;
10288
callback(null, request);
10389
}

0 commit comments

Comments
 (0)