You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rebase your CSS assets based on its belonging source map.
5
+
Rebase your CSS assets relatively to the source file they were imported from.
6
6
7
-
## Installation
7
+
## Example
8
8
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 =newRebaser({
62
+
map: map
63
+
});
64
+
65
+
let data ='';
66
+
let stream =newReadable();
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
+
});
11
84
```
12
85
13
86
## API
@@ -22,7 +95,7 @@ Optionally pass in some opts:
22
95
23
96
* opts.map:
24
97
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.
26
99
27
100
## Events
28
101
@@ -32,6 +105,12 @@ In addition to the usual events emitted by node.js streams, css-source-map-rebas
32
105
33
106
Every time an asset is rebased, this event fires with the rebased path.
0 commit comments