diff --git a/packages/metro-bundler/src/ModuleGraph/output/__tests__/indexed-ram-bundle-test.js b/packages/metro-bundler/src/ModuleGraph/output/__tests__/indexed-ram-bundle-test.js index df23a65c..ed29da34 100644 --- a/packages/metro-bundler/src/ModuleGraph/output/__tests__/indexed-ram-bundle-test.js +++ b/packages/metro-bundler/src/ModuleGraph/output/__tests__/indexed-ram-bundle-test.js @@ -14,6 +14,7 @@ declare var jest: any; jest.disableAutomock(); const indexedRamBundle = require('../indexed-ram-bundle'); +const {addModuleIdsToModuleWrapper} = require('../util'); declare var describe: any; declare var expect: any; @@ -62,7 +63,7 @@ it('contains the code after the offset table', () => { table.forEach(([offset, length], i) => { const moduleCode = code.slice(codeOffset + offset, codeOffset + offset + length - 1); - expect(moduleCode.toString()).toBe(modules[i].file.code); + expect(moduleCode.toString()).toBe(expectedCode(modules[i])); }); }); @@ -93,7 +94,7 @@ describe('Startup section optimization', () => { const startupSection = code.slice(codeOffset, codeOffset + startupSectionLength - 1); expect(startupSection.toString()) - .toBe(preloaded.concat([requireCall]).map(getCode).join('\n')); + .toBe(preloaded.concat([requireCall]).map(expectedCode).join('\n')); preloaded.forEach(m => { @@ -105,7 +106,7 @@ describe('Startup section optimization', () => { if (offset !== 0 && length !== 0) { const moduleCode = code.slice(codeOffset + offset, codeOffset + offset + length - 1); - expect(moduleCode.toString()).toBe(modules[i].file.code); + expect(moduleCode.toString()).toBe(expectedCode(modules[i])); } }); }); @@ -155,7 +156,7 @@ describe('RAM groups / common sections', () => { const [offset, length] = groupEntry; const groupCode = code.slice(codeOffset + offset, codeOffset + offset + length - 1); expect(groupCode.toString()) - .toEqual(group.map(m => m.file.code).join('\n')); + .toEqual(group.map(expectedCode).join('\n')); }); }); @@ -239,6 +240,13 @@ function makeDependency(name) { }; } +function expectedCode(module) { + const {file} = module; + return file.type === 'module' + ? addModuleIdsToModuleWrapper(module, idForPath) + : file.code; +} + function getId(path) { if (path === requireCall.file.path) { return -1; @@ -251,10 +259,6 @@ function getId(path) { return id; } -function getCode(module) { - return module.file.code; -} - function getPath(module) { return module.file.path; } diff --git a/packages/metro-bundler/src/ModuleGraph/output/indexed-ram-bundle.js b/packages/metro-bundler/src/ModuleGraph/output/indexed-ram-bundle.js index 166738b8..286cc27b 100644 --- a/packages/metro-bundler/src/ModuleGraph/output/indexed-ram-bundle.js +++ b/packages/metro-bundler/src/ModuleGraph/output/indexed-ram-bundle.js @@ -15,7 +15,7 @@ const nullthrows = require('fbjs/lib/nullthrows'); const {createRamBundleGroups} = require('../../Bundler/util'); const {buildTableAndContents, createModuleGroups} = require('../../shared/output/unbundle/as-indexed-file'); -const {concat} = require('./util'); +const {addModuleIdsToModuleWrapper, concat} = require('./util'); import type {FBIndexMap} from '../../lib/SourceMap.js'; import type {OutputFn} from '../types.flow'; @@ -35,7 +35,7 @@ function asIndexedRamBundle({ const moduleGroups = createModuleGroups(ramGroups, deferredModules); const tableAndContents = buildTableAndContents( - startupModules.map(getModuleCode).join('\n'), + startupModules.map(m => getModuleCode(m, idForPath)).join('\n'), deferredModules, moduleGroups, 'utf8', @@ -52,9 +52,10 @@ function asIndexedRamBundle({ }; } -function toModuleTransport({dependencies, file}, idForPath) { +function toModuleTransport(module, idForPath) { + const {dependencies, file} = module; return { - code: file.code, + code: getModuleCode(module, idForPath), dependencies, id: idForPath(file), map: file.map, @@ -63,8 +64,11 @@ function toModuleTransport({dependencies, file}, idForPath) { }; } -function getModuleCode(module) { - return module.file.code; +function getModuleCode(module, idForPath) { + const {file} = module; + return file.type === 'module' + ? addModuleIdsToModuleWrapper(module, idForPath) + : file.code; } function partition(modules, preloadedModules) {