WIP: Adding an output to provide a dot file with the dependency graph

Reviewed By: davidaurelio

Differential Revision: D5284123

fbshipit-source-id: e7c7519c4fefb66245dab3c5f205a8643067693a
This commit is contained in:
Miguel Jimenez Esun 2017-06-21 06:53:00 -07:00 committed by Facebook Github Bot
parent b462183830
commit 30458c15ae
4 changed files with 137 additions and 3 deletions

View File

@ -0,0 +1,98 @@
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';
declare var jest: any;
const dependenciesDot = require('../dependencies-dot');
declare var describe: any;
declare var expect: any;
declare var it: (string, () => ?Promise<any>) => void;
declare var beforeAll: (() => ?Promise<any>) => void;
expect.extend({
toBeAMultilineString(received, ...args) {
const built = args.join('\n');
return {
pass: received === built,
message: `Expected ${received} to equal ${built}`,
};
},
});
describe('dependencies-dot', () => {
it('produces a valid digraph file for an empty set of modules', () => {
expect(dependenciesDot({modules: []})).toBeAMultilineString(
'digraph {',
'}',
);
});
it('produces an ordered file for a standard list of modules', () => {
expect(dependenciesDot({modules: [
createModule('a', ['b']),
createModule('b', ['c']),
createModule('c', []),
]})).toBeAMultilineString(
'digraph {',
'\t"a" -> "b";',
'\t"b" -> "c";',
'}',
);
});
it('writes one entry per dependency', () => {
expect(dependenciesDot({modules: [
createModule('a', ['b', 'c']),
createModule('b', ['d']),
createModule('c', []),
createModule('d', []),
]})).toBeAMultilineString(
'digraph {',
'\t"a" -> "b";',
'\t"a" -> "c";',
'\t"b" -> "d";',
'}',
);
});
it('handles non-printable characters', () => {
expect(dependenciesDot({modules: [
createModule('"\n', ['\r\t']),
createModule('\r\t', []),
]})).toBeAMultilineString(
'digraph {',
'\t"\\"\\n" -> "\\r\\t";',
'}',
);
});
it('handles circular dependencies', () => {
expect(dependenciesDot({modules: [
createModule('a', ['b']),
createModule('b', ['a']),
]})).toBeAMultilineString(
'digraph {',
'\t"a" -> "b";',
'\t"b" -> "a";',
'}',
);
});
});
function createModule(path: string, deps: Array<string>) {
return {
file: {code: '', map: null, path, type: 'module'},
dependencies: deps.map(d => ({id: d, path: d})),
};
}

View File

@ -21,7 +21,7 @@ declare var expect: any;
declare var it: (string, () => ?Promise<any>) => void;
declare var beforeAll: (() => ?Promise<any>) => void;
let code: Buffer;
let code;
let map;
let extraFiles;
let ids, modules, requireCall;
@ -46,7 +46,7 @@ it('does not start the bundle file with the magic number (not a binary one)', ()
});
it('contains the startup code on the main file', () => {
expect(code.toString()).toBe('require(1);');
expect(code).toBe('require(1);');
});
it('creates a source map', () => {

View File

@ -0,0 +1,36 @@
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';
import type {Module} from '../types.flow';
function dependenciesDot({modules}: {+modules: Iterable<Module>}) {
const list = [];
// Opening digraph.
list.push('digraph {');
// Adding each module -> dependency.
for (const module of modules) {
const file = JSON.stringify(module.file.path);
module.dependencies.forEach(dependency => {
list.push(`\t${file} -> ${JSON.stringify(dependency.path)};`);
});
}
// Closing digraph.
list.push('}');
return list.join('\n');
}
module.exports = dependenciesDot;

View File

@ -42,7 +42,7 @@ function asMultipleFilesRamBundle({
deferredModules.forEach(deferredModule => {
extraFiles.set(
path.join(JS_MODULES, deferredModule.id + '.js'),
new Buffer(deferredModule.code),
deferredModule.code,
);
});