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: enable better cssOptions override for modules #30112

Merged
merged 18 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions packages/gatsby-plugin-less/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ describe(`gatsby-plugin-less`, () => {
camelCase: false,
},
},
"css-loader use commonjs": {
cssLoaderOptions: {
esModule: false,
modules: {
namedExport: false,
},
},
},
},
}

Expand Down
19 changes: 16 additions & 3 deletions packages/gatsby-plugin-less/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,29 @@ exports.onCreateWebpackConfig = (
? [loaders.null()]
: [
loaders.miniCssExtract(),
loaders.css({ ...cssLoaderOptions, importLoaders: 2 }),
loaders.css({
importLoaders: 2,
...cssLoaderOptions,
modules: false,
}),
loaders.postcss({ plugins: postCssPlugins }),
lessLoader,
],
}
const lessRuleModules = {
test: /\.module\.less$/,
use: [
!isSSR && loaders.miniCssExtract({ modules: true }),
loaders.css({ ...cssLoaderOptions, modules: true, importLoaders: 2 }),
!isSSR &&
loaders.miniCssExtract({
modules: {
namedExport: cssLoaderOptions.modules?.namedExport ?? true,
},
}),
loaders.css({
importLoaders: 2,
...cssLoaderOptions,
modules: cssLoaderOptions.modules ?? true,
}),
loaders.postcss({ plugins: postCssPlugins }),
lessLoader,
].filter(Boolean),
Expand Down
32 changes: 22 additions & 10 deletions packages/gatsby-plugin-postcss/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# gatsby-plugin-postcss

Gatsby plugin to handle PostCSS.
Provides drop-in support for PostCSS
LekoArts marked this conversation as resolved.
Show resolved Hide resolved

## Install

Expand All @@ -11,13 +11,28 @@ Gatsby plugin to handle 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
// in gatsby-config.js
plugins: [`gatsby-plugin-postcss`],
```javascript:title=gatsby-config.js
plugins: [`gatsby-plugin-postcss`]
```

If you need to pass options to PostCSS use the plugins options; see [`postcss-loader`](https://github.com/postcss/postcss-loader) for all available options.

If you need to override the default options passed into [`css-loader`](https://github.com/webpack-contrib/css-loader).
**Note:** Gatsby is using `css-loader@^5.0.0`.

```javascript:title=gatsby-config.js
plugins: [
{
resolve: `gatsby-plugin-postcss`,
options: {
cssLoaderOptions: {
camelCase: false,
},
},
},
]
```

### With CSS Modules

Using CSS modules requires no additional configuration. Simply prepend `.module` to the extension. For example: `app.css` -> `app.module.css`.
Expand All @@ -27,8 +42,7 @@ Any file with the `module` extension will use CSS modules. CSS modules are impor

If you would prefer to add additional postprocessing to your PostCSS output you can specify plugins in the plugin options:

```javascript
// in gatsby-config.js
```javascript:title=gatsby-config.js
plugins: [
{
resolve: `gatsby-plugin-postcss`,
Expand All @@ -41,8 +55,7 @@ plugins: [

Alternatively, you can use `postcss.config.js` to specify your particular PostCSS configuration:

```javascript
// in postcss.config.js
```javascript:title=postcss.config.js
const postcssPresetEnv = require(`postcss-preset-env`)

module.exports = () => ({
Expand All @@ -58,8 +71,7 @@ If you need to override the default options passed into [`css-loader`](https://g

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
// in gatsby-config.js
```javascript:title=gatsby-config.js
plugins: [
{
resolve: `gatsby-plugin-postcss`,
Expand Down
5 changes: 0 additions & 5 deletions packages/gatsby-plugin-postcss/src/__tests__/.eslintrc

This file was deleted.

Loading