Skip to content

Commit 7fb0e4a

Browse files
Ives van Hoornefacebook-github-bot
authored andcommitted
Move asset code generation logic to babel-template
Reviewed By: rafeca Differential Revision: D8661779 fbshipit-source-id: 00799ee1fd76f764634ac1b43b7be823afe80231
1 parent dfab026 commit 7fb0e4a

File tree

1 file changed

+31
-63
lines changed

1 file changed

+31
-63
lines changed

packages/metro/src/Bundler/util.js

Lines changed: 31 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
'use strict';
1212

13+
const template = require('@babel/template').default;
1314
const babelTypes = require('@babel/types');
1415
const babylon = require('babylon');
1516

@@ -58,33 +59,17 @@ function generateAssetCodeFileAst(
5859
);
5960
const t = babelTypes;
6061

61-
// module.exports
62-
const moduleExports = t.memberExpression(
63-
t.identifier('module'),
64-
t.identifier('exports'),
65-
);
66-
67-
// require('AssetRegistry')
68-
const requireCall = t.callExpression(t.identifier('require'), [
69-
t.stringLiteral(assetRegistryPath),
70-
]);
71-
72-
// require('AssetRegistry').registerAsset
73-
const registerAssetFunction = t.memberExpression(
74-
requireCall,
75-
t.identifier('registerAsset'),
76-
);
77-
7862
// require('AssetRegistry').registerAsset({...})
79-
const registerAssetCall = t.callExpression(registerAssetFunction, [
80-
descriptorAst,
81-
]);
63+
const buildRequire = template(`
64+
module.exports = require(ASSET_REGISTRY_PATH).registerAsset(DESCRIPTOR_AST)
65+
`);
8266

8367
return t.file(
8468
t.program([
85-
t.expressionStatement(
86-
t.assignmentExpression('=', moduleExports, registerAssetCall),
87-
),
69+
buildRequire({
70+
ASSET_REGISTRY_PATH: t.stringLiteral(assetRegistryPath),
71+
DESCRIPTOR_AST: descriptorAst,
72+
}),
8873
]),
8974
);
9075
}
@@ -114,56 +99,39 @@ function generateRemoteAssetCodeFileAst(
11499
data[+scale] = descriptor[+scale].handle;
115100
}
116101

117-
// require('AssetSourceResolver')
118-
const requireCall = t.callExpression(t.identifier('require'), [
119-
t.stringLiteral(assetSourceResolverPath),
120-
]);
121-
122-
// require('AssetSourceResolver').pickScale
123-
const pickScale = t.memberExpression(requireCall, t.identifier('pickScale'));
124-
125-
// require('AssetSourceResolver').pickScale([2, 3, ...])
126-
const call = t.callExpression(pickScale, [
127-
t.arrayExpression(
128-
Object.keys(descriptor)
129-
.map(Number)
130-
.sort((a, b) => a - b)
131-
.map(scale => t.numericLiteral(scale)),
132-
),
133-
]);
134-
135102
// {2: 'path/to/image@2x', 3: 'path/to/image@3x', ...}
136103
const astData = babylon.parseExpression(JSON.stringify(data));
137104

138-
// ({2: '...', 3: '...'})[require(...).pickScale(...)]
139-
const handler = t.memberExpression(astData, call, true);
140-
141-
// 'https://remote.server.com/' + ({2: ...})[require(...).pickScale(...)]
142-
const uri = t.binaryExpression('+', t.stringLiteral(remoteServer), handler);
105+
// URI to remote server
106+
const URI = t.stringLiteral(remoteServer);
143107

144108
// Size numbers.
145-
const width = t.numericLiteral(assetDescriptor.width);
146-
const height = t.numericLiteral(assetDescriptor.height);
109+
const WIDTH = t.numericLiteral(assetDescriptor.width);
110+
const HEIGHT = t.numericLiteral(assetDescriptor.height);
147111

148-
// module.exports
149-
const moduleExports = t.memberExpression(
150-
t.identifier('module'),
151-
t.identifier('exports'),
152-
);
112+
const buildRequire = template(`
113+
module.exports = {
114+
"width": WIDTH,
115+
"height": HEIGHT,
116+
"uri": URI + OBJECT_AST[require(ASSET_SOURCE_RESOLVER_PATH).pickScale(SCALE_ARRAY)]
117+
};
118+
`);
153119

154120
return t.file(
155121
t.program([
156-
t.expressionStatement(
157-
t.assignmentExpression(
158-
'=',
159-
moduleExports,
160-
t.objectExpression([
161-
t.objectProperty(t.stringLiteral('width'), width),
162-
t.objectProperty(t.stringLiteral('height'), height),
163-
t.objectProperty(t.stringLiteral('uri'), uri),
164-
]),
122+
buildRequire({
123+
WIDTH,
124+
HEIGHT,
125+
URI,
126+
OBJECT_AST: astData,
127+
ASSET_SOURCE_RESOLVER_PATH: t.stringLiteral(assetSourceResolverPath),
128+
SCALE_ARRAY: t.arrayExpression(
129+
Object.keys(descriptor)
130+
.map(Number)
131+
.sort((a, b) => a - b)
132+
.map(scale => t.numericLiteral(scale)),
165133
),
166-
),
134+
}),
167135
]),
168136
);
169137
}

0 commit comments

Comments
 (0)