mirror of https://github.com/status-im/metro.git
packager: buck library: aggregate assets
Reviewed By: davidaurelio Differential Revision: D4938032 fbshipit-source-id: afc4f4f97b7cc513eca9c925e09fbee4871216f4
This commit is contained in:
parent
ca24ab1da6
commit
a12d8410c3
|
@ -65,18 +65,21 @@ export type GetTransformOptions = (
|
||||||
getDependenciesOf: string => Promise<Array<string>>,
|
getDependenciesOf: string => Promise<Array<string>>,
|
||||||
) => Promise<ExtraTransformOptions>;
|
) => Promise<ExtraTransformOptions>;
|
||||||
|
|
||||||
type Asset = {|
|
type AssetDescriptor = {
|
||||||
+__packager_asset: boolean,
|
+__packager_asset: boolean,
|
||||||
+fileSystemLocation: string,
|
|
||||||
+httpServerLocation: string,
|
+httpServerLocation: string,
|
||||||
+width: ?number,
|
+width: ?number,
|
||||||
+height: ?number,
|
+height: ?number,
|
||||||
+scales: Array<number>,
|
+scales: Array<number>,
|
||||||
+files: Array<string>,
|
|
||||||
+hash: string,
|
+hash: string,
|
||||||
+name: string,
|
+name: string,
|
||||||
+type: string,
|
+type: string,
|
||||||
|};
|
};
|
||||||
|
|
||||||
|
type ExtendedAssetDescriptor = AssetDescriptor & {
|
||||||
|
+fileSystemLocation: string,
|
||||||
|
+files: Array<string>,
|
||||||
|
};
|
||||||
|
|
||||||
const sizeOf = denodeify(imageSize);
|
const sizeOf = denodeify(imageSize);
|
||||||
|
|
||||||
|
@ -665,11 +668,7 @@ class Bundler {
|
||||||
assetUrlPath = assetUrlPath.replace(/\\/g, '/');
|
assetUrlPath = assetUrlPath.replace(/\\/g, '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test extension against all types supported by image-size module.
|
const isImage = Bundler.isAssetTypeAnImage(extname(module.path).slice(1));
|
||||||
// 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;
|
|
||||||
|
|
||||||
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]);
|
||||||
|
@ -692,13 +691,7 @@ class Bundler {
|
||||||
|
|
||||||
return this._applyAssetPlugins(assetPlugins, asset);
|
return this._applyAssetPlugins(assetPlugins, asset);
|
||||||
}).then(asset => {
|
}).then(asset => {
|
||||||
const json = JSON.stringify(filterObject(asset, assetPropertyBlacklist));
|
const {code, dependencies, dependencyOffsets} = Bundler.generateAssetTransformResult(asset);
|
||||||
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 {
|
return {
|
||||||
asset,
|
asset,
|
||||||
code,
|
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<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: Asset,
|
asset: ExtendedAssetDescriptor,
|
||||||
) {
|
) {
|
||||||
if (!assetPlugins.length) {
|
if (!assetPlugins.length) {
|
||||||
return asset;
|
return asset;
|
||||||
|
|
|
@ -134,3 +134,9 @@ export type TransformedFile = {
|
||||||
transformed: TransformResults,
|
transformed: TransformResults,
|
||||||
type: FileTypes,
|
type: FileTypes,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type LibraryOptions = {|
|
||||||
|
dependencies?: Array<string>,
|
||||||
|
platform?: string,
|
||||||
|
root: string,
|
||||||
|
|};
|
||||||
|
|
Loading…
Reference in New Issue