-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (135 loc) · 5.3 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
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
const moduleMap = require('babel-plugin-react-native-web/src/moduleMap');
const isCommonJS = (opts) => opts.commonjs === true;
const electronModuleMap = {
Alert: true,
Platform: true,
Linking: true,
}
const isDefault = (importName) => !(importName && electronModuleMap[importName]);
const getDistLocation = (importName, opts) => {
const format = isCommonJS(opts) ? 'lib/' : 'es/';
const internalName = importName === 'unstable_createElement' ? 'createElement' : importName;
if (internalName === 'index') {
return `react-native-electron/${format}index`;
} else if (internalName && electronModuleMap[internalName]) {
return `react-native-electron/${format}apis/${internalName}`;
} else if (internalName && moduleMap[internalName]) {
return `react-native-web/dist/${isCommonJS(opts) ? 'cjs/' : ''}exports/${internalName}`;
}
};
const isReactNativeRequire = (t, node) => {
const { declarations } = node;
if (declarations.length > 1) {
return false;
}
const { id, init } = declarations[0];
return (
(t.isObjectPattern(id) || t.isIdentifier(id)) &&
t.isCallExpression(init) &&
t.isIdentifier(init.callee) &&
init.callee.name === 'require' &&
init.arguments.length === 1 &&
(init.arguments[0].value === 'react-native' || init.arguments[0].value === 'react-native-web' || init.arguments[0].value === 'react-native-electron')
);
};
const isReactNativeModule = ({ source, specifiers }) =>
source &&
(source.value === 'react-native' || source.value === 'react-native-web' || source.value === 'react-native-electron') &&
specifiers.length;
module.exports = function ({ types: t }) {
return {
name: 'Rewrite react-native to react-native-electron',
visitor: {
ImportDeclaration(path, state) {
const { specifiers } = path.node;
if (isReactNativeModule(path.node)) {
const imports = specifiers
.map((specifier) => {
if (t.isImportSpecifier(specifier)) {
const importName = specifier.imported.name;
const distLocation = getDistLocation(importName, state.opts);
if (distLocation) {
return t.importDeclaration(
isDefault(importName)
? [t.importDefaultSpecifier(t.identifier(specifier.local.name))]
: [t.importNamespaceSpecifier(t.identifier(specifier.local.name))],
t.stringLiteral(distLocation)
);
}
}
return t.importDeclaration(
[specifier],
t.stringLiteral(getDistLocation('index', state.opts))
);
})
.filter(Boolean);
path.replaceWithMultiple(imports);
}
},
ExportNamedDeclaration(path, state) {
const { specifiers } = path.node;
if (isReactNativeModule(path.node)) {
const exports = specifiers
.map((specifier) => {
if (t.isExportSpecifier(specifier)) {
const exportName = specifier.exported.name;
const localName = specifier.local.name;
const distLocation = getDistLocation(localName, state.opts);
if (distLocation) {
return t.exportNamedDeclaration(
null,
[t.exportSpecifier(t.identifier('default'), t.identifier(exportName))],
t.stringLiteral(distLocation)
);
}
}
return t.exportNamedDeclaration(
null,
[specifier],
t.stringLiteral(getDistLocation('index', state.opts))
);
})
.filter(Boolean);
path.replaceWithMultiple(exports);
}
},
VariableDeclaration(path, state) {
if (isReactNativeRequire(t, path.node)) {
const { id } = path.node.declarations[0];
if (t.isObjectPattern(id)) {
const imports = id.properties
.map((identifier) => {
const distLocation = getDistLocation(identifier.key.name, state.opts);
if (distLocation) {
return t.variableDeclaration(path.node.kind, [
t.variableDeclarator(
t.identifier(identifier.value.name),
isDefault(localName)
? t.memberExpression(
t.callExpression(t.identifier('require'), [t.stringLiteral(distLocation)]),
t.identifier('default')
)
: t.callExpression(t.identifier('require'), [t.stringLiteral(distLocation)])
)
]);
}
})
.filter(Boolean);
path.replaceWithMultiple(imports);
} else if (t.isIdentifier(id)) {
const name = id.name;
const importIndex = t.variableDeclaration(path.node.kind, [
t.variableDeclarator(
t.identifier(name),
t.callExpression(t.identifier('require'), [
t.stringLiteral(getDistLocation('index', state.opts))
])
)
]);
path.replaceWith(importIndex);
}
}
}
}
};
};