-
Notifications
You must be signed in to change notification settings - Fork 1
/
eslint-plugin-import-resolver.cjs
204 lines (185 loc) · 8 KB
/
eslint-plugin-import-resolver.cjs
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
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
// <reference path="./node_modules/kolorist/dist/types/index.d.ts"/>
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
// <reference path="./node_modules/resolve.exports/index.d.ts"/>
// https://github.com/import-js/eslint-plugin-import/blob/main/resolvers/README.md
// https://github.com/import-js/eslint-plugin-import#resolvers
const fs = require('fs');
// @ts-expect-error TS7016
const { cyan, green, red } = require('kolorist');
// @ts-expect-error TS7016
const { resolve: resolveExports } = require('resolve.exports');
const { CachedInputFileSystem, ResolverFactory } = require('enhanced-resolve');
const { builtinModules } = require('module'); // createRequire
const path = require('path');
// create.sync
const enhancedResolver = ResolverFactory.createResolver({
fileSystem: new CachedInputFileSystem(fs, 4000),
extensions: ['.mjs', '.cjs', '.json', '.node', '.ts', '.tsx', '.js', '.jsx'],
useSyncFileSystemCalls: true,
// preferRelative: true,
// conditionNames: ['node'],
});
// https://github.com/browserify/resolve
// const { isCore } = require('resolve');
//, sync, SyncOpts
// https://github.com/import-js/eslint-plugin-import/blob/main/resolvers/node/index.js
// https://github.com/alexgorbatchev/eslint-import-resolver-typescript/blob/master/src/index.ts
// https://github.com/laysent/eslint-import-resolver-custom-alias/blob/master/index.js
// // https://gist.github.com/developit/4ba748d7d0196b780076b29e6f7a65c8
// /**
// * @param {any} exp `package.exports`
// * @param {string} entry `./foo` or `.`
// * @param {string[]} envKeys package environment keys
// * @returns {string | boolean} a resolved path, or a boolean indicating if the given entry is exposed
// */
// const resolveExportMap = (exp, entry, envKeys) => {
// console.log('resolveExportMap >>>> ', entry, exp);
// if (typeof exp === 'string') {
// // {"exports":"./foo.js"}
// // {"exports":{"./foo":"./foo.js"}}
// console.log('resolveExportMap >>>> string ', entry, exp);
// return exp;
// }
// let isFileListing;
// let isDirectoryExposed = false;
// for (let i in exp) {
// if (isFileListing === undefined) isFileListing = i[0] === '.';
// if (isFileListing) {
// // {"exports":{".":"./index.js"}}
// if (i === entry) {
// console.log('resolveExportMap >>>> isFileListing ', entry, exp);
// return resolveExportMap(/** @type {Object} */ exp[i], entry, envKeys);
// }
// if (!isDirectoryExposed && i.endsWith('/') && entry.startsWith(i)) {
// isDirectoryExposed = true;
// }
// } else if (envKeys.includes(i)) {
// // {"exports":{"import":"./foo.js"}}
// console.log('resolveExportMap >>>> envKeys ', entry, exp, i);
// return resolveExportMap(exp[i], entry, envKeys);
// }
// }
// console.log('resolveExportMap >>>> isDirectoryExposed ', entry, exp, isDirectoryExposed);
// return isDirectoryExposed;
// };
const builtins = new Set(builtinModules);
/**
* @param {string} source source
* @param {string} file file
* @param {Object} _config config
*/
const resolve = (source, file, _config) => {
// if (source.startsWith('@preact-wmr-twind-zero/')) {
// // const resolvedPath = await import.meta.resolve(source, `file://${file}`);
// // https://github.com/import-js/eslint-plugin-import/issues/1868
// // https://github.com/import-js/eslint-plugin-import/issues/1810
// // https://github.com/browserify/resolve/pull/224
// // https://github.com/wooorm/import-meta-resolve/issues/2
// // https://github.com/weiran-zsd/eslint-plugin-node/pull/4
// // https://github.com/mysticatea/eslint-plugin-node/issues/255
// // https://github.com/mysticatea/eslint-plugin-node/issues/244
// // https://github.com/mysticatea/eslint-plugin-node/issues/258
// // https://github.com/webpack/enhanced-resolve
// console.log(`${red('ESLINT PLUGIN IMPORT RESOLVER')} >> [${green(source)}] in [${cyan(file)}] (...)`);
// return { found: true, path: null };
// // return { found: false };
// }
// if (isCore(source)) {
// console.log(`${red('ESLINT PLUGIN IMPORT RESOLVER')} >> [${green(source)}] in [${cyan(file)}] (builtins)`);
// return { found: true, path: null };
// }
if (builtins.has(source)) {
console.log(
`${red('OK - ESLINT PLUGIN IMPORT RESOLVER (builtins)')} >> [${green(source)}] in [${cyan(file)}] (builtins 1)`,
);
// return { found: false };
return { found: true, path: null };
}
try {
const moduleId = require.resolve(source, { paths: [path.dirname(file)] });
// if (process.versions.pnp && moduleId.includes('__virtual__')) {
// moduleId = require('pnpapi').resolveVirtual(moduleId);
// }
// if (builtinModules.includes(moduleId)) {
// console.log(
// `${red('ESLINT PLUGIN IMPORT RESOLVER 2')} >> [${green(source)}] in [${cyan(file)}] ([${yellow(
// moduleId,
// )}]) (builtins 2)`,
// );
// // return { found: false };
// return { found: true, path: null };
// }
console.log(
`${red('OK - ESLINT PLUGIN IMPORT RESOLVER (require.resolve)')} >> [${green(source)}] in [${cyan(file)}] => [${green(
moduleId,
)}]`,
);
return { found: true, path: moduleId };
} catch (/** @type {any} */ errRequireResolve) {
try {
const result = enhancedResolver.resolveSync({}, path.dirname(file), source);
console.log(
`${red('OK - ESLINT PLUGIN IMPORT RESOLVER (enhanced-resolve)')} >> [${green(source)}] in [${cyan(file)}]]`,
);
return { found: true, path: result };
} catch (/** @type {any} */ _errEnhancedResolver) {
// console.log(
// `FAIL? - ESLINT PLUGIN IMPORT RESOLVER (enhanced-resolve) [${green(source)}] in [${cyan(file)}]`,
// errEnhancedResolver.code,
// errEnhancedResolver.path,
// errEnhancedResolver.message,
// );
// try {
// const req = createRequire(file); // import.meta.url
// const moduleId = req.resolve(source, { paths: [path.dirname(file)] });
// console.log(`${red('ESLINT PLUGIN IMPORT RESOLVER')} >> [${green(source)}] in [${cyan(file)}] => [${green(moduleId)}] (ESM)`);
// return { found: true, path: moduleId };
// } catch (err2) {
// console.log(`ESLINT PLUGIN IMPORT RESOLVER?!! [${green(source)}] in [${cyan(file)}]`, err1, err2);
// return { found: false };
// }
// err1.code => 'MODULE_NOT_FOUND'
// err1.path => '/path/to/node_modules/@preact-wmr-twind-zero/xxx/package.json'
// err1.message => Cannot find module '/path/to/node_modules/@preact-wmr-twind-zero/preact-things/dist/cjs/xx.js'
if (errRequireResolve.code === 'MODULE_NOT_FOUND' && errRequireResolve.path?.endsWith('/package.json')) {
// const str = 'node_modules/';
// const i = err1.path.lastIndexOf(str) + str.length;
// const prefix = err1.path.replace(/\/package\.json$/, '').substr(i);
// const relativeSource = source.replace(prefix, '.');
// const { exports } = require(err1.path);
// const resolved = resolveExportMap(exports, relativeSource, ['import']);
const { name, module, main, exports } = require(errRequireResolve.path);
const resolved = resolveExports({ name, module, main, exports }, source);
const ok = resolved?.startsWith('.');
const moduleId = ok
? path.join(path.dirname(errRequireResolve.path), resolved)
: errRequireResolve.message?.replace(
/Cannot find module '(.*)'/,
(/** @type {string} */ _match, /** @type {string} */ $1) => {
// assumes: err1.message.includes('/dist/cjs/')
return $1.replace(/\/dist\/cjs\//, '/dist/esm/');
},
);
console.log(
`${red('OK - ESLINT PLUGIN IMPORT RESOLVER (resolve.exports)')} >> [${green(source)}] in [${cyan(
file,
)}] => [${green(moduleId)}] (ESM${ok ? '' : ' (fallback)'})`,
);
// [${red(resolved)}] [${prefix}] [${relativeSource}]
return { found: true, path: moduleId };
}
console.log(
`FAIL! - ESLINT PLUGIN IMPORT RESOLVER [${green(source)}] in [${cyan(file)}]`,
errRequireResolve.code,
errRequireResolve.path,
errRequireResolve.message,
);
return { found: false };
}
}
};
module.exports = {
interfaceVersion: 2,
resolve,
};