mirror of https://github.com/status-im/metro.git
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:
parent
a3b6ed81ed
commit
c00e669cf5
|
@ -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;
|
||||||
|
}
|
|
@ -58,3 +58,12 @@ exports.createIdForPathFn = (): ({path: string} => number) => {
|
||||||
return id;
|
return id;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.virtualModule = (code: string): Module => ({
|
||||||
|
dependencies: [],
|
||||||
|
file: {
|
||||||
|
code,
|
||||||
|
path: '',
|
||||||
|
type: 'script',
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
|
@ -13,8 +13,8 @@
|
||||||
import type {Console} from 'console';
|
import type {Console} from 'console';
|
||||||
|
|
||||||
export type Callback<A = void, B = void>
|
export type Callback<A = void, B = void>
|
||||||
= ((error: Error) => mixed)
|
= (Error => mixed)
|
||||||
& ((error: null | void, a: A, b: B) => mixed);
|
& ((null | void, A, B) => mixed);
|
||||||
|
|
||||||
type ResolveOptions = {
|
type ResolveOptions = {
|
||||||
log?: Console,
|
log?: Console,
|
||||||
|
@ -59,7 +59,7 @@ export type GraphFn = (
|
||||||
callback?: Callback<GraphResult>,
|
callback?: Callback<GraphResult>,
|
||||||
) => void;
|
) => void;
|
||||||
|
|
||||||
type GraphResult = {
|
export type GraphResult = {
|
||||||
entryModules: Array<Module>,
|
entryModules: Array<Module>,
|
||||||
modules: Array<Module>,
|
modules: Array<Module>,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue