Skip to content

"Extracting CSS based on entry" example doesn't work #628

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

Closed
pinkiebala opened this issue Oct 20, 2020 · 2 comments · Fixed by #630
Closed

"Extracting CSS based on entry" example doesn't work #628

pinkiebala opened this issue Oct 20, 2020 · 2 comments · Fixed by #630

Comments

@pinkiebala
Copy link

  • Operating System: ubuntu20.04
  • Node Version: 12.16.1
  • NPM Version: 6.13.4
  • webpack Version: 4.36.1
  • mini-css-extract-plugin Version: 1.1.0

Expected Behavior

Extracting CSS based on entry example should work.

In the reproduce repo,
CSS of comp1.css should merge into app1.css.
CSS of comp2.css should merge into app2.css.

Actual Behavior

Each of comp1 & comp2 has its own CSS chunk.

And:

function recursiveIssuer(m) {
  if (m.issuer) {
    return recursiveIssuer(m.issuer);
  } else if (m.name){ // <--- this is `undefined`
    return m.name;
  }
  return false;
}

How Do We Reproduce?

Here is the reproduce repo
https://github.com/pinkiebala/extract-css-entry-failed-reproduce

@cap-Bernardito
Copy link
Member

There is an error in the documentation, we will fix it.

You can achieve the desired effect using the example below:

const webpackVersion = require("webpack").version;

function recursiveIssuer(m) {
  if (m.issuer) {
    return recursiveIssuer(m.issuer);
  }

  const chunks = webpackVersion === "4" ? m._chunks : m.getChunks();

  for (const chunk of chunks) {
    return chunk["name"];
  }

  return false;
}

module.exports = {
  entry: {
    app1: path.resolve(__dirname, "..", "src", "app1.js"),
    app2: path.resolve(__dirname, "..", "src", "app2.js"),
  },
  ...
  optimization: {
    splitChunks: {
      cacheGroups: {
        app1Styles: {
          // For webpack 4 name should not be equal entry. We recommend upgrading to webpack@5
          name: "style_app",
          test: (m, c, entry = "app1") =>
            m.constructor.name === "CssModule" && recursiveIssuer(m) === entry,
          chunks: "all",
          enforce: true,
        },
      },
    },
  },
};

@pinkiebala
Copy link
Author

Thanks a lot!

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

Successfully merging a pull request may close this issue.

2 participants