From 6dfb148d9c88c155a76c224eee74d42d33098adc Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Sun, 15 Mar 2015 08:42:47 +0000 Subject: [PATCH] Fixed remote asset rebasing when passing data as a hash. --- History.md | 1 + lib/utils/source-reader.js | 9 +++++++-- test/module-test.js | 10 ++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index 2b54aae3..de7983df 100644 --- a/History.md +++ b/History.md @@ -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`. diff --git a/lib/utils/source-reader.js b/lib/utils/source-reader.js index 852ad9fa..0440b146 100644 --- a/lib/utils/source-reader.js +++ b/lib/utils/source-reader.js @@ -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); diff --git a/test/module-test.js b/test/module-test.js index 4012a3b9..9b5da0ea 100644 --- a/test/module-test.js +++ b/test/module-test.js @@ -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); -- 2.34.1