mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: @public Dead-lock trying to read package.json because it's both a "module" and a "package". in `Module.getName` it uses the cache key "name" and tries to call `Package.getName` which uses the same cache key and we end up returning the same promise that we're trying to resolve resulting in a dead-lock. This changes the cache keys for the packages that adds a prefix "package-". Reviewed By: @vjeux Differential Revision: D2506979
94 lines
2.0 KiB
JavaScript
94 lines
2.0 KiB
JavaScript
'use strict';
|
|
|
|
const isAbsolutePath = require('absolute-path');
|
|
const path = require('path');
|
|
|
|
class Package {
|
|
|
|
constructor(file, fastfs, cache) {
|
|
this.path = path.resolve(file);
|
|
this.root = path.dirname(this.path);
|
|
this._fastfs = fastfs;
|
|
this.type = 'Package';
|
|
this._cache = cache;
|
|
}
|
|
|
|
getMain() {
|
|
return this._read().then(json => {
|
|
if (typeof json.browser === 'string') {
|
|
return path.join(this.root, json.browser);
|
|
}
|
|
|
|
let main = json.main || 'index';
|
|
|
|
if (json.browser && typeof json.browser === 'object') {
|
|
main = json.browser[main] ||
|
|
json.browser[main + '.js'] ||
|
|
json.browser[main + '.json'] ||
|
|
json.browser[main.replace(/(\.js|\.json)$/, '')] ||
|
|
main;
|
|
}
|
|
|
|
return path.join(this.root, main);
|
|
});
|
|
}
|
|
|
|
isHaste() {
|
|
return this._cache.get(this.path, 'package-haste', () =>
|
|
this._read().then(json => !!json.name)
|
|
);
|
|
}
|
|
|
|
getName() {
|
|
return this._cache.get(this.path, 'package-name', () =>
|
|
this._read().then(json => json.name)
|
|
);
|
|
}
|
|
|
|
invalidate() {
|
|
this._cache.invalidate(this.path);
|
|
}
|
|
|
|
redirectRequire(name) {
|
|
return this._read().then(json => {
|
|
const {browser} = json;
|
|
|
|
if (!browser || typeof browser !== 'object') {
|
|
return name;
|
|
}
|
|
|
|
if (name[0] !== '/') {
|
|
return browser[name] || name;
|
|
}
|
|
|
|
if (!isAbsolutePath(name)) {
|
|
throw new Error(`Expected ${name} to be absolute path`);
|
|
}
|
|
|
|
const relPath = './' + path.relative(this.root, name);
|
|
const redirect = browser[relPath] ||
|
|
browser[relPath + '.js'] ||
|
|
browser[relPath + '.json'];
|
|
if (redirect) {
|
|
return path.join(
|
|
this.root,
|
|
redirect
|
|
);
|
|
}
|
|
|
|
return name;
|
|
});
|
|
}
|
|
|
|
_read() {
|
|
if (!this._reading) {
|
|
this._reading = this._fastfs.readFile(this.path)
|
|
.then(jsonStr => JSON.parse(jsonStr));
|
|
}
|
|
|
|
return this._reading;
|
|
}
|
|
}
|
|
|
|
module.exports = Package;
|