Files
react-native/packager/react-packager/src/Bundler/BundleBase.js
T
Martín Bigio 8edc35004c Move preloaded modules to startup code section
Summary:We found that moving the preloaded modules to the startup section of the RAM Bundle improves TTI quite a bit by saving lots of through the bridge calls and injecting multiple modules at once on JSC. However, doing this on a non hacky way required a lot of work. The main changes this diff does are:
  - Add to `BundleBase` additional bundling options. This options are fetched based on the entry file we're building by invoking a module that exports a function (`getBundleOptionsModulePath`).
  - Implement `BundleOptions` module to include the `numPreloadedModules` attribute as a bundle additional option. This value is computed by getting the dependencies the entry file has and looking for the first module that exports a module we don't want to preload. The `numPreloadedModules` attribute is then used to decide where to splice the array of modules.
- Additional kung fu to make sure sourcemaps work for both preloaded and non preloaded modules.

Reviewed By: davidaurelio

Differential Revision: D3046534

fb-gh-sync-id: 80b676222ca3bb8b9eecc912a7963be94d3dee1a
shipit-source-id: 80b676222ca3bb8b9eecc912a7963be94d3dee1a
2016-03-23 09:28:31 -07:00

106 lines
2.2 KiB
JavaScript

/**
* Copyright (c) 2015-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.
*/
'use strict';
const ModuleTransport = require('../lib/ModuleTransport');
class BundleBase {
constructor() {
this._finalized = false;
this._modules = [];
this._assets = [];
this._mainModuleId = undefined;
}
isEmpty() {
return this._modules.length === 0 && this._assets.length === 0;
}
getMainModuleId() {
return this._mainModuleId;
}
setMainModuleId(moduleId) {
this._mainModuleId = moduleId;
}
addModule(module) {
if (!(module instanceof ModuleTransport)) {
throw new Error('Expeceted a ModuleTransport object');
}
return this._modules.push(module) - 1;
}
replaceModuleAt(index, module) {
if (!(module instanceof ModuleTransport)) {
throw new Error('Expeceted a ModuleTransport object');
}
this._modules[index] = module;
}
getModules() {
return this._modules;
}
getAssets() {
return this._assets;
}
addAsset(asset) {
this._assets.push(asset);
}
finalize(options) {
Object.freeze(this._modules);
Object.freeze(this._assets);
this._finalized = true;
}
getSource(options) {
this.assertFinalized();
if (this._source) {
return this._source;
}
this._source = this._modules.map((module) => module.code).join('\n');
return this._source;
}
assertFinalized(message) {
if (!this._finalized) {
throw new Error(message || 'Bundle needs to be finalized before getting any source');
}
}
toJSON() {
return {
modules: this._modules,
assets: this._assets,
mainModuleId: this.getMainModuleId(),
};
}
static fromJSON(bundle, json) {
bundle._assets = json.assets;
bundle._modules = json.modules;
bundle.setMainModuleId(json.mainModuleId);
Object.freeze(bundle._modules);
Object.freeze(bundle._assets);
bundle._finalized = true;
}
}
module.exports = BundleBase;