Skip to content

Commit ffd8348

Browse files
committed
Support both the hooRequire way and the parser way
1 parent 4d0f59f commit ffd8348

File tree

9 files changed

+105
-20
lines changed

9 files changed

+105
-20
lines changed

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: node_js
2+
node_js:
3+
- "5"
4+
- "4"
5+
- "0.12"
6+
- "0.11"
7+
- "iojs"

README.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,46 @@
11
# postcss-comment
22
Allow postcss to support inline comments.
33

4+
[![npm](https://nodei.co/npm/postcss-comment.png?downloads=true)](https://www.npmjs.org/package/postcss-comment)
5+
6+
[![version](https://img.shields.io/npm/v/postcss-comment.svg)](https://www.npmjs.org/package/postcss-comment)
7+
[![status](https://travis-ci.org/zoubin/postcss-comment.svg?branch=master)](https://travis-ci.org/zoubin/postcss-comment)
8+
[![dependencies](https://david-dm.org/zoubin/postcss-comment.svg)](https://david-dm.org/zoubin/postcss-comment)
9+
[![devDependencies](https://david-dm.org/zoubin/postcss-comment/dev-status.svg)](https://david-dm.org/zoubin/postcss-comment#info=devDependencies)
10+
411
## Usage
512

613
```bash
7-
npm i --save postcss postcss-comment
14+
npm i --save-dev postcss postcss-comment
815

916
```
1017

18+
### As parser
19+
1120
```javascript
12-
require('postcss-comment')
1321
var postcss = require('postcss')
22+
var parser = require('postcss-comment')
1423

15-
// or
16-
// var postcss = require('postcss-comment')
24+
var fs = require('fs')
25+
26+
var file = __dirname + '/inline.css'
27+
28+
postcss()
29+
.process(
30+
fs.readFileSync(file, 'utf8'),
31+
{ from: file, parser: parser }
32+
)
33+
.then(function (result) {
34+
console.log(result.css)
35+
})
36+
37+
```
38+
39+
### Hook require
40+
41+
```javascript
42+
require('postcss-comment/hookRequire')
43+
var postcss = require('postcss')
1744

1845
var fs = require('fs')
1946

@@ -30,6 +57,8 @@ postcss()
3057

3158
```
3259

60+
## Example
61+
3362
inline.css:
3463
```css
3564
/*

hookRequire.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var Parser = require('postcss/lib/parser')
2+
var tokenizer = require('./lib/tokenize')
3+
4+
Parser.prototype.tokenize = function () {
5+
this.tokens = tokenizer(this.input)
6+
}
7+
8+
module.exports = require('postcss')
9+

index.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,2 @@
1-
var Parser = require('postcss/lib/parser')
2-
var tokenizer = require('./lib/tokenize')
3-
4-
Parser.prototype.tokenize = function () {
5-
this.tokens = tokenizer(this.input)
6-
}
7-
8-
module.exports = require('postcss')
1+
exports.parse = require('./lib/parse')
92

lib/parse.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var Input = require('postcss/lib/input')
2+
var Parser = require('./parser')
3+
4+
module.exports = function (css, opts) {
5+
var input = new Input(css, opts)
6+
var parser = new Parser(input)
7+
parser.tokenize()
8+
parser.loop()
9+
10+
return parser.root
11+
}
12+

lib/parser.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var inherits = require('util').inherits
2+
var Parser = require('postcss/lib/parser')
3+
var tokenizer = require('./tokenize')
4+
5+
inherits(InlineParser, Parser)
6+
7+
module.exports = InlineParser
8+
9+
function InlineParser(input) {
10+
Parser.call(this, input)
11+
}
12+
13+
InlineParser.prototype.tokenize = function() {
14+
this.tokens = tokenizer(this.input)
15+
}

lib/tokenize.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,16 +225,11 @@ module.exports = function tokenize(input) {
225225
content = '/*' + css.slice(pos + 2, next + 1);
226226
content = content.replace(/\*\//g, '*\\/') + ' */'
227227

228-
nextLine = line;
229-
nextOffset = offset;
230-
231228
tokens.push(['comment', content,
232229
line, pos - offset,
233-
nextLine, next - nextOffset
230+
line, next - offset
234231
]);
235232

236-
offset = nextOffset;
237-
line = nextLine;
238233
pos = next;
239234

240235
} else {

test/index.js renamed to test/hookRequire.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var test = require('tap').test
2-
var postcss = require('..')
2+
var postcss = require('../hookRequire')
33
var path = require('path')
44
var fs = require('fs')
55
var fixtures = path.resolve.bind(path, __dirname, 'fixtures')
@@ -8,7 +8,7 @@ function readFile(file) {
88
return fs.readFileSync(file, 'utf8')
99
}
1010

11-
test('inline comment', function (t) {
11+
test('hookRequire', function (t) {
1212
t.plan(1)
1313
var file = fixtures('inline.css')
1414
postcss()
@@ -20,3 +20,4 @@ test('inline comment', function (t) {
2020
)
2121
})
2222
})
23+

test/parser.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var test = require('tap').test
2+
var parser = require('..')
3+
var postcss = require('postcss')
4+
var path = require('path')
5+
var fs = require('fs')
6+
var fixtures = path.resolve.bind(path, __dirname, 'fixtures')
7+
8+
function readFile(file) {
9+
return fs.readFileSync(file, 'utf8')
10+
}
11+
12+
test('parser', function (t) {
13+
t.plan(1)
14+
var file = fixtures('inline.css')
15+
postcss()
16+
.process(readFile(file), { from: file, parser: parser })
17+
.then(function (result) {
18+
t.equal(
19+
result.css,
20+
readFile(fixtures('inline.expected.css'))
21+
)
22+
})
23+
})
24+

0 commit comments

Comments
 (0)