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.
Add "assetPlugin" option to allow arbitrary asset processing
Summary: Adds a new URL option to the packager server called "assetPlugin". This can be a name of a Node module or multiple Node modules (`assetPlugin=module1&assetPlugin=module2`). Each plugin is loaded using `require()` and is expected to export a function. Each plugin function is invoked with an asset as the argument. The plugins may be async functions; the packager will properly wait for them to settle and will chain them. A plugin may be used to add extra metadata to an asset. For example it may add an array of hashes for all of the files belonging to an asset, or it may add the duration of a sound clip asset. Closes facebook#9993 Differential Revision: D3895384 Pulled By: davidaurelio fbshipit-source-id: 0afe24012fc54b6d18d9b2df5f5675d27ea58320
- Loading branch information
Showing
4 changed files
with
131 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,6 +208,65 @@ describe('Bundler', function() { | |
}); | ||
}); | ||
|
||
it('loads and runs asset plugins', function() { | ||
jest.mock('mockPlugin1', () => { | ||
return asset => { | ||
asset.extraReverseHash = asset.hash.split('').reverse().join(''); | ||
return asset; | ||
}; | ||
}, {virtual: true}); | ||
|
||
jest.mock('asyncMockPlugin2', () => { | ||
return asset => { | ||
expect(asset.extraReverseHash).toBeDefined(); | ||
return new Promise((resolve) => { | ||
asset.extraPixelCount = asset.width * asset.height; | ||
resolve(asset); | ||
}); | ||
}; | ||
}, {virtual: true}); | ||
|
||
const mockAsset = { | ||
scales: [1,2,3], | ||
files: [ | ||
'/root/img/img.png', | ||
'/root/img/[email protected]', | ||
'/root/img/[email protected]', | ||
], | ||
hash: 'i am a hash', | ||
name: 'img', | ||
type: 'png', | ||
}; | ||
assetServer.getAssetData.mockImpl(() => mockAsset); | ||
|
||
return bundler.bundle({ | ||
entryFile: '/root/foo.js', | ||
runBeforeMainModule: [], | ||
runModule: true, | ||
sourceMapUrl: 'source_map_url', | ||
assetPlugins: ['mockPlugin1', 'asyncMockPlugin2'], | ||
}).then(bundle => { | ||
expect(bundle.addAsset.mock.calls[1]).toEqual([{ | ||
__packager_asset: true, | ||
fileSystemLocation: '/root/img', | ||
httpServerLocation: '/assets/img', | ||
width: 25, | ||
height: 50, | ||
scales: [1, 2, 3], | ||
files: [ | ||
'/root/img/img.png', | ||
'/root/img/[email protected]', | ||
'/root/img/[email protected]', | ||
], | ||
hash: 'i am a hash', | ||
name: 'img', | ||
type: 'png', | ||
extraReverseHash: 'hsah a ma i', | ||
extraPixelCount: 1250, | ||
}]); | ||
}); | ||
}); | ||
|
||
pit('gets the list of dependencies from the resolver', function() { | ||
const entryFile = '/root/foo.js'; | ||
return bundler.getDependencies({entryFile, recursive: true}).then(() => | ||
|
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
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