1.0.1 • Published 11 months ago

postcss-merge-at-rules v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

PostCSS Merge At Rules

npm CircleCI license npm

PostCSS plugin for merging and nesting CSS at rules.

Refer to postcss-sort-media-queries for sorting

Table of Contents

Example

before

@media screen and (width < 640px) {
	.header {
		color: #cdcdcd;
	}
	@layer defaults {
		.header {
			color: #1c1c1c;
		}
	}
}
@media screen and (min-width: 760px) {
	.button {
		color: #cdcdcd;
	}
}
@media screen and (width < 640px) {
	.main {
		color: #cdcdcd;
	}
	@layer defaults {
		.main {
			color: #1c1c1c;
		}
	}
}
@media screen and (min-width: 1280px) {
	.button {
		color: #cdcdcd;
	}
}
@media screen and (max-width: 760px) {
	.footer {
		color: #cdcdcd;
	}
}
@media screen and (max-width: 640px) {
	.footer {
		color: #cdcdcd;
	}
}

after

@media screen {
	@media (max-width: 760px) {
		.footer {
			color: #cdcdcd;
		}
	}
	@media (width < 640px) {
		/* combined */
		.header {
			color: #cdcdcd;
		}
		@layer defaults {
			.header {
				color: #1c1c1c;
			}
			.main {
				color: #1c1c1c;
			}
		}
		.main {
			color: #cdcdcd;
		}
		.footer {
			color: #cdcdcd;
		}
	}
	@media (min-width: 760px) {
		.button {
			color: #cdcdcd;
		}
	}
	@media (min-width: 1280px) {
		.button {
			color: #cdcdcd;
		}
	}
}

Install

First thing's, install the module:

npm install postcss postcss-merge-at-rules --save-dev

Usage

Check you project for existed PostCSS config: postcss.config.js in the project root, "postcss" section in package.json or postcss in bundle config.

If you already use PostCSS, add the plugin to plugins list:

module.exports = {
  plugins: [
+   require('postcss-merge-at-rules')({
+     nest: true,
+   }),
    require('autoprefixer')
  ]
}

or with custom at rule matching

module.exports = {
  plugins: [
+   require('postcss-merge-at-rules')({
+     atRulePattern: 'layer'
+   }),
    require('autoprefixer')
  ]
}

If you do not use PostCSS, add it according to official docs and set this plugin in settings.

Options

At Rule Pattern

String or regex expresion to math CSS at rules

require("postcss")([
	require("postcss-merge-at-rules")({
		atRulePattern: /(media|layer|supports)/im, // default value
	}),
]).process(css);

Merge

Merge (without sorting) any valid CSS at rule.

require("postcss")([
	require("postcss-merge-at-rules")({
		merge: true, // default value
	}),
]).process(css);

Nest

Nest compatible CSS at rules (media, container) within each other when possible.

require("postcss")([
	require("postcss-merge-at-rules")({
		nest: false, // default value
	}),
]).process(css);

Changelog

See Releases history

License

MIT

Thanks