mirror of https://github.com/status-im/metro.git
RN buck: move asset work into transform worker
Reviewed By: davidaurelio Differential Revision: D5872268 fbshipit-source-id: 829a0d26930f8893b93a5f42ef7026350dc01f29
This commit is contained in:
parent
e736518c43
commit
3b6fddc746
|
@ -141,9 +141,46 @@ export type TransformedCodeFile = {
|
||||||
+type: CodeFileTypes,
|
+type: CodeFileTypes,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type ImageSize = {|+width: number, +height: number|};
|
||||||
|
|
||||||
export type AssetFile = {|
|
export type AssetFile = {|
|
||||||
+assetContentBase64: string,
|
/**
|
||||||
|
* The path of the asset that is shared by all potential variants
|
||||||
|
* of this asset. For example `foo/bar@3x.png` would have the
|
||||||
|
* asset path `foo/bar.png`.
|
||||||
|
*/
|
||||||
|
+assetPath: string,
|
||||||
|
/**
|
||||||
|
* The content is encoded in Base64 so that it can be stored in JSON files,
|
||||||
|
* that are used to communicate between different commands of a Buck
|
||||||
|
* build worker, for example.
|
||||||
|
*/
|
||||||
|
+contentBase64: string,
|
||||||
|
/**
|
||||||
|
* Guessed from the file extension, for example `png` or `html`.
|
||||||
|
*/
|
||||||
|
+contentType: string,
|
||||||
|
/**
|
||||||
|
* The path of the original file for this asset. For example
|
||||||
|
* `foo/bar@3x.ios.png`. This is most useful for reporting purposes, such as
|
||||||
|
* error messages.
|
||||||
|
*/
|
||||||
+filePath: string,
|
+filePath: string,
|
||||||
|
/**
|
||||||
|
* If the asset is an image, this contain the size in physical pixels (ie.
|
||||||
|
* regarless of whether it's a `@2x` or `@3x` version of a smaller image).
|
||||||
|
*/
|
||||||
|
+physicalSize: ?ImageSize,
|
||||||
|
/**
|
||||||
|
* The platform this asset is designed for, for example `ios` if the file name
|
||||||
|
* is `foo.ios.js`. `null` if the asset is not platform-specific.
|
||||||
|
*/
|
||||||
|
+platform: ?string,
|
||||||
|
/**
|
||||||
|
* The scale this asset is designed for, for example `2`
|
||||||
|
* if the file name is `foo@2x.png`.
|
||||||
|
*/
|
||||||
|
+scale: number,
|
||||||
|};
|
|};
|
||||||
|
|
||||||
export type TransformedSourceFile =
|
export type TransformedSourceFile =
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2016-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
|
||||||
|
* @format
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* By knowing all the valid platforms, we're able to say that "foo.ios.png" is
|
||||||
|
* effectively the asset "foo" specific to "ios", and not a generic asset
|
||||||
|
* "foo.ios". This is important so that we can discard asset variants that don't
|
||||||
|
* match the platform being built.
|
||||||
|
*/
|
||||||
|
const VALID_PLATFORMS: Set<string> = new Set(['ios', 'android', 'web']);
|
||||||
|
|
||||||
|
module.exports = {VALID_PLATFORMS};
|
|
@ -12,19 +12,24 @@
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const AssetPaths = require('../../node-haste/lib/AssetPaths');
|
||||||
const JsFileWrapping = require('./JsFileWrapping');
|
const JsFileWrapping = require('./JsFileWrapping');
|
||||||
|
const Platforms = require('./Platforms');
|
||||||
|
|
||||||
const collectDependencies = require('./collect-dependencies');
|
const collectDependencies = require('./collect-dependencies');
|
||||||
const defaults = require('../../defaults');
|
const defaults = require('../../defaults');
|
||||||
const docblock = require('jest-docblock');
|
const docblock = require('jest-docblock');
|
||||||
const generate = require('./generate');
|
const generate = require('./generate');
|
||||||
|
const getImageSize = require('image-size');
|
||||||
const invariant = require('fbjs/lib/invariant');
|
const invariant = require('fbjs/lib/invariant');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
|
const {isAssetTypeAnImage} = require('../../Bundler/util');
|
||||||
const {basename} = require('path');
|
const {basename} = require('path');
|
||||||
|
|
||||||
import type {HasteImpl} from '../../node-haste/Module';
|
import type {HasteImpl} from '../../node-haste/Module';
|
||||||
import type {
|
import type {
|
||||||
|
ImageSize,
|
||||||
TransformedCodeFile,
|
TransformedCodeFile,
|
||||||
TransformedSourceFile,
|
TransformedSourceFile,
|
||||||
Transformer,
|
Transformer,
|
||||||
|
@ -147,13 +152,27 @@ function transformAsset<ExtraOptions: {}>(
|
||||||
content: Buffer,
|
content: Buffer,
|
||||||
options: TransformOptions<ExtraOptions>,
|
options: TransformOptions<ExtraOptions>,
|
||||||
): TransformedSourceFile {
|
): TransformedSourceFile {
|
||||||
return {
|
const filePath = options.filename;
|
||||||
details: {
|
const assetData = AssetPaths.parse(filePath, Platforms.VALID_PLATFORMS);
|
||||||
assetContentBase64: content.toString('base64'),
|
const contentType = path.extname(filePath).slice(1);
|
||||||
filePath: options.filename,
|
const details = {
|
||||||
},
|
assetPath: assetData.assetName,
|
||||||
type: 'asset',
|
contentBase64: content.toString('base64'),
|
||||||
|
contentType,
|
||||||
|
filePath,
|
||||||
|
physicalSize: getAssetSize(contentType, content),
|
||||||
|
platform: assetData.platform,
|
||||||
|
scale: assetData.resolution,
|
||||||
};
|
};
|
||||||
|
return {details, type: 'asset'};
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAssetSize(type: string, content: Buffer): ?ImageSize {
|
||||||
|
if (!isAssetTypeAnImage(type)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const {width, height} = getImageSize(content);
|
||||||
|
return {width, height};
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeResult(ast: Ast, filename, sourceCode, isPolyfill = false) {
|
function makeResult(ast: Ast, filename, sourceCode, isPolyfill = false) {
|
||||||
|
|
Loading…
Reference in New Issue