babel-plugin-react-native-direct-imports 1.1.2 → 1.2.1-rc.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +102 -4
- package/index.js +398 -49
- package/maps.json +1 -0
- package/package.json +8 -5
- package/resolve-package.js +455 -0
- package/CHANGELOG.md +0 -13
- package/maps/0.70.0.json +0 -330
- package/maps/0.70.1.json +0 -330
- package/maps/0.70.10.json +0 -330
- package/maps/0.70.11.json +0 -330
- package/maps/0.70.12.json +0 -330
- package/maps/0.70.13.json +0 -330
- package/maps/0.70.14.json +0 -330
- package/maps/0.70.15.json +0 -330
- package/maps/0.70.2.json +0 -330
- package/maps/0.70.3.json +0 -330
- package/maps/0.70.4.json +0 -330
- package/maps/0.70.5.json +0 -330
- package/maps/0.70.6.json +0 -330
- package/maps/0.70.7.json +0 -330
- package/maps/0.70.8.json +0 -330
- package/maps/0.70.9.json +0 -330
- package/maps/0.71.0.json +0 -338
- package/maps/0.71.1.json +0 -338
- package/maps/0.71.10.json +0 -338
- package/maps/0.71.11.json +0 -338
- package/maps/0.71.12.json +0 -338
- package/maps/0.71.13.json +0 -338
- package/maps/0.71.14.json +0 -338
- package/maps/0.71.15.json +0 -338
- package/maps/0.71.16.json +0 -338
- package/maps/0.71.17.json +0 -338
- package/maps/0.71.18.json +0 -338
- package/maps/0.71.19.json +0 -338
- package/maps/0.71.2.json +0 -338
- package/maps/0.71.3.json +0 -338
- package/maps/0.71.4.json +0 -338
- package/maps/0.71.5.json +0 -338
- package/maps/0.71.6.json +0 -338
- package/maps/0.71.7.json +0 -338
- package/maps/0.71.8.json +0 -338
- package/maps/0.71.9.json +0 -338
- package/maps/0.72.0.json +0 -326
- package/maps/0.72.1.json +0 -326
- package/maps/0.72.10.json +0 -326
- package/maps/0.72.11.json +0 -326
- package/maps/0.72.12.json +0 -326
- package/maps/0.72.13.json +0 -326
- package/maps/0.72.14.json +0 -326
- package/maps/0.72.15.json +0 -326
- package/maps/0.72.17.json +0 -326
- package/maps/0.72.2.json +0 -326
- package/maps/0.72.3.json +0 -326
- package/maps/0.72.4.json +0 -326
- package/maps/0.72.5.json +0 -326
- package/maps/0.72.6.json +0 -326
- package/maps/0.72.7.json +0 -326
- package/maps/0.72.8.json +0 -326
- package/maps/0.72.9.json +0 -326
- package/maps/0.73.0.json +0 -326
- package/maps/0.73.1.json +0 -326
- package/maps/0.73.10.json +0 -326
- package/maps/0.73.11.json +0 -326
- package/maps/0.73.2.json +0 -326
- package/maps/0.73.3.json +0 -326
- package/maps/0.73.4.json +0 -326
- package/maps/0.73.5.json +0 -326
- package/maps/0.73.6.json +0 -326
- package/maps/0.73.7.json +0 -326
- package/maps/0.73.8.json +0 -326
- package/maps/0.73.9.json +0 -326
- package/maps/0.74.0.json +0 -314
- package/maps/0.74.1.json +0 -314
- package/maps/0.74.2.json +0 -314
- package/maps/0.74.3.json +0 -314
- package/maps/0.74.4.json +0 -314
- package/maps/0.74.5.json +0 -314
- package/maps/0.74.6.json +0 -314
- package/maps/0.74.7.json +0 -314
- package/maps/0.75.0.json +0 -314
- package/maps/0.75.1.json +0 -314
- package/maps/0.75.2.json +0 -314
- package/maps/0.75.3.json +0 -314
- package/maps/0.75.4.json +0 -314
- package/maps/0.75.5.json +0 -314
- package/maps/0.76.0.json +0 -314
- package/maps/0.76.1.json +0 -314
- package/maps/0.76.2.json +0 -314
- package/maps/0.76.3.json +0 -314
- package/maps/0.76.4.json +0 -314
- package/maps/0.76.5.json +0 -314
- package/maps/0.76.6.json +0 -314
- package/maps/0.76.7.json +0 -314
- package/maps/0.76.8.json +0 -314
- package/maps/0.76.9.json +0 -314
- package/maps/0.77.0.json +0 -318
- package/maps/0.77.1.json +0 -318
- package/maps/0.77.2.json +0 -318
- package/maps/0.77.3.json +0 -318
- package/maps/0.78.0.json +0 -322
- package/maps/0.78.1.json +0 -322
- package/maps/0.78.2.json +0 -322
- package/maps/0.78.3.json +0 -322
- package/maps/0.79.0.json +0 -318
- package/maps/0.79.1.json +0 -318
- package/maps/0.79.2.json +0 -318
- package/maps/0.79.3.json +0 -318
- package/maps/0.79.4.json +0 -318
- package/maps/0.79.5.json +0 -318
- package/maps/0.79.6.json +0 -318
- package/maps/0.79.7.json +0 -318
- package/maps/0.80.0.json +0 -326
- package/maps/0.80.1.json +0 -326
- package/maps/0.80.2.json +0 -326
- package/maps/0.80.3.json +0 -326
- package/maps/0.81.0.json +0 -338
- package/maps/0.81.1.json +0 -338
- package/maps/0.81.2.json +0 -338
- package/maps/0.81.3.json +0 -338
- package/maps/0.81.4.json +0 -338
- package/maps/0.81.5.json +0 -338
- package/maps/0.81.6.json +0 -338
- package/maps/0.82.0.json +0 -346
- package/maps/0.82.1.json +0 -346
- package/maps/0.83.0.json +0 -354
- package/maps/0.83.1.json +0 -354
- package/maps/0.83.10.json +0 -354
- package/maps/0.83.2.json +0 -354
- package/maps/0.83.3.json +0 -354
- package/maps/0.83.4.json +0 -354
- package/maps/0.83.5.json +0 -354
- package/maps/0.83.6.json +0 -354
- package/maps/0.83.7.json +0 -354
- package/maps/0.83.8.json +0 -354
- package/maps/0.83.9.json +0 -354
- package/maps/0.84.0.json +0 -358
- package/maps/0.84.1.json +0 -358
- package/maps/0.85.0.json +0 -370
- package/maps/0.85.1.json +0 -370
- package/maps/0.85.2.json +0 -370
- package/maps/0.85.3.json +0 -370
- package/maps/0.86.0.json +0 -394
- package/maps/0.86.2.json +0 -394
- package/maps/0.86.3.json +0 -394
- package/maps/0.87.0.json +0 -390
- package/maps/0.87.1.json +0 -390
- package/maps/0.88.0.json +0 -390
- package/maps/0.88.1.json +0 -390
package/index.js
CHANGED
|
@@ -37,19 +37,41 @@
|
|
|
37
37
|
*
|
|
38
38
|
* Unknown names and type-only imports/exports are left untouched.
|
|
39
39
|
*
|
|
40
|
+
* Other packages can be opted in with the `packages` option. Packages with a
|
|
41
|
+
* shipped map (`react-native-svg`) use the map of the installed version; any
|
|
42
|
+
* other package is resolved by following the ES re-exports of its installed
|
|
43
|
+
* entry file (see `resolve-package.js`). They stay `import` declarations:
|
|
44
|
+
*
|
|
45
|
+
* import Svg, { Defs, Path } from 'react-native-svg';
|
|
46
|
+
* // ->
|
|
47
|
+
* import Svg from 'react-native-svg/src/elements/Svg';
|
|
48
|
+
* import Defs from 'react-native-svg/src/elements/Defs';
|
|
49
|
+
* import Path from 'react-native-svg/src/elements/Path';
|
|
50
|
+
*
|
|
51
|
+
* `export { Defs } from 'pkg'` re-exports are rewritten the same way, and so is
|
|
52
|
+
* `require('pkg')`, all or nothing: when every usage of
|
|
53
|
+
* `const RNSvg = require('pkg')` is a known `RNSvg.Name` they all become inline
|
|
54
|
+
* `require()`s, but `fn(RNSvg)`, `RNSvg.Unknown`, `RNSvg.Name = ...`, ... keep
|
|
55
|
+
* the whole `require('pkg')` as it is, since it would be loaded anyway.
|
|
56
|
+
*
|
|
40
57
|
* Options:
|
|
41
|
-
* reactNativeVersion -
|
|
58
|
+
* reactNativeVersion - which react-native version to generate imports for
|
|
59
|
+
* (defaults to the latest known one). An unknown version falls back to the
|
|
60
|
+
* closest older map, so `0.88.2` and `0.89.0` both use the `0.88.1` map.
|
|
61
|
+
* packages - names of other packages (besides `react-native`) to rewrite
|
|
62
|
+
* imports of, e.g. `['react-native-svg']`
|
|
63
|
+
* onModuleReexport - what to do on `module.exports = require('pkg')`:
|
|
64
|
+
* 'error' (default) throws, 'warn' logs and keeps the code, 'silent' keeps it
|
|
42
65
|
* silenceBatchedUpdatesWarning - hides the `unstable_batchedUpdates` no-op
|
|
43
66
|
* warning on 0.86+
|
|
44
67
|
*/
|
|
45
68
|
|
|
46
69
|
const fs = require('fs');
|
|
47
70
|
const nodePath = require('path');
|
|
71
|
+
const { createPackageResolver, findPackageRoot } = require('./resolve-package');
|
|
48
72
|
|
|
49
73
|
const RN = 'react-native';
|
|
50
74
|
|
|
51
|
-
const MAPS_DIR = nodePath.join(__dirname, 'maps');
|
|
52
|
-
|
|
53
75
|
let warnedBatchedUpdates = false;
|
|
54
76
|
|
|
55
77
|
const compareVersions = (a, b) => {
|
|
@@ -62,31 +84,95 @@ const compareVersions = (a, b) => {
|
|
|
62
84
|
return 0;
|
|
63
85
|
};
|
|
64
86
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
87
|
+
// `maps.json` is generated by `scripts/build-maps.js` from
|
|
88
|
+
// `maps/<package>/*.json`: one block per package (Format B: one entry per
|
|
89
|
+
// export name, with version ranges and interned values). See that script for
|
|
90
|
+
// the exact format.
|
|
91
|
+
const loadData = () => require(nodePath.join(__dirname, 'maps.json'));
|
|
92
|
+
|
|
93
|
+
// 'Libraries/Image/Image|default' -> ['Libraries/Image/Image', 'default']
|
|
94
|
+
const splitValue = (value) => {
|
|
95
|
+
const separator = value.indexOf('|');
|
|
96
|
+
return [value.slice(0, separator), value.slice(separator + 1)];
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// Windows are sorted by version, so the first one containing `version` wins.
|
|
100
|
+
const pickWindow = (windows, version) => {
|
|
101
|
+
for (const [from, to, valueIndex] of windows) {
|
|
102
|
+
if (compareVersions(version, from) < 0) break;
|
|
103
|
+
if (compareVersions(version, to) <= 0) return valueIndex;
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const resolvedMaps = new Map();
|
|
109
|
+
|
|
110
|
+
// `versions` is ascending, so the last one that doesn't post-date `requested`
|
|
111
|
+
// is the closest match: `0.88.2` and `0.89.0` both pick the `0.88.1` map.
|
|
112
|
+
// Returns `null` when the request predates every known map.
|
|
113
|
+
const findMapVersion = (versions, requested) => {
|
|
114
|
+
let found = null;
|
|
115
|
+
for (const version of versions) {
|
|
116
|
+
if (compareVersions(version, requested) > 0) break;
|
|
117
|
+
found = version;
|
|
118
|
+
}
|
|
119
|
+
return found;
|
|
120
|
+
};
|
|
71
121
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
122
|
+
// Packages with a shipped map (besides `react-native`).
|
|
123
|
+
const hasMap = (packageName) =>
|
|
124
|
+
Object.prototype.hasOwnProperty.call(loadData(), packageName);
|
|
75
125
|
|
|
76
|
-
|
|
126
|
+
// The map of `packageName` for `version` (default: the latest known one):
|
|
127
|
+
// `{ [exportName]: [pathInPackage, exportNameInModule] }`.
|
|
128
|
+
const loadPackageMap = (packageName, version) => {
|
|
129
|
+
const data = loadData()[packageName];
|
|
130
|
+
const selected =
|
|
131
|
+
version == null ? data.latest : findMapVersion(data.versions, version);
|
|
132
|
+
|
|
133
|
+
if (selected === null) {
|
|
77
134
|
throw new Error(
|
|
78
|
-
`[react-native-direct-imports] No map for
|
|
135
|
+
`[react-native-direct-imports] No map for ${packageName} ${version}. Available versions: ${data.versions.join(', ')}`,
|
|
79
136
|
);
|
|
80
137
|
}
|
|
81
138
|
|
|
82
|
-
|
|
139
|
+
const cacheKey = `${packageName}@${selected}`;
|
|
140
|
+
const cached = resolvedMaps.get(cacheKey);
|
|
141
|
+
if (cached) return cached;
|
|
142
|
+
|
|
143
|
+
const map = {};
|
|
144
|
+
for (const [name, entry] of Object.entries(data.exports)) {
|
|
145
|
+
const valueIndex =
|
|
146
|
+
typeof entry === 'number' ? entry : pickWindow(entry, selected);
|
|
147
|
+
// `null` means the name doesn't exist on this version.
|
|
148
|
+
if (valueIndex === null) continue;
|
|
149
|
+
map[name] = splitValue(data.values[valueIndex]);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
resolvedMaps.set(cacheKey, map);
|
|
153
|
+
return map;
|
|
83
154
|
};
|
|
84
155
|
|
|
156
|
+
const loadMap = (version) => loadPackageMap(RN, version);
|
|
157
|
+
|
|
85
158
|
module.exports = function reactNativeDirectImports(
|
|
86
|
-
|
|
87
|
-
{
|
|
159
|
+
api,
|
|
160
|
+
{
|
|
161
|
+
reactNativeVersion,
|
|
162
|
+
packages = [],
|
|
163
|
+
onModuleReexport = 'error',
|
|
164
|
+
silenceBatchedUpdatesWarning = false,
|
|
165
|
+
} = {},
|
|
88
166
|
) {
|
|
167
|
+
const { types: t } = api;
|
|
168
|
+
if (!['error', 'warn', 'silent'].includes(onModuleReexport)) {
|
|
169
|
+
throw new Error(
|
|
170
|
+
`[react-native-direct-imports] \`onModuleReexport\` must be 'error', 'warn' or 'silent', got ${JSON.stringify(onModuleReexport)}`,
|
|
171
|
+
);
|
|
172
|
+
}
|
|
89
173
|
const MAP = loadMap(reactNativeVersion);
|
|
174
|
+
const PACKAGES = new Set(packages);
|
|
175
|
+
const { resolveImport: resolveByAnalysis } = createPackageResolver(api);
|
|
90
176
|
|
|
91
177
|
const buildRequire = (source, exportName) => {
|
|
92
178
|
const call = t.callExpression(t.identifier('require'), [
|
|
@@ -151,8 +237,9 @@ module.exports = function reactNativeDirectImports(
|
|
|
151
237
|
return null;
|
|
152
238
|
};
|
|
153
239
|
|
|
154
|
-
// `require('react-native')
|
|
155
|
-
|
|
240
|
+
// `require('react-native')` or `require('<listed package>')`, also wrapped in
|
|
241
|
+
// `(... as T)`, `(...)!`, `(...)`. Returns the package name, or `null`.
|
|
242
|
+
const requiredPackage = (node, scope) => {
|
|
156
243
|
while (
|
|
157
244
|
t.isTSAsExpression(node) ||
|
|
158
245
|
t.isTSSatisfiesExpression(node) ||
|
|
@@ -163,13 +250,17 @@ module.exports = function reactNativeDirectImports(
|
|
|
163
250
|
) {
|
|
164
251
|
node = node.expression;
|
|
165
252
|
}
|
|
166
|
-
|
|
253
|
+
if (
|
|
167
254
|
t.isCallExpression(node) &&
|
|
168
255
|
t.isIdentifier(node.callee, { name: 'require' }) &&
|
|
169
256
|
!scope.hasBinding('require') &&
|
|
170
257
|
node.arguments.length === 1 &&
|
|
171
|
-
t.isStringLiteral(node.arguments[0]
|
|
172
|
-
|
|
258
|
+
t.isStringLiteral(node.arguments[0]) &&
|
|
259
|
+
(node.arguments[0].value === RN || PACKAGES.has(node.arguments[0].value))
|
|
260
|
+
) {
|
|
261
|
+
return node.arguments[0].value;
|
|
262
|
+
}
|
|
263
|
+
return null;
|
|
173
264
|
};
|
|
174
265
|
|
|
175
266
|
// Metro's `inlinePlatform` only inlines `Platform.OS` / `Platform.select`
|
|
@@ -202,25 +293,31 @@ module.exports = function reactNativeDirectImports(
|
|
|
202
293
|
return t.identifier('Platform');
|
|
203
294
|
};
|
|
204
295
|
|
|
205
|
-
//
|
|
206
|
-
|
|
207
|
-
const replaceMember = (memberPath, state) => {
|
|
296
|
+
// `x.View = ...`, `x.View++`, `delete x.View`
|
|
297
|
+
const isMutation = (memberPath) => {
|
|
208
298
|
const { parentPath } = memberPath;
|
|
209
|
-
|
|
299
|
+
return (
|
|
210
300
|
(parentPath.isAssignmentExpression() &&
|
|
211
301
|
parentPath.node.left === memberPath.node) ||
|
|
212
302
|
parentPath.isUpdateExpression() ||
|
|
213
303
|
parentPath.isUnaryExpression({ operator: 'delete' })
|
|
214
|
-
)
|
|
215
|
-
|
|
216
|
-
|
|
304
|
+
);
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
// Replaces `x.View` with the direct `require()` expression. Returns `false`
|
|
308
|
+
// if the member can't be rewritten. `pkg` is the package `x` was required
|
|
309
|
+
// from.
|
|
310
|
+
const replaceMember = (memberPath, state, pkg = RN) => {
|
|
311
|
+
if (isMutation(memberPath)) return false;
|
|
217
312
|
|
|
218
313
|
const name = memberName(memberPath.node);
|
|
219
314
|
const expression =
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
315
|
+
pkg === RN
|
|
316
|
+
? (name === 'Platform' &&
|
|
317
|
+
MAP.Platform &&
|
|
318
|
+
platformIdentifier(memberPath, state)) ||
|
|
319
|
+
buildDirectExpression(name)
|
|
320
|
+
: buildPackageExpression(pkg, name, state);
|
|
224
321
|
if (!expression) return false;
|
|
225
322
|
|
|
226
323
|
memberPath.replaceWith(expression);
|
|
@@ -280,6 +377,251 @@ module.exports = function reactNativeDirectImports(
|
|
|
280
377
|
}
|
|
281
378
|
};
|
|
282
379
|
|
|
380
|
+
// Whole-module re-export (`module.exports = require('pkg')`): the whole
|
|
381
|
+
// package ends up in the bundle, so `onModuleReexport` decides what to do.
|
|
382
|
+
const reportModuleReexport = (path, pkg) => {
|
|
383
|
+
if (onModuleReexport === 'silent') return;
|
|
384
|
+
|
|
385
|
+
const example = pkg === RN ? 'View' : 'Name';
|
|
386
|
+
const reason =
|
|
387
|
+
onModuleReexport === 'error'
|
|
388
|
+
? 'is not allowed'
|
|
389
|
+
: 'is not rewritten and keeps the whole package in the bundle';
|
|
390
|
+
const error = path.buildCodeFrameError(
|
|
391
|
+
`\`module.exports = require('${pkg}')\` ${reason}, re-export named values instead: \`exports.${example} = require('${pkg}').${example}\``,
|
|
392
|
+
);
|
|
393
|
+
if (onModuleReexport === 'error') throw error;
|
|
394
|
+
console.warn(`[react-native-direct-imports] ${error.message}`);
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
// `module.exports = <ref>`
|
|
398
|
+
const isModuleExportsValue = (ref) => {
|
|
399
|
+
const { parentPath } = ref;
|
|
400
|
+
return (
|
|
401
|
+
parentPath.isAssignmentExpression() &&
|
|
402
|
+
parentPath.node.right === ref.node &&
|
|
403
|
+
parentPath.get('left').matchesPattern('module.exports')
|
|
404
|
+
);
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
// Packages are looked up in `node_modules` from the file being compiled.
|
|
408
|
+
const dirOf = (state) =>
|
|
409
|
+
nodePath.dirname(state.filename || nodePath.join(process.cwd(), '_'));
|
|
410
|
+
|
|
411
|
+
const installedVersions = new Map();
|
|
412
|
+
|
|
413
|
+
// Version of the package installed next to the file being compiled
|
|
414
|
+
// (`15.12.0-rc.1` -> `15.12.0`).
|
|
415
|
+
const installedVersion = (packageName, fromDir) => {
|
|
416
|
+
const cacheKey = `${packageName}\0${fromDir}`;
|
|
417
|
+
const cached = installedVersions.get(cacheKey);
|
|
418
|
+
if (cached) return cached;
|
|
419
|
+
|
|
420
|
+
const root = findPackageRoot(packageName, fromDir);
|
|
421
|
+
if (!root) {
|
|
422
|
+
throw new Error(
|
|
423
|
+
`[react-native-direct-imports] Can't find the package "${packageName}" listed in \`packages\`, looked in node_modules from ${fromDir}`,
|
|
424
|
+
);
|
|
425
|
+
}
|
|
426
|
+
const { version } = JSON.parse(
|
|
427
|
+
fs.readFileSync(nodePath.join(root, 'package.json'), 'utf8'),
|
|
428
|
+
);
|
|
429
|
+
const release = String(version).replace(/[-+].*$/, '');
|
|
430
|
+
installedVersions.set(cacheKey, release);
|
|
431
|
+
return release;
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
// `['<package>/<path>', exportName]` for `importedName` of another
|
|
435
|
+
// package, or `null` to keep the import. Packages with a shipped map use the
|
|
436
|
+
// map of the installed version; any other package is resolved by following
|
|
437
|
+
// the re-exports of its installed entry file.
|
|
438
|
+
const resolveImport = (packageName, fromDir, importedName) => {
|
|
439
|
+
if (!hasMap(packageName)) {
|
|
440
|
+
return resolveByAnalysis(packageName, fromDir, importedName);
|
|
441
|
+
}
|
|
442
|
+
const map = loadPackageMap(
|
|
443
|
+
packageName,
|
|
444
|
+
installedVersion(packageName, fromDir),
|
|
445
|
+
);
|
|
446
|
+
const entry = map[importedName];
|
|
447
|
+
return entry ? [`${packageName}/${entry[0]}`, entry[1]] : null;
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
// `require('<path>').<exportName>` for `importedName` of another package,
|
|
451
|
+
// or `null` to keep the original.
|
|
452
|
+
const buildPackageExpression = (pkg, importedName, state) => {
|
|
453
|
+
const entry =
|
|
454
|
+
importedName != null && resolveImport(pkg, dirOf(state), importedName);
|
|
455
|
+
if (!entry) return null;
|
|
456
|
+
|
|
457
|
+
const [source, exportName] = entry;
|
|
458
|
+
const call = buildRequire(source, '*');
|
|
459
|
+
if (exportName === '*') return call;
|
|
460
|
+
return t.isValidIdentifier(exportName, false)
|
|
461
|
+
? t.memberExpression(call, t.identifier(exportName))
|
|
462
|
+
: t.memberExpression(call, t.stringLiteral(exportName), true);
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
// var x = require('react-native-svg');
|
|
466
|
+
// x.Path; x.default;
|
|
467
|
+
//
|
|
468
|
+
// Unlike `react-native`, all or nothing: if a usage can't be rewritten
|
|
469
|
+
// (`fn(x)`, `x.Unknown`, `x.Path = ...`, `{ ...x }`), the whole package
|
|
470
|
+
// stays loaded through `x` anyway, so nothing is gained by rewriting the rest.
|
|
471
|
+
const rewritePackageBinding = (declaratorPath, state, pkg) => {
|
|
472
|
+
const { name } = declaratorPath.node.id;
|
|
473
|
+
const binding = declaratorPath.scope.getBinding(name);
|
|
474
|
+
if (
|
|
475
|
+
!binding ||
|
|
476
|
+
binding.path !== declaratorPath ||
|
|
477
|
+
!binding.constant ||
|
|
478
|
+
binding.referencePaths.length === 0 ||
|
|
479
|
+
declaratorPath.parentPath.parentPath.isExportNamedDeclaration()
|
|
480
|
+
) {
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
const reexport = binding.referencePaths.find(isModuleExportsValue);
|
|
485
|
+
if (reexport) {
|
|
486
|
+
reportModuleReexport(reexport.parentPath, pkg);
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
const members = [];
|
|
491
|
+
for (const ref of binding.referencePaths) {
|
|
492
|
+
const memberPath = ref.parentPath;
|
|
493
|
+
if (
|
|
494
|
+
!memberPath.isMemberExpression() ||
|
|
495
|
+
memberPath.node.object !== ref.node ||
|
|
496
|
+
isMutation(memberPath) ||
|
|
497
|
+
!buildPackageExpression(pkg, memberName(memberPath.node), state)
|
|
498
|
+
) {
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
members.push(memberPath);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
for (const memberPath of members) replaceMember(memberPath, state, pkg);
|
|
505
|
+
declaratorPath.remove();
|
|
506
|
+
};
|
|
507
|
+
|
|
508
|
+
// const { Svg, Path: P } = require('react-native-svg');
|
|
509
|
+
// All or nothing too: `{ Svg, ...rest }`, `{ Unknown }`, `{ [key]: x }` and
|
|
510
|
+
// `{ Svg = fallback }` keep the original.
|
|
511
|
+
const rewritePackagePattern = (declaratorPath, state, pkg) => {
|
|
512
|
+
const declarators = [];
|
|
513
|
+
|
|
514
|
+
for (const prop of declaratorPath.node.id.properties) {
|
|
515
|
+
if (
|
|
516
|
+
!t.isObjectProperty(prop) ||
|
|
517
|
+
prop.computed ||
|
|
518
|
+
!t.isIdentifier(prop.value) ||
|
|
519
|
+
!(t.isIdentifier(prop.key) || t.isStringLiteral(prop.key))
|
|
520
|
+
) {
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
const expression = buildPackageExpression(pkg, nameOf(prop.key), state);
|
|
524
|
+
if (!expression) return;
|
|
525
|
+
declarators.push(t.variableDeclarator(prop.value, expression));
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
if (declarators.length > 0) declaratorPath.replaceWithMultiple(declarators);
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
// An export name as a Babel node (names like `'a-b'` are string literals).
|
|
532
|
+
const exportNameNode = (name) =>
|
|
533
|
+
t.isValidIdentifier(name, false) ? t.identifier(name) : t.stringLiteral(name);
|
|
534
|
+
|
|
535
|
+
// export { Defs, default as Svg } from 'react-native-svg';
|
|
536
|
+
const rewritePackageReexport = (path, state) => {
|
|
537
|
+
const { node } = path;
|
|
538
|
+
if (node.exportKind === 'type') return;
|
|
539
|
+
|
|
540
|
+
const fromDir = dirOf(state);
|
|
541
|
+
const kept = [];
|
|
542
|
+
const direct = [];
|
|
543
|
+
|
|
544
|
+
for (const spec of node.specifiers) {
|
|
545
|
+
const entry =
|
|
546
|
+
t.isExportSpecifier(spec) &&
|
|
547
|
+
spec.exportKind !== 'type' &&
|
|
548
|
+
resolveImport(node.source.value, fromDir, nameOf(spec.local));
|
|
549
|
+
if (!entry) {
|
|
550
|
+
kept.push(spec);
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
const [source, exportName] = entry;
|
|
555
|
+
const specifier =
|
|
556
|
+
exportName === '*'
|
|
557
|
+
? t.exportNamespaceSpecifier(t.cloneNode(spec.exported))
|
|
558
|
+
: t.exportSpecifier(exportNameNode(exportName), t.cloneNode(spec.exported));
|
|
559
|
+
direct.push(
|
|
560
|
+
t.exportNamedDeclaration(null, [specifier], t.stringLiteral(source)),
|
|
561
|
+
);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
if (direct.length === 0) return;
|
|
565
|
+
|
|
566
|
+
if (kept.length > 0) {
|
|
567
|
+
node.specifiers = kept;
|
|
568
|
+
path.insertAfter(direct);
|
|
569
|
+
} else {
|
|
570
|
+
path.replaceWithMultiple(direct);
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
|
|
574
|
+
// import Svg, { Defs } from 'react-native-svg';
|
|
575
|
+
const rewritePackageImport = (path, state) => {
|
|
576
|
+
const { node } = path;
|
|
577
|
+
if (node.importKind != null && node.importKind !== 'value') return;
|
|
578
|
+
|
|
579
|
+
const fromDir = dirOf(state);
|
|
580
|
+
const kept = [];
|
|
581
|
+
const direct = [];
|
|
582
|
+
|
|
583
|
+
for (const spec of node.specifiers) {
|
|
584
|
+
let imported = null;
|
|
585
|
+
if (t.isImportDefaultSpecifier(spec)) {
|
|
586
|
+
imported = 'default';
|
|
587
|
+
} else if (
|
|
588
|
+
t.isImportSpecifier(spec) &&
|
|
589
|
+
spec.importKind !== 'type' &&
|
|
590
|
+
spec.importKind !== 'typeof'
|
|
591
|
+
) {
|
|
592
|
+
imported = nameOf(spec.imported);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
const entry =
|
|
596
|
+
imported && resolveImport(node.source.value, fromDir, imported);
|
|
597
|
+
if (!entry) {
|
|
598
|
+
kept.push(spec);
|
|
599
|
+
continue;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
const [source, exportName] = entry;
|
|
603
|
+
const local = t.identifier(spec.local.name);
|
|
604
|
+
let specifier;
|
|
605
|
+
if (exportName === 'default') {
|
|
606
|
+
specifier = t.importDefaultSpecifier(local);
|
|
607
|
+
} else if (exportName === '*') {
|
|
608
|
+
specifier = t.importNamespaceSpecifier(local);
|
|
609
|
+
} else {
|
|
610
|
+
specifier = t.importSpecifier(local, exportNameNode(exportName));
|
|
611
|
+
}
|
|
612
|
+
direct.push(t.importDeclaration([specifier], t.stringLiteral(source)));
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
if (direct.length === 0) return;
|
|
616
|
+
|
|
617
|
+
if (kept.length > 0) {
|
|
618
|
+
node.specifiers = kept;
|
|
619
|
+
path.insertAfter(direct);
|
|
620
|
+
} else {
|
|
621
|
+
path.replaceWithMultiple(direct);
|
|
622
|
+
}
|
|
623
|
+
};
|
|
624
|
+
|
|
283
625
|
return {
|
|
284
626
|
name: 'react-native-direct-imports',
|
|
285
627
|
visitor: {
|
|
@@ -288,36 +630,38 @@ module.exports = function reactNativeDirectImports(
|
|
|
288
630
|
// see detached paths.
|
|
289
631
|
AssignmentExpression(path) {
|
|
290
632
|
const { node } = path;
|
|
291
|
-
if (
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
)
|
|
295
|
-
throw path.buildCodeFrameError(
|
|
296
|
-
"`module.exports = require('react-native')` is not allowed, re-export named values instead: `exports.View = require('react-native').View`",
|
|
297
|
-
);
|
|
298
|
-
}
|
|
633
|
+
if (!path.get('left').matchesPattern('module.exports')) return;
|
|
634
|
+
|
|
635
|
+
const pkg = requiredPackage(node.right, path.scope);
|
|
636
|
+
if (pkg) reportModuleReexport(path, pkg);
|
|
299
637
|
},
|
|
300
638
|
|
|
301
639
|
// require('react-native').Linking
|
|
302
640
|
MemberExpression(path, state) {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
}
|
|
641
|
+
const pkg = requiredPackage(path.node.object, path.scope);
|
|
642
|
+
if (pkg) replaceMember(path, state, pkg);
|
|
306
643
|
},
|
|
307
644
|
|
|
308
645
|
VariableDeclarator(path, state) {
|
|
309
646
|
const { node } = path;
|
|
310
|
-
|
|
647
|
+
const pkg = requiredPackage(node.init, path.scope);
|
|
648
|
+
if (!pkg) return;
|
|
311
649
|
|
|
312
650
|
if (t.isIdentifier(node.id)) {
|
|
313
|
-
rewriteBinding(path, state);
|
|
651
|
+
if (pkg === RN) rewriteBinding(path, state);
|
|
652
|
+
else rewritePackageBinding(path, state, pkg);
|
|
314
653
|
} else if (t.isObjectPattern(node.id)) {
|
|
315
|
-
rewritePattern(path);
|
|
654
|
+
if (pkg === RN) rewritePattern(path);
|
|
655
|
+
else rewritePackagePattern(path, state, pkg);
|
|
316
656
|
}
|
|
317
657
|
},
|
|
318
658
|
|
|
319
|
-
ImportDeclaration(path) {
|
|
659
|
+
ImportDeclaration(path, state) {
|
|
320
660
|
const { node } = path;
|
|
661
|
+
if (node.source.value !== RN && PACKAGES.has(node.source.value)) {
|
|
662
|
+
rewritePackageImport(path, state);
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
321
665
|
if (node.source.value !== RN || node.importKind === 'type') return;
|
|
322
666
|
|
|
323
667
|
if (
|
|
@@ -355,8 +699,12 @@ module.exports = function reactNativeDirectImports(
|
|
|
355
699
|
}
|
|
356
700
|
},
|
|
357
701
|
|
|
358
|
-
ExportNamedDeclaration(path) {
|
|
702
|
+
ExportNamedDeclaration(path, state) {
|
|
359
703
|
const { node } = path;
|
|
704
|
+
if (node.source && node.source.value !== RN && PACKAGES.has(node.source.value)) {
|
|
705
|
+
rewritePackageReexport(path, state);
|
|
706
|
+
return;
|
|
707
|
+
}
|
|
360
708
|
if (node.source?.value !== RN || node.exportKind === 'type') return;
|
|
361
709
|
|
|
362
710
|
const kept = [];
|
|
@@ -403,3 +751,4 @@ module.exports = function reactNativeDirectImports(
|
|
|
403
751
|
};
|
|
404
752
|
|
|
405
753
|
module.exports.loadMap = loadMap;
|
|
754
|
+
module.exports.loadPackageMap = loadPackageMap;
|