Code Monkey home page Code Monkey logo

Comments (2)

yepitschunked avatar yepitschunked commented on June 12, 2024

Updated the report to highlight why this is broken for both chunkhash and contenthash.

from webpack-plugin-hash-output.

jpnelson avatar jpnelson commented on June 12, 2024

Here's an MVP reproduction of the bug: https://glitch.com/edit/#!/standing-flashy-slouch

If you change the chunk loaded in the HTML from main to main2 you can see that it works, but otherwise breaks.

There are a couple of ways around this:

  1. We replaced this branch of code with a check to make sure that contenthash is in use for CSS files (which is what we'd want to do anyway). Since we're ok with not running this plugin over those CSS files, this resolved the issue for us.

At the file

// This is a massive hack:

  // Update the name of the main files
chunk.files.filter(isMainFile).forEach((oldChunkName, index) => {
    // PATCHED: Do not run the hashing over CSS files due to the bug described in
    // https://github.com/scinos/webpack-plugin-hash-output/issues/29
    // We assume that CSS and CSS sourcemaps are using contenthash and do not need to be rehashed anyway.
    if (oldChunkName.endsWith('.css') || oldChunkName.endsWith('.css.map')) {
      if (oldChunkName.includes(chunk.renderedHash)) {
        throw new Error(
          'Chunk name included the rendered hash, and it was a .css or .css.map file. Do not use renderedhash with CSS files – use contenthash. See https://github.com/scinos/webpack-plugin-hash-output/issues/29 for details',
        );
      }
      return;
    }
    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 {
      // PATCHED: Throw an error in the case where we did not find the rendered hash in the chunk name. We won't be rehashing that.
      throw new Error(
        'Chunk name did not include the rendered hash, and was not a .css or .css.map file. HashOutputPlugin does not support other types of hashes for assets. See https://github.com/scinos/webpack-plugin-hash-output/issues/29 for details',
      );
    }

    // Change file name to include the new hash
    chunk.files[index] = newChunkName;
    asset._name = newChunkName;
    delete assets[oldChunkName];
    assets[newChunkName] = asset;
  });
  1. Apply the fix that @yepitschunked suggested above:
    At the file

// This is a massive hack:

// 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 {
      const contentHash =
        Object.values(chunk.contentHash).find((hash) => oldChunkName.includes(hash)) || [];
      if (!contentHash) {
        return;
      }
      if (nameMap[contentHash]) {
        // We've done this hash before. The source map in a file is making the
        // newHash different, so replace the same way?
        newChunkName = oldChunkName.replace(contentHash, nameMap[contentHash]);
      } else {
        // Save the hash map for replacing the secondary files
        nameMap[contentHash] = newHash;
        newChunkName = oldChunkName.replace(contentHash, newHash);
      }
    }

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

from webpack-plugin-hash-output.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.