Make transformer result flow types generic all over the place

Reviewed By: jeanlauliac

Differential Revision: D7895095

fbshipit-source-id: df47f4b3e7a08e194fa1a2f874be87ead64ac198
This commit is contained in:
Rafael Oleza 2018-05-11 15:05:34 -07:00 committed by Facebook Github Bot
parent 06c8b6b7a6
commit 35c5abf3ee
24 changed files with 105 additions and 113 deletions

View File

@ -15,13 +15,11 @@ const mkdirp = require('mkdirp');
const path = require('path');
const rimraf = require('rimraf');
import type {TransformedCode} from 'metro/src/JSTransformer/worker';
export type Options = {|
root: string,
|};
class FileStore {
class FileStore<T> {
_root: string;
constructor(options: Options) {
@ -29,7 +27,7 @@ class FileStore {
this._createDirs();
}
get(key: Buffer): ?TransformedCode {
get(key: Buffer): ?T {
try {
return JSON.parse(fs.readFileSync(this._getFilePath(key), 'utf8'));
} catch (err) {
@ -41,7 +39,7 @@ class FileStore {
}
}
set(key: Buffer, value: TransformedCode): void {
set(key: Buffer, value: T): void {
fs.writeFileSync(this._getFilePath(key), JSON.stringify(value));
}

View File

@ -15,8 +15,6 @@ const https = require('https');
const url = require('url');
const zlib = require('zlib');
import type {TransformedCode} from 'metro/src/JSTransformer/worker';
export type Options = {|
endpoint: string,
timeout?: number,
@ -26,7 +24,7 @@ const ZLIB_OPTIONS = {
level: 9,
};
class HttpStore {
class HttpStore<T> {
_module: typeof http | typeof https;
_timeout: number;
@ -63,7 +61,7 @@ class HttpStore {
this._setAgent = new module.Agent(agentConfig);
}
get(key: Buffer): Promise<?TransformedCode> {
get(key: Buffer): Promise<?T> {
return new Promise((resolve, reject) => {
const options = {
agent: this._getAgent,
@ -119,7 +117,7 @@ class HttpStore {
});
}
set(key: Buffer, value: TransformedCode): Promise<void> {
set(key: Buffer, value: T): Promise<void> {
return new Promise((resolve, reject) => {
const gzip = zlib.createGzip(ZLIB_OPTIONS);

View File

@ -22,11 +22,7 @@ const toLocalPath = require('./node-haste/lib/toLocalPath');
const {Cache, stableHash} = require('metro-cache');
import type {TransformResult} from './DeltaBundler';
import type {
JsOutput,
WorkerOptions,
TransformedCode,
} from './JSTransformer/worker';
import type {WorkerOptions} from './JSTransformer/worker';
import type {DynamicRequiresBehavior} from './ModuleGraph/worker/collectDependencies';
import type {Reporter} from './lib/reporting';
import type {BabelSourceMap} from '@babel/core';
@ -72,7 +68,7 @@ export type Options = {|
+assetRegistryPath: string,
+asyncRequireModulePath: string,
+blacklistRE?: RegExp,
+cacheStores: $ReadOnlyArray<CacheStore<TransformedCode>>,
+cacheStores: $ReadOnlyArray<CacheStore<TransformResult<>>>,
+cacheVersion: string,
+dynamicDepsInPackages: DynamicRequiresBehavior,
+enableBabelRCLookup: boolean,
@ -100,7 +96,7 @@ const {hasOwnProperty} = Object.prototype;
class Bundler {
_opts: Options;
_cache: Cache<TransformedCode>;
_cache: Cache<TransformResult<>>;
_baseHash: string;
_transformer: Transformer;
_depGraphPromise: Promise<DependencyGraph>;
@ -218,7 +214,7 @@ class Bundler {
async transformFile(
filePath: string,
transformCodeOptions: WorkerOptions,
): Promise<TransformResult<JsOutput>> {
): Promise<TransformResult<>> {
const cache = this._cache;
const {

View File

@ -21,7 +21,7 @@ import type {
PostMinifyProcess,
PostProcessBundleSourcemap,
} from './Bundler';
import type {TransformedCode} from './JSTransformer/worker';
import type {TransformResult} from './DeltaBundler';
import type {DynamicRequiresBehavior} from './ModuleGraph/worker/collectDependencies';
import type {IncomingMessage, ServerResponse} from 'http';
import type {CacheStore} from 'metro-cache';
@ -38,7 +38,7 @@ export type ConfigT = {
/**
* List of all store caches.
*/
cacheStores: Array<CacheStore<TransformedCode>>,
cacheStores: Array<CacheStore<TransformResult<>>>,
/**
* Can be used to generate a key that will invalidate the whole metro cache

View File

@ -13,7 +13,13 @@
const DeltaCalculator = require('./DeltaBundler/DeltaCalculator');
import type Bundler from './Bundler';
import type {DeltaResult, Graph, Options} from './DeltaBundler/types.flow';
import type {
DeltaResult,
Graph,
// eslint-disable-next-line no-unused-vars
MixedOutput,
Options,
} from './DeltaBundler/types.flow';
export type {
DeltaResult,
@ -29,7 +35,7 @@ export type {
* concurrent clients requesting their own deltas. This is done through the
* `clientId` param (which maps a client to a specific delta transformer).
*/
class DeltaBundler<T> {
class DeltaBundler<T = MixedOutput> {
_bundler: Bundler;
_deltaCalculators: Map<Graph<T>, DeltaCalculator<T>> = new Map();

View File

@ -15,7 +15,6 @@ const getAppendScripts = require('../../lib/getAppendScripts');
const {wrapModule} = require('./helpers/js');
const {getJsOutput, isJsModule} = require('./helpers/js');
import type {JsOutput} from '../../JSTransformer/worker';
import type {DeltaResult, Graph, Module} from '../types.flow';
type Options = {|
@ -29,10 +28,10 @@ type Options = {|
function deltaJSBundle(
entryPoint: string,
pre: $ReadOnlyArray<Module<JsOutput>>,
delta: DeltaResult<JsOutput>,
pre: $ReadOnlyArray<Module<>>,
delta: DeltaResult<>,
sequenceId: string,
graph: Graph<JsOutput>,
graph: Graph<>,
options: Options,
): string {
const outputPre = [];

View File

@ -13,7 +13,6 @@
const {getAssetFiles} = require('../../Assets');
const {getJsOutput, isJsModule} = require('./helpers/js');
import type {JsOutput} from '../../JSTransformer/worker';
import type {Graph, Module} from '../types.flow';
type Options = {|
@ -21,8 +20,8 @@ type Options = {|
|};
async function getAllFiles(
pre: $ReadOnlyArray<Module<JsOutput>>,
graph: Graph<JsOutput>,
pre: $ReadOnlyArray<Module<>>,
graph: Graph<>,
options: Options,
): Promise<$ReadOnlyArray<string>> {
const modules = graph.dependencies;

View File

@ -16,7 +16,6 @@ const {getAssetData} = require('../../Assets');
const {getJsOutput, isJsModule} = require('./helpers/js');
import type {AssetData} from '../../Assets';
import type {JsOutput} from '../../JSTransformer/worker';
import type {Graph} from '../types.flow';
type Options = {|
@ -26,7 +25,7 @@ type Options = {|
|};
async function getAssets(
graph: Graph<JsOutput>,
graph: Graph<>,
options: Options,
): Promise<$ReadOnlyArray<AssetData>> {
const promises = [];

View File

@ -20,7 +20,6 @@ const {createRamBundleGroups} = require('../../Bundler/util');
const {isJsModule, wrapModule} = require('./helpers/js');
import type {GetTransformOptions} from '../../Bundler';
import type {JsOutput} from '../../JSTransformer/worker';
import type {ModuleTransportLike} from '../../shared/types.flow';
import type {Graph, Module} from '../types.flow';
@ -45,8 +44,8 @@ export type RamBundleInfo = {|
async function getRamBundleInfo(
entryPoint: string,
pre: $ReadOnlyArray<Module<JsOutput>>,
graph: Graph<JsOutput>,
pre: $ReadOnlyArray<Module<>>,
graph: Graph<>,
options: Options,
): Promise<RamBundleInfo> {
const modules = [

View File

@ -15,7 +15,7 @@ const invariant = require('fbjs/lib/invariant');
const path = require('path');
import type {JsOutput} from '../../../JSTransformer/worker';
import type {Module} from '../../types.flow';
import type {MixedOutput, Module} from '../../types.flow';
export type Options = {
+createModuleId: string => number | string,
@ -27,8 +27,9 @@ export type Options = {
// Make sure to set PRINT_REQUIRE_PATHS = true too, and restart Metro
const PASS_MODULE_PATHS_TO_DEFINE = false;
function wrapModule(module: Module<JsOutput>, options: Options) {
function wrapModule(module: Module<>, options: Options) {
const output = getJsOutput(module);
if (output.type.startsWith('js/script')) {
return output.data.code;
}
@ -55,7 +56,7 @@ function wrapModule(module: Module<JsOutput>, options: Options) {
return addParamsToDefineCall(output.data.code, ...params);
}
function getJsOutput(module: Module<JsOutput>) {
function getJsOutput(module: Module<>): JsOutput {
const jsModules = module.output.filter(({type}) => type.startsWith('js/'));
invariant(
@ -65,13 +66,15 @@ function getJsOutput(module: Module<JsOutput>) {
} JS outputs.`,
);
return jsModules[0];
return (jsModules[0]: any);
}
function isJsModule(module: Module<JsOutput>) {
const jsModules = module.output.filter(({type}) => type.startsWith('js/'));
function isJsModule(module: Module<>): boolean {
return module.output.filter(isJsOutput).length > 0;
}
return jsModules.length > 0;
function isJsOutput(output: MixedOutput): boolean %checks {
return output.type.startsWith('js/');
}
module.exports = {

View File

@ -14,7 +14,6 @@ const addParamsToDefineCall = require('../../lib/addParamsToDefineCall');
const {isJsModule, wrapModule} = require('./helpers/js');
import type {JsOutput} from '../../JSTransformer/worker';
import type {DeltaResult, Graph, Module} from '../types.flow';
type Options = {
@ -31,8 +30,8 @@ export type Result = {
};
function hmrJSBundle(
delta: DeltaResult<JsOutput>,
graph: Graph<JsOutput>,
delta: DeltaResult<>,
graph: Graph<>,
options: Options,
): Result {
const modules = [];
@ -54,8 +53,8 @@ function hmrJSBundle(
}
function _prepareModule(
module: Module<JsOutput>,
graph: Graph<JsOutput>,
module: Module<>,
graph: Graph<>,
options: Options,
): {|+id: number, +code: string|} {
const code = wrapModule(module, {
@ -87,7 +86,7 @@ function _prepareModule(
*/
function _getInverseDependencies(
path: string,
graph: Graph<JsOutput>,
graph: Graph<>,
inverseDependencies: {[key: string]: Array<string>} = {},
): {[key: string]: Array<string>} {
// Dependency alredy traversed.

View File

@ -14,7 +14,6 @@ const getAppendScripts = require('../../lib/getAppendScripts');
const {isJsModule, wrapModule} = require('./helpers/js');
import type {JsOutput} from '../../JSTransformer/worker';
import type {Graph, Module} from '../types.flow';
type Options = {|
@ -28,8 +27,8 @@ type Options = {|
function plainJSBundle(
entryPoint: string,
pre: $ReadOnlyArray<Module<JsOutput>>,
graph: Graph<JsOutput>,
pre: $ReadOnlyArray<Module<>>,
graph: Graph<>,
options: Options,
): string {
for (const module of graph.dependencies.values()) {

View File

@ -13,13 +13,12 @@
const {isJsModule, getJsOutput} = require('./helpers/js');
const {fromRawMappings} = require('metro-source-map');
import type {JsOutput} from '../../JSTransformer/worker';
import type {Graph, Module} from '../types.flow';
import type {BabelSourceMap} from '@babel/core';
function fullSourceMapObject(
pre: $ReadOnlyArray<Module<JsOutput>>,
graph: Graph<JsOutput>,
pre: $ReadOnlyArray<Module<>>,
graph: Graph<>,
options: {|+excludeSource: boolean|},
): BabelSourceMap {
const modules = [...pre, ...graph.dependencies.values()]

View File

@ -13,12 +13,11 @@
const {isJsModule, getJsOutput} = require('./helpers/js');
const {fromRawMappings} = require('metro-source-map');
import type {JsOutput} from '../../JSTransformer/worker';
import type {Graph, Module} from '../types.flow';
function fullSourceMap(
pre: $ReadOnlyArray<Module<JsOutput>>,
graph: Graph<JsOutput>,
pre: $ReadOnlyArray<Module<>>,
graph: Graph<>,
options: {|+excludeSource: boolean|},
): string {
const modules = [...pre, ...graph.dependencies.values()]

View File

@ -12,12 +12,17 @@
import type {TransformResultDependency} from '../ModuleGraph/types.flow';
export type MixedOutput = {|
+data: mixed,
+type: string,
|};
export type Dependency = {|
+absolutePath: string,
+data: TransformResultDependency,
|};
export type Module<T> = {|
export type Module<T = MixedOutput> = {|
dependencies: Map<string, Dependency>,
inverseDependencies: Set<string>,
output: $ReadOnlyArray<T>,
@ -25,26 +30,28 @@ export type Module<T> = {|
getSource: () => string,
|};
export type Graph<T> = {|
export type Graph<T = MixedOutput> = {|
dependencies: Map<string, Module<T>>,
entryPoints: $ReadOnlyArray<string>,
|};
export type TransformResult<T> = {|
export type TransformResult<T = MixedOutput> = {|
dependencies: $ReadOnlyArray<TransformResultDependency>,
output: $ReadOnlyArray<T>,
+getSource: () => string,
|};
export type TransformFn<T> = string => Promise<TransformResult<T>>;
export type TransformFn<T = MixedOutput> = string => Promise<
TransformResult<T>,
>;
export type Options<T> = {|
export type Options<T = MixedOutput> = {|
resolve: (from: string, to: string) => string,
transform: TransformFn<T>,
onProgress: ?(numProcessed: number, total: number) => mixed,
|};
export type DeltaResult<T> = {|
export type DeltaResult<T = MixedOutput> = {|
+modified: Map<string, Module<T>>,
+deleted: Set<string>,
+reset: boolean,

View File

@ -21,11 +21,11 @@ const {
Logger: {createActionStartEntry, createActionEndEntry, log},
} = require('metro-core');
import type PackagerServer, {JsGraph} from './Server';
import type PackagerServer, {OutputGraph} from './Server';
import type {Reporter} from './lib/reporting';
type Client = {|
graph: JsGraph,
graph: OutputGraph,
sendFn: (data: string) => mixed,
|};

View File

@ -16,7 +16,8 @@ const {Logger} = require('metro-core');
const debug = require('debug')('Metro:JStransformer');
const Worker = require('jest-worker').default;
import type {TransformedCode, WorkerOptions} from './JSTransformer/worker';
import type {TransformResult} from './DeltaBundler';
import type {WorkerOptions} from './JSTransformer/worker';
import type {LocalPath} from './node-haste/lib/toLocalPath';
import type {DynamicRequiresBehavior} from './ModuleGraph/worker/collectDependencies';
@ -32,7 +33,7 @@ type Reporters = {
};
type TransformerResult = {
result: TransformedCode,
result: TransformResult<>,
sha1: string,
};

View File

@ -84,26 +84,24 @@ export type WorkerOptions = {|
+projectRoot: string,
|};
export type Data = {
result: TransformedCode,
export type JsOutput = {|
+data: {|
+code: string,
+map: Array<MetroSourceMapSegmentTuple>,
|},
+type: string,
|};
type Data = {
result: {|
output: $ReadOnlyArray<JsOutput>,
dependencies: $ReadOnlyArray<TransformResultDependency>,
|},
sha1: string,
transformFileStartLogEntry: LogEntry,
transformFileEndLogEntry: LogEntry,
};
export type JsOutput = {|
data: {
+code: string,
+map: Array<MetroSourceMapSegmentTuple>,
},
type: string,
|};
export type TransformedCode = {|
output: $ReadOnlyArray<JsOutput>,
dependencies: $ReadOnlyArray<TransformResultDependency>,
|};
function getDynamicDepsBehavior(
inPackages: DynamicRequiresBehavior,
filename: string,

View File

@ -42,7 +42,7 @@ const {getAsset} = require('./Assets');
const resolveSync: ResolveSync = require('resolve').sync;
import type {CustomError} from './lib/formatBundlingError';
import type {DeltaResult, Graph, Module} from './DeltaBundler';
import type {DeltaResult, Graph, Module, TransformResult} from './DeltaBundler';
import type {IncomingMessage, ServerResponse} from 'http';
import type {Reporter} from './lib/reporting';
import type {RamBundleInfo} from './DeltaBundler/Serializers/getRamBundleInfo';
@ -57,11 +57,7 @@ import type {CustomResolver} from 'metro-resolver';
import type {MetroSourceMap} from 'metro-source-map';
import type {Symbolicate} from './Server/symbolicate/symbolicate';
import type {AssetData} from './Assets';
import type {
CustomTransformOptions,
JsOutput,
TransformedCode,
} from './JSTransformer/worker';
import type {CustomTransformOptions} from './JSTransformer/worker';
const {
Logger: {createActionStartEntry, createActionEndEntry, log},
@ -70,8 +66,8 @@ const {
type ResolveSync = (path: string, opts: ?{baseDir?: string}) => string;
type GraphInfo = {|
graph: Graph<JsOutput>,
prepend: $ReadOnlyArray<Module<JsOutput>>,
graph: Graph<>,
prepend: $ReadOnlyArray<Module<>>,
lastModified: Date,
+sequenceId: string,
|};
@ -87,7 +83,7 @@ export type BuildGraphOptions = {|
+type: 'module' | 'script',
|};
export type JsGraph = Graph<JsOutput>;
export type OutputGraph = Graph<>;
type DeltaOptions = BundleOptions & {
deltaBundleId: ?string,
@ -108,7 +104,7 @@ class Server {
_opts: {
assetExts: Array<string>,
blacklistRE: void | RegExp,
cacheStores: $ReadOnlyArray<CacheStore<TransformedCode>>,
cacheStores: $ReadOnlyArray<CacheStore<TransformResult<>>>,
cacheVersion: string,
createModuleId: (path: string) => number,
enableBabelRCLookup: boolean,
@ -145,7 +141,7 @@ class Server {
_symbolicateInWorker: Symbolicate;
_platforms: Set<string>;
_nextBundleBuildID: number;
_deltaBundler: DeltaBundler<JsOutput>;
_deltaBundler: DeltaBundler<>;
_graphs: Map<string, Promise<GraphInfo>> = new Map();
_deltaGraphs: Map<string, Promise<GraphInfo>> = new Map();
@ -258,7 +254,7 @@ class Server {
this._bundler.end();
}
getDeltaBundler(): DeltaBundler<JsOutput> {
getDeltaBundler(): DeltaBundler<> {
return this._deltaBundler;
}
@ -288,7 +284,7 @@ class Server {
async buildGraph(
entryFiles: $ReadOnlyArray<string>,
options: BuildGraphOptions,
): Promise<JsGraph> {
): Promise<OutputGraph> {
entryFiles = entryFiles.map(entryFile =>
getAbsolutePath(entryFile, this._opts.projectRoots),
);
@ -447,7 +443,7 @@ class Server {
async _getDeltaInfo(
options: DeltaOptions,
): Promise<{...GraphInfo, delta: DeltaResult<JsOutput>}> {
): Promise<{...GraphInfo, delta: DeltaResult<>}> {
const id = this._optionsHash(options);
let graphPromise = this._deltaGraphs.get(id);
let graphInfo;

View File

@ -31,7 +31,7 @@ const {readFile} = require('fs-extra');
const {Terminal} = require('metro-core');
import type {ConfigT} from './Config';
import type {JsGraph} from './Server';
import type {Graph} from './DeltaBundler';
import type {Reporter} from './lib/reporting';
import type {RequestOptions, OutputOptions} from './shared/types.flow.js';
import type {Options as ServerOptions} from './shared/types.flow';
@ -381,7 +381,7 @@ exports.buildGraph = async function({
platform = `web`,
type = 'module',
...rest
}: BuildGraphOptions): Promise<JsGraph> {
}: BuildGraphOptions): Promise<Graph<>> {
const metroServer = await runMetro({
...rest,
config,

View File

@ -11,7 +11,6 @@
'use strict';
import type {Graph, Module} from '../DeltaBundler';
import type {JsOutput} from '../JSTransformer/worker';
type Options<T: number | string> = {
+createModuleId: string => T,
@ -23,9 +22,9 @@ type Options<T: number | string> = {
function getAppendScripts<T: number | string>(
entryPoint: string,
graph: Graph<JsOutput>,
graph: Graph<>,
options: Options<T>,
): $ReadOnlyArray<Module<JsOutput>> {
): $ReadOnlyArray<Module<>> {
const output = [];
if (options.runModule) {

View File

@ -16,7 +16,7 @@ const transformHelpers = require('./transformHelpers');
import type Bundler from '../Bundler';
import type DeltaBundler, {Module} from '../DeltaBundler';
import type {CustomTransformOptions, JsOutput} from '../JSTransformer/worker';
import type {CustomTransformOptions} from '../JSTransformer/worker';
type Options = {
getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>,
@ -35,8 +35,8 @@ async function getPrependedScripts(
options: Options,
bundleOptions: BundleOptions,
bundler: Bundler,
deltaBundler: DeltaBundler<JsOutput>,
): Promise<Array<Module<JsOutput>>> {
deltaBundler: DeltaBundler<>,
): Promise<$ReadOnlyArray<Module<>>> {
// Get all the polyfills from the relevant option params (the
// `getPolyfills()` method and the `polyfillModuleNames` variable).
const polyfillModuleNames = options
@ -79,7 +79,7 @@ async function getPrependedScripts(
];
}
function _getPrelude({dev}: {dev: boolean}): Module<JsOutput> {
function _getPrelude({dev}: {dev: boolean}): Module<> {
const code = getPreludeCode({isDev: dev});
const name = '__prelude__';

View File

@ -12,7 +12,7 @@
import type Bundler from '../Bundler';
import type DeltaBundler, {TransformFn} from '../DeltaBundler';
import type {JsOutput, WorkerOptions} from '../JSTransformer/worker';
import type {WorkerOptions} from '../JSTransformer/worker';
import type {BuildGraphOptions} from '../Server';
type InlineRequiresRaw = {+blacklist: {[string]: true}} | boolean;
@ -20,7 +20,7 @@ type InlineRequiresRaw = {+blacklist: {[string]: true}} | boolean;
async function calcTransformerOptions(
entryFiles: $ReadOnlyArray<string>,
bundler: Bundler,
deltaBundler: DeltaBundler<JsOutput>,
deltaBundler: DeltaBundler<>,
options: BuildGraphOptions,
): Promise<{...WorkerOptions, inlineRequires: InlineRequiresRaw}> {
const {
@ -85,9 +85,9 @@ function removeInlineRequiresBlacklistFromOptions(
async function getTransformFn(
entryFiles: $ReadOnlyArray<string>,
bundler: Bundler,
deltaBundler: DeltaBundler<JsOutput>,
deltaBundler: DeltaBundler<>,
options: BuildGraphOptions,
): Promise<TransformFn<JsOutput>> {
): Promise<TransformFn<>> {
const {inlineRequires, ...transformerOptions} = await calcTransformerOptions(
entryFiles,
bundler,

View File

@ -14,10 +14,8 @@ import type {
PostMinifyProcess,
PostProcessBundleSourcemap,
} from '../Bundler';
import type {
CustomTransformOptions,
TransformedCode,
} from '../JSTransformer/worker';
import type {TransformResult} from '../DeltaBundler';
import type {CustomTransformOptions} from '../JSTransformer/worker';
import type {DynamicRequiresBehavior} from '../ModuleGraph/worker/collectDependencies';
import type {Reporter} from '../lib/reporting';
import type {CacheStore} from 'metro-cache';
@ -80,7 +78,7 @@ export type Options = {|
+asyncRequireModulePath: string,
+assetRegistryPath: string,
blacklistRE?: RegExp,
cacheStores: $ReadOnlyArray<CacheStore<TransformedCode>>,
cacheStores: $ReadOnlyArray<CacheStore<TransformResult<>>>,
cacheVersion: string,
createModuleIdFactory?: () => (path: string) => number,
+dynamicDepsInPackages: DynamicRequiresBehavior,