Add bundle building logic

Summary: Adds the necessary functionality to serialize a series of `Module`s to a bundle and a source map in the context of the new Buck/Packager integration

Reviewed By: cpojer

Differential Revision: D4265867

fbshipit-source-id: f2d3e4ffed64a3dca817101faad7bced9f16edc7
This commit is contained in:
David Aurelio 2016-12-05 16:40:29 -08:00 committed by Facebook Github Bot
parent 27e818fa69
commit 2ab5d2809f
3 changed files with 63 additions and 3 deletions

View File

@ -0,0 +1,51 @@
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';
const {createIndexMap} = require('./source-map');
const {addModuleIdsToModuleWrapper} = require('./util');
import type {Module} from '../types.flow';
import type {SourceMap} from './source-map';
module.exports = (
modules: Iterable<Module>,
filename?: string,
idForPath: {path: string} => number,
): {code: string, map: SourceMap} => {
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;
code += moduleCode + '\n';
if (file.map) {
sections.push({
map: file.map,
offset: {column: 0, line}
});
}
line += countLines(moduleCode);
}
return {code, map: createIndexMap({file: filename, sections})};
};
const reLine = /^/gm;
function countLines(string: string): number {
//$FlowFixMe This regular expression always matches
return string.match(reLine).length;
}

View File

@ -58,3 +58,12 @@ exports.createIdForPathFn = (): ({path: string} => number) => {
return id;
};
};
exports.virtualModule = (code: string): Module => ({
dependencies: [],
file: {
code,
path: '',
type: 'script',
}
});

View File

@ -13,8 +13,8 @@
import type {Console} from 'console';
export type Callback<A = void, B = void>
= ((error: Error) => mixed)
& ((error: null | void, a: A, b: B) => mixed);
= (Error => mixed)
& ((null | void, A, B) => mixed);
type ResolveOptions = {
log?: Console,
@ -59,7 +59,7 @@ export type GraphFn = (
callback?: Callback<GraphResult>,
) => void;
type GraphResult = {
export type GraphResult = {
entryModules: Array<Module>,
modules: Array<Module>,
};