Gatsby plugin to handle PostCSS
npm install gatsby-plugin-postcssProvides drop-in support for PostCSS
npm install postcss gatsby-plugin-postcss
1. Include the plugin in your gatsby-config.js file.
2. Write your stylesheets using PostCSS (.css files) and require or import them as normal.
``javascript:title=gatsby-config.jsgatsby-plugin-postcss
plugins: []`
If you need to pass options to PostCSS use the plugins options; see postcss-loader for all available options.
If you need to override the default options passed into css-loader.
Note: Gatsby is using css-loader@^5.0.0.
`javascript:title=gatsby-config.jsgatsby-plugin-postcss
plugins: [
{
resolve: ,`
options: {
cssLoaderOptions: {
camelCase: false,
},
},
},
]
Using CSS modules requires no additional configuration. Simply prepend .module to the extension. For example: app.css -> app.module.css.module
Any file with the extension will use CSS modules. CSS modules are imported as ES Modules to support treeshaking. You'll need to import styles as: import { yourClassName, anotherClassName } from './app.module.css'
If you would prefer to add additional postprocessing to your PostCSS output you can specify plugins in the plugin options:
`javascript:title=gatsby-config.jsgatsby-plugin-postcss
plugins: [
{
resolve: ,postcss-preset-env
options: {
postCssPlugins: [require()({ stage: 0 })],`
},
},
],
Alternatively, you can use postcss.config.js to specify your particular PostCSS configuration:
`javascript:title=postcss.config.jspostcss-preset-env
const postcssPresetEnv = require()
module.exports = () => ({
plugins: [
postcssPresetEnv({
stage: 0,
}),
],
})
`
If you need to override the default options passed into css-loader.
In this example css-loader is configured to output classnames as is, instead of converting them to camel case. Named exports must be disabled for this to work, and so you have to import CSS using import styles from './file.css instead of import * as styles from './file.module.css'
`javascript:title=gatsby-config.jsgatsby-plugin-postcss
plugins: [
{
resolve: ,``
options: {
cssLoaderOptions: {
exportLocalsConvention: false,
namedExport: false,
},
},
},
]