gulp-clean-css
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -0,1 +1,7 @@ | ||
## 2.0.2 (02/23/2016) | ||
- Include optional callback parameter which returns granular details from underlying clean-css process based on supplied options | ||
- Addition of more unit tests | ||
- README doc reflects new changes | ||
## 2.0.1 (02/22/2016) | ||
@@ -2,0 +8,0 @@ |
131
index.js
@@ -1,8 +0,5 @@ | ||
'use strict'; | ||
var path = require('path'); | ||
var applySourceMap = require('vinyl-sourcemaps-apply'); | ||
var CleanCSS = require('clean-css'); | ||
var objectAssign = require('object-assign'); | ||
var path = require('path'); | ||
var PluginError = require('gulp-util').PluginError; | ||
@@ -12,66 +9,90 @@ var Transform = require('readable-stream/transform'); | ||
module.exports = function gulpCleanCSS(options) { | ||
options = options || {}; | ||
module.exports = function gulpCleanCSS(options, callback) { | ||
return new Transform({ | ||
objectMode: true, | ||
transform: function modifyContents(file, enc, cb) { | ||
var run = new VinylBufferStream(function(buf, done) { | ||
var fileOptions = objectAssign({target: file.path}, options); | ||
// https://github.com/jakubpawlowicz/clean-css/blob/v3.3.0/bin/cleancss#L83-L84 | ||
if (fileOptions.relativeTo === undefined && (fileOptions.root || file.path)) { | ||
fileOptions.relativeTo = path.dirname(path.resolve(options.root || file.path)); | ||
if (arguments.length === 1) { | ||
if (Object.prototype.toString.call(arguments[0]) == '[object Function]') { | ||
callback = arguments[0]; | ||
} else { | ||
options = options || {}; | ||
} | ||
} else { | ||
options = options || {}; | ||
} | ||
if ((options.sourceMap === true || options.sourceMap === undefined) && file.sourceMap) { | ||
fileOptions.sourceMap = JSON.stringify(file.sourceMap); | ||
} | ||
return new Transform({ | ||
objectMode: true, | ||
transform: function modifyContents(file, enc, cb) { | ||
var cssFile; | ||
var self = this; | ||
if (file.path) { | ||
cssFile = {}; | ||
cssFile[file.path] = {styles: buf.toString()}; | ||
} else { | ||
cssFile = buf.toString(); | ||
} | ||
var run = new VinylBufferStream(function (buf, done) { | ||
var fileOptions = objectAssign({target: file.path}, options); | ||
new CleanCSS(fileOptions).minify(cssFile, function(errors, css) { | ||
if (errors) { | ||
done(errors.join(' ')); | ||
return; | ||
} | ||
if (fileOptions.relativeTo === undefined && (fileOptions.root || file.path)) { | ||
fileOptions.relativeTo = path.dirname(path.resolve(options.root || file.path)); | ||
} | ||
if (css.sourceMap) { | ||
var map = JSON.parse(css.sourceMap); | ||
map.file = path.relative(file.base, file.path); | ||
map.sources = map.sources.map(function(src) { | ||
if (/^(https?:)?\/\//.test(src)) { | ||
return src; | ||
} | ||
if ((options.sourceMap === true || options.sourceMap === undefined) && file.sourceMap) { | ||
fileOptions.sourceMap = JSON.stringify(file.sourceMap); | ||
} | ||
return path.relative(file.base, src); | ||
}); | ||
var cssFile; | ||
applySourceMap(file, map); | ||
} | ||
if (file.path) { | ||
cssFile = {}; | ||
cssFile[file.path] = {styles: buf.toString()}; | ||
} else { | ||
cssFile = buf.toString(); | ||
} | ||
done(null, new Buffer(css.styles)); | ||
}); | ||
}); | ||
new CleanCSS(fileOptions).minify(cssFile, function (errors, css) { | ||
var self = this; | ||
if (errors) { | ||
done(errors.join(' ')); | ||
return; | ||
} | ||
run(file, function(err, contents) { | ||
if (err) { | ||
self.emit('error', new PluginError('gulp-clean-css', err, {fileName: file.path})); | ||
} else { | ||
file.contents = contents; | ||
self.push(file); | ||
if (css.sourceMap) { | ||
var map = JSON.parse(css.sourceMap); | ||
map.file = path.relative(file.base, file.path); | ||
map.sources = map.sources.map(function (src) { | ||
if (/^(https?:)?\/\//.test(src)) { | ||
return src; | ||
} | ||
return path.relative(file.base, src); | ||
}); | ||
applySourceMap(file, map); | ||
} | ||
if (typeof callback === 'function') { | ||
var details = { | ||
'stats': css.stats, | ||
'errors': css.errors, | ||
'warnings': css.warnings | ||
} | ||
if (css.sourceMap) | ||
details['sourceMap'] = css.sourceMap; | ||
callback(details); | ||
} | ||
done(null, new Buffer(css.styles)); | ||
}); | ||
}); | ||
run(file, function (err, contents) { | ||
if (err) { | ||
self.emit('error', new PluginError('gulp-clean-css', err, {fileName: file.path})); | ||
} else { | ||
file.contents = contents; | ||
self.push(file); | ||
} | ||
cb(); | ||
}); | ||
} | ||
cb(); | ||
}); | ||
} | ||
}); | ||
}); | ||
}; |
@@ -5,5 +5,9 @@ { | ||
"homepage": "https://github.com/scniro/gulp-clean-css#readme", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"author": "scniro", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/scniro/gulp-clean-css/issues", | ||
"email": "scniro@outlook.com" | ||
}, | ||
"repository": { | ||
@@ -10,0 +14,0 @@ "type": "git", |
@@ -15,3 +15,3 @@ # gulp-clean-css | ||
## Installation | ||
## Install | ||
@@ -24,13 +24,8 @@ ``` | ||
```javascript | ||
var cleanCSS = require('gulp-clean-css'); | ||
``` | ||
### cleanCSS([*options*], [*callback*]) | ||
### cleanCSS([*options*]) | ||
#### options | ||
*options*: `Object` | ||
Return: `Object` ([stream.Transform](https://nodejs.org/docs/latest/api/stream.html#stream_class_stream_transform)) | ||
See the [`CleanCSS` options](https://github.com/jakubpawlowicz/clean-css#how-to-use-clean-css-api). | ||
Options are directly passed to the [`CleanCSS` constructor](https://github.com/jakubpawlowicz/clean-css#how-to-use-clean-css-api) so all the clean-css options are available. | ||
```javascript | ||
@@ -47,5 +42,24 @@ var gulp = require('gulp'); | ||
#### callback | ||
Useful for returning details from the underlying `minify()` call. An example use case include logging `stats` of the minified file. | ||
```javascript | ||
var gulp = require('gulp'); | ||
var cleanCSS = require('gulp-clean-css'); | ||
gulp.task('minify-css', function() { | ||
return gulp.src('styles/*.css') | ||
.pipe(cleanCSS({debug: true}, function(details) { | ||
console.log(details.stats.originalSize); | ||
console.log(details.stats.minifiedSize); | ||
})) | ||
.pipe(gulp.dest('dist')); | ||
}); | ||
``` | ||
[Source Maps](http://www.html5rocks.com/tutorials/developertools/sourcemaps/) can be generated by using [gulp-sourcemaps](https://github.com/floridoo/gulp-sourcemaps). | ||
```javascript | ||
var gulp = require('gulp'); | ||
@@ -56,12 +70,13 @@ var cleanCSS = require('gulp-clean-css'); | ||
gulp.task('minify-css', function() { | ||
return gulp.src('./src/*.css') | ||
.pipe(sourcemaps.init()) | ||
.pipe(cleanCSS()) | ||
.pipe(sourcemaps.write()) | ||
.pipe(gulp.dest('dist')); | ||
return gulp.src('./src/*.css') | ||
.pipe(sourcemaps.init()) | ||
.pipe(cleanCSS()) | ||
.pipe(sourcemaps.write()) | ||
.pipe(gulp.dest('dist')); | ||
}); | ||
}); | ||
``` | ||
## LICENSE | ||
## License | ||
[MIT](./LICENSE) © 2016 [scniro](https://github.com/scniro) |
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
8871
77
0
79