Permalink
Browse files

packager: correct transform for assets to avoid string

Reviewed By: davidaurelio

Differential Revision: D4913509

fbshipit-source-id: 9997da819483056d896fedecdc47510ecd381c03
  • Loading branch information...
jeanlauliac authored and facebook-github-bot committed Apr 20, 2017
1 parent 350b6c6 commit 79d9fd8f83be49f0d367e66b5767cb2872c11387
@@ -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,
@@ -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();
});
});
@@ -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: {},
@@ -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);

0 comments on commit 79d9fd8

Please sign in to comment.