postcss-sort-media-queries 3.11.12 → 4.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +219 -197
  3. package/browser.js +54 -0
  4. package/index.js +52 -49
  5. package/package.json +15 -12
package/CHANGELOG.md CHANGED
@@ -5,6 +5,29 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [4.2.1] 2021-11-28
9
+
10
+ - Update **postcss** to [8.4.4](https://github.com/postcss/postcss/releases/tag/8.4.4)
11
+ - Update dev dependencies
12
+
13
+ ## [4.1.0] 2021-09-13
14
+
15
+ - Add module for browser
16
+
17
+ ## [4.0.0] 2021-09-13
18
+
19
+ - Add [sort-css-media-queries#configuration-options](https://github.com/dutchenkoOleg/sort-css-media-queries#configuration-options) support
20
+
21
+ ## [3.12.13] 2021-09-12
22
+
23
+ - Update **postcss** to [8.3.6](https://github.com/postcss/postcss/releases/tag/8.3.6)
24
+ - Fixed column in `missed semicolon` error (by @Gusted).
25
+ - Update **sort-css-media-queries** to [v2.0.4](https://github.com/dutchenkoOleg/sort-css-media-queries/releases/tag/v2.0.4)
26
+ - Fix package dependencies
27
+ - Update dev dependencies
28
+ - Fixed dependencies vulnerabilities
29
+ - Add online demo
30
+
8
31
  ## [3.11.12] 2021-06-10
9
32
 
10
33
  - Update **postcss** to [postcss/releases/tag/8.3.1](https://github.com/postcss/postcss/releases/tag/8.3.1)
package/README.md CHANGED
@@ -1,197 +1,219 @@
1
- # PostCSS Sort Media Queries
2
-
3
- [PostCSS]: https://github.com/postcss/postcss
4
- [ci-img]: https://travis-ci.org/solversgroup/postcss-sort-media-queries.svg
5
- [ci]: https://travis-ci.org/solversgroup/postcss-sort-media-queries
6
- [MIT]: https://github.com/solversgroup/postcss-sort-media-queries/blob/master/LICENSE
7
- [official docs]: https://github.com/postcss/postcss#usage
8
- [Releases history]: https://github.com/solversgroup/postcss-sort-media-queries/blob/master/CHANGELOG.md
9
-
10
- [![npm](https://img.shields.io/npm/v/postcss-sort-media-queries.svg)](https://www.npmjs.com/package/postcss-sort-media-queries) [![Build Status][ci-img]][ci]
11
- [![npm](https://img.shields.io/npm/dt/postcss-sort-media-queries.svg)](https://www.npmjs.com/package/postcss-sort-media-queries)
12
-
13
- [PostCSS] plugin for combine and sort CSS media queries with **mobile first** or **desktop first** methods.
14
-
15
- > **Combine** same media queries into one - is a unexpected side effect for this plugin 🧬
16
-
17
- ## Examples
18
-
19
- ### Mobile first sorting
20
-
21
- ```css
22
- /* before */
23
- @media screen and (max-width: 640px) {
24
- .head { color: #cfcfcf }
25
- }
26
- @media screen and (max-width: 768px) {
27
- .footer { color: #cfcfcf }
28
- }
29
- @media screen and (max-width: 640px) {
30
- .main { color: #cfcfcf }
31
- }
32
- @media screen and (min-width: 1280px) {
33
- .mobile-first { color: #cfcfcf }
34
- }
35
- @media screen and (min-width: 640px) {
36
- .mobile-first { color: #cfcfcf }
37
- }
38
- @media screen and (max-width: 640px) {
39
- .footer { color: #cfcfcf }
40
- }
41
-
42
- /* after */
43
- @media screen and (min-width: 640px) {
44
- .mobile-first { color: #cfcfcf }
45
- }
46
- @media screen and (min-width: 1280px) {
47
- .mobile-first { color: #cfcfcf }
48
- }
49
- @media screen and (max-width: 768px) {
50
- .footer { color: #cfcfcf }
51
- }
52
- @media screen and (max-width: 640px) {
53
- .head { color: #cfcfcf }
54
- .main { color: #cfcfcf }
55
- .footer { color: #cfcfcf }
56
- }
57
- ```
58
-
59
- ### Desktop first sorting
60
-
61
- ```css
62
- /* before */
63
- @media screen and (max-width: 640px) {
64
- .header { color: #cdcdcd }
65
- }
66
- @media screen and (min-width: 760px) {
67
- .desktop-first { color: #cdcdcd }
68
- }
69
- @media screen and (max-width: 640px) {
70
- .main { color: #cdcdcd }
71
- }
72
- @media screen and (min-width: 1280px) {
73
- .desktop-first { color: #cdcdcd }
74
- }
75
- @media screen and (max-width: 760px) {
76
- .footer { color: #cdcdcd }
77
- }
78
- @media screen and (max-width: 640px) {
79
- .footer { color: #cdcdcd }
80
- }
81
-
82
- /* after */
83
- @media screen and (max-width: 760px) {
84
- .footer { color: #cdcdcd }
85
- }
86
- @media screen and (max-width: 640px) {
87
- .header { color: #cdcdcd }
88
- .main { color: #cdcdcd }
89
- .footer { color: #cdcdcd }
90
- }
91
- @media screen and (min-width: 760px) {
92
- .desktop-first { color: #cdcdcd }
93
- }
94
- @media screen and (min-width: 1280px) {
95
- .desktop-first { color: #cdcdcd }
96
- }
97
- ```
98
-
99
- ## Getting Started
100
-
101
- First thing's, install the module:
102
-
103
- ```
104
- npm install postcss postcss-sort-media-queries --save-dev
105
- ```
106
-
107
- ## 🍳 Usage
108
-
109
- Check you project for existed PostCSS config: `postcss.config.js`
110
- in the project root, `"postcss"` section in `package.json`
111
- or `postcss` in bundle config.
112
-
113
- If you already use PostCSS, add the plugin to plugins list:
114
-
115
- ```diff
116
- module.exports = {
117
- plugins: [
118
- + require('postcss-sort-media-queries')({
119
- + // sort: 'mobile-first' default value
120
- + sort: function(a, b) {
121
- + // custom sorting function
122
- + }
123
- + }),
124
- require('autoprefixer')
125
- ]
126
- }
127
- ```
128
-
129
- If you do not use PostCSS, add it according to [official docs]
130
- and set this plugin in settings.
131
-
132
- ## 🍰 Options
133
-
134
- > Sorting works based on [dutchenkoOleg/sort-css-media-queries](https://github.com/dutchenkoOleg/sort-css-media-queries) function.
135
-
136
- ### sort
137
-
138
- This option support **string** or **function** values.
139
-
140
- - `{string}` `'mobile-first'` - (default) mobile first sorting
141
- - `{string}` `'desktop-first'` - desktop first sorting
142
- - `{function}` your own sorting function
143
-
144
- #### `'mobile-first'`
145
-
146
- ```js
147
- postcss([
148
- sortMediaQueries({
149
- sort: 'mobile-first' // default
150
- })
151
- ]).process(css);
152
- ```
153
-
154
- #### `'desktop-first'`
155
-
156
- ```js
157
- postcss([
158
- sortMediaQueries({
159
- sort: 'desktop-first'
160
- })
161
- ]).process(css);
162
- ```
163
-
164
- #### Custom sort function
165
- ```js
166
- postcss([
167
- sortMediaQueries({
168
- function(a, b) {
169
- return a.localeCompare(b);
170
- }
171
- })
172
- ]).process(css);
173
- ```
174
-
175
- In this example, all your media queries will sort by A-Z order.
176
-
177
- This sorting function is directly passed to Array#sort() method of an array of all your media queries.
178
-
179
- ## Changelog
180
-
181
- See [Releases history]
182
-
183
- ## License
184
-
185
- [MIT]
186
-
187
- ## Other PostCSS plugins
188
-
189
- - [`postcss-momentum-scrolling`](https://github.com/solversgroup/postcss-momentum-scrolling) - plugin for adding **momentum** style scrolling behavior (`-webkit-overflow-scrolling:touch`) for elements with overflow (scroll, auto) on iOS
190
-
191
- ## Thanks 💪
192
-
193
- - Andrey Sitnik [@ai](https://github.com/ai)
194
- - Jakub Caban [@Lustmored](https://github.com/Lustmored)
195
- - Dmytro Symonov [@Kassaila](https://github.com/Kassaila)
196
- - Kai Falkowski [@SassNinja](https://github.com/SassNinja)
197
- - Олег Дутченко [@dutchenkoOleg](https://github.com/dutchenkoOleg)
1
+ # PostCSS Sort Media Queries
2
+
3
+ [PostCSS]: https://github.com/postcss/postcss
4
+ [ci-img]: https://travis-ci.org/solversgroup/postcss-sort-media-queries.svg
5
+ [ci]: https://travis-ci.org/solversgroup/postcss-sort-media-queries
6
+ [MIT]: https://github.com/solversgroup/postcss-sort-media-queries/blob/master/LICENSE
7
+ [official docs]: https://github.com/postcss/postcss#usage
8
+ [Releases history]: https://github.com/solversgroup/postcss-sort-media-queries/blob/master/CHANGELOG.md
9
+
10
+ [![npm](https://img.shields.io/npm/v/postcss-sort-media-queries.svg)](https://www.npmjs.com/package/postcss-sort-media-queries) [![Build Status][ci-img]][ci]
11
+ [![npm](https://img.shields.io/npm/dt/postcss-sort-media-queries.svg)](https://www.npmjs.com/package/postcss-sort-media-queries)
12
+
13
+ [PostCSS] plugin for combine and sort CSS media queries with **mobile first** or **desktop first** methods.
14
+
15
+ > **Combine** same media queries into one - is a unexpected side effect for this plugin 🧬
16
+
17
+ ## Online demo
18
+
19
+ And here is the [online demo](https://postcss-sort-media-queries.github.io)
20
+
21
+ ## Examples
22
+
23
+ ### Mobile first sorting
24
+
25
+ ```css
26
+ /* before */
27
+ @media screen and (max-width: 640px) {
28
+ .head { color: #cfcfcf }
29
+ }
30
+ @media screen and (max-width: 768px) {
31
+ .footer { color: #cfcfcf }
32
+ }
33
+ @media screen and (max-width: 640px) {
34
+ .main { color: #cfcfcf }
35
+ }
36
+ @media screen and (min-width: 1280px) {
37
+ .mobile-first { color: #cfcfcf }
38
+ }
39
+ @media screen and (min-width: 640px) {
40
+ .mobile-first { color: #cfcfcf }
41
+ }
42
+ @media screen and (max-width: 640px) {
43
+ .footer { color: #cfcfcf }
44
+ }
45
+
46
+ /* after */
47
+ @media screen and (min-width: 640px) {
48
+ .mobile-first { color: #cfcfcf }
49
+ }
50
+ @media screen and (min-width: 1280px) {
51
+ .mobile-first { color: #cfcfcf }
52
+ }
53
+ @media screen and (max-width: 768px) {
54
+ .footer { color: #cfcfcf }
55
+ }
56
+ @media screen and (max-width: 640px) {
57
+ .head { color: #cfcfcf }
58
+ .main { color: #cfcfcf }
59
+ .footer { color: #cfcfcf }
60
+ }
61
+ ```
62
+
63
+ ### Desktop first sorting
64
+
65
+ ```css
66
+ /* before */
67
+ @media screen and (max-width: 640px) {
68
+ .header { color: #cdcdcd }
69
+ }
70
+ @media screen and (min-width: 760px) {
71
+ .desktop-first { color: #cdcdcd }
72
+ }
73
+ @media screen and (max-width: 640px) {
74
+ .main { color: #cdcdcd }
75
+ }
76
+ @media screen and (min-width: 1280px) {
77
+ .desktop-first { color: #cdcdcd }
78
+ }
79
+ @media screen and (max-width: 760px) {
80
+ .footer { color: #cdcdcd }
81
+ }
82
+ @media screen and (max-width: 640px) {
83
+ .footer { color: #cdcdcd }
84
+ }
85
+
86
+ /* after */
87
+ @media screen and (max-width: 760px) {
88
+ .footer { color: #cdcdcd }
89
+ }
90
+ @media screen and (max-width: 640px) {
91
+ .header { color: #cdcdcd }
92
+ .main { color: #cdcdcd }
93
+ .footer { color: #cdcdcd }
94
+ }
95
+ @media screen and (min-width: 760px) {
96
+ .desktop-first { color: #cdcdcd }
97
+ }
98
+ @media screen and (min-width: 1280px) {
99
+ .desktop-first { color: #cdcdcd }
100
+ }
101
+ ```
102
+
103
+ ## Getting Started
104
+
105
+ First thing's, install the module:
106
+
107
+ ```
108
+ npm install postcss postcss-sort-media-queries --save-dev
109
+ ```
110
+
111
+ ## 🍳 Usage
112
+
113
+ Check you project for existed PostCSS config: `postcss.config.js`
114
+ in the project root, `"postcss"` section in `package.json`
115
+ or `postcss` in bundle config.
116
+
117
+ If you already use PostCSS, add the plugin to plugins list:
118
+
119
+ ```diff
120
+ module.exports = {
121
+ plugins: [
122
+ + require('postcss-sort-media-queries')({
123
+ + // sort: 'mobile-first' default value
124
+ + sort: function(a, b) {
125
+ + // custom sorting function
126
+ + }
127
+ + }),
128
+ require('autoprefixer')
129
+ ]
130
+ }
131
+ ```
132
+
133
+ If you do not use PostCSS, add it according to [official docs]
134
+ and set this plugin in settings.
135
+
136
+ ## 🍰 Options
137
+
138
+ > Sorting works based on [dutchenkoOleg/sort-css-media-queries](https://github.com/dutchenkoOleg/sort-css-media-queries) function.
139
+
140
+ ### sort
141
+
142
+ This option support **string** or **function** values.
143
+
144
+ - `{string}` `'mobile-first'` - (default) mobile first sorting
145
+ - `{string}` `'desktop-first'` - desktop first sorting
146
+ - `{function}` your own sorting function
147
+
148
+ #### `'mobile-first'`
149
+
150
+ ```js
151
+ postcss([
152
+ sortMediaQueries({
153
+ sort: 'mobile-first' // default
154
+ })
155
+ ]).process(css);
156
+ ```
157
+
158
+ #### `'desktop-first'`
159
+
160
+ ```js
161
+ postcss([
162
+ sortMediaQueries({
163
+ sort: 'desktop-first'
164
+ })
165
+ ]).process(css);
166
+ ```
167
+
168
+ #### Custom sort function
169
+ ```js
170
+ postcss([
171
+ sortMediaQueries({
172
+ function(a, b) {
173
+ return a.localeCompare(b);
174
+ }
175
+ })
176
+ ]).process(css);
177
+ ```
178
+
179
+ In this example, all your media queries will sort by A-Z order.
180
+
181
+ This sorting function is directly passed to Array#sort() method of an array of all your media queries.
182
+
183
+ ### Sort configuration
184
+
185
+ By this configuration you can control sorting behaviour.
186
+
187
+ ```js
188
+ postcss([
189
+ sortMediaQueries({
190
+ configuration: {
191
+ unitlessMqAlwaysFirst: true, // or false
192
+ }
193
+ })
194
+ ]).process(css);
195
+ ```
196
+
197
+ Or alternatively create a `sort-css-mq.config.json` file in the root of your project. Or add property `sortCssMQ: {}` in your `package.json`.
198
+
199
+ ---
200
+
201
+ ## Changelog
202
+
203
+ See [Releases history]
204
+
205
+ ## License
206
+
207
+ [MIT]
208
+
209
+ ## Other PostCSS plugins
210
+
211
+ - [`postcss-momentum-scrolling`](https://github.com/solversgroup/postcss-momentum-scrolling) - plugin for adding **momentum** style scrolling behavior (`-webkit-overflow-scrolling:touch`) for elements with overflow (scroll, auto) on iOS
212
+
213
+ ## Thanks 💪
214
+
215
+ - Andrey Sitnik [@ai](https://github.com/ai)
216
+ - Jakub Caban [@Lustmored](https://github.com/Lustmored)
217
+ - Dmytro Symonov [@Kassaila](https://github.com/Kassaila)
218
+ - Kai Falkowski [@SassNinja](https://github.com/SassNinja)
219
+ - Олег Дутченко [@dutchenkoOleg](https://github.com/dutchenkoOleg)
package/browser.js ADDED
@@ -0,0 +1,54 @@
1
+ function sortAtRules(queries, sort, sortCSSmq) {
2
+ if (typeof sort !== 'function') {
3
+ sort = sort === 'desktop-first' ? sortCSSmq.desktopFirst : sortCSSmq
4
+ }
5
+
6
+ return queries.sort(sort)
7
+ }
8
+
9
+ module.exports = (opts = {}) => {
10
+
11
+ opts = Object.assign(
12
+ {
13
+ sort: 'mobile-first',
14
+ configuration: {
15
+ unitlessMqAlwaysFirst: false,
16
+ },
17
+ },
18
+ opts
19
+ )
20
+
21
+ const createSort = require('sort-css-media-queries/lib/create-sort');
22
+ const sortCSSmq = createSort(opts.configuration);
23
+
24
+ return {
25
+ postcssPlugin: 'postcss-sort-media-queries',
26
+ OnceExit(root, { AtRule }) {
27
+
28
+ let atRules = [];
29
+
30
+ root.walkAtRules('media', atRule => {
31
+ let query = atRule.params
32
+
33
+ if (!atRules[query]) {
34
+ atRules[query] = new AtRule({
35
+ name: atRule.name,
36
+ params: atRule.params,
37
+ source: atRule.source
38
+ })
39
+ }
40
+
41
+ atRule.nodes.forEach(node => {
42
+ atRules[query].append(node.clone())
43
+ })
44
+
45
+ atRule.remove()
46
+ })
47
+
48
+ sortAtRules(Object.keys(atRules), opts.sort, sortCSSmq).forEach(query => {
49
+ root.append(atRules[query])
50
+ })
51
+ }
52
+ }
53
+ }
54
+ module.exports.postcss = true
package/index.js CHANGED
@@ -1,49 +1,52 @@
1
- const sortCSSmq = require('sort-css-media-queries')
2
-
3
- function sortAtRules (queries, sort) {
4
- if (typeof sort !== 'function') {
5
- sort = sort === 'desktop-first' ? sortCSSmq.desktopFirst : sortCSSmq
6
- }
7
-
8
- return queries.sort(sort)
9
- }
10
-
11
- module.exports = (opts = {}) => {
12
- opts = Object.assign(
13
- {
14
- sort: 'mobile-first'
15
- },
16
- opts
17
- )
18
-
19
- return {
20
- postcssPlugin: 'postcss-sort-media-queries',
21
- OnceExit (root, { AtRule }) {
22
-
23
- let atRules = [];
24
-
25
- root.walkAtRules('media', atRule => {
26
- let query = atRule.params
27
-
28
- if (!atRules[query]) {
29
- atRules[query] = new AtRule({
30
- name: atRule.name,
31
- params: atRule.params,
32
- source: atRule.source
33
- })
34
- }
35
-
36
- atRule.nodes.forEach(node => {
37
- atRules[query].append(node.clone())
38
- })
39
-
40
- atRule.remove()
41
- })
42
-
43
- sortAtRules(Object.keys(atRules), opts.sort).forEach(query => {
44
- root.append(atRules[query])
45
- })
46
- }
47
- }
48
- }
49
- module.exports.postcss = true
1
+ function sortAtRules(queries, sort, sortCSSmq) {
2
+ if (typeof sort !== 'function') {
3
+ sort = sort === 'desktop-first' ? sortCSSmq.desktopFirst : sortCSSmq
4
+ }
5
+
6
+ return queries.sort(sort)
7
+ }
8
+
9
+ module.exports = (opts = {}) => {
10
+
11
+ opts = Object.assign(
12
+ {
13
+ sort: 'mobile-first',
14
+ configuration: false,
15
+ },
16
+ opts
17
+ )
18
+
19
+ const createSort = require('sort-css-media-queries/lib/create-sort');
20
+ const sortCSSmq = opts.configuration ? createSort(opts.configuration) : require('sort-css-media-queries');
21
+
22
+ return {
23
+ postcssPlugin: 'postcss-sort-media-queries',
24
+ OnceExit (root, { AtRule }) {
25
+
26
+ let atRules = [];
27
+
28
+ root.walkAtRules('media', atRule => {
29
+ let query = atRule.params
30
+
31
+ if (!atRules[query]) {
32
+ atRules[query] = new AtRule({
33
+ name: atRule.name,
34
+ params: atRule.params,
35
+ source: atRule.source
36
+ })
37
+ }
38
+
39
+ atRule.nodes.forEach(node => {
40
+ atRules[query].append(node.clone())
41
+ })
42
+
43
+ atRule.remove()
44
+ })
45
+
46
+ sortAtRules(Object.keys(atRules), opts.sort, sortCSSmq).forEach(query => {
47
+ root.append(atRules[query])
48
+ })
49
+ }
50
+ }
51
+ }
52
+ module.exports.postcss = true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-sort-media-queries",
3
- "version": "3.11.12",
3
+ "version": "4.2.1",
4
4
  "description": "PostCSS plugin for combine and sort CSS media queries with mobile first or desktop first methods.",
5
5
  "keywords": [
6
6
  "postcss",
@@ -30,25 +30,25 @@
30
30
  "node": ">=10.0.0"
31
31
  },
32
32
  "dependencies": {
33
- "sort-css-media-queries": "1.5.4"
33
+ "sort-css-media-queries": "2.0.4"
34
34
  },
35
35
  "devDependencies": {
36
- "autoprefixer": "^10.2.6",
37
- "eslint": "^7.28.0",
36
+ "autoprefixer": "^10.4.0",
37
+ "eslint": "^8.3.0",
38
38
  "eslint-ci": "^1.0.0",
39
- "eslint-plugin-jest": "^24.3.6",
40
- "husky": "^6.0.0",
41
- "jest": "^27.0.4",
39
+ "eslint-plugin-jest": "^25.3.0",
40
+ "husky": "^7.0.4",
41
+ "jest": "^27.3.1",
42
42
  "jest-ci": "^0.1.1",
43
- "jest-cli": "^27.0.4",
44
- "lint-staged": "^11.0.0",
45
- "postcss": "^8.3.1",
43
+ "jest-cli": "^27.3.1",
44
+ "lint-staged": "^12.1.2",
45
+ "postcss": "^8.4.4",
46
46
  "postcss-flexbugs-fixes": "^5.0.2",
47
47
  "postcss-media-minmax": "^5.0.0",
48
- "postcss-nested": "^5.0.5"
48
+ "postcss-nested": "^5.0.6"
49
49
  },
50
50
  "peerDependencies": {
51
- "postcss": "^8.3.1"
51
+ "postcss": "^8.4.4"
52
52
  },
53
53
  "husky": {
54
54
  "hooks": {
@@ -81,5 +81,8 @@
81
81
  "statements": 100
82
82
  }
83
83
  }
84
+ },
85
+ "sortCssMQ": {
86
+ "unitlessMqAlwaysFirst": false
84
87
  }
85
88
  }