Skip to content

Commit 2647fee

Browse files
author
Eric MORAND
authored
Merge pull request #4 from ericmorand/issue_3
Fix issue #3
2 parents 10dddb5 + 017bc68 commit 2647fee

File tree

1 file changed

+84
-5
lines changed

1 file changed

+84
-5
lines changed

README.md

+84-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,85 @@
22

33
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url]
44

5-
Rebase your CSS assets based on its belonging source map.
5+
Rebase your CSS assets relatively to the source file they were imported from.
66

7-
## Installation
7+
## Example
88

9-
```bash
10-
npm install css-source-map-rebase
9+
Consider the following SASS sources:
10+
11+
index.scss
12+
13+
``` css
14+
@import "partial/bar.scss";
15+
16+
.foo {
17+
background: url('./foo.png');
18+
}
19+
```
20+
21+
partial/bar.scss
22+
23+
``` css
24+
.bar {
25+
background: url('./bar.png');
26+
}
27+
```
28+
29+
By rebasing the assets relatively to the file they were imported from, the resulting CSS would be:
30+
31+
``` css
32+
.bar {
33+
background: url('partial/bar.png');
34+
}
35+
36+
.foo {
37+
background: url('foo.png');
38+
}
39+
```
40+
41+
## How it works
42+
43+
css-source-map-rebase uses the mapping provided by source maps to resolve the original file the assets where imported from. That's why it *needs* a source map to perform its magic. Any tool able to generate a source map from a stylesheet source file (may it be SASS, LESS or any other pre-processor language) is appropriate. Here is how one could use node-sass and css-source-map-rebase together to render a stylesheet and rebase its assets.
44+
45+
``` javascript
46+
let nodeSass = require('node-sass');
47+
let Rebaser = require('css-source-map-rebase');
48+
49+
nodeSass.render({
50+
file: 'index.scss',
51+
sourceMap: true,
52+
outFile: 'index.css'
53+
}, function(error, result) {
54+
if (error) {
55+
// do something on error
56+
}
57+
else {
58+
let css = result.css.toString();
59+
let map = JSON.stringify(result.map);
60+
61+
let rebaser = new Rebaser({
62+
map: map
63+
});
64+
65+
let data = '';
66+
let stream = new Readable();
67+
68+
stream
69+
.pipe(rebaser)
70+
.pipe(through(function (chunk, enc, cb) {
71+
data += chunk;
72+
73+
cb();
74+
}))
75+
.on('finish', function () {
76+
console.log(data); // data contains the rendered stylesheet with rebased assets
77+
})
78+
;
79+
80+
stream.push(css);
81+
stream.push(null);
82+
}
83+
});
1184
```
1285

1386
## API
@@ -22,7 +95,7 @@ Optionally pass in some opts:
2295

2396
* opts.map:
2497

25-
The belonging source map in the form of a JSON string. Defaults to `null`.
98+
The belonging source map in the form of a JSON string. Defaults to `null`. Note that this module basically does nothing without a source map.
2699

27100
## Events
28101

@@ -32,6 +105,12 @@ In addition to the usual events emitted by node.js streams, css-source-map-rebas
32105

33106
Every time an asset is rebased, this event fires with the rebased path.
34107

108+
## Installation
109+
110+
```bash
111+
npm install css-source-map-rebase
112+
```
113+
35114
## Contributing
36115

37116
* Fork the main repository

0 commit comments

Comments
 (0)