Skip to content

Commit

Permalink
packager: buck library: aggregate assets
Browse files Browse the repository at this point in the history
Reviewed By: davidaurelio

Differential Revision: D4938032

fbshipit-source-id: afc4f4f97b7cc513eca9c925e09fbee4871216f4
  • Loading branch information
Jean Lauliac authored and thotegowda committed May 7, 2017
1 parent ecf3bd2 commit ac3187a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
50 changes: 33 additions & 17 deletions packager/src/Bundler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,21 @@ export type GetTransformOptions = (
getDependenciesOf: string => Promise<Array<string>>,
) => Promise<ExtraTransformOptions>;

type Asset = {|
type AssetDescriptor = {
+__packager_asset: boolean,
+fileSystemLocation: string,
+httpServerLocation: string,
+width: ?number,
+height: ?number,
+scales: Array<number>,
+files: Array<string>,
+hash: string,
+name: string,
+type: string,
|};
};

type ExtendedAssetDescriptor = AssetDescriptor & {
+fileSystemLocation: string,
+files: Array<string>,
};

const sizeOf = denodeify(imageSize);

Expand Down Expand Up @@ -665,11 +668,7 @@ class Bundler {
assetUrlPath = assetUrlPath.replace(/\\/g, '/');
}

// Test extension against all types supported by image-size module.
// If it's not one of these, we won't treat it as an image.
const isImage = [
'png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff',
].indexOf(extname(module.path).slice(1)) !== -1;
const isImage = Bundler.isAssetTypeAnImage(extname(module.path).slice(1));

return this._assetServer.getAssetData(relPath, platform).then(assetData => {
return Promise.all([isImage ? sizeOf(assetData.files[0]) : null, assetData]);
Expand All @@ -692,13 +691,7 @@ class Bundler {

return this._applyAssetPlugins(assetPlugins, asset);
}).then(asset => {
const json = JSON.stringify(filterObject(asset, assetPropertyBlacklist));
const assetRegistryPath = 'react-native/Libraries/Image/AssetRegistry';
const code =
`module.exports = require(${JSON.stringify(assetRegistryPath)}).registerAsset(${json});`;
const dependencies = [assetRegistryPath];
const dependencyOffsets = [code.indexOf(assetRegistryPath) - 1];

const {code, dependencies, dependencyOffsets} = Bundler.generateAssetTransformResult(asset);
return {
asset,
code,
Expand All @@ -707,9 +700,32 @@ class Bundler {
});
}

// Test extension against all types supported by image-size module.
// If it's not one of these, we won't treat it as an image.
static isAssetTypeAnImage(type: string): boolean {
return [
'png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff',
].indexOf(type) !== -1;
}

static generateAssetTransformResult(assetDescriptor: AssetDescriptor): {|
code: string,
dependencies: Array<string>,
dependencyOffsets: Array<number>,
|} {
const properDescriptor = filterObject(assetDescriptor, assetPropertyBlacklist);
const json = JSON.stringify(properDescriptor);
const assetRegistryPath = 'react-native/Libraries/Image/AssetRegistry';
const code =
`module.exports = require(${JSON.stringify(assetRegistryPath)}).registerAsset(${json});`;
const dependencies = [assetRegistryPath];
const dependencyOffsets = [code.indexOf(assetRegistryPath) - 1];
return {code, dependencies, dependencyOffsets};
}

_applyAssetPlugins(
assetPlugins: Array<string>,
asset: Asset,
asset: ExtendedAssetDescriptor,
) {
if (!assetPlugins.length) {
return asset;
Expand Down
6 changes: 6 additions & 0 deletions packager/src/ModuleGraph/types.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,9 @@ export type TransformedFile = {
transformed: TransformResults,
type: FileTypes,
};

export type LibraryOptions = {|
dependencies?: Array<string>,
platform?: string,
root: string,
|};

0 comments on commit ac3187a

Please sign in to comment.