gatsby: [gatsby-plugin-sass] prefix missing in css

Summary

When using css grid the necessary -ms prefix is not added in my css, thus not rendering correctly in IE. Is there a setting i am missing?

Environment

System: OS: macOS 10.14.5 CPU: (8) x64 Intel® Core™ i7-7920HQ CPU @ 3.10GHz Shell: 3.2.57 - /bin/bash Binaries: Node: 10.15.0 - ~/.nvm/versions/node/v10.15.0/bin/node Yarn: 1.13.0 - /usr/local/bin/yarn npm: 6.9.0 - ~/.nvm/versions/node/v10.15.0/bin/npm Languages: Python: 2.7.10 - /usr/bin/python Browsers: Chrome: 75.0.3770.100 Firefox: 65.0.1 Safari: 12.1.1 npmPackages: gatsby: ^2.9.4 => 2.9.4 gatsby-plugin-eslint: ^2.0.4 => 2.0.4 gatsby-plugin-sass: ^2.0.10 => 2.0.10 gatsby-source-prismic: ^2.3.0-alpha.3 => 2.3.0-alpha.3 npmGlobalPackages: gatsby-cli: 2.4.17

File contents

gatsby-config.js:

require('dotenv').config({
  path: `.env.${process.env.NODE_ENV}`
});

module.exports = {
  siteMetadata: {
    title: 'XXXXXXX',
    siteUrl: 'XXXXXXX',
    description:
      'XXXXXXXX'
  },
  plugins: [
    {
      resolve: `gatsby-source-prismic`,
      options: {
        repositoryName: `XXXXXX`,
        accessToken: `${process.env.API_KEY}`
      }
    },
    'gatsby-plugin-eslint',
    'gatsby-plugin-sass'
  ]
};

package.json:

{
  "name": "XXXXX",
  "private": true,
  "description": "XXXXXXX",
  "version": "0.1.0",
  "license": "MIT",
  "scripts": {
    "build": "gatsby build",
    "develop": "gatsby develop -H 0.0.0.0",
    "start": "npm run develop",
    "serve": "gatsby serve",
    "test": "echo \"Write tests! -> https://gatsby.app/unit-testing\""
  },
  "dependencies": {
    "gatsby": "^2.9.4",
    "gatsby-plugin-sass": "^2.0.10",
    "gatsby-source-prismic": "^2.3.0-alpha.3",
    "node-sass": "^4.11.0",
    "react": "^16.8.0",
    "react-dom": "^16.8.0"
  },
  "repository": {
    "type": "git",
    "url": "XXXXXX"
  },
  "devDependencies": {
    "babel-eslint": "^10.0.1",
    "classnames": "^2.2.6",
    "dotenv": "^8.0.0",
    "eslint": "^5.13.0",
    "eslint-loader": "^2.1.2",
    "eslint-plugin-import": "^2.16.0",
    "gatsby-plugin-eslint": "^2.0.4",
    "prop-types": "^15.7.2"
  }
}

gatsby-node.js: N/A gatsby-browser.js: N/A gatsby-ssr.js: N/A

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 21 (12 by maintainers)

Most upvoted comments

The question for this thread may be, then: “how do I change autoprefixer configuration in Gatsby?”

Ok - this is very hacky and might need to be iterated on or turned into plugin to make it easier to use - but if you use this in your gatsby-node.js:

const postCssLoaderRe = /\/postcss-loader\//
const targetFiles = [`.module.css`, `.css`]

const processRule = rule => {
  if (rule.oneOf) {
    return {
      ...rule,
      oneOf: rule.oneOf.map(processRule),
    }
  }

  // if rule doesn't target one of targetFiles - leave rule untouched
  if (
    !targetFiles.some(targetFile => {
      if (rule.test instanceof RegExp) {
        return rule.test.test(targetFile)
      } else {
        return false
      }
    })
  ) {
    return rule
  }

  if (Array.isArray(rule.use)) {
    return {
      ...rule,
      use: rule.use.map(use => {
        if (!postCssLoaderRe.test(use.loader)) {
          // if it's not postcss loader - leave loader untouched
          return use
        }

        return {
          ...use,
          options: {
            ...use.options,
            plugins: loader => {
              const result = use.options.plugins(loader)

              return result.map(postCssPlugin => {
                if (postCssPlugin.postcssPlugin === `autoprefixer`) {
                  // modify `postCssPlugin.options` here
                  postCssPlugin.options.grid = `autoplace`
                }

                return postCssPlugin
              })
            },
          },
        }
      }),
    }
  }

  return rule
}

exports.onCreateWebpackConfig = ({ getConfig, actions }) => {
  const config = getConfig()

  const newConfig = {
    ...config,
    module: {
      ...config.module,
      rules: config.module.rules.map(processRule),
    },
  }
  actions.replaceWebpackConfig(newConfig)
}

you can modify autoprefixer options (only important line here for modification is one containing postCssPlugin.options.grid = `autoplace`

–edit: also if you want to target sass files - edit targetFiles in the beginning of the snippet to add .scss/.module.scss

@pieh That page has grid prefixing turned on, which is false by default:

Options The IE grid layout polyfill is enabled, which is not by default in autoprefixer.

Here’s some info about why it’s not enabled by default: https://github.com/postcss/autoprefixer#does-autoprefixer-polyfill-grid-layout-for-ie

/* autoprefixer grid: on */ only adds prefixes in development mode. This is more than likely a bug with Gatsby. No matter what config I try, nothing seems to work. For example, this postcss.config.js works perfectly fine with Next.js and create-react-app but for whatever reason, it doesn’t work with Gatsby.

module.exports = {
  plugins: {
    "postcss-preset-env": {
      autoprefixer: {
        grid: "no-autoplace"
      }
    }
  }
};

Is there any update to this? I am currently having issues trying to get -ms prefixes for my grid.

I am able to use /* autoprefixer grid: on */ and get the prefixes using Gatsby’s development mode, but then they disappear on build.

Thanks @missmatsuko - I was chasing it down the rabbit hole and doing some breakpoint debbuging and I definitely missed that note there. You just saved me a lot of time and frustration 🙏

The question for this thread may be, then: “how do I change autoprefixer configuration in Gatsby?”

Yeah, I think this makes a lot of sense here. Also probably dedicated note specifically for grid layout and IE linking to docs that @missmatsuko pasted.

I’ll take a look if it’s possible right now to hook into autoprefixer options or we need to provide some new API to do that.

The control comments will work as well, but you will need to remember to use them in every file as they have local scope (it might be something you actually want)

Oh yeah, looks like it may be possible to enable grid prefixing through a control comment in the CSS:

/* autoprefixer grid: autoplace */

We can modify webpack config via plugins (and gatsby APIs in general), so it’s technically possible (but certainly will be messy). There is defenitely demand to make it configurable (based on linked issue and this one). I’ll try to come up with code snippet to do this