-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
98 lines (87 loc) · 3.5 KB
/
index.js
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
var fs = require("fs");
var nodePath = require("path");
var babelTemplate = require("@babel/template").default;
module.exports = function(babel) {
var isPlatformImportInserted = false;
return {
name: "react-native-platform-specific-extensions",
visitor: {
Program() {
isPlatformImportInserted = false;
},
ImportDeclaration: function importResolver(path, state) {
var extensions =
state.opts != null &&
Array.isArray(state.opts.extensions) &&
state.opts.extensions;
if (!extensions) {
throw new Error(
"You have not specified any extensions in the plugin options."
);
}
var node = path.node;
var fileName = node.source.value;
var ext = nodePath.extname(fileName);
var matchedExtensions = extensions.filter(e => `.${e}` === ext);
var shouldMakePlatformSpecific = matchedExtensions.length === 1;
if (!shouldMakePlatformSpecific) {
return;
}
var specifier = node.specifiers[0];
var iosFileName = fileName.replace(ext, `.ios${ext}`);
var androidFileName = fileName.replace(ext, `.android${ext}`);
var nativeFileName = fileName.replace(ext, `.native${ext}`);
var transformedFileName = state.file.opts.filename;
var currentDir = nodePath.dirname(transformedFileName);
var iosFileExists = fs.existsSync(
nodePath.resolve(currentDir, iosFileName)
);
var androidFileExists = fs.existsSync(
nodePath.resolve(currentDir, androidFileName)
);
var nativeFileExists = fs.existsSync(
nodePath.resolve(currentDir, nativeFileName)
);
var ast = null;
function astTernary(platform, valueTrue, valueFalse) {
// Omit the var assignment when specifier is empty (global import case, executing for the side-effects only).
const assignee = specifier ? `var ${specifier.local.name} = ` : "";
const platformStr = `import { Platform } from "react-native";`;
const platformImport = !isPlatformImportInserted ? platformStr : "";
if (platformImport) {
isPlatformImportInserted = true;
}
return babelTemplate.ast(
platformImport +
assignee +
`Platform.OS === "${platform}" ? require("${valueTrue}") : require("${valueFalse}");`
);
}
if (iosFileExists && androidFileExists) {
ast = astTernary("ios", iosFileName, androidFileName);
path.replaceWithMultiple(ast);
return;
} else if (iosFileExists && !androidFileExists && nativeFileExists) {
ast = astTernary("ios", iosFileName, nativeFileName);
path.replaceWithMultiple(ast);
return;
} else if (!iosFileExists && androidFileExists && nativeFileExists) {
ast = astTernary("android", androidFileName, nativeFileName);
path.replaceWithMultiple(ast);
return;
} else if (iosFileExists && !androidFileExists && !nativeFileExists) {
ast = astTernary("ios", iosFileName, fileName);
path.replaceWithMultiple(ast);
return;
} else if (!iosFileExists && androidFileExists && !nativeFileExists) {
ast = astTernary("android", androidFileName, fileName);
path.replaceWithMultiple(ast);
return;
}
if (nativeFileExists) {
node.source.value = nativeFileName;
}
}
}
};
};