161 lines
3.7 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) 2013-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.
*
* @flow
*/
'use strict';
Kill fastfs Summary: This kills fastfs in favor of Jest's hasteFS. It gets rid of a ton of code, including the mocking code in ResolutionRequest which we don't need any more. Next step after this is to rewrite HasteMap, ModuleCache, Module/Package. We are getting closer to a nicer and faster world! :) Here is what I did: * Use Jest's HasteFS instead of fastfs. A fresh instance is received every time something changes on the FS. * HasteFS is not shared with everything any more. Only one reference is kept in DependencyGraph and there are a few smaller functions that are passed around (getClosestPackage and dirExists). Note: `dirExists` now does fs access instead of an offline check. This sucks but stat calls aren't slow and aren't going to be a bottleneck in ResolutionRequest, I promise! When it is time to tackle a ResolutionRequest rewrite with jest-resolve, this will go away. "It gets worse before it gets better" :) The ModuleGraph equivalent does *not* do fs access and retains the previous way of doing things because we shouldn't do online fs access there. * Add flow annotations to ResolutionRequest. This required a few tiny hacks for now because of ModuleGraph's duck typing. I'll get rid of this soon. * Updated ModuleGraph to work with the new code, also created a mock HasteFS instance there. * I fixed a few tiny mock issues for `fs` to make the tests work; I had to add one tiny little internal update to `dgraph._hasteFS._files` because the file watching in the tests isn't real. It is instrumented through some function calls, therefore the hasteFS instance doesn't get automatically updated. One way to solve this is to add `JestHasteMap.emit('change', …)` for testing but I didn't want to cut a Jest release just for that. #movefast (Note: I will likely land this in 1.5 weeks from now after my vacation and I have yet to fully test all the product flows. Please give me feedback so I can make sure this is solid!) Reviewed By: davidaurelio Differential Revision: D4204082 fbshipit-source-id: d6dc9fcb77ac224df4554a59f0fce241c01b0512
2016-11-30 04:15:32 -08:00
const fs = require('fs');
const isAbsolutePath = require('absolute-path');
const path = require('path');
type PackageContent = {
name: string,
'react-native': mixed,
browser: mixed,
main: ?string,
};
class Package {
path: string;
root: string;
type: string;
_content: ?PackageContent;
constructor({file}: {
file: string,
}) {
this.path = path.resolve(file);
this.root = path.dirname(this.path);
this.type = 'Package';
this._content = null;
}
getMain(): string {
const json = this.read();
var replacements = getReplacements(json);
if (typeof replacements === 'string') {
return path.join(this.root, replacements);
}
let main = json.main || 'index';
if (replacements && typeof replacements === 'object') {
main = replacements[main] ||
replacements[main + '.js'] ||
replacements[main + '.json'] ||
replacements[main.replace(/(\.js|\.json)$/, '')] ||
main;
}
/* $FlowFixMe: `getReplacements` doesn't validate the return value. */
return path.join(this.root, main);
}
isHaste(): Promise<boolean> {
return Promise.resolve().then(() => !!this.read().name);
}
getName(): Promise<string> {
return Promise.resolve().then(() => this.read().name);
}
invalidate() {
this._content = null;
}
redirectRequire(name: string): string | false {
const json = this.read();
const replacements = getReplacements(json);
if (!replacements || typeof replacements !== 'object') {
return name;
}
if (!isAbsolutePath(name)) {
const replacement = replacements[name];
// support exclude with "someDependency": false
return replacement === false
? false
/* $FlowFixMe: type of replacements is not being validated */
: replacement || name;
}
let relPath = './' + path.relative(this.root, name);
if (path.sep !== '/') {
relPath = relPath.replace(new RegExp('\\' + path.sep, 'g'), '/');
}
let redirect = replacements[relPath];
// false is a valid value
if (redirect == null) {
redirect = replacements[relPath + '.js'];
if (redirect == null) {
redirect = replacements[relPath + '.json'];
}
}
// support exclude with "./someFile": false
if (redirect === false) {
return false;
}
if (redirect) {
return path.join(
this.root,
/* $FlowFixMe: `getReplacements` doesn't validate the return value. */
redirect
);
}
return name;
}
read(): PackageContent {
if (this._content == null) {
this._content = JSON.parse(fs.readFileSync(this.path, 'utf8'));
}
return this._content;
}
}
function getReplacements(pkg: PackageContent): mixed {
let rn = pkg['react-native'];
let browser = pkg.browser;
if (rn == null) {
return browser;
}
if (browser == null) {
return rn;
}
if (typeof rn === 'string') {
/* $FlowFixMe: It is likely unsafe to assume all packages would
* contain a "main" */
rn = {[pkg.main]: rn};
}
if (typeof browser === 'string') {
/* $FlowFixMe: It is likely unsafe to assume all packages would
* contain a "main" */
browser = {[pkg.main]: browser};
}
// merge with "browser" as default,
// "react-native" as override
// $FlowFixMe(>=0.35.0) browser and rn should be objects
return {...browser, ...rn};
}
module.exports = Package;