packager: correct transform for assets to avoid string

Reviewed By: davidaurelio

Differential Revision: D4913509

fbshipit-source-id: 9997da819483056d896fedecdc47510ecd381c03
This commit is contained in:
Jean Lauliac 2017-04-20 02:51:21 -07:00 committed by Facebook Github Bot
parent 350b6c6d7f
commit 79d9fd8f83
4 changed files with 20 additions and 11 deletions

View File

@ -126,6 +126,7 @@ export type TransformResults = {[string]: TransformResult};
export type TransformVariants = {[key: string]: Object};
export type TransformedFile = {
assetContent: ?string,
code: string,
file: string,
hasteID: ?string,

View File

@ -33,9 +33,9 @@ describe('wrapWorkerFn:', () => {
const fs = require('fs');
const mkdirp = require('mkdirp');
it('reads the passed-in file synchronously as UTF-8', done => {
it('reads the passed-in file synchronously as buffer', done => {
wrapped(infile, outfile, {}, () => {
expect(fs.readFileSync).toBeCalledWith(infile, 'utf8');
expect(fs.readFileSync).toBeCalledWith(infile);
done();
});
});

View File

@ -39,17 +39,18 @@ const moduleFactoryParameters = ['global', 'require', 'module', 'exports'];
const polyfillFactoryParameters = ['global'];
function transformModule(
code: string,
content: Buffer,
options: TransformOptions,
callback: Callback<TransformedFile>,
): void {
if (options.filename.endsWith('.json')) {
transformJSON(code, options, callback);
if (options.filename.endsWith('.png')) {
transformAsset(content, options, callback);
return;
}
if (options.filename.endsWith('.png')) {
transformAsset(code, options, callback);
const code = content.toString('utf8');
if (options.filename.endsWith('.json')) {
transformJSON(code, options, callback);
return;
}
@ -85,6 +86,7 @@ function transformModule(
const annotations = docblock.parseAsObject(docblock.extract(code));
callback(null, {
assetContent: null,
code,
file: filename,
hasteID: annotations.providesModule || null,
@ -115,6 +117,7 @@ function transformJSON(json, options, callback) {
.forEach(key => (transformed[key] = moduleData));
const result: TransformedFile = {
assetContent: null,
code: json,
file: filename,
hasteID: value.name,
@ -133,9 +136,14 @@ function transformJSON(json, options, callback) {
callback(null, result);
}
function transformAsset(data, options, callback) {
function transformAsset(
content: Buffer,
options: TransformOptions,
callback: Callback<TransformedFile>,
) {
callback(null, {
code: data,
assetContent: content.toString('base64'),
code: '',
file: options.filename,
hasteID: null,
transformed: {},

View File

@ -19,7 +19,7 @@ import type {Callback} from '../types.flow';
type Path = string;
type WorkerFn<Options> = (
fileContents: string,
fileContents: Buffer,
options: Options,
callback: Callback<Object>,
) => void;
@ -39,7 +39,7 @@ function wrapWorkerFn<Options>(
options: Options,
callback: Callback<>,
) => {
const contents = fs.readFileSync(infile, 'utf8');
const contents = fs.readFileSync(infile);
workerFunction(contents, options, (error, result) => {
if (error) {
callback(error);