Permalink
Browse files

Merge pull request #123 from andreypopp/source-maps

Inline source maps as comments
  • Loading branch information...
2 parents 36a5a7f + d88dc8f commit 0dc85e7e8ee5ea0b52898eebdf0a09a4675c10cb @jonathanong jonathanong committed Dec 23, 2013
Showing with 57 additions and 6 deletions.
  1. +26 −4 lib/rework.js
  2. +8 −2 package.json
  3. +23 −0 test/rework.js
View
@@ -30,12 +30,15 @@ exports.__defineGetter__('properties', function () {
* Initialize a new stylesheet `Rework` with `str`.
*
* @param {String} str
+ * @param {Object} options
* @return {Rework}
* @api public
*/
-function rework(str) {
- return new Rework(css.parse(str));
+function rework(str, options) {
+ options = options || {};
+ options.position = true; // we need this for sourcemaps
+ return new Rework(css.parse(str, options));
}
/**
@@ -89,10 +92,29 @@ Rework.prototype.vendors = function(prefixes){
*/
Rework.prototype.toString = function(options){
- return css.stringify(this.obj, options);
+ options = options || {};
+ var result = css.stringify(this.obj, options);
+ if (options.sourcemap && !options.sourcemapAsObject) {
+ result = result.code + '\n' + sourcemapToComment(result.map);
+ }
+ return result;
};
/**
+ * Convert sourcemap to base64-encoded comment
+ *
+ * @param {Object} map
+ * @return {String}
+ * @api private
+ */
+
+function sourcemapToComment(map) {
+ var convertSourceMap = require('convert-source-map');
+ var content = convertSourceMap.fromObject(map).toBase64();
+ return '/*# sourceMappingURL=data:application/json;base64,' + content + ' */';
+}
+
+/**
* Expose plugins.
*/
@@ -131,4 +153,4 @@ exports.ease = require('./plugins/ease');
exports.inline = require('./plugins/inline');
} catch (err) {}
-function noop(){}
+function noop(){}
View
@@ -2,7 +2,12 @@
"name": "rework",
"version": "0.19.0",
"description": "CSS manipulations built on CSSOM",
- "keywords": ["css", "manipulation", "preprocess", "transform"],
+ "keywords": [
+ "css",
+ "manipulation",
+ "preprocess",
+ "transform"
+ ],
"author": "TJ Holowaychuk <tj@vision-media.ca>",
"repository": {
"type": "git",
@@ -16,7 +21,8 @@
"mime": "1.2.11",
"debug": "*",
"rework-inherit": "0.2.1",
- "rework-visit": "1.0.0"
+ "rework-visit": "1.0.0",
+ "convert-source-map": "~0.3.1"
},
"devDependencies": {
"mocha": "*",
View
@@ -274,4 +274,27 @@ describe('rework', function(){
.should.equal('body{color:red;}');
})
})
+
+ describe('.toString() sourcemap option', function() {
+ it('should inline sourcemap', function() {
+ rework('body { color: red; }')
+ .toString({ compress: true, sourcemap: true })
+ .should.equal(
+ 'body{color:red;}' + '\n' +
+ '/*# sourceMappingURL=data:application/json;base64,' +
+ 'eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ2VuZXJhdGVkLmNzc' +
+ 'yIsInNvdXJjZXMiOlsic291cmNlLmNzcyJdLCJuYW1lcy' +
+ 'I6W10sIm1hcHBpbmdzIjoiQUFBQSxLQUFPLFNBQVUifQ== */');
+ })
+ })
+
+ describe('.toString() sourcemapAsObject and sourcemap options', function() {
+ it('should return sourcemap as an object', function() {
+ var result = rework('body { color: red; }')
+ .toString({ compress: true, sourcemap: true, sourcemapAsObject: true });
+ result.code.should.equal('body{color:red;}');
+ result.map.should.have.property('mappings');
+ result.map.mappings.should.equal('AAAA,KAAO,SAAU');
+ })
+ })
})

0 comments on commit 0dc85e7

Please sign in to comment.