Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mini-css-extract-plugin logic is broken #29

Open
yepitschunked opened this issue May 3, 2019 · 2 comments
Open

mini-css-extract-plugin logic is broken #29

yepitschunked opened this issue May 3, 2019 · 2 comments

Comments

@yepitschunked
Copy link

yepitschunked commented May 3, 2019

// This is a massive hack:

The code added for mini-css-extract doesn't appear to work.

For CSS, we have a problem; if the user configured mini-css-extract-plugin with [chunkhash] in the filename, we'll wind up with that same chunkhash in both the JS and CSS filename. When we do the global replace, it'll replace all instances of that chunkhash with either the JS or CSS content hash depending on the order this code runs. So one of them will always fail. @GoodForOneFare pointed this out in his original pr. I think we could just throw an error if more than one file contains chunk.renderedHash.

Using chunkhash with mini-css-extract-plugin appears to be wrong anyway, you're supposed to use contenthash. That's handled in the 'this is a massive hack' section, which looks for a module hash matching the one in the filename. But this breaks if the same CSS module is included in multiple chunks:

Supposed we have chunk A and B

A.files: [foo-abcd.css, ...]
B.files: [foo-abcd.css, ...]

On chunk A, we'll look for a module that has hash abcd. We find it and proceed normally, adding it to nameMap and updating the hash/renderedHash of the module itself. Now the module has hash hashed, and the asset is renamed to foo-hashed.css.

A.files: [foo-hashed.css, ...]
B.files: [foo-abcd.css, ...]

nameMap: { 'abcd': 'hashed' }

Now we're processing chunk B. We'll still be looking for a module hash 'abcd'. However, that module has been mutated when processing chunk A, so we never find it. As a result, chunk B and any following chunks will continue to emit their old chunk names. However, since the mapping from abcd -> hashed is in nameMap, we're rewriting all the runtime files to reference hashed, and this results in broken module loading.

I think the correct solution here is to look at chunk.contentHash; if any of the values there match the filename, we can use that as oldChunkName. This avoids mutating any modules.

@yepitschunked
Copy link
Author

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

@jpnelson
Copy link

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;
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants