-
Notifications
You must be signed in to change notification settings - Fork 597
/
modern-module-resolution.ts
249 lines (229 loc) · 8.91 KB
/
modern-module-resolution.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
// This is a workaround for https://github.com/eslint/eslint/issues/3458
//
// To correct how ESLint searches for plugin packages, add this line to the top of your project's .eslintrc.js file:
//
// require("@rushstack/eslint-patch/modern-module-resolution");
//
const path = require('path');
const fs = require('fs');
const isModuleResolutionError: (ex: unknown) => boolean = (ex) =>
typeof ex === 'object' && !!ex && 'code' in ex && (ex as { code: unknown }).code === 'MODULE_NOT_FOUND';
// Module path for eslintrc.cjs
// Example: ".../@eslint/eslintrc/dist/eslintrc.cjs"
let eslintrcBundlePath: string | undefined = undefined;
// Module path for config-array-factory.js
// Example: ".../@eslint/eslintrc/lib/config-array-factory"
let configArrayFactoryPath: string | undefined = undefined;
// Module path for relative-module-resolver.js
// Example: ".../@eslint/eslintrc/lib/shared/relative-module-resolver"
let moduleResolverPath: string | undefined = undefined;
// Folder path where ESLint's package.json can be found
// Example: ".../node_modules/eslint"
let eslintFolder: string | undefined = undefined;
// Probe for the ESLint >=8.0.0 layout:
for (let currentModule = module; ; ) {
if (!eslintrcBundlePath) {
// For ESLint >=8.0.0, all @eslint/eslintrc code is bundled at this path:
// .../@eslint/eslintrc/dist/eslintrc.cjs
try {
const eslintrcFolder = path.dirname(
require.resolve('@eslint/eslintrc/package.json', { paths: [currentModule.path] })
);
// Make sure we actually resolved the module in our call path
// and not some other spurious dependency.
if (path.join(eslintrcFolder, 'dist/eslintrc.cjs') === currentModule.filename) {
eslintrcBundlePath = path.join(eslintrcFolder, 'dist/eslintrc.cjs');
}
} catch (ex: unknown) {
// Module resolution failures are expected, as we're walking
// up our require stack to look for eslint. All other errors
// are rethrown.
if (!isModuleResolutionError(ex)) {
throw ex;
}
}
} else {
// Next look for a file in ESLint's folder
// .../eslint/lib/cli-engine/cli-engine.js
try {
const eslintCandidateFolder = path.dirname(
require.resolve('eslint/package.json', {
paths: [currentModule.path]
})
);
// Make sure we actually resolved the module in our call path
// and not some other spurious dependency.
if (path.join(eslintCandidateFolder, 'lib/cli-engine/cli-engine.js') === currentModule.filename) {
eslintFolder = eslintCandidateFolder;
break;
}
} catch (ex: unknown) {
// Module resolution failures are expected, as we're walking
// up our require stack to look for eslint. All other errors
// are rethrown.
if (!isModuleResolutionError(ex)) {
throw ex;
}
}
}
if (!currentModule.parent) {
break;
}
currentModule = currentModule.parent;
}
if (!eslintFolder) {
// Probe for the ESLint >=7.8.0 layout:
for (let currentModule = module; ; ) {
if (!configArrayFactoryPath) {
// For ESLint >=7.8.0, config-array-factory.js is at this path:
// .../@eslint/eslintrc/lib/config-array-factory.js
try {
const eslintrcFolder = path.dirname(
require.resolve('@eslint/eslintrc/package.json', {
paths: [currentModule.path]
})
);
if (path.join(eslintrcFolder, '/lib/config-array-factory.js') == currentModule.filename) {
configArrayFactoryPath = path.join(eslintrcFolder, 'lib/config-array-factory.js');
moduleResolverPath = path.join(eslintrcFolder, 'lib/shared/relative-module-resolver');
}
} catch (ex: unknown) {
// Module resolution failures are expected, as we're walking
// up our require stack to look for eslint. All other errors
// are rethrown.
if (!isModuleResolutionError(ex)) {
throw ex;
}
}
} else {
// Next look for a file in ESLint's folder
// .../eslint/lib/cli-engine/cli-engine.js
try {
const eslintCandidateFolder = path.dirname(
require.resolve('eslint/package.json', {
paths: [currentModule.path]
})
);
if (path.join(eslintCandidateFolder, 'lib/cli-engine/cli-engine.js') == currentModule.filename) {
eslintFolder = eslintCandidateFolder;
break;
}
} catch (ex: unknown) {
// Module resolution failures are expected, as we're walking
// up our require stack to look for eslint. All other errors
// are rethrown.
if (!isModuleResolutionError(ex)) {
throw ex;
}
}
}
if (!currentModule.parent) {
break;
}
currentModule = currentModule.parent;
}
}
if (!eslintFolder) {
// Probe for the <7.8.0 layout:
for (let currentModule = module; ; ) {
// For ESLint <7.8.0, config-array-factory.js was at this path:
// .../eslint/lib/cli-engine/config-array-factory.js
if (/[\\/]eslint[\\/]lib[\\/]cli-engine[\\/]config-array-factory\.js$/i.test(currentModule.filename)) {
eslintFolder = path.join(path.dirname(currentModule.filename), '../..');
configArrayFactoryPath = path.join(eslintFolder, 'lib/cli-engine/config-array-factory');
moduleResolverPath = path.join(eslintFolder, 'lib/shared/relative-module-resolver');
break;
}
if (!currentModule.parent) {
// This was tested with ESLint 6.1.0 .. 7.12.1.
throw new Error(
'Failed to patch ESLint because the calling module was not recognized.\n' +
'If you are using a newer ESLint version that may be unsupported, please create a GitHub issue:\n' +
'https://github.com/microsoft/rushstack/issues'
);
}
currentModule = currentModule.parent;
}
}
// Detect the ESLint package version
const eslintPackageJson = fs.readFileSync(path.join(eslintFolder, 'package.json')).toString();
const eslintPackageObject = JSON.parse(eslintPackageJson);
const eslintPackageVersion = eslintPackageObject.version;
const versionMatch = /^([0-9]+)\./.exec(eslintPackageVersion); // parse the SemVer MAJOR part
if (!versionMatch) {
throw new Error('Unable to parse ESLint version: ' + eslintPackageVersion);
}
const eslintMajorVersion = Number(versionMatch[1]);
if (!(eslintMajorVersion >= 6 && eslintMajorVersion <= 8)) {
throw new Error(
'The patch-eslint.js script has only been tested with ESLint version 6.x, 7.x, and 8.x.' +
` (Your version: ${eslintPackageVersion})\n` +
'Consider reporting a GitHub issue:\n' +
'https://github.com/microsoft/rushstack/issues'
);
}
let ConfigArrayFactory;
if (eslintMajorVersion === 8) {
ConfigArrayFactory = require(eslintrcBundlePath!).Legacy.ConfigArrayFactory;
} else {
ConfigArrayFactory = require(configArrayFactoryPath!).ConfigArrayFactory;
}
if (!ConfigArrayFactory.__patched) {
ConfigArrayFactory.__patched = true;
let ModuleResolver: { resolve: any };
if (eslintMajorVersion === 8) {
ModuleResolver = require(eslintrcBundlePath!).Legacy.ModuleResolver;
} else {
ModuleResolver = require(moduleResolverPath!);
}
const originalLoadPlugin = ConfigArrayFactory.prototype._loadPlugin;
if (eslintMajorVersion === 6) {
// ESLint 6.x
ConfigArrayFactory.prototype._loadPlugin = function (
name: string,
importerPath: string,
importerName: string
) {
const originalResolve = ModuleResolver.resolve;
try {
ModuleResolver.resolve = function (moduleName: string, relativeToPath: string) {
try {
// resolve using importerPath instead of relativeToPath
return originalResolve.call(this, moduleName, importerPath);
} catch (e) {
if (isModuleResolutionError(e)) {
return originalResolve.call(this, moduleName, relativeToPath);
}
throw e;
}
};
return originalLoadPlugin.apply(this, arguments);
} finally {
ModuleResolver.resolve = originalResolve;
}
};
} else {
// ESLint 7.x || 8.x
ConfigArrayFactory.prototype._loadPlugin = function (name: string, ctx: Record<string, unknown>) {
const originalResolve = ModuleResolver.resolve;
try {
ModuleResolver.resolve = function (moduleName: string, relativeToPath: string) {
try {
// resolve using ctx.filePath instead of relativeToPath
return originalResolve.call(this, moduleName, ctx.filePath);
} catch (e) {
if (isModuleResolutionError(e)) {
return originalResolve.call(this, moduleName, relativeToPath);
}
throw e;
}
};
return originalLoadPlugin.apply(this, arguments);
} finally {
ModuleResolver.resolve = originalResolve;
}
};
}
}