mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: This is a very hacky solution to make reloads from packager faster for simple file changes. Simple means that no dependencies have changed, otherwise packager will abort the attempt to update and fall back to the usual rebuilding method. In principle, this change avoids re-walking and analyzing the whole dependency tree, and just updates modules in existing bundles. Reviewed By: bestander Differential Revision: D3713704 fbshipit-source-id: ba182325c4f4003c0a7402ea87444a94c75ebaf8
112 lines
2.2 KiB
JavaScript
112 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('Expected 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) {
|
|
if (!options.allowUpdates) {
|
|
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;
|
|
}
|
|
|
|
invalidateSource() {
|
|
this._source = null;
|
|
}
|
|
|
|
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;
|