diff --git a/packager/src/Bundler/index.js b/packager/src/Bundler/index.js index 32b38c2ee..03396550c 100644 --- a/packager/src/Bundler/index.js +++ b/packager/src/Bundler/index.js @@ -27,6 +27,8 @@ const defaults = require('../../defaults'); const os = require('os'); const invariant = require('fbjs/lib/invariant'); +const {generateAssetTransformResult, isAssetTypeAnImage} = require('./util'); + const { sep: pathSeparator, join: joinPath, @@ -95,12 +97,6 @@ const { log, } = require('../Logger'); -const assetPropertyBlacklist = new Set([ - 'files', - 'fileSystemLocation', - 'path', -]); - export type PostProcessModulesOptions = {| dev: boolean, minify: boolean, @@ -696,7 +692,7 @@ class Bundler { assetUrlPath = assetUrlPath.replace(/\\/g, '/'); } - const isImage = Bundler.isAssetTypeAnImage(extname(module.path).slice(1)); + const isImage = isAssetTypeAnImage(extname(module.path).slice(1)); return this._assetServer.getAssetData(relPath, platform).then(assetData => { return Promise.all([isImage ? sizeOf(assetData.files[0]) : null, assetData]); @@ -719,7 +715,7 @@ class Bundler { return this._applyAssetPlugins(assetPlugins, asset); }).then(asset => { - const {code, dependencies, dependencyOffsets} = Bundler.generateAssetTransformResult(asset); + const {code, dependencies, dependencyOffsets} = generateAssetTransformResult(asset); return { asset, code, @@ -728,29 +724,6 @@ 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: ExtendedAssetDescriptor, @@ -896,12 +869,4 @@ function getMainModule({dependencies, numPrependedDependencies = 0}) { return dependencies[numPrependedDependencies]; } -function filterObject(object, blacklist) { - const copied = Object.assign({}, object); - for (const key of blacklist) { - delete copied[key]; - } - return copied; -} - module.exports = Bundler; diff --git a/packager/src/Bundler/util.js b/packager/src/Bundler/util.js new file mode 100644 index 000000000..e1b7488e7 --- /dev/null +++ b/packager/src/Bundler/util.js @@ -0,0 +1,54 @@ +/** + * 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. + * + * @flow + */ + +'use strict'; + +import type {AssetDescriptor} from '.'; + +const assetPropertyBlacklist = new Set([ + 'files', + 'fileSystemLocation', + 'path', +]); + +function 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}; +} + +// 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. +function isAssetTypeAnImage(type: string): boolean { + return [ + 'png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff', + ].indexOf(type) !== -1; +} + +function filterObject(object, blacklist) { + const copied = Object.assign({}, object); + for (const key of blacklist) { + delete copied[key]; + } + return copied; +} + +exports.generateAssetTransformResult = generateAssetTransformResult; +exports.isAssetTypeAnImage = isAssetTypeAnImage;