From 2a77da76ae6fddba3887f4b39f1d5394355f9dbe Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Fri, 29 Apr 2016 10:15:26 -0700 Subject: [PATCH] Unify source map approach for RA bundles on iOS/Android Reviewed By: javache Differential Revision: D3229780 fb-gh-sync-id: a3148d7626b32a2e6803ae8c35ac75025a992a32 fbshipit-source-id: a3148d7626b32a2e6803ae8c35ac75025a992a32 --- react-packager/src/Bundler/Bundle.js | 71 +++++++++------------------- react-packager/src/Bundler/index.js | 9 ++-- 2 files changed, 26 insertions(+), 54 deletions(-) diff --git a/react-packager/src/Bundler/Bundle.js b/react-packager/src/Bundler/Bundle.js index a2edd080..4a172d10 100644 --- a/react-packager/src/Bundler/Bundle.js +++ b/react-packager/src/Bundler/Bundle.js @@ -107,61 +107,23 @@ class Bundle extends BundleBase { } getUnbundle(type) { - if (this._ramBundle) { - return this._ramBundle; - } + this.assertFinalized(); + if (!this._ramBundle) { + const modules = this.getModules().slice(); - switch (type) { - case 'INDEX': - this._ramBundle = this._getAsIndexedFileUnbundle(); - break; - case 'ASSETS': - this._ramBundle = this._getAsAssetsUnbundle(); - break; - default: - throw new Error('Unkown RAM Bundle type:', type); + // separate modules we need to preload from the ones we don't + const [startupModules, lazyModules] = partition(modules, shouldPreload); + + this._ramBundle = { + startupModules, + lazyModules, + allModules: modules, + }; } return this._ramBundle; } - _getAsIndexedFileUnbundle() { - const modules = this.getModules(); - - // separate modules we need to preload from the ones we don't - const shouldPreload = (module) => module.meta && module.meta.preloaded; - const preloaded = modules.filter(module => shouldPreload(module)); - const notPreloaded = modules.filter(module => !shouldPreload(module)); - - // code that will be executed on bridge start up - const startupCode = preloaded.map(({code}) => code).join('\n'); - - return { - // include copy of all modules on the order they're writen on the bundle: - // polyfills, preloaded, additional requires, non preloaded - allModules: preloaded.concat(notPreloaded), - startupCode, // no entries on the index for these modules, only the code - modules: notPreloaded, // we include both the code and entries on the index - }; - } - - _getAsAssetsUnbundle() { - const allModules = this.getModules().slice(); - const prependedModules = this._numPrependedModules; - const requireCalls = this._numRequireCalls; - - const modules = - allModules - .splice(prependedModules, allModules.length - requireCalls - prependedModules); - const startupCode = allModules.map(({code}) => code).join('\n'); - - return { - startupCode, - startupModules: allModules, - modules, - }; - } - /** * Combine each of the sourcemaps multiple modules have into a single big * one. This works well thanks to a neat trick defined on the sourcemap spec @@ -351,4 +313,15 @@ function generateSourceMapForVirtualModule(module) { }; } +function shouldPreload({meta}) { + return meta && meta.preloaded; +} + +function partition(array, predicate) { + const included = []; + const excluded = []; + array.forEach(item => (predicate(item) ? included : excluded).push(item)); + return [included, excluded]; +} + module.exports = Bundle; diff --git a/react-packager/src/Bundler/index.js b/react-packager/src/Bundler/index.js index c718bb40..2c02809e 100644 --- a/react-packager/src/Bundler/index.js +++ b/react-packager/src/Bundler/index.js @@ -556,12 +556,11 @@ class Bundler { ]).then(( [name, {code, dependencies, dependencyOffsets, map, source}] ) => { + const {preloadedModules} = transformOptions.transform; const preloaded = module.path === entryFilePath || - module.isPolyfill() || ( - transformOptions.transform.preloadedModules && - transformOptions.transform.preloadedModules.hasOwnProperty(module.path) - ); + module.isPolyfill() || + preloadedModules && preloadedModules.hasOwnProperty(module.path); return new ModuleTransport({ name, @@ -571,7 +570,7 @@ class Bundler { meta: {dependencies, dependencyOffsets, preloaded}, sourceCode: source, sourcePath: module.path - }) + }); }); }