From 4d082b09a396d534d95a306d3986f341af83fc46 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Sun, 17 Feb 2019 08:30:19 +0100 Subject: [PATCH] Respect rendered exports when generating chunk hashes (#2695) * Create hash test type and red test * Consider actually exported names in hash, resolve #2692 * Add tests for other hashing situations and fix addon hashing * Rework test to check hashes and contents of all files and throw if two files with different content share the same hash; fix another issue for exports that was uncovered by this. * Remove unnecessary expectations --- src/Chunk.ts | 5 +- src/utils/addons.ts | 5 +- ...4aa-amd.js => chunk-main2-0a527b43-amd.js} | 0 .../_expected/amd/entry-main1-0d1fa73e-amd.js | 5 ++ .../_expected/amd/entry-main1-a0f4fde5-amd.js | 5 -- .../_expected/amd/entry-main2-236b789c-amd.js | 7 --- .../_expected/amd/entry-main2-adf6abe8-amd.js | 7 +++ ...de3-cjs.js => chunk-main2-328c93f2-cjs.js} | 0 .../_expected/cjs/entry-main1-0dfd3e0e-cjs.js | 5 ++ .../_expected/cjs/entry-main1-3cd10506-cjs.js | 5 -- .../_expected/cjs/entry-main2-64fa0b57-cjs.js | 7 --- .../_expected/cjs/entry-main2-b34781c2-cjs.js | 7 +++ ...ba2-esm.js => chunk-main2-d7d48284-esm.js} | 0 .../_expected/es/entry-main1-5a1ab158-esm.js | 3 -- .../_expected/es/entry-main1-a2c4132e-esm.js | 3 ++ .../_expected/es/entry-main2-52329ca1-esm.js | 1 - .../_expected/es/entry-main2-bc7681ae-esm.js | 1 + ...stem.js => chunk-main2-d134c6d9-system.js} | 0 ...stem.js => entry-main1-4e36d280-system.js} | 2 +- ...stem.js => entry-main2-ebb140d8-system.js} | 2 +- .../amd/{5654511d.js => e7648b1d.js} | 0 .../cjs/{69b0b1f3.js => ab63ac1d.js} | 0 .../_expected/es/{21d179d7.js => 227f8e34.js} | 0 .../system/{8714c598.js => e0d21f44.js} | 0 .../_config.js | 6 +-- .../code-splitting-named-inputs/_config.js | 6 +-- test/file-hashes/index.js | 47 +++++++++++++++++++ test/file-hashes/samples/banner/_config.js | 15 ++++++ test/file-hashes/samples/banner/main.js | 1 + test/file-hashes/samples/content/_config.js | 9 ++++ test/file-hashes/samples/content/dep1.js | 1 + test/file-hashes/samples/content/dep2.js | 1 + test/file-hashes/samples/content/main1.js | 3 ++ test/file-hashes/samples/content/main2.js | 3 ++ .../samples/dependency-content/_config.js | 9 ++++ .../samples/dependency-content/dep1.js | 1 + .../samples/dependency-content/dep2.js | 1 + .../samples/dependency-content/main1.js | 3 ++ .../samples/dependency-content/main2.js | 3 ++ test/file-hashes/samples/exports/_config.js | 9 ++++ test/file-hashes/samples/exports/dep.js | 2 + test/file-hashes/samples/exports/main1.js | 3 ++ test/file-hashes/samples/exports/main2.js | 3 ++ test/file-hashes/samples/exports/other.js | 3 ++ .../external-dependency-names/_config.js | 11 +++++ .../external-dependency-names/main1a.js | 3 ++ .../external-dependency-names/main1b.js | 3 ++ .../external-dependency-names/main2a.js | 3 ++ .../external-dependency-names/main2b.js | 3 ++ test/file-hashes/samples/footer/_config.js | 15 ++++++ test/file-hashes/samples/footer/main.js | 1 + test/file-hashes/samples/format/_config.js | 15 ++++++ test/file-hashes/samples/format/main.js | 1 + test/file-hashes/samples/intro/_config.js | 15 ++++++ test/file-hashes/samples/intro/main.js | 1 + test/file-hashes/samples/outro/_config.js | 15 ++++++ test/file-hashes/samples/outro/main.js | 1 + test/misc/bundle-information.js | 8 ++-- test/misc/misc.js | 4 +- test/test.js | 1 + 60 files changed, 252 insertions(+), 47 deletions(-) rename test/chunking-form/samples/filenames-patterns/_expected/amd/{chunk-main2-36fde4aa-amd.js => chunk-main2-0a527b43-amd.js} (100%) create mode 100644 test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main1-0d1fa73e-amd.js delete mode 100644 test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main1-a0f4fde5-amd.js delete mode 100644 test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main2-236b789c-amd.js create mode 100644 test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main2-adf6abe8-amd.js rename test/chunking-form/samples/filenames-patterns/_expected/cjs/{chunk-main2-72ffade3-cjs.js => chunk-main2-328c93f2-cjs.js} (100%) create mode 100644 test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main1-0dfd3e0e-cjs.js delete mode 100644 test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main1-3cd10506-cjs.js delete mode 100644 test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main2-64fa0b57-cjs.js create mode 100644 test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main2-b34781c2-cjs.js rename test/chunking-form/samples/filenames-patterns/_expected/es/{chunk-main2-33a8eba2-esm.js => chunk-main2-d7d48284-esm.js} (100%) delete mode 100644 test/chunking-form/samples/filenames-patterns/_expected/es/entry-main1-5a1ab158-esm.js create mode 100644 test/chunking-form/samples/filenames-patterns/_expected/es/entry-main1-a2c4132e-esm.js delete mode 100644 test/chunking-form/samples/filenames-patterns/_expected/es/entry-main2-52329ca1-esm.js create mode 100644 test/chunking-form/samples/filenames-patterns/_expected/es/entry-main2-bc7681ae-esm.js rename test/chunking-form/samples/filenames-patterns/_expected/system/{chunk-main2-bd8b7ffb-system.js => chunk-main2-d134c6d9-system.js} (100%) rename test/chunking-form/samples/filenames-patterns/_expected/system/{entry-main1-2422e801-system.js => entry-main1-4e36d280-system.js} (74%) rename test/chunking-form/samples/filenames-patterns/_expected/system/{entry-main2-45e98bef-system.js => entry-main2-ebb140d8-system.js} (70%) rename test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/amd/{5654511d.js => e7648b1d.js} (100%) rename test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/cjs/{69b0b1f3.js => ab63ac1d.js} (100%) rename test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/es/{21d179d7.js => 227f8e34.js} (100%) rename test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/system/{8714c598.js => e0d21f44.js} (100%) create mode 100644 test/file-hashes/index.js create mode 100644 test/file-hashes/samples/banner/_config.js create mode 100644 test/file-hashes/samples/banner/main.js create mode 100644 test/file-hashes/samples/content/_config.js create mode 100644 test/file-hashes/samples/content/dep1.js create mode 100644 test/file-hashes/samples/content/dep2.js create mode 100644 test/file-hashes/samples/content/main1.js create mode 100644 test/file-hashes/samples/content/main2.js create mode 100644 test/file-hashes/samples/dependency-content/_config.js create mode 100644 test/file-hashes/samples/dependency-content/dep1.js create mode 100644 test/file-hashes/samples/dependency-content/dep2.js create mode 100644 test/file-hashes/samples/dependency-content/main1.js create mode 100644 test/file-hashes/samples/dependency-content/main2.js create mode 100644 test/file-hashes/samples/exports/_config.js create mode 100644 test/file-hashes/samples/exports/dep.js create mode 100644 test/file-hashes/samples/exports/main1.js create mode 100644 test/file-hashes/samples/exports/main2.js create mode 100644 test/file-hashes/samples/exports/other.js create mode 100644 test/file-hashes/samples/external-dependency-names/_config.js create mode 100644 test/file-hashes/samples/external-dependency-names/main1a.js create mode 100644 test/file-hashes/samples/external-dependency-names/main1b.js create mode 100644 test/file-hashes/samples/external-dependency-names/main2a.js create mode 100644 test/file-hashes/samples/external-dependency-names/main2b.js create mode 100644 test/file-hashes/samples/footer/_config.js create mode 100644 test/file-hashes/samples/footer/main.js create mode 100644 test/file-hashes/samples/format/_config.js create mode 100644 test/file-hashes/samples/format/main.js create mode 100644 test/file-hashes/samples/intro/_config.js create mode 100644 test/file-hashes/samples/intro/main.js create mode 100644 test/file-hashes/samples/outro/_config.js create mode 100644 test/file-hashes/samples/outro/main.js diff --git a/src/Chunk.ts b/src/Chunk.ts index b8576a19efb..a435bf6c8ce 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -667,6 +667,7 @@ export default class Chunk { if (!this.renderedSource) return ''; const hash = sha256(); hash.update(this.renderedSource.toString()); + hash.update(Object.keys(this.exportNames).join(',')); return (this.renderedHash = hash.digest('hex')); } @@ -705,7 +706,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); this.visitDependencies(dep => { if (dep instanceof ExternalModule) hash.update(':' + dep.renderPath); diff --git a/src/utils/addons.ts b/src/utils/addons.ts index d4705048ffc..f2e29c359ef 100644 --- a/src/utils/addons.ts +++ b/src/utils/addons.ts @@ -7,7 +7,6 @@ export interface Addons { outro?: string; banner?: string; footer?: string; - hash: Uint8Array; } function evalIfFn(strOrFn: string | (() => string | Promise)): string | Promise { @@ -38,9 +37,7 @@ export function createAddons(graph: Graph, options: OutputOptions): Promise { diff --git a/test/chunking-form/samples/filenames-patterns/_expected/amd/chunk-main2-36fde4aa-amd.js b/test/chunking-form/samples/filenames-patterns/_expected/amd/chunk-main2-0a527b43-amd.js similarity index 100% rename from test/chunking-form/samples/filenames-patterns/_expected/amd/chunk-main2-36fde4aa-amd.js rename to test/chunking-form/samples/filenames-patterns/_expected/amd/chunk-main2-0a527b43-amd.js diff --git a/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main1-0d1fa73e-amd.js b/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main1-0d1fa73e-amd.js new file mode 100644 index 00000000000..046c1087490 --- /dev/null +++ b/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main1-0d1fa73e-amd.js @@ -0,0 +1,5 @@ +define(['./chunk-main2-0a527b43-amd.js'], function (main2) { 'use strict'; + + main2.log(main2.dep); + +}); diff --git a/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main1-a0f4fde5-amd.js b/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main1-a0f4fde5-amd.js deleted file mode 100644 index ce58304359c..00000000000 --- a/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main1-a0f4fde5-amd.js +++ /dev/null @@ -1,5 +0,0 @@ -define(['./chunk-main2-36fde4aa-amd.js'], function (main2) { 'use strict'; - - main2.log(main2.dep); - -}); diff --git a/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main2-236b789c-amd.js b/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main2-236b789c-amd.js deleted file mode 100644 index 5f7899d1287..00000000000 --- a/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main2-236b789c-amd.js +++ /dev/null @@ -1,7 +0,0 @@ -define(['./chunk-main2-36fde4aa-amd.js'], function (main2) { 'use strict'; - - - - return main2.log; - -}); diff --git a/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main2-adf6abe8-amd.js b/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main2-adf6abe8-amd.js new file mode 100644 index 00000000000..fd3142337a4 --- /dev/null +++ b/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main2-adf6abe8-amd.js @@ -0,0 +1,7 @@ +define(['./chunk-main2-0a527b43-amd.js'], function (main2) { 'use strict'; + + + + return main2.log; + +}); diff --git a/test/chunking-form/samples/filenames-patterns/_expected/cjs/chunk-main2-72ffade3-cjs.js b/test/chunking-form/samples/filenames-patterns/_expected/cjs/chunk-main2-328c93f2-cjs.js similarity index 100% rename from test/chunking-form/samples/filenames-patterns/_expected/cjs/chunk-main2-72ffade3-cjs.js rename to test/chunking-form/samples/filenames-patterns/_expected/cjs/chunk-main2-328c93f2-cjs.js diff --git a/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main1-0dfd3e0e-cjs.js b/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main1-0dfd3e0e-cjs.js new file mode 100644 index 00000000000..9584478cbc8 --- /dev/null +++ b/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main1-0dfd3e0e-cjs.js @@ -0,0 +1,5 @@ +'use strict'; + +var main2 = require('./chunk-main2-328c93f2-cjs.js'); + +main2.log(main2.dep); diff --git a/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main1-3cd10506-cjs.js b/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main1-3cd10506-cjs.js deleted file mode 100644 index f51d2315f4b..00000000000 --- a/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main1-3cd10506-cjs.js +++ /dev/null @@ -1,5 +0,0 @@ -'use strict'; - -var main2 = require('./chunk-main2-72ffade3-cjs.js'); - -main2.log(main2.dep); diff --git a/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main2-64fa0b57-cjs.js b/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main2-64fa0b57-cjs.js deleted file mode 100644 index 9376d5e8485..00000000000 --- a/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main2-64fa0b57-cjs.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict'; - -var main2 = require('./chunk-main2-72ffade3-cjs.js'); - - - -module.exports = main2.log; diff --git a/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main2-b34781c2-cjs.js b/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main2-b34781c2-cjs.js new file mode 100644 index 00000000000..40a250bd4ba --- /dev/null +++ b/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main2-b34781c2-cjs.js @@ -0,0 +1,7 @@ +'use strict'; + +var main2 = require('./chunk-main2-328c93f2-cjs.js'); + + + +module.exports = main2.log; diff --git a/test/chunking-form/samples/filenames-patterns/_expected/es/chunk-main2-33a8eba2-esm.js b/test/chunking-form/samples/filenames-patterns/_expected/es/chunk-main2-d7d48284-esm.js similarity index 100% rename from test/chunking-form/samples/filenames-patterns/_expected/es/chunk-main2-33a8eba2-esm.js rename to test/chunking-form/samples/filenames-patterns/_expected/es/chunk-main2-d7d48284-esm.js diff --git a/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main1-5a1ab158-esm.js b/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main1-5a1ab158-esm.js deleted file mode 100644 index 8b69b0c617d..00000000000 --- a/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main1-5a1ab158-esm.js +++ /dev/null @@ -1,3 +0,0 @@ -import { a as log, b as dep } from './chunk-main2-33a8eba2-esm.js'; - -log(dep); diff --git a/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main1-a2c4132e-esm.js b/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main1-a2c4132e-esm.js new file mode 100644 index 00000000000..80f2dfb9147 --- /dev/null +++ b/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main1-a2c4132e-esm.js @@ -0,0 +1,3 @@ +import { a as log, b as dep } from './chunk-main2-d7d48284-esm.js'; + +log(dep); diff --git a/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main2-52329ca1-esm.js b/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main2-52329ca1-esm.js deleted file mode 100644 index 17153d426c3..00000000000 --- a/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main2-52329ca1-esm.js +++ /dev/null @@ -1 +0,0 @@ -export { a as default } from './chunk-main2-33a8eba2-esm.js'; diff --git a/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main2-bc7681ae-esm.js b/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main2-bc7681ae-esm.js new file mode 100644 index 00000000000..e3039132883 --- /dev/null +++ b/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main2-bc7681ae-esm.js @@ -0,0 +1 @@ +export { a as default } from './chunk-main2-d7d48284-esm.js'; diff --git a/test/chunking-form/samples/filenames-patterns/_expected/system/chunk-main2-bd8b7ffb-system.js b/test/chunking-form/samples/filenames-patterns/_expected/system/chunk-main2-d134c6d9-system.js similarity index 100% rename from test/chunking-form/samples/filenames-patterns/_expected/system/chunk-main2-bd8b7ffb-system.js rename to test/chunking-form/samples/filenames-patterns/_expected/system/chunk-main2-d134c6d9-system.js diff --git a/test/chunking-form/samples/filenames-patterns/_expected/system/entry-main1-2422e801-system.js b/test/chunking-form/samples/filenames-patterns/_expected/system/entry-main1-4e36d280-system.js similarity index 74% rename from test/chunking-form/samples/filenames-patterns/_expected/system/entry-main1-2422e801-system.js rename to test/chunking-form/samples/filenames-patterns/_expected/system/entry-main1-4e36d280-system.js index c8a2dcf7bfa..0ee96309c8d 100644 --- a/test/chunking-form/samples/filenames-patterns/_expected/system/entry-main1-2422e801-system.js +++ b/test/chunking-form/samples/filenames-patterns/_expected/system/entry-main1-4e36d280-system.js @@ -1,4 +1,4 @@ -System.register(['./chunk-main2-bd8b7ffb-system.js'], function (exports, module) { +System.register(['./chunk-main2-d134c6d9-system.js'], function (exports, module) { 'use strict'; var log, dep; return { diff --git a/test/chunking-form/samples/filenames-patterns/_expected/system/entry-main2-45e98bef-system.js b/test/chunking-form/samples/filenames-patterns/_expected/system/entry-main2-ebb140d8-system.js similarity index 70% rename from test/chunking-form/samples/filenames-patterns/_expected/system/entry-main2-45e98bef-system.js rename to test/chunking-form/samples/filenames-patterns/_expected/system/entry-main2-ebb140d8-system.js index 5bd450d0712..63750705848 100644 --- a/test/chunking-form/samples/filenames-patterns/_expected/system/entry-main2-45e98bef-system.js +++ b/test/chunking-form/samples/filenames-patterns/_expected/system/entry-main2-ebb140d8-system.js @@ -1,4 +1,4 @@ -System.register(['./chunk-main2-bd8b7ffb-system.js'], function (exports, module) { +System.register(['./chunk-main2-d134c6d9-system.js'], function (exports, module) { 'use strict'; return { setters: [function (module) { diff --git a/test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/amd/5654511d.js b/test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/amd/e7648b1d.js similarity index 100% rename from test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/amd/5654511d.js rename to test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/amd/e7648b1d.js diff --git a/test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/cjs/69b0b1f3.js b/test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/cjs/ab63ac1d.js similarity index 100% rename from test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/cjs/69b0b1f3.js rename to test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/cjs/ab63ac1d.js diff --git a/test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/es/21d179d7.js b/test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/es/227f8e34.js similarity index 100% rename from test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/es/21d179d7.js rename to test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/es/227f8e34.js diff --git a/test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/system/8714c598.js b/test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/system/e0d21f44.js similarity index 100% rename from test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/system/8714c598.js rename to test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/system/e0d21f44.js diff --git a/test/cli/samples/code-splitting-named-default-inputs/_config.js b/test/cli/samples/code-splitting-named-default-inputs/_config.js index 8a98ebc0613..41e9b7c794d 100644 --- a/test/cli/samples/code-splitting-named-default-inputs/_config.js +++ b/test/cli/samples/code-splitting-named-default-inputs/_config.js @@ -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" ); } diff --git a/test/cli/samples/code-splitting-named-inputs/_config.js b/test/cli/samples/code-splitting-named-inputs/_config.js index 372741e5703..dc4affb2b36 100644 --- a/test/cli/samples/code-splitting-named-inputs/_config.js +++ b/test/cli/samples/code-splitting-named-inputs/_config.js @@ -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" ); } diff --git a/test/file-hashes/index.js b/test/file-hashes/index.js new file mode 100644 index 00000000000..b5843887694 --- /dev/null +++ b/test/file-hashes/index.js @@ -0,0 +1,47 @@ +const path = require('path'); +const rollup = require('../../dist/rollup'); +const { extend, runTestSuiteWithSamples } = require('../utils.js'); +const assert = require('assert'); + +runTestSuiteWithSamples('file hashes', path.resolve(__dirname, 'samples'), (dir, config) => { + (config.skip ? describe.skip : config.solo ? describe.only : describe)( + path.basename(dir) + ': ' + config.description, + () => { + it(`generates correct hashes`, () => { + process.chdir(dir); + return Promise.all( + [config.options1, config.options2].map(options => + rollup + .rollup(options) + .then(bundle => + bundle.generate( + extend( + { format: 'esm', chunkFileNames: '[hash]', entryFileNames: '[hash]' }, + options.output + ) + ) + ) + ) + ).then(([generated1, generated2]) => { + const fileContentsByHash = new Map(); + addAndCheckFileContentsByHash(fileContentsByHash, generated1); + addAndCheckFileContentsByHash(fileContentsByHash, generated2); + }); + }); + } + ); +}); + +function addAndCheckFileContentsByHash(fileContentsByHash, generated) { + for (const chunk of generated.output) { + const hash = chunk.fileName; + if (fileContentsByHash.has(hash)) { + assert.equal( + fileContentsByHash.get(hash), + chunk.code, + 'Two chunks contained different code even though the hashes were the same.' + ); + } + fileContentsByHash.set(hash, chunk.code); + } +} diff --git a/test/file-hashes/samples/banner/_config.js b/test/file-hashes/samples/banner/_config.js new file mode 100644 index 00000000000..805b2096a6f --- /dev/null +++ b/test/file-hashes/samples/banner/_config.js @@ -0,0 +1,15 @@ +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);' + } + } +}; diff --git a/test/file-hashes/samples/banner/main.js b/test/file-hashes/samples/banner/main.js new file mode 100644 index 00000000000..c0b933d7b56 --- /dev/null +++ b/test/file-hashes/samples/banner/main.js @@ -0,0 +1 @@ +console.log('main'); diff --git a/test/file-hashes/samples/content/_config.js b/test/file-hashes/samples/content/_config.js new file mode 100644 index 00000000000..dd2047a083e --- /dev/null +++ b/test/file-hashes/samples/content/_config.js @@ -0,0 +1,9 @@ +module.exports = { + description: 'creates different hashes if the content differs', + options1: { + input: { main: 'main1', dep: 'dep1' } + }, + options2: { + input: { main: 'main2', dep: 'dep2' } + } +}; diff --git a/test/file-hashes/samples/content/dep1.js b/test/file-hashes/samples/content/dep1.js new file mode 100644 index 00000000000..b74a9837c07 --- /dev/null +++ b/test/file-hashes/samples/content/dep1.js @@ -0,0 +1 @@ +console.log('dep'); diff --git a/test/file-hashes/samples/content/dep2.js b/test/file-hashes/samples/content/dep2.js new file mode 100644 index 00000000000..b74a9837c07 --- /dev/null +++ b/test/file-hashes/samples/content/dep2.js @@ -0,0 +1 @@ +console.log('dep'); diff --git a/test/file-hashes/samples/content/main1.js b/test/file-hashes/samples/content/main1.js new file mode 100644 index 00000000000..7fb68233f35 --- /dev/null +++ b/test/file-hashes/samples/content/main1.js @@ -0,0 +1,3 @@ +import './dep1'; + +console.log('main1'); diff --git a/test/file-hashes/samples/content/main2.js b/test/file-hashes/samples/content/main2.js new file mode 100644 index 00000000000..6c39d576b9c --- /dev/null +++ b/test/file-hashes/samples/content/main2.js @@ -0,0 +1,3 @@ +import './dep2'; + +console.log('main2'); diff --git a/test/file-hashes/samples/dependency-content/_config.js b/test/file-hashes/samples/dependency-content/_config.js new file mode 100644 index 00000000000..bfc9ca3485d --- /dev/null +++ b/test/file-hashes/samples/dependency-content/_config.js @@ -0,0 +1,9 @@ +module.exports = { + description: 'creates different hashes if the content of dependencies differs', + options1: { + input: { main: 'main1', dep: 'dep1' } + }, + options2: { + input: { main: 'main2', dep: 'dep2' } + } +}; diff --git a/test/file-hashes/samples/dependency-content/dep1.js b/test/file-hashes/samples/dependency-content/dep1.js new file mode 100644 index 00000000000..6aa6066cec9 --- /dev/null +++ b/test/file-hashes/samples/dependency-content/dep1.js @@ -0,0 +1 @@ +console.log('dep1'); diff --git a/test/file-hashes/samples/dependency-content/dep2.js b/test/file-hashes/samples/dependency-content/dep2.js new file mode 100644 index 00000000000..f5325d80e8a --- /dev/null +++ b/test/file-hashes/samples/dependency-content/dep2.js @@ -0,0 +1 @@ +console.log('dep2'); diff --git a/test/file-hashes/samples/dependency-content/main1.js b/test/file-hashes/samples/dependency-content/main1.js new file mode 100644 index 00000000000..9a3b277b78a --- /dev/null +++ b/test/file-hashes/samples/dependency-content/main1.js @@ -0,0 +1,3 @@ +import './dep1'; + +console.log('main'); diff --git a/test/file-hashes/samples/dependency-content/main2.js b/test/file-hashes/samples/dependency-content/main2.js new file mode 100644 index 00000000000..973fcaf3719 --- /dev/null +++ b/test/file-hashes/samples/dependency-content/main2.js @@ -0,0 +1,3 @@ +import './dep2'; + +console.log('main'); diff --git a/test/file-hashes/samples/exports/_config.js b/test/file-hashes/samples/exports/_config.js new file mode 100644 index 00000000000..10b8e4c6c99 --- /dev/null +++ b/test/file-hashes/samples/exports/_config.js @@ -0,0 +1,9 @@ +module.exports = { + description: 'creates different hashes if the generated exports differ', + options1: { + input: ['main1', 'other'] + }, + options2: { + input: ['main2', 'other'] + } +}; diff --git a/test/file-hashes/samples/exports/dep.js b/test/file-hashes/samples/exports/dep.js new file mode 100644 index 00000000000..cb5a0beab69 --- /dev/null +++ b/test/file-hashes/samples/exports/dep.js @@ -0,0 +1,2 @@ +export const mightNotBeImported = 42; +console.log(mightNotBeImported); diff --git a/test/file-hashes/samples/exports/main1.js b/test/file-hashes/samples/exports/main1.js new file mode 100644 index 00000000000..20c118f07ba --- /dev/null +++ b/test/file-hashes/samples/exports/main1.js @@ -0,0 +1,3 @@ +import {mightNotBeImported} from './dep'; + +console.log('main1', mightNotBeImported); diff --git a/test/file-hashes/samples/exports/main2.js b/test/file-hashes/samples/exports/main2.js new file mode 100644 index 00000000000..99cf3898120 --- /dev/null +++ b/test/file-hashes/samples/exports/main2.js @@ -0,0 +1,3 @@ +import './dep'; + +console.log('main2'); diff --git a/test/file-hashes/samples/exports/other.js b/test/file-hashes/samples/exports/other.js new file mode 100644 index 00000000000..a4b4adefe8d --- /dev/null +++ b/test/file-hashes/samples/exports/other.js @@ -0,0 +1,3 @@ +import './dep'; + +console.log('other'); diff --git a/test/file-hashes/samples/external-dependency-names/_config.js b/test/file-hashes/samples/external-dependency-names/_config.js new file mode 100644 index 00000000000..e3a80e8db9d --- /dev/null +++ b/test/file-hashes/samples/external-dependency-names/_config.js @@ -0,0 +1,11 @@ +module.exports = { + description: 'creates different hashes if the names of external dependencies differ', + options1: { + input: { mainA: 'main1a', mainB: 'main1b' }, + external: ['external', 'external1'] + }, + options2: { + input: { mainA: 'main2a', mainB: 'main2b' }, + external: ['external', 'external2'] + } +}; diff --git a/test/file-hashes/samples/external-dependency-names/main1a.js b/test/file-hashes/samples/external-dependency-names/main1a.js new file mode 100644 index 00000000000..cedb294ffee --- /dev/null +++ b/test/file-hashes/samples/external-dependency-names/main1a.js @@ -0,0 +1,3 @@ +import 'external'; + +console.log('main'); diff --git a/test/file-hashes/samples/external-dependency-names/main1b.js b/test/file-hashes/samples/external-dependency-names/main1b.js new file mode 100644 index 00000000000..83a6cb712d2 --- /dev/null +++ b/test/file-hashes/samples/external-dependency-names/main1b.js @@ -0,0 +1,3 @@ +import 'external1'; + +console.log('main'); diff --git a/test/file-hashes/samples/external-dependency-names/main2a.js b/test/file-hashes/samples/external-dependency-names/main2a.js new file mode 100644 index 00000000000..cedb294ffee --- /dev/null +++ b/test/file-hashes/samples/external-dependency-names/main2a.js @@ -0,0 +1,3 @@ +import 'external'; + +console.log('main'); diff --git a/test/file-hashes/samples/external-dependency-names/main2b.js b/test/file-hashes/samples/external-dependency-names/main2b.js new file mode 100644 index 00000000000..ff71d9a9dc4 --- /dev/null +++ b/test/file-hashes/samples/external-dependency-names/main2b.js @@ -0,0 +1,3 @@ +import 'external2'; + +console.log('main'); diff --git a/test/file-hashes/samples/footer/_config.js b/test/file-hashes/samples/footer/_config.js new file mode 100644 index 00000000000..49b41fc6d05 --- /dev/null +++ b/test/file-hashes/samples/footer/_config.js @@ -0,0 +1,15 @@ +module.exports = { + description: 'creates different hashes if the footer differs', + options1: { + input: 'main', + output: { + footer: 'console.log(1);' + } + }, + options2: { + input: 'main', + output: { + footer: 'console.log(2);' + } + } +}; diff --git a/test/file-hashes/samples/footer/main.js b/test/file-hashes/samples/footer/main.js new file mode 100644 index 00000000000..c0b933d7b56 --- /dev/null +++ b/test/file-hashes/samples/footer/main.js @@ -0,0 +1 @@ +console.log('main'); diff --git a/test/file-hashes/samples/format/_config.js b/test/file-hashes/samples/format/_config.js new file mode 100644 index 00000000000..604f450a8ca --- /dev/null +++ b/test/file-hashes/samples/format/_config.js @@ -0,0 +1,15 @@ +module.exports = { + description: 'creates different hashes if the format differs', + options1: { + input: 'main', + output: { + format: 'esm' + } + }, + options2: { + input: 'main', + output: { + format: 'cjs' + } + } +}; diff --git a/test/file-hashes/samples/format/main.js b/test/file-hashes/samples/format/main.js new file mode 100644 index 00000000000..ad1d380d6cc --- /dev/null +++ b/test/file-hashes/samples/format/main.js @@ -0,0 +1 @@ +export const x = 1; diff --git a/test/file-hashes/samples/intro/_config.js b/test/file-hashes/samples/intro/_config.js new file mode 100644 index 00000000000..04eb80772c6 --- /dev/null +++ b/test/file-hashes/samples/intro/_config.js @@ -0,0 +1,15 @@ +module.exports = { + description: 'creates different hashes if the intro differs', + options1: { + input: 'main', + output: { + intro: 'console.log(1);' + } + }, + options2: { + input: 'main', + output: { + intro: 'console.log(2);' + } + } +}; diff --git a/test/file-hashes/samples/intro/main.js b/test/file-hashes/samples/intro/main.js new file mode 100644 index 00000000000..c0b933d7b56 --- /dev/null +++ b/test/file-hashes/samples/intro/main.js @@ -0,0 +1 @@ +console.log('main'); diff --git a/test/file-hashes/samples/outro/_config.js b/test/file-hashes/samples/outro/_config.js new file mode 100644 index 00000000000..5248af72f0e --- /dev/null +++ b/test/file-hashes/samples/outro/_config.js @@ -0,0 +1,15 @@ +module.exports = { + description: 'creates different hashes if the outro differs', + options1: { + input: 'main', + output: { + outro: 'console.log(1);' + } + }, + options2: { + input: 'main', + output: { + outro: 'console.log(2);' + } + } +}; diff --git a/test/file-hashes/samples/outro/main.js b/test/file-hashes/samples/outro/main.js new file mode 100644 index 00000000000..c0b933d7b56 --- /dev/null +++ b/test/file-hashes/samples/outro/main.js @@ -0,0 +1 @@ +console.log('main'); diff --git a/test/misc/bundle-information.js b/test/misc/bundle-information.js index 8dabe6d9b35..62fcd380aa5 100644 --- a/test/misc/bundle-information.js +++ b/test/misc/bundle-information.js @@ -26,14 +26,14 @@ describe('The bundle object', () => { .then(({ output }) => { assert.deepEqual( output.map(chunk => chunk.fileName), - ['input1-6792d712.js', 'input2-82fc6fc0.js', 'generated-chunk-d6db1a1e.js'], + ['input1-eebe7cfb.js', 'input2-370063a6.js', 'generated-chunk-e9283962.js'], 'fileName' ); assert.deepEqual( output.map(chunk => chunk.code), [ - `import './generated-chunk-d6db1a1e.js';\n\nconsole.log("input1");const out = true;\n\nexport { out };\n`, - `import './generated-chunk-d6db1a1e.js';\n\nconsole.log("input2");var input2 = 42;\n\nexport default input2;\n`, + `import './generated-chunk-e9283962.js';\n\nconsole.log("input1");const out = true;\n\nexport { out };\n`, + `import './generated-chunk-e9283962.js';\n\nconsole.log("input2");var input2 = 42;\n\nexport default input2;\n`, 'console.log("shared");\n' ], 'code' @@ -48,7 +48,7 @@ describe('The bundle object', () => { ); assert.deepEqual( output.map(chunk => chunk.imports), - [['generated-chunk-d6db1a1e.js'], ['generated-chunk-d6db1a1e.js'], []], + [['generated-chunk-e9283962.js'], ['generated-chunk-e9283962.js'], []], 'imports' ); assert.deepEqual(output.map(chunk => chunk.dynamicImports), [[], [], []], 'dynamicImports'); diff --git a/test/misc/misc.js b/test/misc/misc.js index 803d6430ec8..e0f23574c09 100644 --- a/test/misc/misc.js +++ b/test/misc/misc.js @@ -86,8 +86,8 @@ describe('misc', () => { assert.deepEqual(output.map(({ fileName }) => fileName), [ 'main1.js', 'main2.js', - 'chunk-82df00d2.js', - 'chunk-b9217e30.js' + 'chunk-9d1272f4.js', + 'chunk-80285050.js' ]); }); }); diff --git a/test/test.js b/test/test.js index 53eb5d0874b..366ad843275 100644 --- a/test/test.js +++ b/test/test.js @@ -12,6 +12,7 @@ describe('rollup', function() { require('./function/index.js'); require('./form/index.js'); require('./chunking-form/index.js'); + require('./file-hashes/index.js'); require('./sourcemaps/index.js'); require('./incremental/index.js'); require('./hooks/index.js');