Use the new Graph object for returning bundle dependencies

Reviewed By: mjesun

Differential Revision: D7275602

fbshipit-source-id: 9c07d85b085c297ed5efe958e3b7efe001f3542f
This commit is contained in:
Rafael Oleza 2018-03-20 06:53:17 -07:00 committed by Facebook Github Bot
parent 0520fb254b
commit 12fe345e1b
8 changed files with 136 additions and 256 deletions

View File

@ -114,15 +114,6 @@ async function fullBundle(
};
}
async function getAllModules(
deltaBundler: DeltaBundler,
options: BundleOptions,
): Promise<$ReadOnlyArray<DeltaEntry>> {
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,
};

View File

@ -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();
});
});

View File

@ -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],

View File

@ -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],
]);
});
});

View File

@ -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<DependencyEdge>,
graph: Graph,
options: Options,
): Promise<$ReadOnlyArray<string>> {
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;

View File

@ -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<Array<string>> {
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<GraphInfo> {

View File

@ -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],
]);
});
});

View File

@ -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<Array<string>> {
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;