From 62f7f7607e4f03eab4a5bc0b26bd66183627915f Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Fri, 19 May 2017 07:52:22 -0700 Subject: [PATCH] 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 --- .../src/ModuleGraph/output/as-plain-bundle.js | 67 +++++++++++-------- .../src/ModuleGraph/types.flow.js | 9 +-- 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/packages/metro-bundler/src/ModuleGraph/output/as-plain-bundle.js b/packages/metro-bundler/src/ModuleGraph/output/as-plain-bundle.js index dd403e85..b2a2427c 100644 --- a/packages/metro-bundler/src/ModuleGraph/output/as-plain-bundle.js +++ b/packages/metro-bundler/src/ModuleGraph/output/as-plain-bundle.js @@ -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(...iterables: Array>): Iterable { + for (const it of iterables) { + yield* it; + } +} diff --git a/packages/metro-bundler/src/ModuleGraph/types.flow.js b/packages/metro-bundler/src/ModuleGraph/types.flow.js index ed70ff68..da283a7a 100644 --- a/packages/metro-bundler/src/ModuleGraph/types.flow.js +++ b/packages/metro-bundler/src/ModuleGraph/types.flow.js @@ -70,16 +70,17 @@ export type Module = {| file: File, |}; -export type OutputFn = ( - modules: Iterable, +export type OutputFn = ({| filename: string, idForPath: IdForPathFn, + modules: Iterable, + requireCalls: Iterable, sourceMapPath?: string, -) => OutputResult; +|}) => OutputResult; type OutputResult = {| code: string | Buffer, - extraFiles: Iterable<[string, string | Buffer]>, + extraFiles?: Iterable<[string, string | Buffer]>, map: SourceMap, |};