forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpaths-plugin.ts
206 lines (174 loc) · 5.92 KB
/
paths-plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as path from 'path';
import * as ts from 'typescript';
import {
Callback,
NormalModuleFactoryRequest,
} from './webpack';
export function resolveWithPaths(
request: NormalModuleFactoryRequest,
callback: Callback<NormalModuleFactoryRequest>,
compilerOptions: ts.CompilerOptions,
host: ts.CompilerHost,
cache?: ts.ModuleResolutionCache,
) {
if (!request || !request.request || !compilerOptions.paths) {
callback(null, request);
return;
}
// Only work on Javascript/TypeScript issuers.
if (!request.contextInfo.issuer || !request.contextInfo.issuer.match(/\.[jt]s$/)) {
callback(null, request);
return;
}
const originalRequest = request.request.trim();
// Relative requests are not mapped
if (originalRequest.startsWith('.') || originalRequest.startsWith('/')) {
callback(null, request);
return;
}
// Amd requests are not mapped
if (originalRequest.startsWith('!!webpack amd')) {
callback(null, request);
return;
}
// check if any path mapping rules are relevant
const pathMapOptions = [];
for (const pattern in compilerOptions.paths) {
// get potentials and remove duplicates; JS Set maintains insertion order
const potentials = Array.from(new Set(compilerOptions.paths[pattern]));
if (potentials.length === 0) {
// no potential replacements so skip
continue;
}
// can only contain zero or one
const starIndex = pattern.indexOf('*');
if (starIndex === -1) {
if (pattern === originalRequest) {
pathMapOptions.push({
starIndex,
partial: '',
potentials,
});
}
} else if (starIndex === 0 && pattern.length === 1) {
pathMapOptions.push({
starIndex,
partial: originalRequest,
potentials,
});
} else if (starIndex === pattern.length - 1) {
if (originalRequest.startsWith(pattern.slice(0, -1))) {
pathMapOptions.push({
starIndex,
partial: originalRequest.slice(pattern.length - 1),
potentials,
});
}
} else {
const [prefix, suffix] = pattern.split('*');
if (originalRequest.startsWith(prefix) && originalRequest.endsWith(suffix)) {
pathMapOptions.push({
starIndex,
partial: originalRequest.slice(prefix.length).slice(0, -suffix.length),
potentials,
});
}
}
}
if (pathMapOptions.length === 0) {
callback(null, request);
return;
}
// exact matches take priority then largest prefix match
pathMapOptions.sort((a, b) => {
if (a.starIndex === -1) {
return -1;
} else if (b.starIndex === -1) {
return 1;
} else {
return b.starIndex - a.starIndex;
}
});
if (pathMapOptions[0].potentials.length === 1) {
const onlyPotential = pathMapOptions[0].potentials[0];
let replacement;
const starIndex = onlyPotential.indexOf('*');
if (starIndex === -1) {
replacement = onlyPotential;
} else if (starIndex === onlyPotential.length - 1) {
replacement = onlyPotential.slice(0, -1) + pathMapOptions[0].partial;
} else {
const [prefix, suffix] = onlyPotential.split('*');
replacement = prefix + pathMapOptions[0].partial + suffix;
}
request.request = path.resolve(compilerOptions.baseUrl || '', replacement);
callback(null, request);
return;
}
// TODO: The following is used when there is more than one potential and will not be
// needed once this is turned into a full webpack resolver plugin
const moduleResolver = ts.resolveModuleName(
originalRequest,
request.contextInfo.issuer,
compilerOptions,
host,
cache,
);
const moduleFilePath = moduleResolver.resolvedModule
&& moduleResolver.resolvedModule.resolvedFileName;
// If there is no result, let webpack try to resolve
if (!moduleFilePath) {
callback(null, request);
return;
}
// If TypeScript gives us a `.d.ts`, it is probably a node module
if (moduleFilePath.endsWith('.d.ts')) {
const pathNoExtension = moduleFilePath.slice(0, -5);
const pathDirName = path.dirname(moduleFilePath);
const packageRootPath = path.join(pathDirName, 'package.json');
const jsFilePath = `${pathNoExtension}.js`;
if (host.fileExists(pathNoExtension)) {
// This is mainly for secondary entry points
// ex: 'node_modules/@angular/core/testing.d.ts' -> 'node_modules/@angular/core/testing'
request.request = pathNoExtension;
} else {
const packageJsonContent = host.readFile(packageRootPath);
let newRequest: string | undefined;
if (packageJsonContent) {
try {
const packageJson = JSON.parse(packageJsonContent);
// Let webpack resolve the correct module format IIF there is a module resolution field
// in the package.json. These are all official fields that Angular uses.
if (typeof packageJson.main == 'string'
|| typeof packageJson.browser == 'string'
|| typeof packageJson.module == 'string'
|| typeof packageJson.es2015 == 'string'
|| typeof packageJson.fesm5 == 'string'
|| typeof packageJson.fesm2015 == 'string') {
newRequest = pathDirName;
}
} catch {
// Ignore exceptions and let it fall through (ie. if package.json file is invalid).
}
}
if (newRequest === undefined && host.fileExists(jsFilePath)) {
// Otherwise, if there is a file with a .js extension use that
newRequest = jsFilePath;
}
if (newRequest !== undefined) {
request.request = newRequest;
}
}
callback(null, request);
return;
}
request.request = moduleFilePath;
callback(null, request);
}