Make `ResolveFn` sync

Summary: now that `ResolutionRequest.resolveDependency` is synchronous, we can also make `ResolveFn` synchronous.

Reviewed By: fkgozali

Differential Revision: D5528094

fbshipit-source-id: 0b40df29024b809a99b7e577716b24e9fa499578
This commit is contained in:
David Aurelio 2017-07-31 17:10:15 -07:00 committed by Facebook Github Bot
parent 0272069002
commit 35e5b486c3
3 changed files with 46 additions and 37 deletions

View File

@ -23,7 +23,7 @@ import type {
File, File,
GraphFn, GraphFn,
LoadFn, LoadFn,
ResolveFn, ResolveFnCallback,
} from './types.flow'; } from './types.flow';
type Async$Queue<T, C> = { type Async$Queue<T, C> = {
@ -52,7 +52,7 @@ type LoadQueue =
const NO_OPTIONS = {}; const NO_OPTIONS = {};
exports.create = function create(resolve: ResolveFn, load: LoadFn): GraphFn { exports.create = function create(resolve: ResolveFnCallback, load: LoadFn): GraphFn {
function Graph(entryPoints, platform, options, callback = emptyFunction) { function Graph(entryPoints, platform, options, callback = emptyFunction) {
const { const {
log = (console: any), log = (console: any),

View File

@ -7,20 +7,11 @@
* of patent rights can be found in the PATENTS file in the same directory. * of patent rights can be found in the PATENTS file in the same directory.
* *
* @flow * @flow
* @format
*/ */
'use strict'; 'use strict';
import type { // eslint-disable-line sort-requires
Extensions,
Path,
} from './node-haste.flow';
import type {
ResolveFn,
TransformedCodeFile,
} from '../types.flow';
const AssetResolutionCache = require('../../node-haste/AssetResolutionCache'); const AssetResolutionCache = require('../../node-haste/AssetResolutionCache');
const DependencyGraphHelpers = require('../../node-haste/DependencyGraph/DependencyGraphHelpers'); const DependencyGraphHelpers = require('../../node-haste/DependencyGraph/DependencyGraphHelpers');
const FilesByDirNameIndex = require('../../node-haste/FilesByDirNameIndex'); const FilesByDirNameIndex = require('../../node-haste/FilesByDirNameIndex');
@ -32,9 +23,20 @@ const ResolutionRequest = require('../../node-haste/DependencyGraph/ResolutionRe
const defaults = require('../../defaults'); const defaults = require('../../defaults');
const {ModuleResolver} = require('../../node-haste/DependencyGraph/ModuleResolution'); const {
ModuleResolver,
} = require('../../node-haste/DependencyGraph/ModuleResolution');
import type {Moduleish, Packageish} from '../../node-haste/DependencyGraph/ResolutionRequest'; import type {
Moduleish,
Packageish,
} from '../../node-haste/DependencyGraph/ResolutionRequest';
import type {ResolveFn, TransformedCodeFile} from '../types.flow';
import type {
// eslint-disable-line sort-requires
Extensions,
Path,
} from './node-haste.flow';
type ResolveOptions = {| type ResolveOptions = {|
assetExts: Extensions, assetExts: Extensions,
@ -69,19 +71,24 @@ const nullModule: Moduleish = {
hash() { hash() {
throw new Error('not implemented'); throw new Error('not implemented');
}, },
readCached() { throw new Error('not implemented'); }, readCached() {
readFresh() { return Promise.reject(new Error('not implemented')); }, throw new Error('not implemented');
isHaste() { throw new Error('not implemented'); }, },
getName() { throw new Error('not implemented'); }, readFresh() {
return Promise.reject(new Error('not implemented'));
},
isHaste() {
throw new Error('not implemented');
},
getName() {
throw new Error('not implemented');
},
}; };
exports.createResolveFn = function(options: ResolveOptions): ResolveFn { exports.createResolveFn = function(
const { options: ResolveOptions,
assetExts, ): Promise<ResolveFn> {
extraNodeModules, const {assetExts, extraNodeModules, transformedFiles, sourceExts} = options;
transformedFiles,
sourceExts,
} = options;
const files = Object.keys(transformedFiles); const files = Object.keys(transformedFiles);
function getTransformedFile(path) { function getTransformedFile(path) {
const result = transformedFiles[path]; const result = transformedFiles[path];
@ -131,7 +138,7 @@ exports.createResolveFn = function(options: ResolveOptions): ResolveFn {
sourceExts, sourceExts,
}); });
return (id, source, platform, _, callback) => { return hasteMapBuilt.then(() => (id, source, platform, _, callback) => {
let resolutionRequest = resolutionRequests[platform]; let resolutionRequest = resolutionRequests[platform];
if (!resolutionRequest) { if (!resolutionRequest) {
resolutionRequest = resolutionRequests[platform] = new ResolutionRequest({ resolutionRequest = resolutionRequests[platform] = new ResolutionRequest({
@ -143,15 +150,10 @@ exports.createResolveFn = function(options: ResolveOptions): ResolveFn {
}); });
} }
const from = source != null const from =
? new Module(source, moduleCache, getTransformedFile(source)) source != null
: nullModule; ? new Module(source, moduleCache, getTransformedFile(source))
hasteMapBuilt : nullModule;
.then(() => resolutionRequest.resolveDependency(from, id)) return resolutionRequest.resolveDependency(from, id).path;
.then( });
// nextTick to escape promise error handling
module => process.nextTick(callback, null, module.path),
error => process.nextTick(callback, error),
);
};
}; };

View File

@ -106,6 +106,13 @@ export type ResolveFn = (
source: ?string, source: ?string,
platform: string, platform: string,
options?: ResolveOptions, options?: ResolveOptions,
) => string;
export type ResolveFnCallback = (
id: string,
source: ?string,
platform: string,
options?: ResolveOptions,
callback: Callback<string>, callback: Callback<string>,
) => void; ) => void;