mirror of https://github.com/status-im/metro.git
metro-bundler/src/ModuleGraph: throw on empty asset images
Reviewed By: fkgozali Differential Revision: D6030788 fbshipit-source-id: 000757096f7af68c077e80829f48d52814752599
This commit is contained in:
parent
9bb438ed1d
commit
0608332cb9
|
@ -0,0 +1,3 @@
|
|||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`transforming JS modules: assets throws on empty images 1`] = `"Image asset \`foo.png\` cannot be an empty file."`;
|
|
@ -210,6 +210,14 @@ describe('transforming JS modules:', () => {
|
|||
});
|
||||
expect(result.details.package).toEqual(pkg);
|
||||
});
|
||||
|
||||
describe('assets', () => {
|
||||
it('throws on empty images', () => {
|
||||
expect(() =>
|
||||
transformModule(new Buffer(0), {...options(), filename: 'foo.png'}),
|
||||
).toThrowErrorMatchingSnapshot();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function createTestData() {
|
||||
|
|
|
@ -64,7 +64,7 @@ function transformModule(
|
|||
options: TransformOptions<{+retainLines?: boolean}>,
|
||||
): TransformedSourceFile {
|
||||
if (ASSET_EXTENSIONS.has(path.extname(options.filename).substr(1))) {
|
||||
return transformAsset(content, options);
|
||||
return transformAsset(content, options.filename);
|
||||
}
|
||||
|
||||
const code = content.toString('utf8');
|
||||
|
@ -148,11 +148,10 @@ function transformJSON(json, options): TransformedSourceFile {
|
|||
return {type: 'code', details: result};
|
||||
}
|
||||
|
||||
function transformAsset<ExtraOptions: {}>(
|
||||
function transformAsset(
|
||||
content: Buffer,
|
||||
options: TransformOptions<ExtraOptions>,
|
||||
filePath: string,
|
||||
): TransformedSourceFile {
|
||||
const filePath = options.filename;
|
||||
const assetData = AssetPaths.parse(filePath, Platforms.VALID_PLATFORMS);
|
||||
const contentType = path.extname(filePath).slice(1);
|
||||
const details = {
|
||||
|
@ -160,17 +159,24 @@ function transformAsset<ExtraOptions: {}>(
|
|||
contentBase64: content.toString('base64'),
|
||||
contentType,
|
||||
filePath,
|
||||
physicalSize: getAssetSize(contentType, content),
|
||||
physicalSize: getAssetSize(contentType, content, filePath),
|
||||
platform: assetData.platform,
|
||||
scale: assetData.resolution,
|
||||
};
|
||||
return {details, type: 'asset'};
|
||||
}
|
||||
|
||||
function getAssetSize(type: string, content: Buffer): ?ImageSize {
|
||||
function getAssetSize(
|
||||
type: string,
|
||||
content: Buffer,
|
||||
filePath: string,
|
||||
): ?ImageSize {
|
||||
if (!isAssetTypeAnImage(type)) {
|
||||
return null;
|
||||
}
|
||||
if (content.length === 0) {
|
||||
throw new Error(`Image asset \`${filePath}\` cannot be an empty file.`);
|
||||
}
|
||||
const {width, height} = getImageSize(content);
|
||||
return {width, height};
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue