mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Use array lookup for localized dependency IDs
Summary:
The current transform for require calls replaces strings with module-local IDs. That means that each module would need a local require function.
To save hundreds of closure allocations, we can just use an array that maps local IDs to global IDs.
This diff changes the dependency collection and replacement transform to change a call like `require('React')` to something like `require(_dependencyMap[0])` rather than `require(0)`.
Reviewed By: cpojer
Differential Revision: D4153714
fbshipit-source-id: a63455834c6c2a75da6977cacb9aac9f2cb1b3aa
This commit is contained in:
1 parent
0d0f44439c
commit
242bc6928f
4 files changed
+67
-25
No files matched your search
+11
-5
@@ -144,16 +144,21 @@ function optimizeModule(
|
||||
}
|
||||
|
||||
function makeResult(ast, filename, sourceCode, isPolyfill = false) {
|
||||
const dependencies = isPolyfill ? [] : collectDependencies(ast);
|
||||
const file = isPolyfill ? wrapPolyfill(ast) : wrapModule(ast);
|
||||
const {dependencies, dependencyMapName} = isPolyfill
|
||||
? {dependencies: []}
|
||||
: collectDependencies(ast);
|
||||
const file = isPolyfill
|
||||
? wrapPolyfill(ast)
|
||||
: wrapModule(ast, dependencyMapName);
|
||||
|
||||
const gen = generate(file, filename, sourceCode);
|
||||
return {code: gen.code, map: gen.map, dependencies};
|
||||
return {code: gen.code, map: gen.map, dependencies, dependencyMapName};
|
||||
}
|
||||
|
||||
function wrapModule(file) {
|
||||
function wrapModule(file, dependencyMapName) {
|
||||
const t = babel.types;
|
||||
const factory = functionFromProgram(file.program, moduleFactoryParameters);
|
||||
const params = moduleFactoryParameters.concat(dependencyMapName);
|
||||
const factory = functionFromProgram(file.program, params);
|
||||
const def = t.callExpression(t.identifier('__d'), [factory]);
|
||||
return t.file(t.program([t.expressionStatement(def)]));
|
||||
}
|
||||
@@ -183,6 +188,7 @@ function optimize(transformed, file, originalCode, options) {
|
||||
: collectDependencies.forOptimization(
|
||||
optimized.ast,
|
||||
transformed.dependencies,
|
||||
transformed.dependencyMapName,
|
||||
);
|
||||
|
||||
const inputMap = transformed.map;
|
||||
|
||||
Reference in new issue
Block a user