Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use explicit key for tracking dependencies in bundler #835

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ beforeEach(() => {
'bar',
{
absolutePath: '/bar',
data: {data: {asyncType: null, locs: []}, name: 'bar'},
data: {data: {asyncType: null, locs: [], key: 'bar'}, name: 'bar'},
},
],
[
'baz',
{
absolutePath: '/baz',
data: {data: {asyncType: null, locs: []}, name: 'baz'},
data: {data: {asyncType: null, locs: [], key: 'baz'}, name: 'baz'},
},
],
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ beforeEach(() => {
'bar',
{
absolutePath: '/bar',
data: {data: {asyncType: null, locs: []}, name: 'bar'},
data: {data: {asyncType: null, locs: [], key: 'bar'}, name: 'bar'},
},
],
[
'baz',
{
absolutePath: '/baz',
data: {data: {asyncType: null, locs: []}, name: 'baz'},
data: {data: {asyncType: null, locs: [], key: 'baz'}, name: 'baz'},
},
],
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ Object {
"dependencies": Map {
"/bundle" => Object {
"dependencies": Map {
"foo" => Object {
"C+7Hteo/D9vJXQ3UfzxbwnXaijM=" => Object {
"absolutePath": "/foo",
"data": Object {
"data": Object {
"asyncType": null,
"key": "C+7Hteo/D9vJXQ3UfzxbwnXaijM=",
"locs": Array [],
},
"name": "foo",
Expand All @@ -32,21 +33,23 @@ Object {
},
"/foo" => Object {
"dependencies": Map {
"bar" => Object {
"Ys23Ag/5IOWqZCw9QGaVDdHwH00=" => Object {
"absolutePath": "/bar",
"data": Object {
"data": Object {
"asyncType": null,
"key": "Ys23Ag/5IOWqZCw9QGaVDdHwH00=",
"locs": Array [],
},
"name": "bar",
},
},
"baz" => Object {
"u+lgol6jEdIdQGaek98gA7qbkKI=" => Object {
"absolutePath": "/baz",
"data": Object {
"data": Object {
"asyncType": null,
"key": "u+lgol6jEdIdQGaek98gA7qbkKI=",
"locs": Array [],
},
"name": "baz",
Expand Down Expand Up @@ -127,11 +130,12 @@ Object {
"dependencies": Map {
"/bundle" => Object {
"dependencies": Map {
"foo" => Object {
"C+7Hteo/D9vJXQ3UfzxbwnXaijM=" => Object {
"absolutePath": "/foo",
"data": Object {
"data": Object {
"asyncType": null,
"key": "C+7Hteo/D9vJXQ3UfzxbwnXaijM=",
"locs": Array [],
},
"name": "foo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const {
traverseDependencies: traverseDependenciesImpl,
} = require('../graphOperations');

const {objectContaining} = expect;

type DependencyDataInput = $Shape<TransformResultDependency['data']>;

let mockedDependencies: Set<string> = new Set();
Expand All @@ -30,7 +32,7 @@ let mockedDependencyTree: Map<
$ReadOnly<{
name: string,
path: string,
data?: DependencyDataInput,
data: DependencyDataInput,
}>,
>,
> = new Map();
Expand Down Expand Up @@ -79,11 +81,22 @@ const Actions = {
data?: DependencyDataInput,
) {
const deps = nullthrows(mockedDependencyTree.get(path));
const depName = name ?? dependencyPath.replace('/', '');
const key = require('crypto')
.createHash('sha1')
.update(depName)
.digest('base64');
const dep = {
name: name ?? dependencyPath.replace('/', ''),
name: depName,
path: dependencyPath,
data: data ?? {},
data: {key, ...(data ?? {})},
};
if (
deps.findIndex(existingDep => existingDep.data.key === dep.data.key) !==
-1
) {
throw new Error('Found existing mock dep with key: ' + dep.data.key);
}
if (position == null) {
deps.push(dep);
} else {
Expand Down Expand Up @@ -221,6 +234,7 @@ beforeEach(async () => {
data: {
asyncType: null,
locs: [],
key: dep.data.key,
...dep.data,
},
})),
Expand Down Expand Up @@ -787,24 +801,33 @@ describe('edge cases', () => {
...nullthrows(graph.dependencies.get(moduleFoo)).dependencies,
]).toEqual([
[
'qux',
expect.any(String),
{
absolutePath: '/qux',
data: {data: {asyncType: null, locs: []}, name: 'qux'},
data: {
data: objectContaining({asyncType: null, locs: []}),
name: 'qux',
},
},
],
[
'bar',
expect.any(String),
{
absolutePath: '/bar',
data: {data: {asyncType: null, locs: []}, name: 'bar'},
data: {
data: objectContaining({asyncType: null, locs: []}),
name: 'bar',
},
},
],
[
'baz',
expect.any(String),
{
absolutePath: '/baz',
data: {data: {asyncType: null, locs: []}, name: 'baz'},
data: {
data: objectContaining({asyncType: null, locs: []}),
name: 'baz',
},
},
],
]);
Expand Down Expand Up @@ -1901,21 +1924,21 @@ describe('edge cases', () => {
...nullthrows(graph.dependencies.get(entryModule)).dependencies,
]).toEqual([
[
'foo.js',
expect.any(String),
{
absolutePath: '/foo',
data: {
data: {asyncType: null, locs: []},
data: objectContaining({asyncType: null, locs: []}),
name: 'foo.js',
},
},
],
[
'foo',
expect.any(String),
{
absolutePath: '/foo',
data: {
data: {asyncType: null, locs: []},
data: objectContaining({asyncType: null, locs: []}),
name: 'foo',
},
},
Expand Down Expand Up @@ -2007,12 +2030,12 @@ describe('edge cases', () => {
[
entryModule,
[
{name: 'foo', path: moduleFoo},
{name: 'bar', path: moduleBar},
{name: 'foo', path: moduleFoo, data: {key: 'foo'}},
{name: 'bar', path: moduleBar, data: {key: 'bar'}},
],
],
[moduleFoo, [{name: 'baz', path: moduleBaz}]],
[moduleBar, [{name: 'baz', path: moduleBaz}]],
[moduleFoo, [{name: 'baz', path: moduleBaz, data: {key: 'baz'}}]],
[moduleBar, [{name: 'baz', path: moduleBaz, data: {key: 'baz'}}]],
]);

// Test that even when having different modules taking longer, the order
Expand All @@ -2039,6 +2062,7 @@ describe('reorderGraph', () => {
data: {
asyncType: null,
locs: [],
key: path.substr(1),
},
name: path.substr(1),
},
Expand Down Expand Up @@ -2171,17 +2195,14 @@ describe('optional dependencies', () => {
});

describe('parallel edges', () => {
it('add twice w/ same key, build and remove once', async () => {
it('add twice w/ same name, build and remove once', async () => {
// Create a second edge between /foo and /bar.
Actions.addDependency('/foo', '/bar', undefined);
Actions.addDependency('/foo', '/bar', undefined, undefined, {
key: 'bar-second-key',
});

await initialTraverseDependencies(graph, options);

const fooDeps = nullthrows(graph.dependencies.get('/foo')).dependencies;
const fooDepsResolved = [...fooDeps.values()].map(dep => dep.absolutePath);
// We dedupe the dependencies because they have the same `name`.
expect(fooDepsResolved).toEqual(['/bar', '/baz']);

// Remove one of the edges between /foo and /bar (arbitrarily)
Actions.removeDependency('/foo', '/bar');

Expand All @@ -2194,17 +2215,14 @@ describe('parallel edges', () => {
});
});

it('add twice w/ same key, build and remove twice', async () => {
it('add twice w/ same name, build and remove twice', async () => {
// Create a second edge between /foo and /bar.
Actions.addDependency('/foo', '/bar', undefined);
Actions.addDependency('/foo', '/bar', undefined, undefined, {
key: 'bar-second-key',
});

await initialTraverseDependencies(graph, options);

const fooDeps = nullthrows(graph.dependencies.get('/foo')).dependencies;
const fooDepsResolved = [...fooDeps.values()].map(dep => dep.absolutePath);
// We dedupe the dependencies because they have the same `name`.
expect(fooDepsResolved).toEqual(['/bar', '/baz']);

// Remove both edges between /foo and /bar
Actions.removeDependency('/foo', '/bar');
Actions.removeDependency('/foo', '/bar');
Expand All @@ -2218,17 +2236,12 @@ describe('parallel edges', () => {
});
});

it('add twice w/ different keys, build and remove once', async () => {
it('add twice w/ different names, build and remove once', async () => {
// Create a second edge between /foo and /bar, with a different `name`.
Actions.addDependency('/foo', '/bar', undefined, 'bar-second');

await initialTraverseDependencies(graph, options);

const fooDeps = nullthrows(graph.dependencies.get('/foo')).dependencies;
const fooDepsResolved = [...fooDeps.values()].map(dep => dep.absolutePath);
// We don't dedupe the dependencies because they have different `name`s.
expect(fooDepsResolved).toEqual(['/bar', '/baz', '/bar']);

// Remove one of the edges between /foo and /bar (arbitrarily)
Actions.removeDependency('/foo', '/bar');

Expand All @@ -2241,17 +2254,12 @@ describe('parallel edges', () => {
});
});

it('add twice w/ different keys, build and remove twice', async () => {
it('add twice w/ different names, build and remove twice', async () => {
// Create a second edge between /foo and /bar, with a different `name`.
Actions.addDependency('/foo', '/bar', undefined, 'bar-second');

await initialTraverseDependencies(graph, options);

const fooDeps = nullthrows(graph.dependencies.get('/foo')).dependencies;
const fooDepsResolved = [...fooDeps.values()].map(dep => dep.absolutePath);
// We don't dedupe the dependencies because they have different `name`s.
expect(fooDepsResolved).toEqual(['/bar', '/baz', '/bar']);

// Remove both edges between /foo and /bar
Actions.removeDependency('/foo', '/bar');
Actions.removeDependency('/foo', '/bar');
Expand Down
Loading