Separate build modules from require calls when bundling

Summary: separates modules of the dependency graph from the generated require calls to kick of the app. This is required to make RAM bundle generation work properly'

Reviewed By: jeanlauliac

Differential Revision: D5094635

fbshipit-source-id: fca69a3e2d9b030cdc4d4405c2b5e795b0d55f87
This commit is contained in:
David Aurelio 2017-05-19 07:52:22 -07:00 committed by Facebook Github Bot
parent 8d60605a90
commit 3f176008e8
2 changed files with 45 additions and 31 deletions

View File

@ -17,41 +17,54 @@ const {addModuleIdsToModuleWrapper} = require('./util');
import type {OutputFn} from '../types.flow';
module.exports = (
(modules, filename, idForPath, sourceMapPath) => {
let code = '';
let line = 0;
const sections = [];
function asPlainBundle({
filename,
idForPath,
modules,
requireCalls,
sourceMapPath,
}) {
let code = '';
let line = 0;
const sections = [];
for (const module of modules) {
const {file} = module;
const moduleCode = file.type === 'module'
? addModuleIdsToModuleWrapper(module, idForPath)
: file.code;
for (const module of concat(modules, requireCalls)) {
const {file} = module;
const moduleCode = file.type === 'module'
? addModuleIdsToModuleWrapper(module, idForPath)
: file.code;
code += moduleCode + '\n';
if (file.map) {
sections.push({
map: file.map,
offset: {column: 0, line},
});
}
line += countLines(moduleCode);
code += moduleCode + '\n';
if (file.map) {
sections.push({
map: file.map,
offset: {column: 0, line},
});
}
line += countLines(moduleCode);
}
if (sourceMapPath) {
code += `/*# sourceMappingURL=${sourceMapPath}*/`;
}
if (sourceMapPath) {
code += `/*# sourceMappingURL=${sourceMapPath}*/`;
}
return {
code,
extraFiles: [[`${filename}.meta`, meta(code)]],
map: createIndexMap({file: filename, sections}),
};
}: OutputFn);
return {
code,
extraFiles: [[`${filename}.meta`, meta(code)]],
map: createIndexMap({file: filename, sections}),
};
}
module.exports = (asPlainBundle: OutputFn);
const reLine = /^/gm;
function countLines(string: string): number {
//$FlowFixMe This regular expression always matches
return string.match(reLine).length;
}
function* concat<T>(...iterables: Array<Iterable<T>>): Iterable<T> {
for (const it of iterables) {
yield* it;
}
}

View File

@ -70,16 +70,17 @@ export type Module = {|
file: File,
|};
export type OutputFn = (
modules: Iterable<Module>,
export type OutputFn = ({|
filename: string,
idForPath: IdForPathFn,
modules: Iterable<Module>,
requireCalls: Iterable<Module>,
sourceMapPath?: string,
) => OutputResult;
|}) => OutputResult;
type OutputResult = {|
code: string | Buffer,
extraFiles: Iterable<[string, string | Buffer]>,
extraFiles?: Iterable<[string, string | Buffer]>,
map: SourceMap,
|};