Skip to content

Commit 5b25f20

Browse files
committed
[ReactNative] Teach flow how to deal with images
1 parent ac5c1e9 commit 5b25f20

File tree

6 files changed

+77
-60
lines changed

6 files changed

+77
-60
lines changed

.flowconfig

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030

3131
[libs]
3232
Libraries/react-native/react-native-interface.js
33-
Examples/UIExplorer/ImageMocks.js
3433

3534
[options]
3635
module.system=haste
3736

3837
munge_underscores=true
3938

39+
module.name_mapper='^image![a-zA-Z0-9$_-]+$' -> 'GlobalImageStub'
40+
module.name_mapper='^[./a-zA-Z0-9$_-]+\.png$' -> 'RelativeImageStub'
41+
4042
suppress_type=$FlowIssue
4143
suppress_type=$FlowFixMe
4244
suppress_type=$FixMe

Examples/SampleApp/_flowconfig

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ module.system=haste
3434

3535
munge_underscores=true
3636

37+
module.name_mapper='^image![a-zA-Z0-9$_-]+$' -> 'GlobalImageStub'
38+
module.name_mapper='^[./a-zA-Z0-9$_-]+\.png$' -> 'RelativeImageStub'
39+
3740
suppress_type=$FlowIssue
3841
suppress_type=$FlowFixMe
3942
suppress_type=$FixMe

Examples/UIExplorer/ImageMocks.js

-56
This file was deleted.

Libraries/Image/AssetRegistry.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,32 @@
22
* Copyright 2004-present Facebook. All Rights Reserved.
33
*
44
* @providesModule AssetRegistry
5+
* @flow
56
*/
67
'use strict';
78

8-
var assets = [];
9+
export type PackagerAsset = {
10+
__packager_asset: boolean,
11+
fileSystemLocation: string,
12+
httpServerLocation: string,
13+
width: number,
14+
height: number,
15+
scales: Array<number>,
16+
hash: string,
17+
name: string,
18+
type: string,
19+
};
920

10-
function registerAsset(asset) {
21+
22+
var assets: Array<PackagerAsset> = [];
23+
24+
function registerAsset(asset: PackagerAsset): number {
1125
// `push` returns new array length, so the first asset will
1226
// get id 1 (not 0) to make the value truthy
1327
return assets.push(asset);
1428
}
1529

16-
function getAssetByID(assetId) {
30+
function getAssetByID(assetId: number): PackagerAsset {
1731
return assets[assetId - 1];
1832
}
1933

Libraries/Image/GlobalImageStub.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule GlobalImageStub
10+
* @flow
11+
*/
12+
'use strict';
13+
14+
// This is a stub for flow to make it understand require('image!icon')
15+
// See packager/react-packager/src/Bundler/index.js
16+
17+
module.exports = {
18+
__packager_asset: true,
19+
isStatic: true,
20+
path: '/full/path/to/something.png',
21+
uri: 'icon',
22+
width: 100,
23+
height: 100,
24+
deprecated: true,
25+
};

Libraries/Image/RelativeImageStub.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule RelativeImageStub
10+
* @flow
11+
*/
12+
'use strict';
13+
14+
// This is a stub for flow to make it understand require('./icon.png')
15+
// See packager/react-packager/src/Bundler/index.js
16+
17+
var AssetRegistry = require('AssetRegistry');
18+
19+
module.exports = AssetRegistry.registerAsset({
20+
__packager_asset: true,
21+
fileSystemLocation: '/full/path/to/directory',
22+
httpServerLocation: '/assets/full/path/to/directory',
23+
width: 100,
24+
height: 100,
25+
scales: [1, 2, 3],
26+
hash: 'nonsense',
27+
name: 'icon',
28+
type: 'png',
29+
});

0 commit comments

Comments
 (0)