mirror of https://github.com/status-im/metro.git
pull reusable logic out of `Bundler`
Reviewed By: jeanlauliac Differential Revision: D4978045 fbshipit-source-id: 8e2ea14c983d4812e84ab644484380c98eb8da36
This commit is contained in:
parent
d016b2e9e4
commit
e591351a56
|
@ -27,6 +27,8 @@ const defaults = require('../../defaults');
|
||||||
const os = require('os');
|
const os = require('os');
|
||||||
const invariant = require('fbjs/lib/invariant');
|
const invariant = require('fbjs/lib/invariant');
|
||||||
|
|
||||||
|
const {generateAssetTransformResult, isAssetTypeAnImage} = require('./util');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
sep: pathSeparator,
|
sep: pathSeparator,
|
||||||
join: joinPath,
|
join: joinPath,
|
||||||
|
@ -95,12 +97,6 @@ const {
|
||||||
log,
|
log,
|
||||||
} = require('../Logger');
|
} = require('../Logger');
|
||||||
|
|
||||||
const assetPropertyBlacklist = new Set([
|
|
||||||
'files',
|
|
||||||
'fileSystemLocation',
|
|
||||||
'path',
|
|
||||||
]);
|
|
||||||
|
|
||||||
export type PostProcessModulesOptions = {|
|
export type PostProcessModulesOptions = {|
|
||||||
dev: boolean,
|
dev: boolean,
|
||||||
minify: boolean,
|
minify: boolean,
|
||||||
|
@ -696,7 +692,7 @@ class Bundler {
|
||||||
assetUrlPath = assetUrlPath.replace(/\\/g, '/');
|
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 this._assetServer.getAssetData(relPath, platform).then(assetData => {
|
||||||
return Promise.all([isImage ? sizeOf(assetData.files[0]) : null, assetData]);
|
return Promise.all([isImage ? sizeOf(assetData.files[0]) : null, assetData]);
|
||||||
|
@ -719,7 +715,7 @@ class Bundler {
|
||||||
|
|
||||||
return this._applyAssetPlugins(assetPlugins, asset);
|
return this._applyAssetPlugins(assetPlugins, asset);
|
||||||
}).then(asset => {
|
}).then(asset => {
|
||||||
const {code, dependencies, dependencyOffsets} = Bundler.generateAssetTransformResult(asset);
|
const {code, dependencies, dependencyOffsets} = generateAssetTransformResult(asset);
|
||||||
return {
|
return {
|
||||||
asset,
|
asset,
|
||||||
code,
|
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(
|
_applyAssetPlugins(
|
||||||
assetPlugins: Array<string>,
|
assetPlugins: Array<string>,
|
||||||
asset: ExtendedAssetDescriptor,
|
asset: ExtendedAssetDescriptor,
|
||||||
|
@ -896,12 +869,4 @@ function getMainModule({dependencies, numPrependedDependencies = 0}) {
|
||||||
return dependencies[numPrependedDependencies];
|
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;
|
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;
|
Loading…
Reference in New Issue