Fixed remote asset rebasing when passing data as a hash.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Sun, 15 Mar 2015 08:42:47 +0000 (08:42 +0000)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 16 Mar 2015 20:13:22 +0000 (20:13 +0000)
History.md
lib/utils/source-reader.js
test/module-test.js

index 2b54aae..de7983d 100644 (file)
@@ -1,6 +1,7 @@
 [3.2.0 / 2015-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v3.1.1...HEAD)
 ==================
 
+* Fixes remote asset rebasing when passing data as a hash.
 * Improves path resolution inside source maps.
 * Makes `root` option implicitely default to `process.cwd()`.
 * Fixed issue [#376](https://github.com/jakubpawlowicz/clean-css/issues/376) - option to disable `0[unit]` -> `0`.
index 852ad9f..0440b14 100644 (file)
@@ -1,6 +1,8 @@
 var path = require('path');
 var UrlRewriter = require('../images/url-rewriter');
 
+var REMOTE_RESOURCE = /^(https?:)?\/\//;
+
 function SourceReader(context, data) {
   this.outerContext = context;
   this.data = data;
@@ -40,14 +42,17 @@ function fromHash(outerContext, sources) {
   for (var source in sources) {
     var styles = sources[source].styles;
     var inputSourceMap = sources[source].sourceMap;
+    var isRemote = REMOTE_RESOURCE.test(source);
+    var absoluteSource = isRemote ? source : path.resolve(source);
+    var absolutePath = path.dirname(absoluteSource);
 
     var rewriter = new UrlRewriter({
       absolute: outerContext.options.explicitRoot,
       relative: !outerContext.options.explicitRoot,
       imports: true,
       urls: outerContext.options.rebase,
-      fromBase: path.dirname(path.resolve(source)),
-      toBase: toBase
+      fromBase: absolutePath,
+      toBase: isRemote ? absolutePath : toBase
     }, this.outerContext);
     styles = rewriter.process(styles);
 
index 4012a3b..9b5da0e 100644 (file)
@@ -557,6 +557,16 @@ vows.describe('module tests').addBatch({
           assert.equal(minified.styles, '@import url(/partials/one.css);@import url(/partials/extra/three.css);@import url(/partials/extra/four.css);.two{color:#fff}');
         }
       }
+    },
+    'with remote paths': {
+      'topic': new CleanCSS({ sourceMap: true, target: process.cwd() }).minify({
+        'http://127.0.0.1/styles.css': {
+          styles: 'div{background-image:url(image.png)}'
+        }
+      }),
+      'gives right output': function (minified) {
+        assert.equal(minified.styles, 'div{background-image:url(http://127.0.0.1/image.png)}');
+      }
     }
   }
 }).export(module);