in packages/metro/src/Bundler/util.js [88:142]
function generateRemoteAssetCodeFileAst(
assetUtilsPath: string,
assetDescriptor: AssetDataWithoutFiles,
remoteServer: string,
remoteFileMap: RemoteFileMap,
): ?File {
const t = babelTypes;
const file = remoteFileMap[assetDescriptor.fileSystemLocation];
const descriptor = file && file[assetDescriptor.name];
const data = {};
if (!descriptor) {
return null;
}
for (const scale in descriptor) {
data[+scale] = descriptor[+scale].handle;
}
// {2: 'path/to/image@2x', 3: 'path/to/image@3x', ...}
const astData = babylon.parseExpression(JSON.stringify(data));
// URI to remote server
const URI = t.stringLiteral(remoteServer);
// Size numbers.
const WIDTH = t.numericLiteral(nullthrows(assetDescriptor.width));
const HEIGHT = t.numericLiteral(nullthrows(assetDescriptor.height));
const buildRequire = template.program(`
const {pickScale, getUrlCacheBreaker}= require(ASSET_UTILS_PATH);
module.exports = {
"width": WIDTH,
"height": HEIGHT,
"uri": URI + OBJECT_AST[pickScale(SCALE_ARRAY)] + getUrlCacheBreaker()
};
`);
return t.file(
buildRequire({
WIDTH,
HEIGHT,
URI,
OBJECT_AST: astData,
ASSET_UTILS_PATH: t.stringLiteral(assetUtilsPath),
SCALE_ARRAY: t.arrayExpression(
Object.keys(descriptor)
.map(Number)
.sort((a: number, b: number) => a - b)
.map((scale: number) => t.numericLiteral(scale)),
),
}),
);
}