Skip to content

Commit f389d8f

Browse files
jessebluemrscniro
authored andcommittedMay 31, 2017
ensure that URLs of inlined imports are recalculated relative to current file (#44)
1 parent 5dc705c commit f389d8f

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed
 

‎index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ module.exports = function gulpCleanCSS(options, callback) {
2727
options.sourceMap = JSON.parse(JSON.stringify(file.sourceMap));
2828

2929
let contents = file.contents ? file.contents.toString() : '';
30-
let pass = options.rebaseTo ? {[file.path]: {styles: contents}} : contents;
30+
let pass = {[file.path]: {styles: contents}};
31+
if (!options.rebaseTo && options.rebase!==false){
32+
options.rebaseTo = path.dirname(file.path);
33+
}
3134

3235
new CleanCSS(options).minify(pass, function (errors, css) {
3336

‎test/test.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ describe('gulp-clean-css: base functionality', function () {
226226
.pipe(cleanCSS({debug: true}, function (details) {
227227
expect(details.warnings).to.exist &&
228228
expect(details.warnings.length).to.equal(1) &&
229-
expect(details.warnings[0]).to.equal('Missing \'}\' at 1:14.');
229+
expect(details.warnings[0]).to.equal('Missing \'}\' at fixtures/test.css:1:14.');
230230
}))
231231
.on('data', function (file) {
232232
i += 1;
@@ -305,4 +305,30 @@ describe('gulp-clean-css: rebase', function () {
305305
})
306306
.once('end', done);
307307
});
308+
309+
it('should rebase to current relative file location - relative imports are resolved like in the browser', function (done) {
310+
311+
gulp.src(['test/fixtures/rebasing/subdir/import.css'])
312+
.pipe(cleanCSS({}))
313+
.on('data', function (file) {
314+
315+
let expected = `
316+
p.imported_nested{background:url(../otherdir/nestedsub/nested.png)}
317+
p.imported_same{background:url(../otherdir/imported.png)}
318+
p.imported_parent{background:url(../parent.png)}
319+
p.imported_other{background:url(../othersub/inother.png)}
320+
p.imported_absolute{background:url(/inroot.png)}
321+
p.insub_same{background:url(insub.png)}
322+
p.insub_child{background:url(child/child.png)}
323+
p.insub_parent{background:url(../parent.png)}
324+
p.insub_other{background:url(../othersub/inother.png)}
325+
p.insub_absolute{background:url(/inroot.png)}
326+
p.import{background:url(import.png)}`;
327+
328+
let actual = file.contents.toString();
329+
330+
expect(actual).to.equalIgnoreSpaces(expected)
331+
})
332+
.once('end', done);
333+
});
308334
});

0 commit comments

Comments
 (0)
Please sign in to comment.