Simplify contract of `getTransformOptionsModulePath`

Summary:
instead of passing the `Bundler` instance, the transform options module now receives a `getDependencies()` function.
The idea is to make the contract stricter, to integrate more easily with the new `ModuleGraph` system.

Reviewed By: cpojer

Differential Revision: D4233529

fbshipit-source-id: 1c6d462c7dae1c61cbf45fd13989ce77f76e7384
This commit is contained in:
David Aurelio 2016-11-28 07:27:07 -08:00 committed by Facebook Github Bot
parent 2be362b017
commit fb9d114861
1 changed files with 26 additions and 9 deletions

View File

@ -36,6 +36,12 @@ import AssetServer from '../AssetServer';
import Module from '../node-haste/Module'; import Module from '../node-haste/Module';
import ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse'; import ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse';
export type TransformOptionsModule<T> = (
string,
Object,
string => Promise<Array<string>>,
) => T | Promise<T>;
const sizeOf = denodeify(imageSize); const sizeOf = denodeify(imageSize);
const noop = () => {}; const noop = () => {};
@ -132,7 +138,7 @@ class Bundler {
_resolver: Resolver; _resolver: Resolver;
_projectRoots: Array<string>; _projectRoots: Array<string>;
_assetServer: AssetServer; _assetServer: AssetServer;
_transformOptionsModule: (path: string, options: {}, bunler: Bundler) => {}; _transformOptionsModule: TransformOptionsModule<*>;
constructor(options: Options) { constructor(options: Options) {
const opts = this._opts = validateOpts(options); const opts = this._opts = validateOpts(options);
@ -274,7 +280,7 @@ class Bundler {
); );
} }
hmrBundle(options: {platform: mixed}, host: string, port: number) { hmrBundle(options: {platform: ?string}, host: string, port: number) {
return this._bundle({ return this._bundle({
...options, ...options,
bundle: new HMRBundle({ bundle: new HMRBundle({
@ -466,7 +472,7 @@ class Bundler {
generateSourceMaps = false, generateSourceMaps = false,
}: { }: {
entryFile: string, entryFile: string,
platform: mixed, platform: ?string,
dev?: boolean, dev?: boolean,
minify?: boolean, minify?: boolean,
hot?: boolean, hot?: boolean,
@ -513,7 +519,7 @@ class Bundler {
onProgress, onProgress,
}: { }: {
entryFile: string, entryFile: string,
platform: mixed, platform: ?string,
dev?: boolean, dev?: boolean,
minify?: boolean, minify?: boolean,
hot?: boolean, hot?: boolean,
@ -552,7 +558,7 @@ class Bundler {
getOrderedDependencyPaths({ entryFile, dev, platform }: { getOrderedDependencyPaths({ entryFile, dev, platform }: {
entryFile: string, entryFile: string,
dev: boolean, dev: boolean,
platform: mixed, platform: ?string,
}) { }) {
return this.getDependencies({entryFile, dev, platform}).then( return this.getDependencies({entryFile, dev, platform}).then(
({ dependencies }) => { ({ dependencies }) => {
@ -630,7 +636,7 @@ class Bundler {
}); });
} }
_generateAssetObjAndCode(module, assetPlugins, platform: mixed = null) { _generateAssetObjAndCode(module, assetPlugins, platform: ?string = null) {
const relPath = getPathRelativeToRoot(this._projectRoots, module.path); const relPath = getPathRelativeToRoot(this._projectRoots, module.path);
var assetUrlPath = joinPath('/assets', pathDirname(relPath)); var assetUrlPath = joinPath('/assets', pathDirname(relPath));
@ -707,7 +713,7 @@ class Bundler {
module, module,
moduleId, moduleId,
assetPlugins: Array<string> = [], assetPlugins: Array<string> = [],
platform: mixed = null, platform: ?string = null,
) { ) {
return Promise.all([ return Promise.all([
module.getName(), module.getName(),
@ -726,9 +732,20 @@ class Bundler {
}); });
} }
getTransformOptions(mainModuleName: string, options: {}) { getTransformOptions(
mainModuleName: string,
options: {
dev?: boolean,
platform: ?string,
hot?: boolean,
generateSourceMaps?: boolean,
},
) {
const getDependencies = (entryFile: string) =>
this.getDependencies({...options, entryFile})
.then(r => r.dependencies.map(d => d.path));
const extraOptions = this._transformOptionsModule const extraOptions = this._transformOptionsModule
? this._transformOptionsModule(mainModuleName, options, this) ? this._transformOptionsModule(mainModuleName, options, getDependencies)
: null; : null;
return Promise.resolve(extraOptions) return Promise.resolve(extraOptions)
.then(extraOpts => Object.assign(options, extraOpts)); .then(extraOpts => Object.assign(options, extraOpts));