forked from LeadingLight/eslint-import-resolver-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
88 lines (71 loc) · 2.42 KB
/
index.js
File metadata and controls
88 lines (71 loc) · 2.42 KB
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
const nodeResolve = require( 'eslint-import-resolver-node').resolve;
exports.interfaceVersion = 2;
exports.resolve = resolve;
// Setting
// platform: 'both' || 'ios' || 'android' || 'any'
// default = 'both'
var imageSuffixes = [ '.png', '.jpg', '.gif', '.jpeg' ];
var sizeSuffixes = ['@1x', '@1.5x', '@2x', '@3x', '@3.5x'];
function resolve(source, file, config) {
let resolve = nodeResolve(source, file, config);
if (resolve.found) return resolve;
if (isImage(source)) {
resolve = checkImages(source, file, config);
if (resolve.found) return resolve;
}
config = config || {};
const platform = config.platform || 'both';
if (platform === 'both') return both(source, file, config);
if (platform === 'any') return any(source, file, config);
// look for specific platform endings = '.' + platform
return specific(source, file, config, platform);
}
function isImage(source) {
var imageFound = false;
imageSuffixes.forEach(function(suffix) {
if (source.endsWith(suffix)) {
imageFound = true;
}
})
return imageFound;
}
function checkImages(source, file, config, platform=null) {
var splitSource = source.split('.');
var noSuffix = splitSource.slice(0, -1).join('.');
var suffix = '.' + splitSource.slice(-1);
var platformPart = (platform === null ? '' : `.${platform}`);
for(sizeSuffix of ['', ...sizeSuffixes]) {
const pathToTry = `${noSuffix}${sizeSuffix}${platformPart}${suffix}`;
const image = nodeResolve(pathToTry, file, config);
if(image.found) {
return image
}
}
return {found: false};
}
function resolvePlatform(source, file, config, platform) {
if (isImage(source)) {
return checkImages(source, file, config, platform);
}
else {
return nodeResolve(source + '.' + platform, file, config);
}
}
function both(source, file, config) {
const ios = resolvePlatform(source, file, config, 'ios');
if (!ios.found) return {found: false};
const android = resolvePlatform(source, file, config, 'android');
if (!android.found) return {found: false};
return ios;
}
function any(source, file, config) {
const ios = resolvePlatform(source, file, config, 'ios');
if (ios.found) return ios;
const android = resolvePlatform(source, file, config, 'android');
if (android.found) return android;
// no platform file found
return {found: false};
}
function specific(source, file, config, platform) {
return resolvePlatform(source, file, config, platform);
}