Fix code generation for indexed RAM bundles

Summary:
Code generation for indexed RAM bundles did not append module ID and dependencies. This fixes that.

Eventually, we only want to have the call to `addModuleIdsToModuleWrapper` in one place.

Reviewed By: jeanlauliac

Differential Revision: D5129255

fbshipit-source-id: 7f6148dd607bbf7c97e9df7936a07bde3f05b3aa
This commit is contained in:
David Aurelio 2017-05-25 10:11:19 -07:00 committed by Facebook Github Bot
parent 683c8ddad8
commit 6ac824264d
2 changed files with 22 additions and 14 deletions

View File

@ -14,6 +14,7 @@ declare var jest: any;
jest.disableAutomock(); jest.disableAutomock();
const indexedRamBundle = require('../indexed-ram-bundle'); const indexedRamBundle = require('../indexed-ram-bundle');
const {addModuleIdsToModuleWrapper} = require('../util');
declare var describe: any; declare var describe: any;
declare var expect: any; declare var expect: any;
@ -62,7 +63,7 @@ it('contains the code after the offset table', () => {
table.forEach(([offset, length], i) => { table.forEach(([offset, length], i) => {
const moduleCode = const moduleCode =
code.slice(codeOffset + offset, codeOffset + offset + length - 1); 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 = const startupSection =
code.slice(codeOffset, codeOffset + startupSectionLength - 1); code.slice(codeOffset, codeOffset + startupSectionLength - 1);
expect(startupSection.toString()) expect(startupSection.toString())
.toBe(preloaded.concat([requireCall]).map(getCode).join('\n')); .toBe(preloaded.concat([requireCall]).map(expectedCode).join('\n'));
preloaded.forEach(m => { preloaded.forEach(m => {
@ -105,7 +106,7 @@ describe('Startup section optimization', () => {
if (offset !== 0 && length !== 0) { if (offset !== 0 && length !== 0) {
const moduleCode = const moduleCode =
code.slice(codeOffset + offset, codeOffset + offset + length - 1); 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 [offset, length] = groupEntry;
const groupCode = code.slice(codeOffset + offset, codeOffset + offset + length - 1); const groupCode = code.slice(codeOffset + offset, codeOffset + offset + length - 1);
expect(groupCode.toString()) 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) { function getId(path) {
if (path === requireCall.file.path) { if (path === requireCall.file.path) {
return -1; return -1;
@ -251,10 +259,6 @@ function getId(path) {
return id; return id;
} }
function getCode(module) {
return module.file.code;
}
function getPath(module) { function getPath(module) {
return module.file.path; return module.file.path;
} }

View File

@ -15,7 +15,7 @@ const nullthrows = require('fbjs/lib/nullthrows');
const {createRamBundleGroups} = require('../../Bundler/util'); const {createRamBundleGroups} = require('../../Bundler/util');
const {buildTableAndContents, createModuleGroups} = require('../../shared/output/unbundle/as-indexed-file'); 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 {FBIndexMap} from '../../lib/SourceMap.js';
import type {OutputFn} from '../types.flow'; import type {OutputFn} from '../types.flow';
@ -35,7 +35,7 @@ function asIndexedRamBundle({
const moduleGroups = createModuleGroups(ramGroups, deferredModules); const moduleGroups = createModuleGroups(ramGroups, deferredModules);
const tableAndContents = buildTableAndContents( const tableAndContents = buildTableAndContents(
startupModules.map(getModuleCode).join('\n'), startupModules.map(m => getModuleCode(m, idForPath)).join('\n'),
deferredModules, deferredModules,
moduleGroups, moduleGroups,
'utf8', 'utf8',
@ -52,9 +52,10 @@ function asIndexedRamBundle({
}; };
} }
function toModuleTransport({dependencies, file}, idForPath) { function toModuleTransport(module, idForPath) {
const {dependencies, file} = module;
return { return {
code: file.code, code: getModuleCode(module, idForPath),
dependencies, dependencies,
id: idForPath(file), id: idForPath(file),
map: file.map, map: file.map,
@ -63,8 +64,11 @@ function toModuleTransport({dependencies, file}, idForPath) {
}; };
} }
function getModuleCode(module) { function getModuleCode(module, idForPath) {
return module.file.code; const {file} = module;
return file.type === 'module'
? addModuleIdsToModuleWrapper(module, idForPath)
: file.code;
} }
function partition(modules, preloadedModules) { function partition(modules, preloadedModules) {