diff --git a/packages/metro/src/JSTransformer/worker.js b/packages/metro/src/JSTransformer/worker.js index b4feee98..b6f2d05c 100644 --- a/packages/metro/src/JSTransformer/worker.js +++ b/packages/metro/src/JSTransformer/worker.js @@ -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, - map: Array, - type: string, -}; +|}; export type TransformArgs = {| 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, diff --git a/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js b/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js index d1a4caae..3c81636f 100644 --- a/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js +++ b/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js @@ -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) {', diff --git a/packages/metro/src/lib/transformHelpers.js b/packages/metro/src/lib/transformHelpers.js index a04cbd28..02351d19 100644 --- a/packages/metro/src/lib/transformHelpers.js +++ b/packages/metro/src/lib/transformHelpers.js @@ -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, }; }; diff --git a/packages/metro/src/node-haste/Module.js b/packages/metro/src/node-haste/Module.js index 4bf2ceb0..f4b02e1f 100644 --- a/packages/metro/src/node-haste/Module.js +++ b/packages/metro/src/node-haste/Module.js @@ -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, - +map: Array, + ...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(); }, diff --git a/packages/metro/src/node-haste/__tests__/AssetModule-test.js b/packages/metro/src/node-haste/__tests__/AssetModule-test.js index 16e4fc04..b77c5c2d 100644 --- a/packages/metro/src/node-haste/__tests__/AssetModule-test.js +++ b/packages/metro/src/node-haste/__tests__/AssetModule-test.js @@ -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";'); }); }); diff --git a/packages/metro/src/node-haste/__tests__/Module-test.js b/packages/metro/src/node-haste/__tests__/Module-test.js index 122c8f59..199a822b 100644 --- a/packages/metro/src/node-haste/__tests__/Module-test.js +++ b/packages/metro/src/node-haste/__tests__/Module-test.js @@ -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'], - map: [], + output: [ + { + code: 'int main(void) { return -1; }', + + map: [], + }, + ], source: 'original code', }); });