Skip to content

Commit 29af79a

Browse files
committed
.
0 parents  commit 29af79a

File tree

6 files changed

+68
-0
lines changed

6 files changed

+68
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const postcss = require('postcss')
2+
3+
module.exports = postcss.plugin('postcss-prefix', postcssPrefix)
4+
5+
function postcssPrefix (prefix, options) {
6+
options = options || {}
7+
8+
return function (root) {
9+
root.walkRules(function (rule) {
10+
if (rule.selector === ':root') return
11+
rule.selector = prefix + rule.selector
12+
})
13+
}
14+
}

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "postcss-prefix",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"postcss": "^5.0.8"
6+
},
7+
"devDependencies": {
8+
"tape": "^4.2.1"
9+
}
10+
}

test/fixture-out.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
:root {
2+
--color-red: #f00;
3+
--color-blue: #00f;
4+
}
5+
6+
#hello-world h1 {}
7+
#hello-world h1.title {}
8+
#hello-world h2#thing {}
9+
#hello-world #thing h3 {}
10+
11+
@media screen and (max-width: 42rem) {
12+
#hello-world #another .thing > x-here {}
13+
}

test/fixture.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
:root {
2+
--color-red: #f00;
3+
--color-blue: #00f;
4+
}
5+
6+
h1 {}
7+
h1.title {}
8+
h2#thing {}
9+
#thing h3 {}
10+
11+
@media screen and (max-width: 42rem) {
12+
#another .thing > x-here {}
13+
}

test/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const prefix = require('../')
2+
const postcss = require('postcss')
3+
const test = require('tape')
4+
const path = require('path')
5+
const fs = require('fs')
6+
7+
test('postcss-prefix', function (t) {
8+
const expected = fs.readFileSync(path.join(__dirname, 'fixture-out.css'), 'utf8')
9+
const css = fs.readFileSync(path.join(__dirname, 'fixture.css'), 'utf8')
10+
const out = postcss()
11+
.use(prefix('#hello-world '))
12+
.process(css)
13+
.toString()
14+
15+
t.equal(expected, out, 'output is as expected')
16+
t.end()
17+
})

0 commit comments

Comments
 (0)