From 8ee03af854d27b39bb5da6a18c6ca845eebf4aa2 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Wed, 28 Aug 2019 13:57:07 +0200 Subject: [PATCH] Properly reflect dependency file names in hash (#3083) * Create tests * Include rendered file name without hash in dependency hashes * Improve and fix documentation * Update 999-big-list-of-options.md --- docs/999-big-list-of-options.md | 2 +- src/Chunk.ts | 28 +++++++++---- src/utils/assignChunkIds.ts | 11 +---- ...c19-amd.js => chunk-main2-6a714ad3-amd.js} | 0 .../_expected/amd/entry-main1-e7c7d1b5-amd.js | 5 --- .../_expected/amd/entry-main1-f47d0370-amd.js | 5 +++ .../_expected/amd/entry-main2-4db8cd0c-amd.js | 7 ++++ .../_expected/amd/entry-main2-f9a2200a-amd.js | 7 ---- ...40d-cjs.js => chunk-main2-397efa8f-cjs.js} | 0 .../_expected/cjs/entry-main1-5358eeba-cjs.js | 5 +++ .../_expected/cjs/entry-main1-639831da-cjs.js | 5 --- .../_expected/cjs/entry-main2-4508233a-cjs.js | 7 ---- .../_expected/cjs/entry-main2-6a001c26-cjs.js | 7 ++++ ...346-esm.js => chunk-main2-5251e7d2-esm.js} | 0 .../_expected/es/entry-main1-2901d8b1-esm.js | 3 -- .../_expected/es/entry-main1-d5bffcd6-esm.js | 3 ++ .../_expected/es/entry-main2-2d73322a-esm.js | 1 + .../_expected/es/entry-main2-62d27906-esm.js | 1 - ...stem.js => chunk-main2-97f5caac-system.js} | 0 ...stem.js => entry-main1-e6585a3e-system.js} | 2 +- ...stem.js => entry-main2-2e964c97-system.js} | 2 +- .../amd/{3eaceb21.js => 4ef5e079.js} | 0 .../cjs/{3257e13f.js => be351642.js} | 0 .../_expected/es/{9e951b77.js => 4cadb2a6.js} | 0 .../system/{a6fb0f6b.js => b0f0487c.js} | 0 .../_config.js | 6 +-- .../code-splitting-named-inputs/_config.js | 6 +-- test/file-hashes/index.js | 40 +++++++++++++------ .../file-hashes/samples/chunk-name/_config.js | 21 ++++++++++ test/file-hashes/samples/chunk-name/main.js | 2 + test/file-hashes/samples/chunk-name/main2.js | 1 + .../samples/name-pattern/_config.js | 15 +++++++ test/file-hashes/samples/name-pattern/dep.js | 1 + test/file-hashes/samples/name-pattern/main.js | 1 + test/hooks/index.js | 24 +++++------ test/misc/bundle-information.js | 8 ++-- test/misc/misc.js | 4 +- 37 files changed, 146 insertions(+), 84 deletions(-) rename test/chunking-form/samples/filenames-patterns/_expected/amd/{chunk-main2-6bb39c19-amd.js => chunk-main2-6a714ad3-amd.js} (100%) delete mode 100644 test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main1-e7c7d1b5-amd.js create mode 100644 test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main1-f47d0370-amd.js create mode 100644 test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main2-4db8cd0c-amd.js delete mode 100644 test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main2-f9a2200a-amd.js rename test/chunking-form/samples/filenames-patterns/_expected/cjs/{chunk-main2-33c8b40d-cjs.js => chunk-main2-397efa8f-cjs.js} (100%) create mode 100644 test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main1-5358eeba-cjs.js delete mode 100644 test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main1-639831da-cjs.js delete mode 100644 test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main2-4508233a-cjs.js create mode 100644 test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main2-6a001c26-cjs.js rename test/chunking-form/samples/filenames-patterns/_expected/es/{chunk-main2-cc2b1346-esm.js => chunk-main2-5251e7d2-esm.js} (100%) delete mode 100644 test/chunking-form/samples/filenames-patterns/_expected/es/entry-main1-2901d8b1-esm.js create mode 100644 test/chunking-form/samples/filenames-patterns/_expected/es/entry-main1-d5bffcd6-esm.js create mode 100644 test/chunking-form/samples/filenames-patterns/_expected/es/entry-main2-2d73322a-esm.js delete mode 100644 test/chunking-form/samples/filenames-patterns/_expected/es/entry-main2-62d27906-esm.js rename test/chunking-form/samples/filenames-patterns/_expected/system/{chunk-main2-63744fb4-system.js => chunk-main2-97f5caac-system.js} (100%) rename test/chunking-form/samples/filenames-patterns/_expected/system/{entry-main1-379caf56-system.js => entry-main1-e6585a3e-system.js} (72%) rename test/chunking-form/samples/filenames-patterns/_expected/system/{entry-main2-9e49ba04-system.js => entry-main2-2e964c97-system.js} (69%) rename test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/amd/{3eaceb21.js => 4ef5e079.js} (100%) rename test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/cjs/{3257e13f.js => be351642.js} (100%) rename test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/es/{9e951b77.js => 4cadb2a6.js} (100%) rename test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/system/{a6fb0f6b.js => b0f0487c.js} (100%) create mode 100644 test/file-hashes/samples/chunk-name/_config.js create mode 100644 test/file-hashes/samples/chunk-name/main.js create mode 100644 test/file-hashes/samples/chunk-name/main2.js create mode 100644 test/file-hashes/samples/name-pattern/_config.js create mode 100644 test/file-hashes/samples/name-pattern/dep.js create mode 100644 test/file-hashes/samples/name-pattern/main.js diff --git a/docs/999-big-list-of-options.md b/docs/999-big-list-of-options.md index 56132eb8bab..5b105f9a8af 100755 --- a/docs/999-big-list-of-options.md +++ b/docs/999-big-list-of-options.md @@ -397,7 +397,7 @@ Default: `"[name]-[hash].js"` The pattern to use for naming shared chunks created when code-splitting. Pattern supports the following placeholders: * `[format]`: The rendering format defined in the output options, e.g. `esm` or `cjs`. * `[hash]`: A hash based on the content of the chunk and the content of all its dependencies. - * `[name]`: The name of the chunk. This will be `chunk` unless the chunk was created via the [`manualChunks`](guide/en/#manualchunks) options. + * `[name]`: The name of the chunk. This can be explicitly set via the [`manualChunks`](guide/en/#manualchunks) option or when the chunk is created by a plugin via [`this.emitFile`](guide/en/#thisemitfileemittedfile-emittedchunk--emittedasset--string). Otherwise it will be derived from the chunk contents. Forward slashes `/` can be used to place files in sub-directories. See also [`output.assetFileNames`](guide/en/#outputassetfilenames), [`output.entryFileNames`](guide/en/#outputentryfilenames). diff --git a/src/Chunk.ts b/src/Chunk.ts index b07ef826801..38286584553 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -250,19 +250,25 @@ export default class Chunk { } generateId( - pattern: string, - patternName: string, addons: Addons, options: OutputOptions, - existingNames: Record + existingNames: Record, + includeHash: boolean ): string { if (this.fileName !== null) { return this.fileName; } + const [pattern, patternName] = + this.facadeModule && this.facadeModule.isUserDefinedEntryPoint + ? [options.entryFileNames || '[name].js', 'output.entryFileNames'] + : [options.chunkFileNames || '[name]-[hash].js', 'output.chunkFileNames']; return makeUnique( renderNamePattern(pattern, patternName, { format: () => (options.format === 'es' ? 'esm' : (options.format as string)), - hash: () => this.computeContentHashWithDependencies(addons, options), + hash: () => + includeHash + ? this.computeContentHashWithDependencies(addons, options, existingNames) + : '[hash]', name: () => this.getChunkName() }), existingNames @@ -831,15 +837,23 @@ export default class Chunk { return hashAugmentation; } - private computeContentHashWithDependencies(addons: Addons, options: OutputOptions): string { + private computeContentHashWithDependencies( + addons: Addons, + options: OutputOptions, + existingNames: Record + ): string { const hash = sha256(); 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); - else hash.update(dep.getRenderedHash()); + if (dep instanceof ExternalModule) { + hash.update(':' + dep.renderPath); + } else { + hash.update(dep.getRenderedHash()); + hash.update(dep.generateId(addons, options, existingNames, false)); + } }); return hash.digest('hex').substr(0, 8); diff --git a/src/utils/assignChunkIds.ts b/src/utils/assignChunkIds.ts index 452de880205..5ed2d58faaa 100644 --- a/src/utils/assignChunkIds.ts +++ b/src/utils/assignChunkIds.ts @@ -24,21 +24,12 @@ export function assignChunkIds( // make sure entry chunk names take precedence with regard to deconflicting const chunksForNaming: Chunk[] = entryChunks.concat(otherChunks); for (const chunk of chunksForNaming) { - const facadeModule = chunk.facadeModule; if (outputOptions.file) { chunk.id = basename(outputOptions.file); } else if (inputOptions.preserveModules) { chunk.id = chunk.generateIdPreserveModules(inputBase, bundle); } else { - let pattern, patternName; - if (facadeModule && facadeModule.isUserDefinedEntryPoint) { - pattern = outputOptions.entryFileNames || '[name].js'; - patternName = 'output.entryFileNames'; - } else { - pattern = outputOptions.chunkFileNames || '[name]-[hash].js'; - patternName = 'output.chunkFileNames'; - } - chunk.id = chunk.generateId(pattern, patternName, addons, outputOptions, bundle); + chunk.id = chunk.generateId(addons, outputOptions, bundle, true); } bundle[chunk.id] = FILE_PLACEHOLDER; } diff --git a/test/chunking-form/samples/filenames-patterns/_expected/amd/chunk-main2-6bb39c19-amd.js b/test/chunking-form/samples/filenames-patterns/_expected/amd/chunk-main2-6a714ad3-amd.js similarity index 100% rename from test/chunking-form/samples/filenames-patterns/_expected/amd/chunk-main2-6bb39c19-amd.js rename to test/chunking-form/samples/filenames-patterns/_expected/amd/chunk-main2-6a714ad3-amd.js diff --git a/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main1-e7c7d1b5-amd.js b/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main1-e7c7d1b5-amd.js deleted file mode 100644 index 791e2547a0f..00000000000 --- a/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main1-e7c7d1b5-amd.js +++ /dev/null @@ -1,5 +0,0 @@ -define(['./chunk-main2-6bb39c19-amd'], function (main2) { 'use strict'; - - main2.log(main2.dep); - -}); diff --git a/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main1-f47d0370-amd.js b/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main1-f47d0370-amd.js new file mode 100644 index 00000000000..2c10541ae90 --- /dev/null +++ b/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main1-f47d0370-amd.js @@ -0,0 +1,5 @@ +define(['./chunk-main2-6a714ad3-amd'], function (main2) { 'use strict'; + + main2.log(main2.dep); + +}); diff --git a/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main2-4db8cd0c-amd.js b/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main2-4db8cd0c-amd.js new file mode 100644 index 00000000000..34d0dd28197 --- /dev/null +++ b/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main2-4db8cd0c-amd.js @@ -0,0 +1,7 @@ +define(['./chunk-main2-6a714ad3-amd'], function (main2) { 'use strict'; + + + + return main2.log; + +}); diff --git a/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main2-f9a2200a-amd.js b/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main2-f9a2200a-amd.js deleted file mode 100644 index 0e357dbc084..00000000000 --- a/test/chunking-form/samples/filenames-patterns/_expected/amd/entry-main2-f9a2200a-amd.js +++ /dev/null @@ -1,7 +0,0 @@ -define(['./chunk-main2-6bb39c19-amd'], function (main2) { 'use strict'; - - - - return main2.log; - -}); diff --git a/test/chunking-form/samples/filenames-patterns/_expected/cjs/chunk-main2-33c8b40d-cjs.js b/test/chunking-form/samples/filenames-patterns/_expected/cjs/chunk-main2-397efa8f-cjs.js similarity index 100% rename from test/chunking-form/samples/filenames-patterns/_expected/cjs/chunk-main2-33c8b40d-cjs.js rename to test/chunking-form/samples/filenames-patterns/_expected/cjs/chunk-main2-397efa8f-cjs.js diff --git a/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main1-5358eeba-cjs.js b/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main1-5358eeba-cjs.js new file mode 100644 index 00000000000..5528fde39bd --- /dev/null +++ b/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main1-5358eeba-cjs.js @@ -0,0 +1,5 @@ +'use strict'; + +var main2 = require('./chunk-main2-397efa8f-cjs.js'); + +main2.log(main2.dep); diff --git a/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main1-639831da-cjs.js b/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main1-639831da-cjs.js deleted file mode 100644 index df68f3f3591..00000000000 --- a/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main1-639831da-cjs.js +++ /dev/null @@ -1,5 +0,0 @@ -'use strict'; - -var main2 = require('./chunk-main2-33c8b40d-cjs.js'); - -main2.log(main2.dep); diff --git a/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main2-4508233a-cjs.js b/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main2-4508233a-cjs.js deleted file mode 100644 index 60dc76f979f..00000000000 --- a/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main2-4508233a-cjs.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict'; - -var main2 = require('./chunk-main2-33c8b40d-cjs.js'); - - - -module.exports = main2.log; diff --git a/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main2-6a001c26-cjs.js b/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main2-6a001c26-cjs.js new file mode 100644 index 00000000000..3479ebb3f9c --- /dev/null +++ b/test/chunking-form/samples/filenames-patterns/_expected/cjs/entry-main2-6a001c26-cjs.js @@ -0,0 +1,7 @@ +'use strict'; + +var main2 = require('./chunk-main2-397efa8f-cjs.js'); + + + +module.exports = main2.log; diff --git a/test/chunking-form/samples/filenames-patterns/_expected/es/chunk-main2-cc2b1346-esm.js b/test/chunking-form/samples/filenames-patterns/_expected/es/chunk-main2-5251e7d2-esm.js similarity index 100% rename from test/chunking-form/samples/filenames-patterns/_expected/es/chunk-main2-cc2b1346-esm.js rename to test/chunking-form/samples/filenames-patterns/_expected/es/chunk-main2-5251e7d2-esm.js diff --git a/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main1-2901d8b1-esm.js b/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main1-2901d8b1-esm.js deleted file mode 100644 index d269251063e..00000000000 --- a/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main1-2901d8b1-esm.js +++ /dev/null @@ -1,3 +0,0 @@ -import { l as log, d as dep } from './chunk-main2-cc2b1346-esm.js'; - -log(dep); diff --git a/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main1-d5bffcd6-esm.js b/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main1-d5bffcd6-esm.js new file mode 100644 index 00000000000..a3165a74ff8 --- /dev/null +++ b/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main1-d5bffcd6-esm.js @@ -0,0 +1,3 @@ +import { l as log, d as dep } from './chunk-main2-5251e7d2-esm.js'; + +log(dep); diff --git a/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main2-2d73322a-esm.js b/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main2-2d73322a-esm.js new file mode 100644 index 00000000000..8c65aad6124 --- /dev/null +++ b/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main2-2d73322a-esm.js @@ -0,0 +1 @@ +export { l as default } from './chunk-main2-5251e7d2-esm.js'; diff --git a/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main2-62d27906-esm.js b/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main2-62d27906-esm.js deleted file mode 100644 index 780b6aba3d3..00000000000 --- a/test/chunking-form/samples/filenames-patterns/_expected/es/entry-main2-62d27906-esm.js +++ /dev/null @@ -1 +0,0 @@ -export { l as default } from './chunk-main2-cc2b1346-esm.js'; diff --git a/test/chunking-form/samples/filenames-patterns/_expected/system/chunk-main2-63744fb4-system.js b/test/chunking-form/samples/filenames-patterns/_expected/system/chunk-main2-97f5caac-system.js similarity index 100% rename from test/chunking-form/samples/filenames-patterns/_expected/system/chunk-main2-63744fb4-system.js rename to test/chunking-form/samples/filenames-patterns/_expected/system/chunk-main2-97f5caac-system.js diff --git a/test/chunking-form/samples/filenames-patterns/_expected/system/entry-main1-379caf56-system.js b/test/chunking-form/samples/filenames-patterns/_expected/system/entry-main1-e6585a3e-system.js similarity index 72% rename from test/chunking-form/samples/filenames-patterns/_expected/system/entry-main1-379caf56-system.js rename to test/chunking-form/samples/filenames-patterns/_expected/system/entry-main1-e6585a3e-system.js index 56fe0f70e2c..63ae6a96879 100644 --- a/test/chunking-form/samples/filenames-patterns/_expected/system/entry-main1-379caf56-system.js +++ b/test/chunking-form/samples/filenames-patterns/_expected/system/entry-main1-e6585a3e-system.js @@ -1,4 +1,4 @@ -System.register(['./chunk-main2-63744fb4-system.js'], function () { +System.register(['./chunk-main2-97f5caac-system.js'], function () { 'use strict'; var log, dep; return { diff --git a/test/chunking-form/samples/filenames-patterns/_expected/system/entry-main2-9e49ba04-system.js b/test/chunking-form/samples/filenames-patterns/_expected/system/entry-main2-2e964c97-system.js similarity index 69% rename from test/chunking-form/samples/filenames-patterns/_expected/system/entry-main2-9e49ba04-system.js rename to test/chunking-form/samples/filenames-patterns/_expected/system/entry-main2-2e964c97-system.js index 74ee594635c..e36c442c5f5 100644 --- a/test/chunking-form/samples/filenames-patterns/_expected/system/entry-main2-9e49ba04-system.js +++ b/test/chunking-form/samples/filenames-patterns/_expected/system/entry-main2-2e964c97-system.js @@ -1,4 +1,4 @@ -System.register(['./chunk-main2-63744fb4-system.js'], function (exports) { +System.register(['./chunk-main2-97f5caac-system.js'], function (exports) { 'use strict'; return { setters: [function (module) { diff --git a/test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/amd/3eaceb21.js b/test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/amd/4ef5e079.js similarity index 100% rename from test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/amd/3eaceb21.js rename to test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/amd/4ef5e079.js diff --git a/test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/cjs/3257e13f.js b/test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/cjs/be351642.js similarity index 100% rename from test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/cjs/3257e13f.js rename to test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/cjs/be351642.js diff --git a/test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/es/9e951b77.js b/test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/es/4cadb2a6.js similarity index 100% rename from test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/es/9e951b77.js rename to test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/es/4cadb2a6.js diff --git a/test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/system/a6fb0f6b.js b/test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/system/b0f0487c.js similarity index 100% rename from test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/system/a6fb0f6b.js rename to test/chunking-form/samples/tree-shaken-dynamic-hash/_expected/system/b0f0487c.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 41e9b7c794d..acc79b15858 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-d8c4343d.js:${standard}\n` + + `${color}//→ entry1-b70571c1.js:${standard}\n` + "console.log('main1');\n" + '\n' + - `${color}//→ Entry 2-99f48ca0.js:${standard}\n` + + `${color}//→ Entry 2-cc781491.js:${standard}\n` + "console.log('main2');\n" + '\n' + - `${color}//→ main3-a6240449.js:${standard}\n` + + `${color}//→ main3-5e259623.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 dc4affb2b36..ce0141c3e3d 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-d8c4343d.js:${standard}\n` + + `${color}//→ entry1-b70571c1.js:${standard}\n` + "console.log('main1');\n" + '\n' + - `${color}//→ Entry 2-99f48ca0.js:${standard}\n` + + `${color}//→ Entry 2-cc781491.js:${standard}\n` + "console.log('main2');\n" + '\n' + - `${color}//→ main3-a6240449.js:${standard}\n` + + `${color}//→ main3-5e259623.js:${standard}\n` + "console.log('main3');\n" ); } diff --git a/test/file-hashes/index.js b/test/file-hashes/index.js index b5843887694..60f46cf4b98 100644 --- a/test/file-hashes/index.js +++ b/test/file-hashes/index.js @@ -1,7 +1,6 @@ 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)( @@ -24,24 +23,41 @@ runTestSuiteWithSamples('file hashes', path.resolve(__dirname, 'samples'), (dir, ) ).then(([generated1, generated2]) => { const fileContentsByHash = new Map(); - addAndCheckFileContentsByHash(fileContentsByHash, generated1); - addAndCheckFileContentsByHash(fileContentsByHash, generated2); + addFileContentsByFileName(fileContentsByHash, generated1); + addFileContentsByFileName(fileContentsByHash, generated2); + if (config.show) { + console.log(fileContentsByHash); + } + for (const contents of fileContentsByHash.values()) { + if (contents.size > 1) { + throw new Error( + `Two chunks contained different code even though the hashes were the same: ${Array.from( + contents + ) + .map(JSON.stringify) + .join(' != ')}` + ); + } + } }); }); } ); }); -function addAndCheckFileContentsByHash(fileContentsByHash, generated) { +function addFileContentsByFileName(fileContentsByFileName, 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.' - ); + const fileName = chunk.fileName; + if (fileContentsByFileName.has(fileName)) { + fileContentsByFileName.get(fileName).add(chunk.code); + } else { + fileContentsByFileName.set(fileName, new Set([chunk.code])); } - fileContentsByHash.set(hash, chunk.code); } } + +// assert.equal( +// fileContentsByHash.get(hash), +// chunk.code, +// 'Two chunks contained different code even though the hashes were the same.' +// ); diff --git a/test/file-hashes/samples/chunk-name/_config.js b/test/file-hashes/samples/chunk-name/_config.js new file mode 100644 index 00000000000..5e4473d7d48 --- /dev/null +++ b/test/file-hashes/samples/chunk-name/_config.js @@ -0,0 +1,21 @@ +module.exports = { + description: 'creates different hashes if the name pattern differs', + options1: { + input: { + main: 'main', + foo: 'main2' + }, + output: { + entryFileNames: '[name]-[hash].js' + } + }, + options2: { + input: { + main: 'main', + bar: 'main2' + }, + output: { + entryFileNames: '[name]-[hash].js' + } + } +}; diff --git a/test/file-hashes/samples/chunk-name/main.js b/test/file-hashes/samples/chunk-name/main.js new file mode 100644 index 00000000000..d764458aeac --- /dev/null +++ b/test/file-hashes/samples/chunk-name/main.js @@ -0,0 +1,2 @@ +import './main2'; +console.log('main'); diff --git a/test/file-hashes/samples/chunk-name/main2.js b/test/file-hashes/samples/chunk-name/main2.js new file mode 100644 index 00000000000..ac653633351 --- /dev/null +++ b/test/file-hashes/samples/chunk-name/main2.js @@ -0,0 +1 @@ +console.log('main2'); diff --git a/test/file-hashes/samples/name-pattern/_config.js b/test/file-hashes/samples/name-pattern/_config.js new file mode 100644 index 00000000000..e0127aa0aab --- /dev/null +++ b/test/file-hashes/samples/name-pattern/_config.js @@ -0,0 +1,15 @@ +module.exports = { + description: 'creates different hashes if the name pattern differs', + options1: { + input: 'main', + output: { + chunkFileNames: '[hash]-[name]' + } + }, + options2: { + input: 'main', + output: { + chunkFileNames: '[name]-[hash]' + } + } +}; diff --git a/test/file-hashes/samples/name-pattern/dep.js b/test/file-hashes/samples/name-pattern/dep.js new file mode 100644 index 00000000000..46d3ca8c61f --- /dev/null +++ b/test/file-hashes/samples/name-pattern/dep.js @@ -0,0 +1 @@ +export const value = 42; diff --git a/test/file-hashes/samples/name-pattern/main.js b/test/file-hashes/samples/name-pattern/main.js new file mode 100644 index 00000000000..16f5277fbe9 --- /dev/null +++ b/test/file-hashes/samples/name-pattern/main.js @@ -0,0 +1 @@ +import('./dep').then(({ value }) => console.log(value)); diff --git a/test/hooks/index.js b/test/hooks/index.js index 1ecff4f0602..2e4d800ca83 100644 --- a/test/hooks/index.js +++ b/test/hooks/index.js @@ -268,9 +268,9 @@ describe('hooks', () => { .then(({ output }) => { assert.equal( output[0].code, - `var input = new URL('chunk-01406d83.js', import.meta.url).href;\n\nexport default input;\n` + `var input = new URL('chunk-928cb70b.js', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'chunk-01406d83.js'); + assert.equal(output[1].fileName, 'chunk-928cb70b.js'); assert.equal(output[1].code, `console.log('chunk');\n`); return rollup.rollup({ @@ -293,9 +293,9 @@ describe('hooks', () => { .then(({ output }) => { assert.equal( output[0].code, - `var input = new URL('chunk-01406d83.js', import.meta.url).href;\n\nexport default input;\n` + `var input = new URL('chunk-928cb70b.js', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'chunk-01406d83.js'); + assert.equal(output[1].fileName, 'chunk-928cb70b.js'); assert.equal(output[1].code, `console.log('chunk');\n`); return rollup.rollup({ @@ -315,9 +315,9 @@ describe('hooks', () => { .then(({ output }) => { assert.equal( output[0].code, - `var input = new URL('chunk-01406d83.js', import.meta.url).href;\n\nexport default input;\n` + `var input = new URL('chunk-928cb70b.js', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'chunk-01406d83.js'); + assert.equal(output[1].fileName, 'chunk-928cb70b.js'); assert.equal(output[1].code, `console.log('chunk');\n`); }); }); @@ -1242,9 +1242,9 @@ describe('hooks', () => { .then(({ output }) => { assert.equal( output[0].code, - `var input = new URL('chunk-01406d83.js', import.meta.url).href;\n\nexport default input;\n` + `var input = new URL('chunk-928cb70b.js', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'chunk-01406d83.js'); + assert.equal(output[1].fileName, 'chunk-928cb70b.js'); assert.equal(output[1].code, `console.log('chunk');\n`); return rollup.rollup({ @@ -1267,9 +1267,9 @@ describe('hooks', () => { .then(({ output }) => { assert.equal( output[0].code, - `var input = new URL('chunk-01406d83.js', import.meta.url).href;\n\nexport default input;\n` + `var input = new URL('chunk-928cb70b.js', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'chunk-01406d83.js'); + assert.equal(output[1].fileName, 'chunk-928cb70b.js'); assert.equal(output[1].code, `console.log('chunk');\n`); return rollup.rollup({ @@ -1289,9 +1289,9 @@ describe('hooks', () => { .then(({ output }) => { assert.equal( output[0].code, - `var input = new URL('chunk-01406d83.js', import.meta.url).href;\n\nexport default input;\n` + `var input = new URL('chunk-928cb70b.js', import.meta.url).href;\n\nexport default input;\n` ); - assert.equal(output[1].fileName, 'chunk-01406d83.js'); + assert.equal(output[1].fileName, 'chunk-928cb70b.js'); assert.equal(output[1].code, `console.log('chunk');\n`); }); }); diff --git a/test/misc/bundle-information.js b/test/misc/bundle-information.js index cc0e6aa8bde..eb1a13f8e21 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-91cf8b5e.js', 'input2-28e1210a.js', 'generated-shared-e9283962.js'], + ['input1-3810e839.js', 'input2-14354d1f.js', 'generated-shared-f6027271.js'], 'fileName' ); assert.deepEqual( output.map(chunk => chunk.code), [ - `import './generated-shared-e9283962.js';\n\nconsole.log("input1");const out = true;\n\nexport { out };\n`, - `import './generated-shared-e9283962.js';\n\nconsole.log("input2");var input2 = 42;\n\nexport default input2;\n`, + `import './generated-shared-f6027271.js';\n\nconsole.log("input1");const out = true;\n\nexport { out };\n`, + `import './generated-shared-f6027271.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-shared-e9283962.js'], ['generated-shared-e9283962.js'], []], + [['generated-shared-f6027271.js'], ['generated-shared-f6027271.js'], []], 'imports' ); assert.deepEqual(output.map(chunk => chunk.dynamicImports), [[], [], []], 'dynamicImports'); diff --git a/test/misc/misc.js b/test/misc/misc.js index 2cd6bf0878a..f2e6a32a8b3 100644 --- a/test/misc/misc.js +++ b/test/misc/misc.js @@ -112,8 +112,8 @@ describe('misc', () => { assert.deepEqual(output.map(({ fileName }) => fileName), [ 'main1.js', 'main2.js', - 'dep-9d1272f4.js', - 'dyndep-80285050.js' + 'dep-f8bec8a7.js', + 'dyndep-b0a9ee12.js' ]); }); });