-
Notifications
You must be signed in to change notification settings - Fork 630
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transform _dependencyMap[n] into the corresponding integer to save space
Summary: Uses Babel to transform references to `_dependencyMap[n]` into the corresponding integer, thus saving space when generating the bundles. Reviewed By: davidaurelio Differential Revision: D10488230 fbshipit-source-id: 072a2273e0d67a6051f7a5ab0b2da68c24e59df3
- Loading branch information
1 parent
df212a4
commit e7deea1
Showing
6 changed files
with
161 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/metro/src/ModuleGraph/output/reverse-dependency-map-references.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import typeof {types as BabelTypes} from '@babel/core'; | ||
import type {Path} from '@babel/traverse'; | ||
|
||
type State = {| | ||
opts: {| | ||
+dependencyIds: $ReadOnlyArray<number>, | ||
|}, | ||
|}; | ||
|
||
function reverseDependencyMapReferences({types: t}: {types: BabelTypes}) { | ||
return { | ||
visitor: { | ||
CallExpression(path: Path, state: State) { | ||
const {node} = path; | ||
|
||
if (node.callee.name === '__d') { | ||
const lastArg = node.arguments[0].params.slice(-1)[0]; | ||
const depMapName = lastArg && lastArg.name; | ||
|
||
if (!depMapName) { | ||
return; | ||
} | ||
|
||
const scope = path.get('arguments.0.body').scope; | ||
const binding = scope.getBinding(depMapName); | ||
|
||
binding.referencePaths.forEach(({parentPath}) => { | ||
const memberNode = parentPath.node; | ||
|
||
if ( | ||
memberNode.type === 'MemberExpression' && | ||
memberNode.property.type === 'NumericLiteral' | ||
) { | ||
parentPath.replaceWith( | ||
t.numericLiteral( | ||
state.opts.dependencyIds[memberNode.property.value], | ||
), | ||
); | ||
} | ||
}); | ||
} | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = reverseDependencyMapReferences; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters