src/OutputHash.js
function reHashChunk(chunk, assets, hashFn, nameMap) {
    const isMainFile = file => file.endsWith('.js') || file.endsWith('.css');

    // Update the name of the main files
    chunk.files.filter(isMainFile).forEach((oldChunkName, index) => {
        const asset = assets[oldChunkName];
        const { fullHash, shortHash: newHash } = hashFn(asset.source());

        let newChunkName;

        if (oldChunkName.includes(chunk.renderedHash)) {
            // Save the hash map for replacing the secondary files
            nameMap[chunk.renderedHash] = newHash;
            newChunkName = oldChunkName.replace(chunk.renderedHash, newHash);

            // Keep the chunk hashes in sync
            chunk.hash = fullHash;
            chunk.renderedHash = newHash;
        } else {
            // This is a massive hack:
            //
            // The oldHash of the main file is in `chunk.renderedHash`. But some plugins add a
            // second "main" file to the chunk (for example, `mini-css-extract-plugin` adds a
            // css file). That other main file has to be rehashed too, but we don't know the
            // oldHash of the file, so we don't know what string we have to replace by the new
            // hash.
            //
            // However, the hash present in the file name must be one of the hashes of the
            // modules inside the chunk (modules[].renderedHash). So we try to replace each
            // module hash with the new hash.
            const module = Array.from(chunk.modulesIterable).find(m =>
                oldChunkName.includes(m.renderedHash)
            );

            // Can't find a module with this hash... not sure what is going on, just return and
            // hope for the best.
            if (!module) return;

            // Save the hash map for replacing the secondary files
            nameMap[module.renderedHash] = newHash;
            newChunkName = oldChunkName.replace(module.renderedHash, newHash);

            // Keep the module hashes in sync
            module.hash = fullHash;
            module.renderedHash = newHash;
        }

        // Change file name to include the new hash
        chunk.files[index] = newChunkName;
        asset._name = newChunkName;
        delete assets[oldChunkName];
        assets[newChunkName] = asset;
    });

    // Update the content of the rest of the files in the chunk
    chunk.files
        .filter(file => !isMainFile(file))
        .forEach(file => {
            Object.keys(nameMap).forEach(old => {
                const newHash = nameMap[old];
                replaceStringInAsset(assets[file], old, newHash);
            });
        });
}
function reHashChunk(chunk, assets, hashFn, nameMap) {
    const isMainFile = file => file.endsWith('.js') || file.endsWith('.css');

    // Update the name of the main files
    chunk.files.filter(isMainFile).forEach((oldChunkName, index) => {
        const asset = assets[oldChunkName];
        const { fullHash, shortHash: newHash } = hashFn(asset.source());

        let newChunkName;

        if (oldChunkName.includes(chunk.renderedHash)) {
            // Save the hash map for replacing the secondary files
            nameMap[chunk.renderedHash] = newHash;
            newChunkName = oldChunkName.replace(chunk.renderedHash, newHash);

            // Keep the chunk hashes in sync
            chunk.hash = fullHash;
            chunk.renderedHash = newHash;
        } else {
            // This is a massive hack:
            //
            // The oldHash of the main file is in `chunk.renderedHash`. But some plugins add a
            // second "main" file to the chunk (for example, `mini-css-extract-plugin` adds a
            // css file). That other main file has to be rehashed too, but we don't know the
            // oldHash of the file, so we don't know what string we have to replace by the new
            // hash.
            //
            // However, the hash present in the file name must be one of the hashes of the
            // modules inside the chunk (modules[].renderedHash). So we try to replace each
            // module hash with the new hash.
            const module = Array.from(chunk.modulesIterable).find(m =>
                oldChunkName.includes(m.renderedHash)
            );

            // Can't find a module with this hash... not sure what is going on, just return and
            // hope for the best.
            if (!module) return;

            // Save the hash map for replacing the secondary files
            nameMap[module.renderedHash] = newHash;
            newChunkName = oldChunkName.replace(module.renderedHash, newHash);

            // Keep the module hashes in sync
            module.hash = fullHash;
            module.renderedHash = newHash;
        }

        // Change file name to include the new hash
        chunk.files[index] = newChunkName;
        asset._name = newChunkName;
        delete assets[oldChunkName];
        assets[newChunkName] = asset;
    });

    // Update the content of the rest of the files in the chunk
    chunk.files
        .filter(file => !isMainFile(file))
        .forEach(file => {
            Object.keys(nameMap).forEach(old => {
                const newHash = nameMap[old];
                replaceStringInAsset(assets[file], old, newHash);
            });
        });
}
function reHashChunk ( chunk , assets , hashFn , nameMap ) { const isMainFile = file => file . endsWith ( '.js' ) || file . endsWith ( '.css' ) ; // Update the name of the main files chunk . files . filter ( isMainFile ) . forEach ( ( oldChunkName , index ) => { const asset = assets [ oldChunkName ] ; const { fullHash , shortHash : newHash } = hashFn ( asset . source ( ) ) ; let newChunkName ; if ( oldChunkName . includes ( chunk . renderedHash ) ) { // Save the hash map for replacing the secondary files nameMap [ chunk . renderedHash ] = newHash ; newChunkName = oldChunkName . replace ( chunk . renderedHash , newHash ) ; // Keep the chunk hashes in sync chunk . hash = fullHash ; chunk . renderedHash = newHash ; } else { // This is a massive hack: // // The oldHash of the main file is in `chunk.renderedHash`. But some plugins add a // second "main" file to the chunk (for example, `mini-css-extract-plugin` adds a // css file). That other main file has to be rehashed too, but we don't know the // oldHash of the file, so we don't know what string we have to replace by the new // hash. // // However, the hash present in the file name must be one of the hashes of the // modules inside the chunk (modules[].renderedHash). So we try to replace each // module hash with the new hash. const module = Array . from ( chunk . modulesIterable ) . find ( m => oldChunkName . includes ( m . renderedHash ) ) ; // Can't find a module with this hash... not sure what is going on, just return and // hope for the best. if ( ! module ) return ; // Save the hash map for replacing the secondary files nameMap [ module . renderedHash ] = newHash ; newChunkName = oldChunkName . replace ( module . renderedHash , newHash ) ; // Keep the module hashes in sync module . hash = fullHash ; module . renderedHash = newHash ; } // Change file name to include the new hash chunk . files [ index ] = newChunkName ; asset . _name = newChunkName ; delete assets [ oldChunkName ] ; assets [ newChunkName ] = asset ; } ) ; // Update the content of the rest of the files in the chunk chunk . files . filter ( file => ! isMainFile ( file ) ) . forEach ( file => { Object . keys ( nameMap ) . forEach ( old => { const newHash = nameMap [ old ] ; replaceStringInAsset ( assets [ file ] , old , newHash ) ; } ) ; } ) ; }
Computes the new hash of a chunk.

This function updates the *name* of the main file (i.e. source code), and the *content* of the
secondary files (i.e source maps)
Computes the new hash of a chunk .