From cdbacc96b766ddc542d0fba672dab0cf1279c5c8 Mon Sep 17 00:00:00 2001 From: Rafael Oleza Date: Fri, 11 May 2018 15:05:13 -0700 Subject: [PATCH] Make output types from Delta Bundler generic Reviewed By: jeanlauliac Differential Revision: D7894080 fbshipit-source-id: 0f560c4600b4931bcc8f32ded10a7933d1ead984 --- packages/metro/src/DeltaBundler.js | 26 ++--- .../metro/src/DeltaBundler/DeltaCalculator.js | 20 ++-- .../DeltaBundler/Serializers/deltaJSBundle.js | 10 +- .../DeltaBundler/Serializers/getAllFiles.js | 5 +- .../src/DeltaBundler/Serializers/getAssets.js | 3 +- .../Serializers/getRamBundleInfo.js | 5 +- .../helpers/getTransitiveDependencies.js | 11 +- .../DeltaBundler/Serializers/helpers/js.js | 11 +- .../DeltaBundler/Serializers/hmrJSBundle.js | 16 ++- .../DeltaBundler/Serializers/plainJSBundle.js | 5 +- .../Serializers/sourceMapObject.js | 5 +- .../Serializers/sourceMapString.js | 5 +- .../src/DeltaBundler/traverseDependencies.js | 103 ++++++++---------- packages/metro/src/HmrServer.js | 5 +- packages/metro/src/JSTransformer/worker.js | 11 +- packages/metro/src/Server.js | 18 +-- packages/metro/src/index.js | 4 +- packages/metro/src/lib/getAppendScripts.js | 5 +- packages/metro/src/lib/getPrependedScripts.js | 8 +- packages/metro/src/lib/transformHelpers.js | 8 +- 20 files changed, 154 insertions(+), 130 deletions(-) diff --git a/packages/metro/src/DeltaBundler.js b/packages/metro/src/DeltaBundler.js index 7b29fb06..7fa5b30f 100644 --- a/packages/metro/src/DeltaBundler.js +++ b/packages/metro/src/DeltaBundler.js @@ -13,14 +13,9 @@ const DeltaCalculator = require('./DeltaBundler/DeltaCalculator'); import type Bundler from './Bundler'; -import type { - DeltaResult, - Graph as CalculatorGraph, - Options, -} from './DeltaBundler/DeltaCalculator'; +import type {DeltaResult, Graph, Options} from './DeltaBundler/DeltaCalculator'; -export type Delta = DeltaResult; -export type Graph = CalculatorGraph; +export type {DeltaResult, Graph} from './DeltaBundler/DeltaCalculator'; /** * `DeltaBundler` uses the `DeltaTransformer` to build bundle deltas. This @@ -28,9 +23,9 @@ export type Graph = CalculatorGraph; * concurrent clients requesting their own deltas. This is done through the * `clientId` param (which maps a client to a specific delta transformer). */ -class DeltaBundler { +class DeltaBundler { _bundler: Bundler; - _deltaCalculators: Map = new Map(); + _deltaCalculators: Map, DeltaCalculator> = new Map(); constructor(bundler: Bundler) { this._bundler = bundler; @@ -43,8 +38,8 @@ class DeltaBundler { async buildGraph( entryPoints: $ReadOnlyArray, - options: Options, - ): Promise { + options: Options, + ): Promise> { const depGraph = await this._bundler.getDependencyGraph(); const deltaCalculator = new DeltaCalculator(entryPoints, depGraph, options); @@ -57,7 +52,10 @@ class DeltaBundler { return graph; } - async getDelta(graph: Graph, {reset}: {reset: boolean}): Promise { + async getDelta( + graph: Graph, + {reset}: {reset: boolean}, + ): Promise> { const deltaCalculator = this._deltaCalculators.get(graph); if (!deltaCalculator) { @@ -67,7 +65,7 @@ class DeltaBundler { return await deltaCalculator.getDelta({reset}); } - listen(graph: Graph, callback: () => mixed) { + listen(graph: Graph, callback: () => mixed) { const deltaCalculator = this._deltaCalculators.get(graph); if (!deltaCalculator) { @@ -77,7 +75,7 @@ class DeltaBundler { deltaCalculator.on('change', callback); } - endGraph(graph: Graph) { + endGraph(graph: Graph) { const deltaCalculator = this._deltaCalculators.get(graph); if (!deltaCalculator) { diff --git a/packages/metro/src/DeltaBundler/DeltaCalculator.js b/packages/metro/src/DeltaBundler/DeltaCalculator.js index a5b5d64d..3add2c09 100644 --- a/packages/metro/src/DeltaBundler/DeltaCalculator.js +++ b/packages/metro/src/DeltaBundler/DeltaCalculator.js @@ -20,8 +20,8 @@ const {EventEmitter} = require('events'); import type DependencyGraph from '../node-haste/DependencyGraph'; import type {Graph, Module, Options} from './traverseDependencies'; -export type DeltaResult = {| - +modified: Map, +export type DeltaResult = {| + +modified: Map>, +deleted: Set, +reset: boolean, |}; @@ -34,20 +34,20 @@ export type {Graph, Options} from './traverseDependencies'; * traverse the files that have been changed between calls and avoid having to * traverse the whole dependency tree for trivial small changes. */ -class DeltaCalculator extends EventEmitter { +class DeltaCalculator extends EventEmitter { _dependencyGraph: DependencyGraph; - _options: Options; + _options: Options; - _currentBuildPromise: ?Promise; + _currentBuildPromise: ?Promise>; _deletedFiles: Set = new Set(); _modifiedFiles: Set = new Set(); - _graph: Graph; + _graph: Graph; constructor( entryPoints: $ReadOnlyArray, dependencyGraph: DependencyGraph, - options: Options, + options: Options, ) { super(); @@ -87,7 +87,7 @@ class DeltaCalculator extends EventEmitter { * Main method to calculate the delta of modules. It returns a DeltaResult, * which contain the modified/added modules and the removed modules. */ - async getDelta({reset}: {reset: boolean}): Promise { + async getDelta({reset}: {reset: boolean}): Promise> { // If there is already a build in progress, wait until it finish to start // processing a new one (delta server doesn't support concurrent builds). if (this._currentBuildPromise) { @@ -155,7 +155,7 @@ class DeltaCalculator extends EventEmitter { * needed information to do the traversing (dependencies, inverseDependencies) * plus some metadata. */ - getGraph(): Graph { + getGraph(): Graph { return this._graph; } @@ -191,7 +191,7 @@ class DeltaCalculator extends EventEmitter { async _getChangedDependencies( modifiedFiles: Set, deletedFiles: Set, - ): Promise { + ): Promise> { if (!this._graph.dependencies.size) { const {added} = await initialTraverseDependencies( this._graph, diff --git a/packages/metro/src/DeltaBundler/Serializers/deltaJSBundle.js b/packages/metro/src/DeltaBundler/Serializers/deltaJSBundle.js index 060ff696..8ba035c3 100644 --- a/packages/metro/src/DeltaBundler/Serializers/deltaJSBundle.js +++ b/packages/metro/src/DeltaBundler/Serializers/deltaJSBundle.js @@ -15,7 +15,9 @@ const getAppendScripts = require('../../lib/getAppendScripts'); const {wrapModule} = require('./helpers/js'); const {getJsOutput, isJsModule} = require('./helpers/js'); -import type {Delta, Graph} from '../../DeltaBundler'; +import type {DeltaResult} from '../../DeltaBundler/DeltaCalculator'; +import type {Graph} from '../../DeltaBundler'; +import type {JsOutput} from '../../JSTransformer/worker'; import type {Module} from '../traverseDependencies'; type Options = {| @@ -29,10 +31,10 @@ type Options = {| function deltaJSBundle( entryPoint: string, - pre: $ReadOnlyArray, - delta: Delta, + pre: $ReadOnlyArray>, + delta: DeltaResult, sequenceId: string, - graph: Graph, + graph: Graph, options: Options, ): string { const outputPre = []; diff --git a/packages/metro/src/DeltaBundler/Serializers/getAllFiles.js b/packages/metro/src/DeltaBundler/Serializers/getAllFiles.js index 3cc52727..91b61e41 100644 --- a/packages/metro/src/DeltaBundler/Serializers/getAllFiles.js +++ b/packages/metro/src/DeltaBundler/Serializers/getAllFiles.js @@ -13,6 +13,7 @@ const {getAssetFiles} = require('../../Assets'); const {getJsOutput, isJsModule} = require('./helpers/js'); +import type {JsOutput} from '../../JSTransformer/worker'; import type {Graph} from '../DeltaCalculator'; import type {Module} from '../traverseDependencies'; @@ -21,8 +22,8 @@ type Options = {| |}; async function getAllFiles( - pre: $ReadOnlyArray, - graph: Graph, + pre: $ReadOnlyArray>, + graph: Graph, options: Options, ): Promise<$ReadOnlyArray> { const modules = graph.dependencies; diff --git a/packages/metro/src/DeltaBundler/Serializers/getAssets.js b/packages/metro/src/DeltaBundler/Serializers/getAssets.js index 3d900985..9762e11b 100644 --- a/packages/metro/src/DeltaBundler/Serializers/getAssets.js +++ b/packages/metro/src/DeltaBundler/Serializers/getAssets.js @@ -16,6 +16,7 @@ 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 '../DeltaCalculator'; type Options = {| @@ -25,7 +26,7 @@ type Options = {| |}; async function getAssets( - graph: Graph, + graph: Graph, options: Options, ): Promise<$ReadOnlyArray> { const promises = []; diff --git a/packages/metro/src/DeltaBundler/Serializers/getRamBundleInfo.js b/packages/metro/src/DeltaBundler/Serializers/getRamBundleInfo.js index 6207f0d6..45272235 100644 --- a/packages/metro/src/DeltaBundler/Serializers/getRamBundleInfo.js +++ b/packages/metro/src/DeltaBundler/Serializers/getRamBundleInfo.js @@ -20,6 +20,7 @@ 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} from '../DeltaCalculator'; import type {Module} from '../traverseDependencies'; @@ -45,8 +46,8 @@ export type RamBundleInfo = {| async function getRamBundleInfo( entryPoint: string, - pre: $ReadOnlyArray, - graph: Graph, + pre: $ReadOnlyArray>, + graph: Graph, options: Options, ): Promise { const modules = [ diff --git a/packages/metro/src/DeltaBundler/Serializers/helpers/getTransitiveDependencies.js b/packages/metro/src/DeltaBundler/Serializers/helpers/getTransitiveDependencies.js index 23c8a5c5..b07437c8 100644 --- a/packages/metro/src/DeltaBundler/Serializers/helpers/getTransitiveDependencies.js +++ b/packages/metro/src/DeltaBundler/Serializers/helpers/getTransitiveDependencies.js @@ -12,7 +12,10 @@ import type {Graph} from '../../DeltaCalculator'; -function getTransitiveDependencies(path: string, graph: Graph): Set { +function getTransitiveDependencies( + path: string, + graph: Graph, +): Set { const dependencies = _getDeps(path, graph, new Set()); // Remove the main entry point, since this method only returns the @@ -22,7 +25,11 @@ function getTransitiveDependencies(path: string, graph: Graph): Set { return dependencies; } -function _getDeps(path: string, graph: Graph, deps: Set): Set { +function _getDeps( + path: string, + graph: Graph, + deps: Set, +): Set { if (deps.has(path)) { return deps; } diff --git a/packages/metro/src/DeltaBundler/Serializers/helpers/js.js b/packages/metro/src/DeltaBundler/Serializers/helpers/js.js index 484abd80..4a1d31d3 100644 --- a/packages/metro/src/DeltaBundler/Serializers/helpers/js.js +++ b/packages/metro/src/DeltaBundler/Serializers/helpers/js.js @@ -14,6 +14,7 @@ const addParamsToDefineCall = require('../../../lib/addParamsToDefineCall'); const invariant = require('fbjs/lib/invariant'); const path = require('path'); +import type {JsOutput} from '../../../JSTransformer/worker'; import type {Module} from '../../traverseDependencies'; export type Options = { @@ -26,7 +27,7 @@ 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, options: Options) { +function wrapModule(module: Module, options: Options) { const output = getJsOutput(module); if (output.type.startsWith('js/script')) { return output.data.code; @@ -54,7 +55,7 @@ function wrapModule(module: Module, options: Options) { return addParamsToDefineCall(output.data.code, ...params); } -function getJsOutput(module: Module) { +function getJsOutput(module: Module) { const jsModules = module.output.filter(({type}) => type.startsWith('js/')); invariant( @@ -67,8 +68,10 @@ function getJsOutput(module: Module) { return jsModules[0]; } -function isJsModule(module: Module) { - return module.output.some(output => output.type.startsWith('js/')); +function isJsModule(module: Module) { + const jsModules = module.output.filter(({type}) => type.startsWith('js/')); + + return jsModules.length > 0; } module.exports = { diff --git a/packages/metro/src/DeltaBundler/Serializers/hmrJSBundle.js b/packages/metro/src/DeltaBundler/Serializers/hmrJSBundle.js index 5ae73439..e90e5928 100644 --- a/packages/metro/src/DeltaBundler/Serializers/hmrJSBundle.js +++ b/packages/metro/src/DeltaBundler/Serializers/hmrJSBundle.js @@ -14,7 +14,9 @@ const addParamsToDefineCall = require('../../lib/addParamsToDefineCall'); const {isJsModule, wrapModule} = require('./helpers/js'); -import type {Delta, Graph} from '../../DeltaBundler'; +import type {DeltaResult} from '../../DeltaBundler/DeltaCalculator'; +import type {Graph} from '../../DeltaBundler'; +import type {JsOutput} from '../../JSTransformer/worker'; import type {Module} from '../traverseDependencies'; type Options = { @@ -30,7 +32,11 @@ export type Result = { }, }; -function hmrJSBundle(delta: Delta, graph: Graph, options: Options): Result { +function hmrJSBundle( + delta: DeltaResult, + graph: Graph, + options: Options, +): Result { const modules = []; for (const module of delta.modified.values()) { @@ -50,8 +56,8 @@ function hmrJSBundle(delta: Delta, graph: Graph, options: Options): Result { } function _prepareModule( - module: Module, - graph: Graph, + module: Module, + graph: Graph, options: Options, ): {|+id: number, +code: string|} { const code = wrapModule(module, { @@ -83,7 +89,7 @@ function _prepareModule( */ function _getInverseDependencies( path: string, - graph: Graph, + graph: Graph, inverseDependencies: {[key: string]: Array} = {}, ): {[key: string]: Array} { // Dependency alredy traversed. diff --git a/packages/metro/src/DeltaBundler/Serializers/plainJSBundle.js b/packages/metro/src/DeltaBundler/Serializers/plainJSBundle.js index a11d2be4..2d3b868f 100644 --- a/packages/metro/src/DeltaBundler/Serializers/plainJSBundle.js +++ b/packages/metro/src/DeltaBundler/Serializers/plainJSBundle.js @@ -14,6 +14,7 @@ const getAppendScripts = require('../../lib/getAppendScripts'); const {isJsModule, wrapModule} = require('./helpers/js'); +import type {JsOutput} from '../../JSTransformer/worker'; import type {Graph} from '../DeltaCalculator'; import type {Module} from '../traverseDependencies'; @@ -28,8 +29,8 @@ type Options = {| function plainJSBundle( entryPoint: string, - pre: $ReadOnlyArray, - graph: Graph, + pre: $ReadOnlyArray>, + graph: Graph, options: Options, ): string { for (const module of graph.dependencies.values()) { diff --git a/packages/metro/src/DeltaBundler/Serializers/sourceMapObject.js b/packages/metro/src/DeltaBundler/Serializers/sourceMapObject.js index 79efa1db..9157dae0 100644 --- a/packages/metro/src/DeltaBundler/Serializers/sourceMapObject.js +++ b/packages/metro/src/DeltaBundler/Serializers/sourceMapObject.js @@ -13,13 +13,14 @@ const {isJsModule, getJsOutput} = require('./helpers/js'); const {fromRawMappings} = require('metro-source-map'); +import type {JsOutput} from '../../JSTransformer/worker'; import type {Graph} from '../DeltaCalculator'; import type {Module} from '../traverseDependencies'; import type {BabelSourceMap} from '@babel/core'; function fullSourceMapObject( - pre: $ReadOnlyArray, - graph: Graph, + pre: $ReadOnlyArray>, + graph: Graph, options: {|+excludeSource: boolean|}, ): BabelSourceMap { const modules = [...pre, ...graph.dependencies.values()] diff --git a/packages/metro/src/DeltaBundler/Serializers/sourceMapString.js b/packages/metro/src/DeltaBundler/Serializers/sourceMapString.js index 13ff7614..fde2f0c2 100644 --- a/packages/metro/src/DeltaBundler/Serializers/sourceMapString.js +++ b/packages/metro/src/DeltaBundler/Serializers/sourceMapString.js @@ -13,12 +13,13 @@ const {isJsModule, getJsOutput} = require('./helpers/js'); const {fromRawMappings} = require('metro-source-map'); +import type {JsOutput} from '../../JSTransformer/worker'; import type {Graph} from '../DeltaCalculator'; import type {Module} from '../traverseDependencies'; function fullSourceMap( - pre: $ReadOnlyArray, - graph: Graph, + pre: $ReadOnlyArray>, + graph: Graph, options: {|+excludeSource: boolean|}, ): string { const modules = [...pre, ...graph.dependencies.values()] diff --git a/packages/metro/src/DeltaBundler/traverseDependencies.js b/packages/metro/src/DeltaBundler/traverseDependencies.js index 28f08646..8a5069f5 100644 --- a/packages/metro/src/DeltaBundler/traverseDependencies.js +++ b/packages/metro/src/DeltaBundler/traverseDependencies.js @@ -11,27 +11,26 @@ 'use strict'; import type {TransformResultDependency} from '../ModuleGraph/types.flow'; -import type {MetroSourceMapSegmentTuple} from 'metro-source-map'; export type Dependency = {| absolutePath: string, data: TransformResultDependency, |}; -export type Module = {| +export type Module = {| dependencies: Map, inverseDependencies: Set, - output: TransformOutput, + output: $ReadOnlyArray, path: string, getSource: () => string, |}; -export type Graph = {| - dependencies: Map, +export type Graph = {| + dependencies: Map>, entryPoints: $ReadOnlyArray, |}; -type Result = {added: Map, deleted: Set}; +type Result = {added: Map>, deleted: Set}; /** * Internal data structure that the traversal logic uses to know which of the @@ -39,29 +38,21 @@ type Result = {added: Map, deleted: Set}; * (a file should not be deleted if it has been added, but it should if it * just has been modified). **/ -type Delta = { - added: Map, - modified: Map, +type Delta = { + added: Map>, + modified: Map>, deleted: Set, }; -export type TransformOutput = $ReadOnlyArray<{| - +data: { - +code: string, - +map: Array, - }, - +type: string, -|}>; - -export type TransformFn = string => Promise<{| +export type TransformFn = string => Promise<{| dependencies: $ReadOnlyArray, - output: TransformOutput, + output: $ReadOnlyArray, +getSource: () => string, |}>; -export type Options = {| +export type Options = {| resolve: (from: string, to: string) => string, - transform: TransformFn, + transform: TransformFn, onProgress: ?(numProcessed: number, total: number) => mixed, |}; @@ -76,11 +67,11 @@ export type Options = {| * method should traverse. Normally, these paths should be the modified files * since the last traversal. */ -async function traverseDependencies( +async function traverseDependencies( paths: $ReadOnlyArray, - graph: Graph, - options: Options, -): Promise { + graph: Graph, + options: Options, +): Promise> { const delta = { added: new Map(), modified: new Map(), @@ -134,10 +125,10 @@ async function traverseDependencies( }; } -async function initialTraverseDependencies( - graph: Graph, - options: Options, -): Promise { +async function initialTraverseDependencies( + graph: Graph, + options: Options, +): Promise> { graph.entryPoints.forEach(entryPoint => createModule(entryPoint, graph)); await traverseDependencies(graph.entryPoints, graph, options); @@ -150,11 +141,11 @@ async function initialTraverseDependencies( }; } -async function traverseDependenciesForSingleFile( - module: Module, - graph: Graph, - delta: Delta, - options: Options, +async function traverseDependenciesForSingleFile( + module: Module, + graph: Graph, + delta: Delta, + options: Options, ): Promise { let numProcessed = 0; let total = 1; @@ -179,11 +170,11 @@ async function traverseDependenciesForSingleFile( options.onProgress && options.onProgress(numProcessed, total); } -async function processModule( - module: Module, - graph: Graph, - delta: Delta, - options: Options, +async function processModule( + module: Module, + graph: Graph, + delta: Delta, + options: Options, onDependencyAdd: () => mixed, onDependencyAdded: () => mixed, ): Promise { @@ -239,12 +230,12 @@ async function processModule( await Promise.all(promises); } -async function addDependency( - parentModule: Module, +async function addDependency( + parentModule: Module, path: string, - graph: Graph, - delta: Delta, - options: Options, + graph: Graph, + delta: Delta, + options: Options, onDependencyAdd: () => mixed, onDependencyAdded: () => mixed, ): Promise { @@ -276,11 +267,11 @@ async function addDependency( onDependencyAdded(); } -function removeDependency( - parentModule: Module, +function removeDependency( + parentModule: Module, absolutePath: string, - graph: Graph, - delta: Delta, + graph: Graph, + delta: Delta, ): void { const module = graph.dependencies.get(absolutePath); @@ -309,7 +300,7 @@ function removeDependency( graph.dependencies.delete(module.path); } -function createModule(filePath: string, graph: Graph): Module { +function createModule(filePath: string, graph: Graph): Module { const module = { dependencies: new Map(), inverseDependencies: new Set(), @@ -323,10 +314,10 @@ function createModule(filePath: string, graph: Graph): Module { return module; } -function resolveDependencies( +function resolveDependencies( parentPath: string, dependencies: $ReadOnlyArray, - options: Options, + options: Options, ): Map { return new Map( dependencies.map(result => { @@ -346,7 +337,7 @@ function resolveDependencies( * Re-traverse the dependency graph in DFS order to reorder the modules and * guarantee the same order between runs. This method mutates the passed graph. */ -function reorderGraph(graph: Graph) { +function reorderGraph(graph: Graph) { const orderedDependencies = new Map(); graph.entryPoints.forEach(entryPoint => { @@ -362,10 +353,10 @@ function reorderGraph(graph: Graph) { graph.dependencies = orderedDependencies; } -function reorderDependencies( - graph: Graph, - module: Module, - orderedDependencies: Map, +function reorderDependencies( + graph: Graph, + module: Module, + orderedDependencies: Map>, ): void { if (module.path) { if (orderedDependencies.has(module.path)) { diff --git a/packages/metro/src/HmrServer.js b/packages/metro/src/HmrServer.js index 33b483dc..fd3d8f34 100644 --- a/packages/metro/src/HmrServer.js +++ b/packages/metro/src/HmrServer.js @@ -21,12 +21,11 @@ const { Logger: {createActionStartEntry, createActionEndEntry, log}, } = require('metro-core'); -import type {Graph} from './DeltaBundler/DeltaCalculator'; -import type PackagerServer from './Server'; +import type PackagerServer, {JsGraph} from './Server'; import type {Reporter} from './lib/reporting'; type Client = {| - graph: Graph, + graph: JsGraph, sendFn: (data: string) => mixed, |}; diff --git a/packages/metro/src/JSTransformer/worker.js b/packages/metro/src/JSTransformer/worker.js index b6f2d05c..a19e6468 100644 --- a/packages/metro/src/JSTransformer/worker.js +++ b/packages/metro/src/JSTransformer/worker.js @@ -37,10 +37,17 @@ import type {Ast} from '@babel/core'; import type {Plugins as BabelPlugins} from 'babel-core'; import type {LogEntry} from 'metro-core/src/Logger'; import type {MetroSourceMapSegmentTuple} from 'metro-source-map'; -import type {TransformOutput} from '../DeltaBundler/traverseDependencies'; + +export type JsOutput = {| + data: { + +code: string, + +map: Array, + }, + type: string, +|}; export type TransformedCode = {| - output: TransformOutput, + output: $ReadOnlyArray, dependencies: $ReadOnlyArray, |}; diff --git a/packages/metro/src/Server.js b/packages/metro/src/Server.js index f36f83de..f7f159fa 100644 --- a/packages/metro/src/Server.js +++ b/packages/metro/src/Server.js @@ -53,13 +53,15 @@ import type { PostProcessBundleSourcemap, } from './Bundler'; import type {CacheStore} from 'metro-cache'; -import type {Delta, Graph} from './DeltaBundler'; +import type {Graph} from './DeltaBundler'; +import type {DeltaResult} from './DeltaBundler/DeltaCalculator'; 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'; @@ -70,8 +72,8 @@ const { type ResolveSync = (path: string, opts: ?{baseDir?: string}) => string; type GraphInfo = {| - graph: Graph, - prepend: $ReadOnlyArray, + graph: Graph, + prepend: $ReadOnlyArray>, lastModified: Date, +sequenceId: string, |}; @@ -87,6 +89,8 @@ export type BuildGraphOptions = {| +type: 'module' | 'script', |}; +export type JsGraph = Graph; + type DeltaOptions = BundleOptions & { deltaBundleId: ?string, }; @@ -143,7 +147,7 @@ class Server { _symbolicateInWorker: Symbolicate; _platforms: Set; _nextBundleBuildID: number; - _deltaBundler: DeltaBundler; + _deltaBundler: DeltaBundler; _graphs: Map> = new Map(); _deltaGraphs: Map> = new Map(); @@ -256,7 +260,7 @@ class Server { this._bundler.end(); } - getDeltaBundler(): DeltaBundler { + getDeltaBundler(): DeltaBundler { return this._deltaBundler; } @@ -286,7 +290,7 @@ class Server { async buildGraph( entryFiles: $ReadOnlyArray, options: BuildGraphOptions, - ): Promise { + ): Promise { entryFiles = entryFiles.map(entryFile => getAbsolutePath(entryFile, this._opts.projectRoots), ); @@ -445,7 +449,7 @@ class Server { async _getDeltaInfo( options: DeltaOptions, - ): Promise<{...GraphInfo, delta: Delta}> { + ): Promise<{...GraphInfo, delta: DeltaResult}> { const id = this._optionsHash(options); let graphPromise = this._deltaGraphs.get(id); let graphInfo; diff --git a/packages/metro/src/index.js b/packages/metro/src/index.js index e106c2f0..51384e70 100644 --- a/packages/metro/src/index.js +++ b/packages/metro/src/index.js @@ -31,7 +31,7 @@ const {readFile} = require('fs-extra'); const {Terminal} = require('metro-core'); import type {ConfigT} from './Config'; -import type {Graph} from './DeltaBundler'; +import type {JsGraph} from './Server'; 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 { +}: BuildGraphOptions): Promise { const metroServer = await runMetro({ ...rest, config, diff --git a/packages/metro/src/lib/getAppendScripts.js b/packages/metro/src/lib/getAppendScripts.js index 1638db65..06a3878c 100644 --- a/packages/metro/src/lib/getAppendScripts.js +++ b/packages/metro/src/lib/getAppendScripts.js @@ -12,6 +12,7 @@ import type {Graph} from '../DeltaBundler/DeltaCalculator'; import type {Module} from '../DeltaBundler/traverseDependencies'; +import type {JsOutput} from '../JSTransformer/worker'; type Options = { +createModuleId: string => T, @@ -23,9 +24,9 @@ type Options = { function getAppendScripts( entryPoint: string, - graph: Graph, + graph: Graph, options: Options, -): $ReadOnlyArray { +): $ReadOnlyArray> { const output = []; if (options.runModule) { diff --git a/packages/metro/src/lib/getPrependedScripts.js b/packages/metro/src/lib/getPrependedScripts.js index 678248bf..f73f522c 100644 --- a/packages/metro/src/lib/getPrependedScripts.js +++ b/packages/metro/src/lib/getPrependedScripts.js @@ -17,7 +17,7 @@ const transformHelpers = require('./transformHelpers'); import type Bundler from '../Bundler'; import type {Module} from '../DeltaBundler/traverseDependencies'; import type DeltaBundler from '../DeltaBundler'; -import type {CustomTransformOptions} from '../JSTransformer/worker'; +import type {CustomTransformOptions, JsOutput} from '../JSTransformer/worker'; type Options = { getPolyfills: ({platform: ?string}) => $ReadOnlyArray, @@ -36,8 +36,8 @@ async function getPrependedScripts( options: Options, bundleOptions: BundleOptions, bundler: Bundler, - deltaBundler: DeltaBundler, -): Promise> { + deltaBundler: DeltaBundler, +): Promise>> { // Get all the polyfills from the relevant option params (the // `getPolyfills()` method and the `polyfillModuleNames` variable). const polyfillModuleNames = options @@ -80,7 +80,7 @@ async function getPrependedScripts( ]; } -function _getPrelude({dev}: {dev: boolean}): Module { +function _getPrelude({dev}: {dev: boolean}): Module { const code = getPreludeCode({isDev: dev}); const name = '__prelude__'; diff --git a/packages/metro/src/lib/transformHelpers.js b/packages/metro/src/lib/transformHelpers.js index 02351d19..2102b434 100644 --- a/packages/metro/src/lib/transformHelpers.js +++ b/packages/metro/src/lib/transformHelpers.js @@ -13,7 +13,7 @@ import type Bundler from '../Bundler'; import type {TransformFn} from '../DeltaBundler/traverseDependencies'; import type DeltaBundler from '../DeltaBundler'; -import type {TransformOptions} from '../JSTransformer/worker'; +import type {JsOutput, TransformOptions} from '../JSTransformer/worker'; import type {BuildGraphOptions} from '../Server'; type InlineRequiresRaw = {+blacklist: {[string]: true}} | boolean; @@ -21,7 +21,7 @@ type InlineRequiresRaw = {+blacklist: {[string]: true}} | boolean; async function calcTransformerOptions( entryFiles: $ReadOnlyArray, bundler: Bundler, - deltaBundler: DeltaBundler, + deltaBundler: DeltaBundler, options: BuildGraphOptions, ): Promise<{...TransformOptions, inlineRequires: InlineRequiresRaw}> { const { @@ -85,9 +85,9 @@ function removeInlineRequiresBlacklistFromOptions( async function getTransformFn( entryFiles: $ReadOnlyArray, bundler: Bundler, - deltaBundler: DeltaBundler, + deltaBundler: DeltaBundler, options: BuildGraphOptions, -): Promise { +): Promise> { const dependencyGraph = await bundler.getDependencyGraph(); const {inlineRequires, ...transformerOptions} = await calcTransformerOptions( entryFiles,