Summary:
Similar to https://github.com/facebook/jest/pull/2877, this introduces an optional config `HasteImpl` of type `{getHasteName(filePath: string): (string|void)}` that returns the haste name for a module at filePath if it is a haste module or undefined otherwise.

This allows us to inject a custom implementation of haste's module id resolution rather than only relying on `providesModule` annotations

Reviewed By: davidaurelio

Differential Revision: D4589372

fbshipit-source-id: 4d1983dfbf09c9d67faf725e86ae86ab42433b7d
This commit is contained in:
Bhuwan Khattar 2017-02-27 10:48:17 -08:00 коммит произвёл Facebook Github Bot
Родитель 4ba983401f
Коммит 234f4f538d
9 изменённых файлов: 75 добавлений и 18 удалений

Просмотреть файл

@ -76,6 +76,7 @@ function buildBundle(
extraNodeModules: config.extraNodeModules, extraNodeModules: config.extraNodeModules,
getTransformOptions: config.getTransformOptions, getTransformOptions: config.getTransformOptions,
globalTransformCache: null, globalTransformCache: null,
hasteImpl: config.hasteImpl,
platforms: defaultPlatforms.concat(platforms), platforms: defaultPlatforms.concat(platforms),
projectRoots: config.getProjectRoots(), projectRoots: config.getProjectRoots(),
providesModuleNodeModules: providesModuleNodeModules, providesModuleNodeModules: providesModuleNodeModules,

Просмотреть файл

@ -16,6 +16,7 @@ const defaultConfig = require('./default.config');
const minimist = require('minimist'); const minimist = require('minimist');
import type {GetTransformOptions} from '../../packager/src/Bundler'; import type {GetTransformOptions} from '../../packager/src/Bundler';
import type {HasteImpl} from '../../packager/src/node-haste/Module';
import type {CommandT} from '../commands'; import type {CommandT} from '../commands';
/** /**
@ -66,6 +67,13 @@ export type ConfigT = {
* Returns dependency config from <node_modules>/packageName * Returns dependency config from <node_modules>/packageName
*/ */
getDependencyConfig(pkgName: string): Object, getDependencyConfig(pkgName: string): Object,
/**
* A module that exports:
* - a `getHasteName(filePath)` method that returns `hasteName` for module at
* `filePath`, or undefined if `filePath` is not a haste module.
*/
hasteImpl?: HasteImpl,
}; };
/** /**

Просмотреть файл

@ -29,6 +29,7 @@ function dependencies(argv, config, args, packagerInstance) {
projectRoots: config.getProjectRoots(), projectRoots: config.getProjectRoots(),
blacklistRE: config.getBlacklistRE(), blacklistRE: config.getBlacklistRE(),
getTransformOptions: config.getTransformOptions, getTransformOptions: config.getTransformOptions,
hasteImpl: config.hasteImpl,
transformModulePath: transformModulePath, transformModulePath: transformModulePath,
extraNodeModules: config.extraNodeModules, extraNodeModules: config.extraNodeModules,
verbose: config.verbose, verbose: config.verbose,

Просмотреть файл

@ -94,6 +94,7 @@ function getPackagerServer(args, config) {
cacheVersion: '3', cacheVersion: '3',
extraNodeModules: config.extraNodeModules, extraNodeModules: config.extraNodeModules,
getTransformOptions: config.getTransformOptions, getTransformOptions: config.getTransformOptions,
hasteImpl: config.hasteImpl,
platforms: defaultPlatforms.concat(args.platforms), platforms: defaultPlatforms.concat(args.platforms),
projectRoots: args.projectRoots, projectRoots: args.projectRoots,
providesModuleNodeModules: providesModuleNodeModules, providesModuleNodeModules: providesModuleNodeModules,

3
packager/react-packager.js поставляемый
Просмотреть файл

@ -18,11 +18,13 @@ const invariant = require('fbjs/lib/invariant');
import type GlobalTransformCache from './src/lib/GlobalTransformCache'; import type GlobalTransformCache from './src/lib/GlobalTransformCache';
import type {Reporter} from './src/lib/reporting'; import type {Reporter} from './src/lib/reporting';
import type {HasteImpl} from './src/node-haste/Module';
exports.createServer = createServer; exports.createServer = createServer;
exports.Logger = Logger; exports.Logger = Logger;
type Options = { type Options = {
hasteImpl?: HasteImpl,
globalTransformCache: ?GlobalTransformCache, globalTransformCache: ?GlobalTransformCache,
nonPersistent?: boolean, nonPersistent?: boolean,
projectRoots: Array<string>, projectRoots: Array<string>,
@ -31,6 +33,7 @@ type Options = {
}; };
type StrictOptions = { type StrictOptions = {
hasteImpl?: HasteImpl,
globalTransformCache: ?GlobalTransformCache, globalTransformCache: ?GlobalTransformCache,
nonPersistent?: boolean, nonPersistent?: boolean,
projectRoots: Array<string>, projectRoots: Array<string>,

Просмотреть файл

@ -36,7 +36,7 @@ const {
const VERSION = require('../../package.json').version; const VERSION = require('../../package.json').version;
import type AssetServer from '../AssetServer'; import type AssetServer from '../AssetServer';
import type Module from '../node-haste/Module'; import type Module, {HasteImpl} from '../node-haste/Module';
import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse'; import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse';
import type {Options as JSTransformerOptions, TransformOptions} from '../JSTransformer/worker/worker'; import type {Options as JSTransformerOptions, TransformOptions} from '../JSTransformer/worker/worker';
import type {Reporter} from '../lib/reporting'; import type {Reporter} from '../lib/reporting';
@ -86,6 +86,7 @@ type Options = {
extraNodeModules: {}, extraNodeModules: {},
getTransformOptions?: GetTransformOptions, getTransformOptions?: GetTransformOptions,
globalTransformCache: ?GlobalTransformCache, globalTransformCache: ?GlobalTransformCache,
hasteImpl?: HasteImpl,
moduleFormat: string, moduleFormat: string,
platforms: Array<string>, platforms: Array<string>,
polyfillModuleNames: Array<string>, polyfillModuleNames: Array<string>,
@ -172,6 +173,7 @@ class Bundler {
extraNodeModules: opts.extraNodeModules, extraNodeModules: opts.extraNodeModules,
getTransformCacheKey, getTransformCacheKey,
globalTransformCache: opts.globalTransformCache, globalTransformCache: opts.globalTransformCache,
hasteImpl: opts.hasteImpl,
minifyCode: this._transformer.minify, minifyCode: this._transformer.minify,
moduleFormat: opts.moduleFormat, moduleFormat: opts.moduleFormat,
platforms: opts.platforms, platforms: opts.platforms,

Просмотреть файл

@ -17,7 +17,7 @@ const defaults = require('../../defaults');
const pathJoin = require('path').join; const pathJoin = require('path').join;
import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse'; import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse';
import type Module from '../node-haste/Module'; import type Module, {HasteImpl} from '../node-haste/Module';
import type {SourceMap} from '../lib/SourceMap'; import type {SourceMap} from '../lib/SourceMap';
import type {Options as TransformOptions} from '../JSTransformer/worker/worker'; import type {Options as TransformOptions} from '../JSTransformer/worker/worker';
import type {Reporter} from '../lib/reporting'; import type {Reporter} from '../lib/reporting';
@ -36,6 +36,7 @@ type Options = {
extraNodeModules?: {}, extraNodeModules?: {},
getTransformCacheKey: GetTransformCacheKey, getTransformCacheKey: GetTransformCacheKey,
globalTransformCache: ?GlobalTransformCache, globalTransformCache: ?GlobalTransformCache,
hasteImpl?: HasteImpl,
minifyCode: MinifyCode, minifyCode: MinifyCode,
platforms: Array<string>, platforms: Array<string>,
polyfillModuleNames?: Array<string>, polyfillModuleNames?: Array<string>,
@ -70,6 +71,7 @@ class Resolver {
maxWorkers: null, maxWorkers: null,
moduleOptions: { moduleOptions: {
cacheTransformResults: true, cacheTransformResults: true,
hasteImpl: opts.hasteImpl,
resetCache: opts.resetCache, resetCache: opts.resetCache,
}, },
platforms: new Set(opts.platforms), platforms: new Set(opts.platforms),

Просмотреть файл

@ -26,7 +26,7 @@ const url = require('url');
const debug = require('debug')('RNP:Server'); const debug = require('debug')('RNP:Server');
import type Module from '../node-haste/Module'; import type Module, {HasteImpl} from '../node-haste/Module';
import type {Stats} from 'fs'; import type {Stats} from 'fs';
import type {IncomingMessage, ServerResponse} from 'http'; import type {IncomingMessage, ServerResponse} from 'http';
import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse'; import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse';
@ -62,6 +62,7 @@ type Options = {
extraNodeModules?: {}, extraNodeModules?: {},
getTransformOptions?: GetTransformOptions, getTransformOptions?: GetTransformOptions,
globalTransformCache: ?GlobalTransformCache, globalTransformCache: ?GlobalTransformCache,
hasteImpl?: HasteImpl,
moduleFormat?: string, moduleFormat?: string,
platforms?: Array<string>, platforms?: Array<string>,
polyfillModuleNames?: Array<string>, polyfillModuleNames?: Array<string>,
@ -178,6 +179,7 @@ class Server {
cacheVersion: string, cacheVersion: string,
extraNodeModules: {}, extraNodeModules: {},
getTransformOptions?: GetTransformOptions, getTransformOptions?: GetTransformOptions,
hasteImpl?: HasteImpl,
moduleFormat: string, moduleFormat: string,
platforms: Array<string>, platforms: Array<string>,
polyfillModuleNames: Array<string>, polyfillModuleNames: Array<string>,
@ -212,6 +214,7 @@ class Server {
extraNodeModules: options.extraNodeModules || {}, extraNodeModules: options.extraNodeModules || {},
getTransformOptions: options.getTransformOptions, getTransformOptions: options.getTransformOptions,
globalTransformCache: options.globalTransformCache, globalTransformCache: options.globalTransformCache,
hasteImpl: options.hasteImpl,
moduleFormat: options.moduleFormat != null ? options.moduleFormat : 'haste', moduleFormat: options.moduleFormat != null ? options.moduleFormat : 'haste',
platforms: options.platforms || defaults.platforms, platforms: options.platforms || defaults.platforms,
polyfillModuleNames: options.polyfillModuleNames || [], polyfillModuleNames: options.polyfillModuleNames || [],

Просмотреть файл

@ -46,9 +46,20 @@ export type TransformCode = (
transformOptions: TransformOptions, transformOptions: TransformOptions,
) => Promise<TransformedCode>; ) => Promise<TransformedCode>;
export type HasteImpl = {
getHasteName(filePath: string): (string | void),
// This exists temporarily to enforce consistency while we deprecate
// @providesModule.
enforceHasteNameMatches?: (
filePath: string,
expectedName: (string | void),
) => void,
};
export type Options = { export type Options = {
resetCache?: boolean,
cacheTransformResults?: boolean, cacheTransformResults?: boolean,
hasteImpl?: HasteImpl,
resetCache?: boolean,
}; };
export type ConstructorArgs = { export type ConstructorArgs = {
@ -191,8 +202,32 @@ class Module {
return this._docBlock; return this._docBlock;
} }
_getHasteName() { _getHasteName(): Promise<string | void> {
if (!this._hasteName) { if (!this._hasteName) {
const hasteImpl = this._options.hasteImpl;
if (hasteImpl === undefined || hasteImpl.enforceHasteNameMatches) {
this._hasteName = this._readDocBlock().then(moduleDocBlock => {
const {providesModule} = moduleDocBlock;
return providesModule
&& !this._depGraphHelpers.isNodeModulesDir(this.path)
? /^\S+/.exec(providesModule)[0]
: undefined;
});
}
if (hasteImpl !== undefined) {
const {enforceHasteNameMatches} = hasteImpl;
if (enforceHasteNameMatches) {
this._hasteName = this._hasteName.then(providesModule => {
enforceHasteNameMatches(
this.path,
providesModule,
);
return hasteImpl.getHasteName(this.path);
});
} else {
this._hasteName = Promise.resolve(hasteImpl.getHasteName(this.path));
}
} else {
// Extract an id for the module if it's using @providesModule syntax // Extract an id for the module if it's using @providesModule syntax
// and if it's NOT in node_modules (and not a whitelisted node_module). // and if it's NOT in node_modules (and not a whitelisted node_module).
// This handles the case where a project may have a dep that has @providesModule // This handles the case where a project may have a dep that has @providesModule
@ -207,6 +242,7 @@ class Module {
: undefined; : undefined;
}); });
} }
}
return this._hasteName; return this._hasteName;
} }