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

feat(gatsby-plugin-sass): Allow miniCssExtract to accept options #18889

Closed

Conversation

tmilewski
Copy link

Description

This PR allows for miniCssExtract to accept options from gatsby-plugin-sass.

Related Issues

None

@tmilewski tmilewski requested a review from a team as a code owner October 21, 2019 18:04
@pieh
Copy link
Contributor

pieh commented Oct 22, 2019

What is the use case for this? If there is issue that require changing the miniCssExtract options, maybe we could attack this directly and change default settings in gatsby core?

@LekoArts LekoArts added the status: awaiting author response Additional information has been requested from the author label Nov 20, 2019
@LekoArts
Copy link
Contributor

Friendly ping @tmilewski

@tmilewski
Copy link
Author

@LekoArts @pieh I'll defer to you two as to whether this should go into core or not. I wanted to make sure people had the option one way or the other.

This solves the case where miniCssExtract is warning on the ordering of styles. There are cases where this isn't an issue (see: self-contained styles with each component) but it's still throwing out warnings left-and-right.

webpack-contrib/mini-css-extract-plugin#250 (comment)

This seems like an unreasonable request (to ensure order within our graph)... we need a way to turn this off for projects that don't care. For example, in my projects, each component's styles are self-contained so the order is only important within each component. (each component has a very unique class name root and all selectors are scoped to them)

webpack-contrib/mini-css-extract-plugin#250 (comment)

This issue has been resolved by the addition of ignoreOrder option: https://github.com/webpack-contrib/mini-css-extract-plugin/blob/v0.8.0/README.md#remove-order-warnings

Please open an issue in the repository of the tool you're using that uses mini-css-extract-plugin to discuss if they should use ignoreOrder.

I understand the maintainers here have understood that the warnings are difficult to decipher, and they don't currently need new comments to reflect that. See the comment by @sokra near the first comments in this issue thread #250 (comment):

I must admit that the warning could be improved to point to the locations that need to be changed.

Please open a new issue if ignoreOrder is not working as expected, with reproduction steps, if you think there is an issue with that option.

@pieh
Copy link
Contributor

pieh commented Nov 27, 2019

Just to understand what ignoreOrder does - it just silence the warning about "conflicting styles imports order" in terminal?

@pieh pieh removed the status: awaiting author response Additional information has been requested from the author label Nov 27, 2019
@sidharthachatterjee sidharthachatterjee changed the title [gatsby-plugin-sass] Allow for miniCssExtract to accept options feat(gatsby-plugin-sass): Allow miniCssExtract to accept options Nov 29, 2019
@tmilewski
Copy link
Author

@pieh That's correct.

@NathHorrigan
Copy link

It would be really great if this could be merged in, currently having issues regarding order warnings.

@pvdz
Copy link
Contributor

pvdz commented Apr 16, 2020

I've opened #23170 to rebase the changes and get this merged.

@pvdz
Copy link
Contributor

pvdz commented Apr 20, 2020

@tmilewski could you rebase this PR to current master, and add some documentation around it? I'm not entirely sure how to document it and otherwise I might have to close this. It looks good otherwise. Cheers!

@wardpeet
Copy link
Contributor

Hey @tmilewski,

first of all, sorry to keep you waiting. I've looked at your changes and they're not fixing the problem. Your PR changes the options for the "mini-css-extract-plugin loader". The loader only has the following options:

  • publicPath
  • hmr
  • esModule
  • reloadAll

IgnoreOrder is defined on the plugin itself.

new MiniCssExtractPlugin({
filename: `[name].[contenthash].css`,
chunkFilename: `[name].[contenthash].css`,
...options,
})

Sadly, we currently do not have a proper API to change these values from a plugin. What you can do is use the replaceWebpackConfig action, in your gatsby-node.js file. With replaceWebpackConfig you can remove the minicssextract plugin and add yours with the ignorePlugin option.

exports.onCreateWebpackConfig = ({ actions, stage, getConfig, loaders }) => {
  if (stage === "build-javascript") {
    const config = getConfig()
    const index = config.plugins.findIndex(plugin => {
      return plugin.constructor.name === "MiniCssExtractPlugin"
    })
    //remove extract plugin from current plugin list
    config.plugins.splice(index, 1)
    // add our own miniCssExtract plugin
    config.plugins.push(loaders.miniCssExtract({ ignoreOrder: true }))
    actions.replaceWebpackConfig(config)
  }
}

@wardpeet wardpeet closed this Apr 21, 2020
@AmruthPillai
Copy link
Contributor

@wardpeet I'm getting this issue when I try to run your code:

Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration.plugins[7] misses the property 'apply'.
   function
   -> The run point of the plugin, required method.

@AmruthPillai AmruthPillai reopened this Jul 7, 2020
@AmruthPillai
Copy link
Contributor

This is what worked for me.

gatsby-node.js

const MiniCssExtractPlugin = require("mini-css-extract-plugin");

exports.onCreateWebpackConfig = ({ actions, stage, getConfig, loaders }) => {
  if (stage === "build-javascript") {
    const config = getConfig();
    const index = config.plugins.findIndex((plugin) => {
      return plugin.constructor.name === "MiniCssExtractPlugin";
    });
    config.plugins[index] = new MiniCssExtractPlugin({ ignoreOrder: true });
    actions.replaceWebpackConfig(config);
  }
};

mini-css-extract-plugin is already a dependency, so you don't need to install it again.

@gabrielpoca
Copy link

For anyone else finding this issue, this is what worked for me:

exports.onCreateWebpackConfig = ({ actions, stage, getConfig, loaders }) => {
  if (stage === "build-javascript" || stage === "develop") {
    const config = getConfig()

    const index = config.plugins.findIndex(
      (plugin) => plugin.constructor.name === "MiniCssExtractPlugin"
    )

    if (index) config.plugins[index].options.ignoreOrder = true

    actions.replaceWebpackConfig(config)
  }
}

Instead of replacing the plugin I just change the option ignoreOrder to true, that way we don't remove any options set by Gatsby.

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 this pull request may close these issues.

8 participants