mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: public Since the combination of node and haste modules (and modules that can be required as both node and haste module) can lead to situations where it’s impossible to decide an unambiguous module identifier, this diff switches all module ids to integers. Each integer maps to an absolute path to a JS file on disk. We also had a problem, where haste modules outside and inside node_modules could end up with the same module identifier. This problem has not manifested yet, because the last definition of a module wins. It becomes a problem when writing file-based unbundle modules to disk: the same file might be written to concurrently, leading to invalid code. Using indexed modules will also help indexed file unbundles, as we can encode module IDs as integers rather than scanning string IDs. Reviewed By: martinbigio Differential Revision: D2842418 fb-gh-sync-id: 97addd28e964ac5f2b5081dcd3f36124d2864df8
86 lines
1.9 KiB
JavaScript
86 lines
1.9 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';
|
|
|
|
class ResolutionResponse {
|
|
constructor() {
|
|
this.dependencies = [];
|
|
this.asyncDependencies = [];
|
|
this.mainModuleId = null;
|
|
this.mocks = null;
|
|
this.numPrependedDependencies = 0;
|
|
this._mappings = Object.create(null);
|
|
this._finalized = false;
|
|
}
|
|
|
|
_assertNotFinalized() {
|
|
if (this._finalized) {
|
|
throw new Error('Attempted to mutate finalized response.');
|
|
}
|
|
}
|
|
|
|
_assertFinalized() {
|
|
if (!this._finalized) {
|
|
throw new Error('Attempted to access unfinalized response.');
|
|
}
|
|
}
|
|
|
|
finalize() {
|
|
return this._mainModule.getName().then(id => {
|
|
this.mainModuleId = id;
|
|
this._finalized = true;
|
|
return this;
|
|
});
|
|
}
|
|
|
|
getMainModule() {
|
|
this._assertFinalized();
|
|
return this._mainModule;
|
|
}
|
|
|
|
pushDependency(module) {
|
|
this._assertNotFinalized();
|
|
if (this.dependencies.length === 0) {
|
|
this._mainModule = module;
|
|
}
|
|
|
|
this.dependencies.push(module);
|
|
}
|
|
|
|
prependDependency(module) {
|
|
this._assertNotFinalized();
|
|
this.dependencies.unshift(module);
|
|
this.numPrependedDependencies += 1;
|
|
}
|
|
|
|
pushAsyncDependency(dependency) {
|
|
this._assertNotFinalized();
|
|
this.asyncDependencies.push(dependency);
|
|
}
|
|
|
|
setResolvedDependencyPairs(module, pairs) {
|
|
this._assertNotFinalized();
|
|
const hash = module.hash();
|
|
if (this._mappings[hash] == null) {
|
|
this._mappings[hash] = pairs;
|
|
}
|
|
}
|
|
|
|
setMocks(mocks) {
|
|
this.mocks = mocks;
|
|
}
|
|
|
|
getResolvedDependencyPairs(module) {
|
|
this._assertFinalized();
|
|
return this._mappings[module.hash()];
|
|
}
|
|
}
|
|
|
|
module.exports = ResolutionResponse;
|