Permalink
Browse files

pull reusable logic out of `Bundler`

Reviewed By: jeanlauliac

Differential Revision: D4978045

fbshipit-source-id: 8e2ea14c983d4812e84ab644484380c98eb8da36
  • Loading branch information...
davidaurelio authored and facebook-github-bot committed May 3, 2017
1 parent 976a181 commit 66d51bb5d1f7df07694698094b2dbe6a6642a05c
Showing with 58 additions and 39 deletions.
  1. +4 −39 packager/src/Bundler/index.js
  2. +54 −0 packager/src/Bundler/util.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<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: 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;
@@ -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<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};
}
// 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;

0 comments on commit 66d51bb

Please sign in to comment.