diff --git a/packages/metro-bundler/src/Bundler/__tests__/__snapshots__/util-test.js.snap b/packages/metro-bundler/src/Bundler/__tests__/__snapshots__/util-test.js.snap new file mode 100644 index 00000000..96b4f461 --- /dev/null +++ b/packages/metro-bundler/src/Bundler/__tests__/__snapshots__/util-test.js.snap @@ -0,0 +1,27 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Util generates a local asset for a given descriptor 1`] = ` +"module.exports = require(\\"react-native-module/asset-resolver\\").registerAsset({ + \\"__packager_asset\\": true, + \\"hash\\": \\"9ec9c5721fcd5cc401b4499a0cc8878bc1a18bb5\\", + \\"height\\": 24, + \\"name\\": \\"my-asset\\", + \\"scales\\": [1, 1.5, 2, 3, 4], + \\"type\\": \\"png\\", + \\"width\\": 240 +});" +`; + +exports[`Util generates a remote asset for a given descriptor 1`] = ` +"module.exports = { + \\"width\\": 240, + \\"height\\": 24, + \\"uri\\": \\"https://example.com\\" + { + \\"1\\": \\"GCRaTwHwaI1plCgBAAAAAAC5oAcJbnsvAAAZ\\", + \\"2\\": \\"GMsbUgHQlgBGbPsCAAAAAAABXchsbnsvAAAZ\\", + \\"3\\": \\"GMEgUgG9llQL8EUBAAAAAAB2uXdrbnsvAAAZ\\", + \\"4\\": \\"GFleUAEiuVDxD5wGAAAAAAZWLd1dbnsvAAAZ\\", + \\"1.5\\": \\"GAdeUAEMbQH8hyQGAAAAAAC9H193bnsvAAAZ\\" + }[require(\\"react-native-module/asset-resolver\\").pickScale([1, 1.5, 2, 3, 4])] +};" +`; diff --git a/packages/metro-bundler/src/Bundler/__tests__/util-test.js b/packages/metro-bundler/src/Bundler/__tests__/util-test.js new file mode 100644 index 00000000..ce2f8f1c --- /dev/null +++ b/packages/metro-bundler/src/Bundler/__tests__/util-test.js @@ -0,0 +1,79 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @emails oncall+javascript_foundation + * @format + */ + +'use strict'; + +const babelGenerate = require('babel-generator').default; +const { + generateAssetCodeFileAst, + generateRemoteAssetCodeFileAst, +} = require('../util'); + +describe('Util', () => { + const assetDescriptor = { + __packager_asset: true, + fileSystemLocation: '/foo/bar', + hash: '9ec9c5721fcd5cc401b4499a0cc8878bc1a18bb5', + height: 24, + name: 'my-asset', + scales: [1, 1.5, 2, 3, 4], + type: 'png', + width: 240, + }; + + const remoteFileMap = { + '/foo/bar': { + 'my-asset': { + 1: 'GCRaTwHwaI1plCgBAAAAAAC5oAcJbnsvAAAZ', + 1.5: 'GAdeUAEMbQH8hyQGAAAAAAC9H193bnsvAAAZ', + 2: 'GMsbUgHQlgBGbPsCAAAAAAABXchsbnsvAAAZ', + 3: 'GMEgUgG9llQL8EUBAAAAAAB2uXdrbnsvAAAZ', + 4: 'GFleUAEiuVDxD5wGAAAAAAZWLd1dbnsvAAAZ', + }, + }, + }; + + it('generates a local asset for a given descriptor', () => { + const {code} = babelGenerate( + generateAssetCodeFileAst( + 'react-native-module/asset-resolver', + assetDescriptor, + ), + ); + + expect(code).toMatchSnapshot(); + }); + + it('generates a remote asset for a given descriptor', () => { + const {code} = babelGenerate( + generateRemoteAssetCodeFileAst( + 'react-native-module/asset-resolver', + assetDescriptor, + 'https://example.com', + remoteFileMap, + ), + ); + + expect(code).toMatchSnapshot(); + }); + + it('returns null if the asset is not present on the map', () => { + const asset = generateRemoteAssetCodeFileAst( + 'react-native-module/asset-resolver', + assetDescriptor, + 'https://example.com', + {}, + ); + + expect(asset).toBe(null); + }); +}); diff --git a/packages/metro-bundler/src/Bundler/util.js b/packages/metro-bundler/src/Bundler/util.js index 9ea77c33..8b68751b 100644 --- a/packages/metro-bundler/src/Bundler/util.js +++ b/packages/metro-bundler/src/Bundler/util.js @@ -131,11 +131,11 @@ function generateRemoteAssetCodeFileAst( const handler = t.memberExpression(data, call, true); // 'https://remote.server.com/' + ({2: ...})[require(...).pickScale(...)] - const result = t.binaryExpression( - '+', - t.stringLiteral(remoteServer), - handler, - ); + const uri = t.binaryExpression('+', t.stringLiteral(remoteServer), handler); + + // Size numbers. + const width = t.numericLiteral(assetDescriptor.width); + const height = t.numericLiteral(assetDescriptor.height); // module.exports const moduleExports = t.memberExpression( @@ -150,7 +150,9 @@ function generateRemoteAssetCodeFileAst( '=', moduleExports, t.objectExpression([ - t.objectProperty(t.stringLiteral('uri'), result), + t.objectProperty(t.stringLiteral('width'), width), + t.objectProperty(t.stringLiteral('height'), height), + t.objectProperty(t.stringLiteral('uri'), uri), ]), ), ),