Skip to content

Commit e90effd

Browse files
Glen Huangromainmenke
Glen Huang
authored andcommitted
Add append option
1 parent 9928d9f commit e90effd

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

plugins/postcss-custom-media/index.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* Module dependencies
33
*/
4+
var postcss = require("postcss")
45
var helpers = require("postcss-message-helpers")
56

67
/**
@@ -20,7 +21,8 @@ function customMedia(options) {
2021
return function(styles) {
2122
options = options || {}
2223
var extensions = options.extensions || {}
23-
var preserve = options.preserve
24+
var append = options.append
25+
var preserve = append || options.preserve
2426
var map = {}
2527
var toRemove = []
2628

@@ -41,8 +43,8 @@ function customMedia(options) {
4143
})
4244

4345
// apply js-defined media queries
44-
Object.keys(extensions).forEach(function(extension) {
45-
map[extension] = extensions[extension]
46+
Object.keys(extensions).forEach(function(name) {
47+
map[name] = extensions[name]
4648
})
4749

4850
// transform custom media query aliases
@@ -61,6 +63,22 @@ function customMedia(options) {
6163
})
6264
})
6365

66+
if (append) {
67+
var names = Object.keys(map)
68+
if (names.length) {
69+
names.forEach(function(name) {
70+
var atRule = postcss.atRule({
71+
name: "custom-media",
72+
afterName: " ",
73+
params: name + " " + map[name],
74+
})
75+
styles.append(atRule)
76+
})
77+
styles.semicolon = true
78+
styles.after = "\n"
79+
}
80+
}
81+
6482
// remove @custom-media
6583
toRemove.forEach(function(rule) { rule.removeSelf() })
6684
}

plugins/postcss-custom-media/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
"index.js"
2222
],
2323
"dependencies": {
24-
"postcss-message-helpers": "^2.0.0"
24+
"postcss-message-helpers": "^2.0.0",
25+
"postcss": "^4.0.2"
2526
},
2627
"devDependencies": {
2728
"jscs": "^1.6.2",
2829
"jshint": "^2.5.6",
29-
"postcss": "^4.0.2",
3030
"tape": "^3.0.0"
3131
},
3232
"scripts": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@media (--viewport-max-s) {
2+
body { font-size: 1rem; }
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@media (max-width: 30em) {
2+
body { font-size: 1rem; }
3+
}
4+
@custom-media --viewport-max-s (max-width: 30em);

plugins/postcss-custom-media/test/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,12 @@ test("@custom-media", function(t) {
3434

3535
compareFixtures(t, "preserve", "should preserve custom media", {preserve: true})
3636

37+
compareFixtures(t, "append", "should append custom media", {
38+
extensions: {
39+
"--viewport-max-s": "(max-width: 30em)",
40+
},
41+
append: true,
42+
})
43+
3744
t.end()
3845
})

0 commit comments

Comments
 (0)