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 65cb03c22f
commit b8e9ed1559
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 TransformVariants = {[key: string]: Object};
export type TransformedFile = { export type TransformedFile = {
assetContent: ?string,
code: string, code: string,
file: string, file: string,
hasteID: ?string, hasteID: ?string,

View File

@ -33,9 +33,9 @@ describe('wrapWorkerFn:', () => {
const fs = require('fs'); const fs = require('fs');
const mkdirp = require('mkdirp'); 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, {}, () => { wrapped(infile, outfile, {}, () => {
expect(fs.readFileSync).toBeCalledWith(infile, 'utf8'); expect(fs.readFileSync).toBeCalledWith(infile);
done(); done();
}); });
}); });

View File

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

View File

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