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, +|};