From 12fe345e1b5b34436333fd8e0c4a17474fcd0b0e Mon Sep 17 00:00:00 2001 From: Rafael Oleza Date: Tue, 20 Mar 2018 06:53:17 -0700 Subject: [PATCH] Use the new Graph object for returning bundle dependencies Reviewed By: mjesun Differential Revision: D7275602 fbshipit-source-id: 9c07d85b085c297ed5efe958e3b7efe001f3542f --- .../DeltaBundler/Serializers/Serializers.js | 10 --- .../Serializers/__tests__/Serializers-test.js | 25 ------ .../__snapshots__/Serializers-test.js.snap | 87 ------------------- .../Serializers/__tests__/getAllFiles-test.js | 78 +++++++++++++++++ .../DeltaBundler/Serializers/getAllFiles.js | 48 ++++++++++ packages/metro/src/Server/index.js | 21 +++-- .../getOrderedDependencyPaths-test.js | 80 ----------------- .../src/lib/getOrderedDependencyPaths.js | 43 --------- 8 files changed, 136 insertions(+), 256 deletions(-) create mode 100644 packages/metro/src/DeltaBundler/Serializers/__tests__/getAllFiles-test.js create mode 100644 packages/metro/src/DeltaBundler/Serializers/getAllFiles.js delete mode 100644 packages/metro/src/lib/__tests__/getOrderedDependencyPaths-test.js delete mode 100644 packages/metro/src/lib/getOrderedDependencyPaths.js diff --git a/packages/metro/src/DeltaBundler/Serializers/Serializers.js b/packages/metro/src/DeltaBundler/Serializers/Serializers.js index a5739010..32c50222 100644 --- a/packages/metro/src/DeltaBundler/Serializers/Serializers.js +++ b/packages/metro/src/DeltaBundler/Serializers/Serializers.js @@ -114,15 +114,6 @@ async function fullBundle( }; } -async function getAllModules( - deltaBundler: DeltaBundler, - options: BundleOptions, -): Promise<$ReadOnlyArray> { - const {modules} = await _getAllModules(deltaBundler, options); - - return modules; -} - async function _getAllModules( deltaBundler: DeltaBundler, options: BundleOptions, @@ -264,6 +255,5 @@ module.exports = { fullBundle, fullSourceMap, fullSourceMapObject, - getAllModules, getRamBundleInfo, }; diff --git a/packages/metro/src/DeltaBundler/Serializers/__tests__/Serializers-test.js b/packages/metro/src/DeltaBundler/Serializers/__tests__/Serializers-test.js index 79707b8c..12ab1c90 100644 --- a/packages/metro/src/DeltaBundler/Serializers/__tests__/Serializers-test.js +++ b/packages/metro/src/DeltaBundler/Serializers/__tests__/Serializers-test.js @@ -141,21 +141,6 @@ describe('Serializers', () => { expect(await Serializers.fullSourceMap(deltaBundler, {})).toMatchSnapshot(); }); - it('should return all the bundle modules', async () => { - expect(await Serializers.getAllModules(deltaBundler, {})).toMatchSnapshot(); - - getDelta.mockReturnValueOnce( - Promise.resolve({ - delta: new Map([[3, {code: 'modified module;'}], [4, null]]), - pre: new Map([[5, {code: 'more pre;'}]]), - post: new Map([[6, {code: 'bananas;'}], [7, {code: 'apples;'}]]), - inverseDependencies: [], - }), - ); - - expect(await Serializers.getAllModules(deltaBundler, {})).toMatchSnapshot(); - }); - it('should return the RAM bundle info', async () => { expect( await Serializers.getRamBundleInfo(deltaBundler, {}), @@ -225,14 +210,4 @@ describe('Serializers', () => { await Serializers.getRamBundleInfo(deltaBundler, {}, getTransformOptions), ).toMatchSnapshot(); }); - - it('should post-process the modules', async () => { - postProcessModules.mockImplementation(modules => { - return modules.sort( - (a, b) => +(a.path < b.path) || +(a.path === b.path) - 1, - ); - }); - - expect(await Serializers.getAllModules(deltaBundler, {})).toMatchSnapshot(); - }); }); diff --git a/packages/metro/src/DeltaBundler/Serializers/__tests__/__snapshots__/Serializers-test.js.snap b/packages/metro/src/DeltaBundler/Serializers/__tests__/__snapshots__/Serializers-test.js.snap index c860c918..cd1a29d7 100644 --- a/packages/metro/src/DeltaBundler/Serializers/__tests__/__snapshots__/Serializers-test.js.snap +++ b/packages/metro/src/DeltaBundler/Serializers/__tests__/__snapshots__/Serializers-test.js.snap @@ -28,93 +28,6 @@ exports[`Serializers should build the full Source Maps 1`] = `"{\\"version\\":3, exports[`Serializers should build the full Source Maps 2`] = `"{\\"version\\":3,\\"sources\\":[],\\"sourcesContent\\":[],\\"names\\":[],\\"mappings\\":\\"\\"}"`; -exports[`Serializers should post-process the modules 1`] = ` -Array [ - Object { - "code": "pre;", - "id": 1, - "path": "/pre.js", - "type": "script", - }, - Object { - "code": "another;", - "id": 4, - "path": "/4.js", - "type": "module", - }, - Object { - "code": "module3;", - "id": 3, - "path": "/3.js", - "type": "module", - }, - Object { - "code": "post;", - "id": 2, - "path": "/p", - "type": "require", - }, -] -`; - -exports[`Serializers should return all the bundle modules 1`] = ` -Array [ - Object { - "code": "pre;", - "id": 1, - "path": "/pre.js", - "type": "script", - }, - Object { - "code": "module3;", - "id": 3, - "path": "/3.js", - "type": "module", - }, - Object { - "code": "another;", - "id": 4, - "path": "/4.js", - "type": "module", - }, - Object { - "code": "post;", - "id": 2, - "path": "/p", - "type": "require", - }, -] -`; - -exports[`Serializers should return all the bundle modules 2`] = ` -Array [ - Object { - "code": "pre;", - "id": 1, - "path": "/pre.js", - "type": "script", - }, - Object { - "code": "more pre;", - }, - Object { - "code": "modified module;", - }, - Object { - "code": "post;", - "id": 2, - "path": "/p", - "type": "require", - }, - Object { - "code": "bananas;", - }, - Object { - "code": "apples;", - }, -] -`; - exports[`Serializers should return the RAM bundle info 1`] = ` Object { "getDependencies": [Function], diff --git a/packages/metro/src/DeltaBundler/Serializers/__tests__/getAllFiles-test.js b/packages/metro/src/DeltaBundler/Serializers/__tests__/getAllFiles-test.js new file mode 100644 index 00000000..9d45db1d --- /dev/null +++ b/packages/metro/src/DeltaBundler/Serializers/__tests__/getAllFiles-test.js @@ -0,0 +1,78 @@ +/** + * 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. + * + * @emails oncall+javascript_foundation + * @format + */ + +'use strict'; + +jest.mock('../../../Assets'); + +const getAllFiles = require('../getAllFiles'); + +const {getAssetFiles} = require('../../../Assets'); + +describe('getOrderedDependencyPaths', () => { + beforeEach(() => { + getAssetFiles.mockImplementation(async path => [ + `${path}@2x`, + `${path}@3x`, + ]); + }); + + it('Should return all module dependencies correctly', async () => { + const graph = { + dependencies: new Map([ + [1, {path: '/tmp/1.js', output: {type: 'module'}}], + [2, {path: '/tmp/2.js', output: {type: 'module'}}], + [3, {path: '/tmp/3.js', output: {type: 'module'}}], + [4, {path: '/tmp/4.js', output: {type: 'module'}}], + ]), + }; + + expect( + await getAllFiles( + [{path: '/tmp/0.js', output: {type: 'module'}}], + graph, + {}, + ), + ).toEqual([ + '/tmp/0.js', + '/tmp/1.js', + '/tmp/2.js', + '/tmp/3.js', + '/tmp/4.js', + ]); + }); + + it('Should add assets data dependencies correctly', async () => { + const graph = { + dependencies: new Map([ + [1, {path: '/tmp/1.js', output: {type: 'module'}}], + [2, {path: '/tmp/2.png', output: {type: 'asset'}}], + [3, {path: '/tmp/3.js', output: {type: 'module'}}], + [4, {path: '/tmp/4.png', output: {type: 'asset'}}], + [5, {path: '/tmp/5.js', output: {type: 'module'}}], + ]), + }; + + expect(await getAllFiles([], graph, {})).toEqual([ + '/tmp/1.js', + '/tmp/2.png@2x', + '/tmp/2.png@3x', + '/tmp/3.js', + '/tmp/4.png@2x', + '/tmp/4.png@3x', + '/tmp/5.js', + ]); + + expect(getAssetFiles.mock.calls).toEqual([ + ['/tmp/2.png', undefined], + ['/tmp/4.png', undefined], + ]); + }); +}); diff --git a/packages/metro/src/DeltaBundler/Serializers/getAllFiles.js b/packages/metro/src/DeltaBundler/Serializers/getAllFiles.js new file mode 100644 index 00000000..38194c8b --- /dev/null +++ b/packages/metro/src/DeltaBundler/Serializers/getAllFiles.js @@ -0,0 +1,48 @@ +/** + * 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'; + +const {getAssetFiles} = require('../../Assets'); + +import type {Graph} from '../DeltaCalculator'; +import type {DependencyEdge} from '../traverseDependencies'; + +type Options = {| + platform: ?string, +|}; + +async function getAllFiles( + pre: $ReadOnlyArray, + graph: Graph, + options: Options, +): Promise<$ReadOnlyArray> { + const modules = graph.dependencies; + + const dependencies = await Promise.all( + [...pre, ...modules.values()].map(async module => { + if (module.output.type !== 'asset') { + return [module.path]; + } else { + return await getAssetFiles(module.path, options.platform); + } + }), + ); + + const output = []; + + for (const dependencyArray of dependencies) { + output.push(...dependencyArray); + } + + return output; +} + +module.exports = getAllFiles; diff --git a/packages/metro/src/Server/index.js b/packages/metro/src/Server/index.js index b4ab61c3..934aa7e7 100644 --- a/packages/metro/src/Server/index.js +++ b/packages/metro/src/Server/index.js @@ -16,6 +16,7 @@ const MultipartResponse = require('./MultipartResponse'); const Serializers = require('../DeltaBundler/Serializers/Serializers'); const defaultCreateModuleIdFactory = require('../lib/createModuleIdFactory'); +const getAllFiles = require('../DeltaBundler/Serializers/getAllFiles'); const getAssets = require('../DeltaBundler/Serializers/getAssets'); const plainJSBundle = require('../DeltaBundler/Serializers/plainJSBundle'); const sourceMapString = require('../DeltaBundler/Serializers/sourceMapString'); @@ -24,7 +25,6 @@ const defaults = require('../defaults'); const formatBundlingError = require('../lib/formatBundlingError'); const getAbsolutePath = require('../lib/getAbsolutePath'); const getMaxWorkers = require('../lib/getMaxWorkers'); -const getOrderedDependencyPaths = require('../lib/getOrderedDependencyPaths'); const getPrependedScripts = require('../lib/getPrependedScripts'); const mime = require('mime-types'); const mapGraph = require('../lib/mapGraph'); @@ -301,21 +301,20 @@ class Server { +platform: string, +minify: boolean, }): Promise> { - const bundleOptions = { + options = { ...Server.DEFAULT_BUNDLE_OPTIONS, ...options, - entryFile: getAbsolutePath(options.entryFile, this._opts.projectRoots), - bundleType: 'delta', + minify: false, // minification does not affect the dependencies. + bundleType: 'bundle', }; - if (!bundleOptions.platform) { - bundleOptions.platform = parsePlatformFilePath( - bundleOptions.entryFile, - this._platforms, - ).platform; - } + const {prepend, graph} = await this._buildGraph(options); - return await getOrderedDependencyPaths(this._deltaBundler, bundleOptions); + const platform = + options.platform || + parsePlatformFilePath(options.entryFile, this._platforms).platform; + + return await getAllFiles(prepend, graph, {platform}); } async _buildGraph(options: BundleOptions): Promise { diff --git a/packages/metro/src/lib/__tests__/getOrderedDependencyPaths-test.js b/packages/metro/src/lib/__tests__/getOrderedDependencyPaths-test.js deleted file mode 100644 index 084c9d47..00000000 --- a/packages/metro/src/lib/__tests__/getOrderedDependencyPaths-test.js +++ /dev/null @@ -1,80 +0,0 @@ -/** - * 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. - * - * @emails oncall+javascript_foundation - * @format - */ - -'use strict'; - -jest.mock('../../DeltaBundler/Serializers/Serializers'); -jest.mock('../../Assets'); - -const getOrderedDependencyPaths = require('../getOrderedDependencyPaths'); -const Serializers = require('../../DeltaBundler/Serializers/Serializers'); - -const {getAssetFiles} = require('../../Assets'); - -describe('getOrderedDependencyPaths', () => { - const deltaBundler = {}; - - beforeEach(() => { - getAssetFiles.mockImplementation(async path => [ - `${path}@2x`, - `${path}@3x`, - ]); - }); - - it('Should return all module dependencies correctly', async () => { - Serializers.getAllModules.mockReturnValue( - Promise.resolve( - new Map([ - [1, {path: '/tmp/1.js'}], - [2, {path: '/tmp/2.js'}], - [3, {path: '/tmp/3.js'}], - [4, {path: '/tmp/4.js'}], - ]), - ), - ); - - expect(await getOrderedDependencyPaths(deltaBundler, ['/tmp'], {})).toEqual( - ['/tmp/1.js', '/tmp/2.js', '/tmp/3.js', '/tmp/4.js'], - ); - }); - - it('Should add assets data dependencies correctly', async () => { - deltaBundler.getOptions = () => ({projectRoots: ['/root']}); - - Serializers.getAllModules.mockReturnValue( - Promise.resolve( - new Map([ - [1, {path: '/tmp/1.js'}], - [2, {path: '/tmp/2.png', type: 'asset'}], - [3, {path: '/tmp/3.js'}], - [4, {path: '/tmp/4.png', type: 'asset'}], - [5, {path: '/tmp/5.js'}], - ]), - ), - ); - - expect(await getOrderedDependencyPaths(deltaBundler, ['/tmp'], {})).toEqual( - [ - '/tmp/1.js', - '/tmp/2.png@2x', - '/tmp/2.png@3x', - '/tmp/3.js', - '/tmp/4.png@2x', - '/tmp/4.png@3x', - '/tmp/5.js', - ], - ); - - expect(getAssetFiles.mock.calls).toEqual([ - ['/tmp/2.png', undefined], - ['/tmp/4.png', undefined], - ]); - }); -}); diff --git a/packages/metro/src/lib/getOrderedDependencyPaths.js b/packages/metro/src/lib/getOrderedDependencyPaths.js deleted file mode 100644 index df6cce92..00000000 --- a/packages/metro/src/lib/getOrderedDependencyPaths.js +++ /dev/null @@ -1,43 +0,0 @@ -/** - * 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'; - -const Serializers = require('../DeltaBundler/Serializers/Serializers'); - -const {getAssetFiles} = require('../Assets'); - -import type DeltaBundler from '../DeltaBundler'; -import type {BundleOptions} from '../shared/types.flow'; - -async function getOrderedDependencyPaths( - deltaBundler: DeltaBundler, - options: BundleOptions, -): Promise> { - const modules = await Serializers.getAllModules(deltaBundler, options); - - const dependencies = await Promise.all( - Array.from(modules.values()).map(async module => { - if (module.type !== 'asset') { - return [module.path]; - } else { - return await getAssetFiles(module.path, options.platform); - } - }), - ); - - const output = []; - for (const dependencyArray of dependencies) { - output.push(...dependencyArray); - } - return output; -} - -module.exports = getOrderedDependencyPaths;