gulp-css-background-remove
Advanced tools
Comparing version 1.1.0 to 1.2.0
43
index.js
@@ -8,5 +8,17 @@ 'use strict'; | ||
var fs = require('fs'); | ||
var extend = require('util')._extend; | ||
var File = require('vinyl'); | ||
var source = require('vinyl-source-stream'); | ||
var buffer = require('vinyl-buffer'); | ||
module.exports = function (opts) { | ||
return through.obj(function (file, enc, cb) { | ||
opts = extend({ | ||
filename: 'image.css', | ||
dest: '', | ||
writeImagesFile: true, | ||
regex: /.*(url\((["|']?.+["|']?)?\))+.*/g | ||
}, opts); | ||
var imagesContent; | ||
var throughObj = through.obj(function (file, enc, cb) { | ||
if (file.isNull()) { | ||
@@ -51,3 +63,3 @@ cb(null, file); | ||
// Search regex to separate out elements | ||
var regex = opts.regex || /.*(url\((["|']?.+["|']?)?\))+.*/g; | ||
var regex = opts.regex; | ||
@@ -92,6 +104,9 @@ // Empty stylesheet or broken | ||
// Write images file | ||
fs.writeFileSync( | ||
path.join((opts.dest || ''), (opts.filename || 'image.css')), | ||
stylesheets.images | ||
); | ||
if (opts.writeImagesFile) { | ||
fs.writeFileSync( | ||
path.join(opts.dest, opts.filename), | ||
stylesheets.images | ||
); | ||
} | ||
imagesContent = stylesheets.images; | ||
@@ -103,2 +118,18 @@ // Return rules file | ||
}); | ||
return extend(throughObj, { | ||
images: function (callback) { | ||
throughObj.on('finish', function () { | ||
var imagesFile = new File({ | ||
contents: new Buffer(imagesContent) | ||
}); | ||
callback( | ||
imagesFile | ||
.pipe(source(opts.filename)) | ||
.pipe(buffer()) | ||
); | ||
}); | ||
return throughObj; | ||
} | ||
}); | ||
}; |
{ | ||
"name": "gulp-css-background-remove", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Remove `background-image` from CSS file to load asynchron", | ||
@@ -29,5 +29,7 @@ "main": "index.js", | ||
"devDependencies": { | ||
"mocha": "*" | ||
"mocha": "*", | ||
"vinyl-buffer": "^1.0.0", | ||
"vinyl-source-stream": "^1.1.0" | ||
}, | ||
"license": "MIT" | ||
} |
20
test.js
@@ -49,1 +49,21 @@ /* eslint-env mocha */ | ||
}); | ||
it('should be possible to get a stream from the image properties', function (cb) { | ||
var stream = cssBackgroundRemove({ | ||
writeImagesFile: false | ||
}); | ||
stream.images(function (imagesStream) { | ||
imagesStream.on('data', function (file) { | ||
assert(file.contents.toString(), 'div {\n\tbackground-image: url(image.png);\n}'); | ||
}) | ||
}); | ||
stream.on('data', function (file) { | ||
assert(file.contents.toString(), 'a {\n\tbackground: #ddd; color: #ddd;\n}\na {\n\tcolor: #fff;\n}'); | ||
}); | ||
stream.on('end', cb); | ||
stream.write(generateFile('a {\n\tbackground: #ddd; color: #ddd;\n}\na {\n\tcolor: #fff;\n}\ndiv {\n\tbackground-image: url(image.jpg);\n}')); | ||
stream.end(); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8528
165
3