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();
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;
}

View File

@ -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) {