RN buck: move asset work into transform worker

Reviewed By: davidaurelio

Differential Revision: D5872268

fbshipit-source-id: 829a0d26930f8893b93a5f42ef7026350dc01f29
This commit is contained in:
Jean Lauliac 2017-09-22 07:20:08 -07:00 committed by Facebook Github Bot
parent e736518c43
commit 3b6fddc746
3 changed files with 86 additions and 7 deletions

View File

@ -141,9 +141,46 @@ export type TransformedCodeFile = {
+type: CodeFileTypes,
};
export type ImageSize = {|+width: number, +height: number|};
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,
/**
* 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 =

View File

@ -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};

View File

@ -12,19 +12,24 @@
'use strict';
const AssetPaths = require('../../node-haste/lib/AssetPaths');
const JsFileWrapping = require('./JsFileWrapping');
const Platforms = require('./Platforms');
const collectDependencies = require('./collect-dependencies');
const defaults = require('../../defaults');
const docblock = require('jest-docblock');
const generate = require('./generate');
const getImageSize = require('image-size');
const invariant = require('fbjs/lib/invariant');
const path = require('path');
const {isAssetTypeAnImage} = require('../../Bundler/util');
const {basename} = require('path');
import type {HasteImpl} from '../../node-haste/Module';
import type {
ImageSize,
TransformedCodeFile,
TransformedSourceFile,
Transformer,
@ -147,13 +152,27 @@ function transformAsset<ExtraOptions: {}>(
content: Buffer,
options: TransformOptions<ExtraOptions>,
): TransformedSourceFile {
return {
details: {
assetContentBase64: content.toString('base64'),
filePath: options.filename,
},
type: 'asset',
const filePath = options.filename;
const assetData = AssetPaths.parse(filePath, Platforms.VALID_PLATFORMS);
const contentType = path.extname(filePath).slice(1);
const details = {
assetPath: assetData.assetName,
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) {