Change the worker response to return an array of outputs

Reviewed By: davidaurelio

Differential Revision: D7877462

fbshipit-source-id: 94dba306c7a7f8611df5e5f59d0147e38ef89f0e
This commit is contained in:
Rafael Oleza 2018-05-11 15:05:08 -07:00 committed by Facebook Github Bot
parent 5c8b86aed5
commit 095426d038
6 changed files with 30 additions and 40 deletions

View File

@ -37,13 +37,12 @@ import type {Ast} from '@babel/core';
import type {Plugins as BabelPlugins} from 'babel-core'; import type {Plugins as BabelPlugins} from 'babel-core';
import type {LogEntry} from 'metro-core/src/Logger'; import type {LogEntry} from 'metro-core/src/Logger';
import type {MetroSourceMapSegmentTuple} from 'metro-source-map'; import type {MetroSourceMapSegmentTuple} from 'metro-source-map';
import type {TransformOutput} from '../DeltaBundler/traverseDependencies';
export type TransformedCode = { export type TransformedCode = {|
code: string, output: TransformOutput,
dependencies: $ReadOnlyArray<TransformResultDependency>, dependencies: $ReadOnlyArray<TransformResultDependency>,
map: Array<MetroSourceMapSegmentTuple>, |};
type: string,
};
export type TransformArgs<ExtraOptions: {}> = {| export type TransformArgs<ExtraOptions: {}> = {|
filename: string, filename: string,
@ -173,7 +172,7 @@ async function transformCode(
} }
return { return {
result: {dependencies: [], code, map, type}, result: {dependencies: [], output: [{data: {code, map}, type}]},
sha1, sha1,
transformFileStartLogEntry, transformFileStartLogEntry,
transformFileEndLogEntry, transformFileEndLogEntry,
@ -289,7 +288,7 @@ async function transformCode(
} }
return { return {
result: {dependencies, code, map, type}, result: {dependencies, output: [{data: {code, map}, type}]},
sha1, sha1,
transformFileStartLogEntry, transformFileStartLogEntry,
transformFileEndLogEntry, transformFileEndLogEntry,

View File

@ -43,14 +43,15 @@ describe('code transformation worker:', () => {
'reject', 'reject',
); );
expect(result.code).toBe( expect(result.output[0].type).toBe('js/script');
expect(result.output[0].data.code).toBe(
[ [
'(function (global) {', '(function (global) {',
' someReallyArbitrary(code);', ' someReallyArbitrary(code);',
'})(this);', '})(this);',
].join('\n'), ].join('\n'),
); );
expect(result.map).toHaveLength(3); expect(result.output[0].data.map).toHaveLength(3);
expect(result.dependencies).toEqual([]); expect(result.dependencies).toEqual([]);
}); });
@ -72,14 +73,15 @@ describe('code transformation worker:', () => {
'reject', 'reject',
); );
expect(result.code).toBe( expect(result.output[0].type).toBe('js/module');
expect(result.output[0].data.code).toBe(
[ [
'__d(function (global, _$$_REQUIRE, module, exports, _dependencyMap) {', '__d(function (global, _$$_REQUIRE, module, exports, _dependencyMap) {',
' arbitrary(code);', ' arbitrary(code);',
'});', '});',
].join('\n'), ].join('\n'),
); );
expect(result.map).toHaveLength(3); expect(result.output[0].data.map).toHaveLength(3);
expect(result.dependencies).toEqual([]); expect(result.dependencies).toEqual([]);
}); });
@ -107,7 +109,8 @@ describe('code transformation worker:', () => {
'reject', 'reject',
); );
expect(result.code).toBe( expect(result.output[0].type).toBe('js/module');
expect(result.output[0].data.code).toBe(
[ [
'__d(function (global, _$$_REQUIRE, module, exports, _dependencyMap) {', '__d(function (global, _$$_REQUIRE, module, exports, _dependencyMap) {',
" 'use strict';", " 'use strict';",
@ -122,7 +125,7 @@ describe('code transformation worker:', () => {
'});', '});',
].join('\n'), ].join('\n'),
); );
expect(result.map).toHaveLength(14); expect(result.output[0].data.map).toHaveLength(14);
expect(result.dependencies).toEqual([ expect(result.dependencies).toEqual([
{isAsync: false, name: './c'}, {isAsync: false, name: './c'},
{isAsync: false, name: './a'}, {isAsync: false, name: './a'},
@ -200,7 +203,7 @@ describe('code transformation worker:', () => {
'minifyModulePath', 'minifyModulePath',
'asyncRequire', 'asyncRequire',
'throwAtRuntime', 'throwAtRuntime',
)).result.code, )).result.output[0].data.code,
).toBe( ).toBe(
[ [
'__d(function (global, _$$_REQUIRE, module, exports, _dependencyMap) {', '__d(function (global, _$$_REQUIRE, module, exports, _dependencyMap) {',
@ -229,7 +232,7 @@ describe('code transformation worker:', () => {
'minifyModulePath', 'minifyModulePath',
'asyncRequire', 'asyncRequire',
'throwAtRuntime', 'throwAtRuntime',
)).result.code, )).result.output[0].data.code,
).toBe( ).toBe(
[ [
'__d(function(global, require, module, exports) {', '__d(function(global, require, module, exports) {',

View File

@ -113,15 +113,7 @@ async function getTransformFn(
getSource() { getSource() {
return result.source; return result.source;
}, },
output: [ output: result.output,
{
data: {
code: result.code,
map: result.map,
},
type: result.type,
},
],
dependencies: result.dependencies, dependencies: result.dependencies,
}; };
}; };

View File

@ -17,17 +17,12 @@ import type {
TransformedCode, TransformedCode,
Options as WorkerOptions, Options as WorkerOptions,
} from '../JSTransformer/worker'; } from '../JSTransformer/worker';
import type {TransformResultDependency} from '../ModuleGraph/types.flow';
import type ModuleCache from './ModuleCache'; import type ModuleCache from './ModuleCache';
import type {LocalPath} from './lib/toLocalPath'; import type {LocalPath} from './lib/toLocalPath';
import type {MetroSourceMapSegmentTuple} from 'metro-source-map';
type ReadResult = { type ReadResult = {
+code: string, ...TransformedCode,
+dependencies: $ReadOnlyArray<TransformResultDependency>,
+map: Array<MetroSourceMapSegmentTuple>,
+source: string, +source: string,
+type: string,
}; };
export type TransformCode = ( export type TransformCode = (
@ -89,10 +84,8 @@ class Module {
const module = this; const module = this;
return { return {
code: result.code,
dependencies: result.dependencies, dependencies: result.dependencies,
map: result.map, output: result.output,
type: result.type,
get source() { get source() {
return module._readSourceCode(); return module._readSourceCode();
}, },

View File

@ -41,13 +41,12 @@ describe('AssetModule:', () => {
localPath: 'image.png', localPath: 'image.png',
moduleCache: new ModuleCache({}), moduleCache: new ModuleCache({}),
transformCode: () => { transformCode: () => {
return Promise.resolve({code: 'module.exports = "asset";'}); return Promise.resolve({output: [{code: 'module.exports = "asset";'}]});
}, },
}); });
const data = await module.read(); const data = await module.read();
expect(data.code).toBe('module.exports = "asset";'); expect(data.output[0].code).toBe('module.exports = "asset";');
expect(data.source).toBe('');
}); });
}); });

View File

@ -23,9 +23,8 @@ describe('Module', () => {
beforeEach(() => { beforeEach(() => {
transformCode = jest.fn().mockReturnValue({ transformCode = jest.fn().mockReturnValue({
code: 'int main(void) { return -1; }',
dependencies: ['stdlib.h', 'conio.h'], dependencies: ['stdlib.h', 'conio.h'],
map: [], output: [{code: 'int main(void) { return -1; }', map: []}],
}); });
moduleCache = new ModuleCache(); moduleCache = new ModuleCache();
@ -54,9 +53,14 @@ describe('Module', () => {
fs.readFileSync.mockReturnValue('original code'); fs.readFileSync.mockReturnValue('original code');
expect(await module.read({})).toEqual({ expect(await module.read({})).toEqual({
code: 'int main(void) { return -1; }',
dependencies: ['stdlib.h', 'conio.h'], dependencies: ['stdlib.h', 'conio.h'],
output: [
{
code: 'int main(void) { return -1; }',
map: [], map: [],
},
],
source: 'original code', source: 'original code',
}); });
}); });