From a12d8410c35519820345ee36d05818bc583fd89b Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Mon, 24 Apr 2017 09:31:51 -0700 Subject: [PATCH] packager: buck library: aggregate assets Reviewed By: davidaurelio Differential Revision: D4938032 fbshipit-source-id: afc4f4f97b7cc513eca9c925e09fbee4871216f4 --- packages/metro-bundler/src/Bundler/index.js | 50 ++++++++++++------- .../src/ModuleGraph/types.flow.js | 6 +++ 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/packages/metro-bundler/src/Bundler/index.js b/packages/metro-bundler/src/Bundler/index.js index b195496c..b8b3221a 100644 --- a/packages/metro-bundler/src/Bundler/index.js +++ b/packages/metro-bundler/src/Bundler/index.js @@ -65,18 +65,21 @@ export type GetTransformOptions = ( getDependenciesOf: string => Promise>, ) => Promise; -type Asset = {| +type AssetDescriptor = { +__packager_asset: boolean, - +fileSystemLocation: string, +httpServerLocation: string, +width: ?number, +height: ?number, +scales: Array, - +files: Array, +hash: string, +name: string, +type: string, -|}; +}; + +type ExtendedAssetDescriptor = AssetDescriptor & { + +fileSystemLocation: string, + +files: Array, +}; const sizeOf = denodeify(imageSize); @@ -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]); @@ -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, @@ -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, + dependencyOffsets: Array, + |} { + 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, - asset: Asset, + asset: ExtendedAssetDescriptor, ) { if (!assetPlugins.length) { return asset; diff --git a/packages/metro-bundler/src/ModuleGraph/types.flow.js b/packages/metro-bundler/src/ModuleGraph/types.flow.js index 92ecc3ce..de86d678 100644 --- a/packages/metro-bundler/src/ModuleGraph/types.flow.js +++ b/packages/metro-bundler/src/ModuleGraph/types.flow.js @@ -134,3 +134,9 @@ export type TransformedFile = { transformed: TransformResults, type: FileTypes, }; + +export type LibraryOptions = {| + dependencies?: Array, + platform?: string, + root: string, +|};