forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ReactNative] Use network image for new image assets
- Loading branch information
Showing
3 changed files
with
155 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* 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. | ||
*/ | ||
'use strict'; | ||
|
||
jest.dontMock('../resolveAssetSource'); | ||
|
||
var resolveAssetSource; | ||
var SourceCode; | ||
|
||
describe('resolveAssetSource', () => { | ||
beforeEach(() => { | ||
jest.resetModuleRegistry(); | ||
SourceCode = require('NativeModules').SourceCode; | ||
resolveAssetSource = require('../resolveAssetSource'); | ||
}); | ||
|
||
it('returns same source for simple static and network images', () => { | ||
var source1 = {uri: 'https://www.facebook.com/logo'}; | ||
expect(resolveAssetSource(source1)).toBe(source1); | ||
|
||
var source2 = {isStatic: true, uri: 'logo'}; | ||
expect(resolveAssetSource(source2)).toBe(source2); | ||
}); | ||
|
||
describe('bundle was loaded from network', () => { | ||
beforeEach(() => { | ||
SourceCode.scriptURL = 'http://10.0.0.1:8081/main.bundle'; | ||
}); | ||
|
||
it('uses network image', () => { | ||
var source = { | ||
path: '/Users/react/project/logo.png', | ||
uri: 'assets/logo.png', | ||
}; | ||
expect(resolveAssetSource(source)).toEqual({ | ||
isStatic: false, | ||
uri: 'http://10.0.0.1:8081/assets/logo.png', | ||
}); | ||
}); | ||
|
||
it('does not change deprecated assets', () => { | ||
// Deprecated require('image!logo') should stay unchanged | ||
var source = { | ||
path: '/Users/react/project/logo.png', | ||
uri: 'logo', | ||
deprecated: true, | ||
}; | ||
expect(resolveAssetSource(source)).toEqual({ | ||
isStatic: true, | ||
uri: 'logo', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('bundle was loaded from file', () => { | ||
it('uses pre-packed image', () => { | ||
SourceCode.scriptURL = 'file:///Path/To/Simulator/main.bundle'; | ||
|
||
var source = { | ||
path: '/Users/react/project/logo.png', | ||
uri: 'assets/logo.png', | ||
}; | ||
expect(resolveAssetSource(source)).toEqual({ | ||
isStatic: true, | ||
uri: 'assets/logo.png', | ||
}); | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* 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. | ||
* | ||
* @providesModule resolveAssetSource | ||
*/ | ||
'use strict'; | ||
|
||
var SourceCode = require('NativeModules').SourceCode; | ||
|
||
var _serverURL; | ||
|
||
function getServerURL() { | ||
if (_serverURL === undefined) { | ||
var scriptURL = SourceCode.scriptURL; | ||
var serverURLMatch = scriptURL && scriptURL.match(/^https?:\/\/.*?\//); | ||
if (serverURLMatch) { | ||
_serverURL = serverURLMatch[0]; | ||
} else { | ||
_serverURL = null; | ||
} | ||
} | ||
|
||
return _serverURL; | ||
} | ||
|
||
// TODO(frantic): | ||
// * Use something other than `path`/`isStatic` for asset identification, `__packager_asset`? | ||
// * Add cache invalidating hashsum | ||
// * Move code that selects scale to client | ||
function resolveAssetSource(source) { | ||
if (source.deprecated) { | ||
return { | ||
...source, | ||
path: undefined, | ||
isStatic: true, | ||
deprecated: undefined, | ||
}; | ||
} | ||
|
||
var serverURL = getServerURL(); | ||
if (source.path) { | ||
if (serverURL) { | ||
return { | ||
...source, | ||
path: undefined, | ||
uri: serverURL + source.uri, | ||
isStatic: false, | ||
}; | ||
} else { | ||
return { | ||
...source, | ||
path: undefined, | ||
isStatic: true, | ||
}; | ||
} | ||
} | ||
|
||
return source; | ||
} | ||
|
||
module.exports = resolveAssetSource; |