From 356d896c01424597a5d9ae871bcbc8cb4ab7af1e Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Tue, 27 Feb 2018 09:28:15 -0800 Subject: [PATCH] remove `getRamOptions` from `DeltaTransformer` and `Bundler` Summary: Removes `getRamOptions` from `DeltaTransformer` and `Bundler` for better separation of concerns, and ultimately, porting delta functionality to `ModuleGraph` Reviewed By: rafeca Differential Revision: D7084487 fbshipit-source-id: e8a7ea4addbad9057e7d55627f77ebd01e64786b --- packages/metro/src/Bundler/index.js | 36 --------------- .../src/DeltaBundler/DeltaTransformer.js | 18 +------- .../src/DeltaBundler/Serializers/RamBundle.js | 46 +++++++++++++++++++ .../{ => Serializers}/Serializers.js | 23 ++++++---- .../__tests__/Serializers-test.js | 11 +++-- .../metro/src/Server/__tests__/Server-test.js | 4 +- packages/metro/src/Server/index.js | 14 ++++-- .../getOrderedDependencyPaths-test.js | 4 +- .../src/lib/getOrderedDependencyPaths.js | 2 +- .../src/shared/output/unbundle/as-assets.js | 2 +- .../shared/output/unbundle/as-indexed-file.js | 2 +- .../build-unbundle-sourcemap-with-metadata.js | 2 +- .../metro/src/shared/output/unbundle/index.js | 2 +- .../metro/src/shared/output/unbundle/util.js | 2 +- 14 files changed, 87 insertions(+), 81 deletions(-) create mode 100644 packages/metro/src/DeltaBundler/Serializers/RamBundle.js rename packages/metro/src/DeltaBundler/{ => Serializers}/Serializers.js (90%) diff --git a/packages/metro/src/Bundler/index.js b/packages/metro/src/Bundler/index.js index f6942ef4..8f896b81 100644 --- a/packages/metro/src/Bundler/index.js +++ b/packages/metro/src/Bundler/index.js @@ -41,12 +41,6 @@ import type { MetroSourceMap, } from 'metro-source-map'; -export type BundlingOptions = {| - +preloadedModules: ?{[string]: true} | false, - +ramGroups: ?Array, - +transformer: JSTransformerOptions, -|}; - type TransformOptions = {| +inlineRequires: {+blacklist: {[string]: true}} | boolean, |}; @@ -211,36 +205,6 @@ class Bundler { return transform || {inlineRequires: false}; } - /** - * Returns the options needed to create a RAM bundle. - */ - async getRamOptions( - entryFile: string, - options: {dev: boolean, platform: ?string}, - getDependencies: string => Promise>, - ): Promise<{| - +preloadedModules: {[string]: true}, - +ramGroups: Array, - |}> { - if (!this._getTransformOptions) { - return { - preloadedModules: {}, - ramGroups: [], - }; - } - - const {preloadedModules, ramGroups} = await this._getTransformOptions( - [entryFile], - {dev: options.dev, hot: true, platform: options.platform}, - getDependencies, - ); - - return { - preloadedModules: preloadedModules || {}, - ramGroups: ramGroups || [], - }; - } - /* * Helper method to return the global transform options that are kept in the * Bundler. diff --git a/packages/metro/src/DeltaBundler/DeltaTransformer.js b/packages/metro/src/DeltaBundler/DeltaTransformer.js index cac53aef..ebccf3de 100644 --- a/packages/metro/src/DeltaBundler/DeltaTransformer.js +++ b/packages/metro/src/DeltaBundler/DeltaTransformer.js @@ -153,7 +153,7 @@ class DeltaTransformer extends EventEmitter { * Returns a function that can be used to calculate synchronously the * transitive dependencies of any given file within the dependency graph. **/ - async getDependenciesFn() { + async getDependenciesFn(): Promise<(string) => Set> { if (!this._deltaCalculator.getDependencyEdges().size) { // If by any means the dependency graph has not been initialized, call // getDelta() to initialize it. @@ -187,22 +187,6 @@ class DeltaTransformer extends EventEmitter { return output; } - async getRamOptions( - entryFile: string, - options: {dev: boolean, platform: ?string}, - ): Promise<{| - +preloadedModules: {[string]: true}, - +ramGroups: $ReadOnlyArray, - |}> { - const getDependenciesFn = await this.getDependenciesFn(); - - return await this._bundler.getRamOptions( - entryFile, - options, - async (path: string) => Array.from(getDependenciesFn(path)), - ); - } - /** * Main method to calculate the bundle delta. It returns a DeltaResult, * which contain the source code of the modified and added modules and the diff --git a/packages/metro/src/DeltaBundler/Serializers/RamBundle.js b/packages/metro/src/DeltaBundler/Serializers/RamBundle.js new file mode 100644 index 00000000..4aae65d3 --- /dev/null +++ b/packages/metro/src/DeltaBundler/Serializers/RamBundle.js @@ -0,0 +1,46 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + * @format + */ + +'use strict'; + +import type {GetTransformOptions} from '../../Bundler'; + +/** + * Returns the options needed to create a RAM bundle. + */ +async function getRamOptions( + entryFile: string, + options: {dev: boolean, platform: ?string}, + getDependencies: string => Iterable, + getTransformOptions: ?GetTransformOptions, +): Promise<{| + +preloadedModules: {[string]: true}, + +ramGroups: Array, +|}> { + if (getTransformOptions == null) { + return { + preloadedModules: {}, + ramGroups: [], + }; + } + + const {preloadedModules, ramGroups} = await getTransformOptions( + [entryFile], + {dev: options.dev, hot: true, platform: options.platform}, + async x => Array.from(getDependencies), + ); + + return { + preloadedModules: preloadedModules || {}, + ramGroups: ramGroups || [], + }; +} + +exports.getRamOptions = getRamOptions; diff --git a/packages/metro/src/DeltaBundler/Serializers.js b/packages/metro/src/DeltaBundler/Serializers/Serializers.js similarity index 90% rename from packages/metro/src/DeltaBundler/Serializers.js rename to packages/metro/src/DeltaBundler/Serializers/Serializers.js index d2cfdccf..67605224 100644 --- a/packages/metro/src/DeltaBundler/Serializers.js +++ b/packages/metro/src/DeltaBundler/Serializers/Serializers.js @@ -10,22 +10,24 @@ 'use strict'; -const DeltaPatcher = require('./DeltaPatcher'); +const DeltaPatcher = require('../DeltaPatcher'); +const RamBundle = require('./RamBundle'); const stableHash = require('metro-cache/src/stableHash'); -const toLocalPath = require('../node-haste/lib/toLocalPath'); +const toLocalPath = require('../../node-haste/lib/toLocalPath'); -const {getAssetData} = require('../Assets'); -const {createRamBundleGroups} = require('../Bundler/util'); +const {getAssetData} = require('../../Assets'); +const {createRamBundleGroups} = require('../../Bundler/util'); const {fromRawMappings} = require('metro-source-map'); -import type {AssetData} from '../Assets'; -import type {BundleOptions, ModuleTransportLike} from '../shared/types.flow'; -import type DeltaBundler from './'; +import type {AssetData} from '../../Assets'; +import type {GetTransformOptions} from '../../Bundler'; +import type {BundleOptions, ModuleTransportLike} from '../../shared/types.flow'; +import type DeltaBundler from '../'; import type DeltaTransformer, { DeltaEntry, DeltaTransformResponse, -} from './DeltaTransformer'; +} from '../DeltaTransformer'; import type {BabelSourceMap} from '@babel/core'; export type DeltaOptions = BundleOptions & { @@ -166,6 +168,7 @@ async function _getAllModules( async function getRamBundleInfo( deltaBundler: DeltaBundler, options: BundleOptions, + getTransformOptions: ?GetTransformOptions, ): Promise { const {modules, deltaTransformer} = await _getAllModules( deltaBundler, @@ -184,12 +187,14 @@ async function getRamBundleInfo( type: module.type, })); - const {preloadedModules, ramGroups} = await deltaTransformer.getRamOptions( + const {preloadedModules, ramGroups} = await RamBundle.getRamOptions( options.entryFile, { dev: options.dev, platform: options.platform, }, + await deltaTransformer.getDependenciesFn(), + getTransformOptions, ); const startupModules = []; diff --git a/packages/metro/src/DeltaBundler/__tests__/Serializers-test.js b/packages/metro/src/DeltaBundler/__tests__/Serializers-test.js index 38d31f79..c2f73583 100644 --- a/packages/metro/src/DeltaBundler/__tests__/Serializers-test.js +++ b/packages/metro/src/DeltaBundler/__tests__/Serializers-test.js @@ -12,6 +12,7 @@ jest.mock('../../node-haste/lib/toLocalPath'); jest.mock('../../Assets'); +jest.mock('../Serializers/RamBundle'); const {getAssetData} = require('../../Assets'); const toLocalPath = require('../../node-haste/lib/toLocalPath'); @@ -22,10 +23,10 @@ describe('Serializers', () => { const OriginalDate = global.Date; const getDelta = jest.fn(); const getDependenciesFn = jest.fn(); - const getRamOptions = jest.fn(); const postProcessModules = jest.fn(); let deltaBundler; let Serializers; + let RamBundle; const deltaResponse = { id: '1234', @@ -44,11 +45,12 @@ describe('Serializers', () => { } beforeEach(() => { - Serializers = require('../Serializers'); + Serializers = require('../Serializers/Serializers'); + RamBundle = require('../Serializers/RamBundle'); getDelta.mockReturnValueOnce(Promise.resolve(deltaResponse)); getDependenciesFn.mockReturnValue(Promise.resolve(() => new Set())); - getRamOptions.mockReturnValue( + RamBundle.getRamOptions.mockReturnValue( Promise.resolve({ preloadedModules: {}, ramGroups: [], @@ -61,7 +63,6 @@ describe('Serializers', () => { return { getDelta, getDependenciesFn, - getRamOptions, }; }, getPostProcessModulesFn() { @@ -215,7 +216,7 @@ describe('Serializers', () => { }), ); - getRamOptions.mockReturnValue( + RamBundle.getRamOptions.mockReturnValue( Promise.resolve({ preloadedModules: {'/foo/3.js': true}, ramGroups: ['/foo/5.js'], diff --git a/packages/metro/src/Server/__tests__/Server-test.js b/packages/metro/src/Server/__tests__/Server-test.js index 44924506..b8624bae 100644 --- a/packages/metro/src/Server/__tests__/Server-test.js +++ b/packages/metro/src/Server/__tests__/Server-test.js @@ -22,7 +22,7 @@ jest .mock('../../node-haste/DependencyGraph') .mock('metro-core/src/Logger') .mock('../../lib/GlobalTransformCache') - .mock('../../DeltaBundler/Serializers'); + .mock('../../DeltaBundler/Serializers/Serializers'); describe('processRequest', () => { let Bundler; @@ -40,7 +40,7 @@ describe('processRequest', () => { Server = require('../'); getAsset = require('../../Assets').getAsset; symbolicate = require('../symbolicate'); - Serializers = require('../../DeltaBundler/Serializers'); + Serializers = require('../../DeltaBundler/Serializers/Serializers'); DeltaBundler = require('../../DeltaBundler'); }); diff --git a/packages/metro/src/Server/index.js b/packages/metro/src/Server/index.js index e7357233..288e56eb 100644 --- a/packages/metro/src/Server/index.js +++ b/packages/metro/src/Server/index.js @@ -13,7 +13,7 @@ const Bundler = require('../Bundler'); const DeltaBundler = require('../DeltaBundler'); const MultipartResponse = require('./MultipartResponse'); -const Serializers = require('../DeltaBundler/Serializers'); +const Serializers = require('../DeltaBundler/Serializers/Serializers'); const debug = require('debug')('Metro:Server'); const defaults = require('../defaults'); const formatBundlingError = require('../lib/formatBundlingError'); @@ -33,7 +33,10 @@ const resolveSync: ResolveSync = require('resolve').sync; import type {CustomError} from '../lib/formatBundlingError'; import type {IncomingMessage, ServerResponse} from 'http'; import type {Reporter} from '../lib/reporting'; -import type {DeltaOptions} from '../DeltaBundler/Serializers'; +import type { + DeltaOptions, + RamBundleInfo, +} from '../DeltaBundler/Serializers/Serializers'; import type {BundleOptions, Options} from '../shared/types.flow'; import type { GetTransformOptions, @@ -45,7 +48,6 @@ import type {MetroSourceMap} from 'metro-source-map'; import type {TransformCache} from '../lib/TransformCaching'; import type {Symbolicate} from './symbolicate'; import type {AssetData} from '../Assets'; -import type {RamBundleInfo} from '../DeltaBundler/Serializers'; import type {PostProcessModules} from '../DeltaBundler'; import type {TransformedCode} from '../JSTransformer/worker'; const { @@ -251,7 +253,11 @@ class Server { } async getRamBundleInfo(options: BundleOptions): Promise { - return await Serializers.getRamBundleInfo(this._deltaBundler, options); + return await Serializers.getRamBundleInfo( + this._deltaBundler, + options, + this._opts.getTransformOptions, + ); } async getAssets(options: BundleOptions): Promise<$ReadOnlyArray> { diff --git a/packages/metro/src/lib/__tests__/getOrderedDependencyPaths-test.js b/packages/metro/src/lib/__tests__/getOrderedDependencyPaths-test.js index ea6c62c7..084c9d47 100644 --- a/packages/metro/src/lib/__tests__/getOrderedDependencyPaths-test.js +++ b/packages/metro/src/lib/__tests__/getOrderedDependencyPaths-test.js @@ -10,11 +10,11 @@ 'use strict'; -jest.mock('../../DeltaBundler/Serializers'); +jest.mock('../../DeltaBundler/Serializers/Serializers'); jest.mock('../../Assets'); const getOrderedDependencyPaths = require('../getOrderedDependencyPaths'); -const Serializers = require('../../DeltaBundler/Serializers'); +const Serializers = require('../../DeltaBundler/Serializers/Serializers'); const {getAssetFiles} = require('../../Assets'); diff --git a/packages/metro/src/lib/getOrderedDependencyPaths.js b/packages/metro/src/lib/getOrderedDependencyPaths.js index 74c88780..df6cce92 100644 --- a/packages/metro/src/lib/getOrderedDependencyPaths.js +++ b/packages/metro/src/lib/getOrderedDependencyPaths.js @@ -10,7 +10,7 @@ 'use strict'; -const Serializers = require('../DeltaBundler/Serializers'); +const Serializers = require('../DeltaBundler/Serializers/Serializers'); const {getAssetFiles} = require('../Assets'); diff --git a/packages/metro/src/shared/output/unbundle/as-assets.js b/packages/metro/src/shared/output/unbundle/as-assets.js index 16fa9a5c..e02c812b 100644 --- a/packages/metro/src/shared/output/unbundle/as-assets.js +++ b/packages/metro/src/shared/output/unbundle/as-assets.js @@ -20,7 +20,7 @@ const writeSourceMap = require('./write-sourcemap'); const {joinModules} = require('./util'); -import type {RamBundleInfo} from '../../../DeltaBundler/Serializers'; +import type {RamBundleInfo} from '../../../DeltaBundler/Serializers/Serializers'; import type {OutputOptions} from '../../types.flow'; // must not start with a dot, as that won't go into the apk diff --git a/packages/metro/src/shared/output/unbundle/as-indexed-file.js b/packages/metro/src/shared/output/unbundle/as-indexed-file.js index d4a497df..3329f91e 100644 --- a/packages/metro/src/shared/output/unbundle/as-indexed-file.js +++ b/packages/metro/src/shared/output/unbundle/as-indexed-file.js @@ -18,7 +18,7 @@ const writeSourceMap = require('./write-sourcemap'); const {joinModules} = require('./util'); -import type {RamBundleInfo} from '../../../DeltaBundler/Serializers'; +import type {RamBundleInfo} from '../../../DeltaBundler/Serializers/Serializers'; import type { ModuleGroups, ModuleTransportLike, diff --git a/packages/metro/src/shared/output/unbundle/build-unbundle-sourcemap-with-metadata.js b/packages/metro/src/shared/output/unbundle/build-unbundle-sourcemap-with-metadata.js index 86d6fff3..e96903c8 100644 --- a/packages/metro/src/shared/output/unbundle/build-unbundle-sourcemap-with-metadata.js +++ b/packages/metro/src/shared/output/unbundle/build-unbundle-sourcemap-with-metadata.js @@ -15,7 +15,7 @@ const { joinModules, } = require('./util'); -import type {RamModule} from '../../../DeltaBundler/Serializers'; +import type {RamModule} from '../../../DeltaBundler/Serializers/Serializers'; import type {ModuleGroups, ModuleTransportLike} from '../../types.flow'; type Params = {| diff --git a/packages/metro/src/shared/output/unbundle/index.js b/packages/metro/src/shared/output/unbundle/index.js index 4ebcf6df..ad962dfa 100644 --- a/packages/metro/src/shared/output/unbundle/index.js +++ b/packages/metro/src/shared/output/unbundle/index.js @@ -16,7 +16,7 @@ const asAssets = require('./as-assets'); const asIndexedFile = require('./as-indexed-file').save; import type {OutputOptions, RequestOptions} from '../../types.flow'; -import type {RamBundleInfo} from '../../../DeltaBundler/Serializers'; +import type {RamBundleInfo} from '../../../DeltaBundler/Serializers/Serializers'; async function buildBundle( packagerClient: Server, diff --git a/packages/metro/src/shared/output/unbundle/util.js b/packages/metro/src/shared/output/unbundle/util.js index ed02f4bf..1eaffbc2 100644 --- a/packages/metro/src/shared/output/unbundle/util.js +++ b/packages/metro/src/shared/output/unbundle/util.js @@ -11,7 +11,7 @@ const invariant = require('fbjs/lib/invariant'); -import type {RamModule} from '../../../DeltaBundler/Serializers'; +import type {RamModule} from '../../../DeltaBundler/Serializers/Serializers'; import type {ModuleGroups, ModuleTransportLike} from '../../types.flow'; import type {BabelSourceMap} from '@babel/core'; import type {FBIndexMap, IndexMap, MetroSourceMap} from 'metro-source-map';