Skip to content

Commit

Permalink
Add tests for other hashing situations and fix addon hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Feb 15, 2019
1 parent cd95ae1 commit ac50033
Show file tree
Hide file tree
Showing 240 changed files with 1,037 additions and 65 deletions.
4 changes: 3 additions & 1 deletion src/Chunk.ts
Expand Up @@ -705,7 +705,9 @@ export default class Chunk {
private computeContentHashWithDependencies(addons: Addons, options: OutputOptions): string {
const hash = sha256();

hash.update(addons.hash);
hash.update(
[addons.intro, addons.outro, addons.banner, addons.footer].map(addon => addon || '').join(':')
);
hash.update(options.format);
hash.update(Object.keys(this.exportNames).join(','));
this.visitDependencies(dep => {
Expand Down
5 changes: 1 addition & 4 deletions src/utils/addons.ts
Expand Up @@ -7,7 +7,6 @@ export interface Addons {
outro?: string;
banner?: string;
footer?: string;
hash: Uint8Array;
}

function evalIfFn(strOrFn: string | (() => string | Promise<string>)): string | Promise<string> {
Expand Down Expand Up @@ -38,9 +37,7 @@ export function createAddons(graph: Graph, options: OutputOptions): Promise<Addo
if (banner.length) banner += '\n';
if (footer.length) footer = '\n' + footer;

const hash = new Uint8Array(4);

return { intro, outro, banner, footer, hash };
return { intro, outro, banner, footer };
})
.catch(
(err): any => {
Expand Down
@@ -0,0 +1,5 @@
define(['./chunk-main2-c2e7408c-amd.js'], function (main2) { 'use strict';

main2.log(main2.dep);

});

This file was deleted.

@@ -0,0 +1,7 @@
define(['./chunk-main2-c2e7408c-amd.js'], function (main2) { 'use strict';



return main2.log;

});

This file was deleted.

This file was deleted.

@@ -0,0 +1,5 @@
'use strict';

var main2 = require('./chunk-main2-ae5ee0ea-cjs.js');

main2.log(main2.dep);
@@ -0,0 +1,7 @@
'use strict';

var main2 = require('./chunk-main2-ae5ee0ea-cjs.js');



module.exports = main2.log;

This file was deleted.

This file was deleted.

@@ -0,0 +1,3 @@
import { a as log, b as dep } from './chunk-main2-f2de568c-esm.js';

log(dep);
@@ -0,0 +1 @@
export { a as default } from './chunk-main2-f2de568c-esm.js';

This file was deleted.

@@ -1,4 +1,4 @@
System.register(['./chunk-main2-441b49d2-system.js'], function (exports, module) {
System.register(['./chunk-main2-878d81e1-system.js'], function (exports, module) {
'use strict';
var log, dep;
return {
Expand Down
@@ -1,4 +1,4 @@
System.register(['./chunk-main2-441b49d2-system.js'], function (exports, module) {
System.register(['./chunk-main2-878d81e1-system.js'], function (exports, module) {
'use strict';
return {
setters: [function (module) {
Expand Down
Expand Up @@ -17,13 +17,13 @@ module.exports = {
assert.equal(
code,
'\n' +
`${color}//→ entry1-a47394dc.js:${standard}\n` +
`${color}//→ entry1-d8c4343d.js:${standard}\n` +
"console.log('main1');\n" +
'\n' +
`${color}//→ Entry 2-d6c85f3f.js:${standard}\n` +
`${color}//→ Entry 2-99f48ca0.js:${standard}\n` +
"console.log('main2');\n" +
'\n' +
`${color}//→ main3-22d8845f.js:${standard}\n` +
`${color}//→ main3-a6240449.js:${standard}\n` +
"console.log('main3');\n"
);
}
Expand Down
6 changes: 3 additions & 3 deletions test/cli/samples/code-splitting-named-inputs/_config.js
Expand Up @@ -17,13 +17,13 @@ module.exports = {
assert.equal(
code,
'\n' +
`${color}//→ entry1-a47394dc.js:${standard}\n` +
`${color}//→ entry1-d8c4343d.js:${standard}\n` +
"console.log('main1');\n" +
'\n' +
`${color}//→ Entry 2-d6c85f3f.js:${standard}\n` +
`${color}//→ Entry 2-99f48ca0.js:${standard}\n` +
"console.log('main2');\n" +
'\n' +
`${color}//→ main3-22d8845f.js:${standard}\n` +
`${color}//→ main3-a6240449.js:${standard}\n` +
"console.log('main3');\n"
);
}
Expand Down
18 changes: 10 additions & 8 deletions test/file-hashes/index.js
Expand Up @@ -29,15 +29,15 @@ runTestSuiteWithSamples('file hashes', path.resolve(__dirname, 'samples'), (dir,
assert.equal(
hashMap1[name],
hashMap2[name],
`Expected hashes for chunk containing "${name}" to be equal but they were different.`
`Expected hashes for chunk "${name}" to be equal but they were different.`
);
}
for (const name of config.expectedDifferentHashes || []) {
checkChunkExists(hashMap1, hashMap2, name);
assert.notEqual(
hashMap1[name],
hashMap2[name],
`Expected hashes for chunk containing "${name}" to be different but they were equal.`
`Expected hashes for chunk "${name}" to be different but they were equal.`
);
}
});
Expand All @@ -48,11 +48,13 @@ runTestSuiteWithSamples('file hashes', path.resolve(__dirname, 'samples'), (dir,

function mapGeneratedToHashMap(generated) {
const hashMap = {};
const dirLength = process.cwd().length + 1;
for (const chunk of generated.output) {
for (const moduleName of Object.keys(chunk.modules)) {
hashMap[moduleName.slice(dirLength)] = chunk.fileName;
let name = chunk.name;
let index = 1;
while (hashMap[name]) {
name = `${chunk.name}${index++}`;
}
hashMap[name] = chunk.fileName;
}
return hashMap;
}
Expand All @@ -61,9 +63,9 @@ function checkChunkExists(hashMap1, hashMap2, name) {
[hashMap1, hashMap2].forEach((hashMap, index) =>
assert.ok(
hashMap[name],
`Bundle ${index} did not contain a chunk containing module "${name}", found chunks for modules: ${Object.keys(
hashMap
).join(', ')}`
`Bundle ${index + 1} did not contain chunk "${name}", found chunks: ${Object.keys(hashMap)
.map(key => `"${key}"`)
.join(', ')}`
)
);
}
16 changes: 16 additions & 0 deletions test/file-hashes/samples/banner/_config.js
@@ -0,0 +1,16 @@
module.exports = {
description: 'creates different hashes if the banner differs',
options1: {
input: 'main',
output: {
banner: 'console.log(1);'
}
},
options2: {
input: 'main',
output: {
banner: 'console.log(2);'
}
},
expectedDifferentHashes: ['main']
};
1 change: 1 addition & 0 deletions test/file-hashes/samples/banner/main.js
@@ -0,0 +1 @@
console.log('main');
11 changes: 11 additions & 0 deletions test/file-hashes/samples/content/_config.js
@@ -0,0 +1,11 @@
module.exports = {
description: 'creates different hashes if the content differs',
options1: {
input: { main: 'main1', dep: 'dep1' }
},
options2: {
input: { main: 'main2', dep: 'dep2' }
},
expectedDifferentHashes: ['main'],
expectedEqualHashes: ['dep']
};
@@ -0,0 +1,5 @@
define(function () { 'use strict';

console.log('main4');

});
@@ -0,0 +1,5 @@
define(function () { 'use strict';

console.log('main5');

});
7 changes: 7 additions & 0 deletions test/file-hashes/samples/content/_expected/amd/main1.js
@@ -0,0 +1,7 @@
define(['require'], function (require) { 'use strict';

console.log('main1');
new Promise(function (resolve, reject) { require(['./generated-chunk.js'], resolve, reject) });
new Promise(function (resolve, reject) { require(['./generated-chunk2.js'], resolve, reject) });

});
5 changes: 5 additions & 0 deletions test/file-hashes/samples/content/_expected/amd/main2.js
@@ -0,0 +1,5 @@
define(function () { 'use strict';

console.log('main2');

});
5 changes: 5 additions & 0 deletions test/file-hashes/samples/content/_expected/amd/main3.js
@@ -0,0 +1,5 @@
define(function () { 'use strict';

console.log('main3');

});
@@ -0,0 +1,3 @@
'use strict';

console.log('main4');
@@ -0,0 +1,3 @@
'use strict';

console.log('main5');
5 changes: 5 additions & 0 deletions test/file-hashes/samples/content/_expected/cjs/main1.js
@@ -0,0 +1,5 @@
'use strict';

console.log('main1');
Promise.resolve(require('./generated-chunk.js'));
Promise.resolve(require('./generated-chunk2.js'));
3 changes: 3 additions & 0 deletions test/file-hashes/samples/content/_expected/cjs/main2.js
@@ -0,0 +1,3 @@
'use strict';

console.log('main2');
3 changes: 3 additions & 0 deletions test/file-hashes/samples/content/_expected/cjs/main3.js
@@ -0,0 +1,3 @@
'use strict';

console.log('main3');
@@ -0,0 +1 @@
console.log('main4');
@@ -0,0 +1 @@
console.log('main5');
3 changes: 3 additions & 0 deletions test/file-hashes/samples/content/_expected/es/main1.js
@@ -0,0 +1,3 @@
console.log('main1');
import('./generated-chunk.js');
import('./generated-chunk2.js');
1 change: 1 addition & 0 deletions test/file-hashes/samples/content/_expected/es/main2.js
@@ -0,0 +1 @@
console.log('main2');
1 change: 1 addition & 0 deletions test/file-hashes/samples/content/_expected/es/main3.js
@@ -0,0 +1 @@
console.log('main3');
@@ -0,0 +1,10 @@
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

console.log('main4');

}
};
});
@@ -0,0 +1,10 @@
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

console.log('main5');

}
};
});
12 changes: 12 additions & 0 deletions test/file-hashes/samples/content/_expected/system/main1.js
@@ -0,0 +1,12 @@
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

console.log('main1');
module.import('./generated-chunk.js');
module.import('./generated-chunk2.js');

}
};
});
10 changes: 10 additions & 0 deletions test/file-hashes/samples/content/_expected/system/main2.js
@@ -0,0 +1,10 @@
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

console.log('main2');

}
};
});
10 changes: 10 additions & 0 deletions test/file-hashes/samples/content/_expected/system/main3.js
@@ -0,0 +1,10 @@
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

console.log('main3');

}
};
});
1 change: 1 addition & 0 deletions test/file-hashes/samples/content/dep1.js
@@ -0,0 +1 @@
console.log('dep');
1 change: 1 addition & 0 deletions test/file-hashes/samples/content/dep2.js
@@ -0,0 +1 @@
console.log('dep');
3 changes: 3 additions & 0 deletions test/file-hashes/samples/content/main1.js
@@ -0,0 +1,3 @@
import './dep1';

console.log('main1');
3 changes: 3 additions & 0 deletions test/file-hashes/samples/content/main2.js
@@ -0,0 +1,3 @@
import './dep2';

console.log('main2');
10 changes: 10 additions & 0 deletions test/file-hashes/samples/dependency-content/_config.js
@@ -0,0 +1,10 @@
module.exports = {
description: 'creates different hashes if the content of dependencies differs',
options1: {
input: { main: 'main1', dep: 'dep1' }
},
options2: {
input: { main: 'main2', dep: 'dep2' }
},
expectedDifferentHashes: ['main', 'dep']
};
@@ -0,0 +1,5 @@
define(function () { 'use strict';

console.log('main4');

});
@@ -0,0 +1,5 @@
define(function () { 'use strict';

console.log('main5');

});
@@ -0,0 +1,7 @@
define(['require'], function (require) { 'use strict';

console.log('main1');
new Promise(function (resolve, reject) { require(['./generated-chunk.js'], resolve, reject) });
new Promise(function (resolve, reject) { require(['./generated-chunk2.js'], resolve, reject) });

});
@@ -0,0 +1,5 @@
define(function () { 'use strict';

console.log('main2');

});

0 comments on commit ac50033

Please sign in to comment.