mirror of https://github.com/status-im/metro.git
Add asset size to the module
Reviewed By: jeanlauliac Differential Revision: D6284248 fbshipit-source-id: 3b860d7b123a1d2470c7b6c7e9e9082d0622c369
This commit is contained in:
parent
e7b4e50813
commit
e08b34239e
|
@ -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])]
|
||||
};"
|
||||
`;
|
|
@ -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);
|
||||
});
|
||||
});
|
|
@ -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),
|
||||
]),
|
||||
),
|
||||
),
|
||||
|
|
Loading…
Reference in New Issue