Skip to content

Commit eb117d8

Browse files
committed
update docs with media-query-plugin section
1 parent 30b89d0 commit eb117d8

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,68 @@ module.exports = {
272272

273273
For long term caching use `filename: "[contenthash].css"`. Optionally add `[name]`.
274274

275+
### Media Query
276+
277+
When writing CSS with the help of a framework (such as [Bootstrap](https://getbootstrap.com/) or [Foundation](https://foundation.zurb.com/sites.html)) and with a modular design pattern your emited CSS files will probably contain many media queries. This is quite bad for mobile users who have to load all desktop specific CSS which they will never need.
278+
279+
To improve this you can use the [media-query-plugin](https://github.com/SassNinja/media-query-plugin) which plays together well with the mini-css-extract-plugin. It will extract the defined media queries and emit them as separate files (or inject into existing chunks if working with dynamic imports).
280+
281+
Afterwards a mobile user only needs to load this
282+
283+
```scss
284+
.foo { color: red }
285+
.bar { font-size: 1rem }
286+
```
287+
288+
instead of this
289+
290+
```css
291+
.foo { color: red }
292+
@media (min-width: 75em) {
293+
.foo { color: blue }
294+
}
295+
.bar { font-size: 1rem }
296+
```
297+
298+
#### Usage
299+
300+
Using the media-query-plugin is quite easy. Add the included loader to your rules and the plugin to your plugins. It will take over the filename option of the mini-css-extract-plugin and recognize its generated CSS chunks.
301+
302+
```javascript
303+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
304+
const MediaQueryPlugin = require('media-query-plugin');
305+
306+
module.exports = {
307+
module: {
308+
rules: [
309+
{
310+
test: /\.scss$/,
311+
use: [
312+
MiniCssExtractPlugin.loader,
313+
'css-loader',
314+
MediaQueryPlugin.loader,
315+
'postcss-loader',
316+
'sass-loader'
317+
]
318+
}
319+
]
320+
},
321+
plugins: [
322+
new MiniCssExtractPlugin({
323+
filename: '[name].css'
324+
}),
325+
new MediaQueryPlugin({
326+
include: true,
327+
queries: {
328+
'(min-width: 75em)': 'desktop'
329+
}
330+
})
331+
]
332+
};
333+
```
334+
335+
For more information please check the media-query-plugin [docs](https://github.com/SassNinja/media-query-plugin) and [examples](https://github.com/SassNinja/media-query-plugin/tree/master/examples).
336+
275337
<h2 align="center">Maintainers</h2>
276338

277339
<table>

0 commit comments

Comments
 (0)