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

View File

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

View File

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

View File

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

View File

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

View File

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