forked from reworkcss/rework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
99 lines (84 loc) · 1.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* Module dependencies.
*/
var css = require('css');
var convertSourceMap = require('convert-source-map');
var parse = css.parse;
var stringify = css.stringify;
/**
* Expose `rework`.
*/
exports = module.exports = rework;
/**
* Initialize a new stylesheet `Rework` with `str`.
*
* @param {String} str
* @param {Object} options
* @return {Rework}
* @api public
*/
function rework(str, options) {
return new Rework(parse(str, options));
}
/**
* Initialize a new stylesheet `Rework` with `obj`.
*
* @param {Object} obj
* @api private
*/
function Rework(obj) {
this.obj = obj;
this.map = undefined;
}
/**
* Use the given plugin `fn(style, rework)`.
*
* @param {Function} fn
* @return {Rework}
* @api public
*/
Rework.prototype.use = function(fn){
fn(this.obj.stylesheet, this);
return this;
};
/**
* Stringify the stylesheet.
*
* @param {Object} options
* @return {String}
* @api public
*/
Rework.prototype.toString = function(options){
options = options || {};
var result = stringify(this.obj, options);
if (options.sourcemap && !options.sourcemapAsObject) {
result = result.code + '\n' + sourcemapToComment(result.map);
}
return result;
};
/**
* For user of rework to set the current sourcemap for the input,
* and for plugins to read the sourcemap for example to be able to
* map error messages to the correct file and line.
*
* @param {Object} [map] Optional sourcemap to set
* @return {Object} The sourcemap for the input data
* @api public
*/
Rework.prototype.sourcemap = function(map){
if (map) {
this.map = map;
}
return this.map;
};
/**
* Convert sourcemap to base64-encoded comment
*
* @param {Object} map
* @return {String}
* @api private
*/
function sourcemapToComment(map) {
var content = convertSourceMap.fromObject(map).toBase64();
return '/*# sourceMappingURL=data:application/json;base64,' + content + ' */';
}