postcss-merge-at-rules

1.1.0 • Public • Published

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;
		}
	}
}
@layer reset {
	@media screen {
		.picture {
			display: block;
		}
	}
}
@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 and (max-width: 760px) {
	.footer {
		color: #cdcdcd;
	}
}
@media screen {
	@layer reset {
		.picture {
			display: block;
		}
	}
}
@media screen and (width < 640px) {
	/* combined */
	.header {
		color: #cdcdcd;
	}
	@layer defaults {
		.header {
			color: #1c1c1c;
		}
		.main {
			color: #1c1c1c;
		}
	}
	.main {
		color: #cdcdcd;
	}
	.footer {
		color: #cdcdcd;
	}
}
@media screen and (min-width: 760px) {
	.button {
		color: #cdcdcd;
	}
}
@media screen and (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);

Flatten

Flatten (without sorting) any valid CSS at rule. media at rules take precedence

require("postcss")([
	require("postcss-merge-at-rules")({
		flatten: true, // 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: true, // false by default
	}),
]).process(css);

Changelog

See Releases history

License

MIT

Thanks

Package Sidebar

Install

npm i postcss-merge-at-rules

Weekly Downloads

31

Version

1.1.0

License

MIT

Unpacked Size

22.4 kB

Total Files

11

Last publish

Collaborators

  • vis97c