Skip to content

Commit 65b4351

Browse files
See #7 - adds a beforeMinify CLI callback.
Why: * When used as a module a custom CLI implementation can hook into pre-minify configuration and alter it; * it may be useful as a way of customizing CLI beyond command line arguments.
1 parent fd928dd commit 65b4351

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

Diff for: index.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ var path = require('path');
44
var CleanCSS = require('clean-css');
55
var commands = require('commander');
66

7-
function cli(process) {
8-
var packageConfig = fs.readFileSync(path.join(path.dirname(fs.realpathSync(process.argv[1])), '../package.json'));
7+
function cli(process, beforeMinifyCallback) {
8+
var packageConfig = fs.readFileSync(path.join(__dirname, 'package.json'));
99
var buildVersion = JSON.parse(packageConfig).version;
1010
var fromStdin;
1111
var debugMode;
1212
var options;
1313
var stdin;
1414
var data;
1515

16+
beforeMinifyCallback = beforeMinifyCallback || Function.prototype;
17+
1618
// Specify commander options to parse command line params correctly
1719
commands
1820
.version(buildVersion, '-v, --version')
@@ -153,7 +155,7 @@ function cli(process) {
153155

154156
// ... and do the magic!
155157
if (commands.args.length > 0) {
156-
minify(process, options, debugMode, commands.args);
158+
minify(process, beforeMinifyCallback, options, debugMode, commands.args);
157159
} else {
158160
stdin = process.openStdin();
159161
stdin.setEncoding('utf-8');
@@ -162,7 +164,7 @@ function cli(process) {
162164
data += chunk;
163165
});
164166
stdin.on('end', function () {
165-
minify(process, options, debugMode, data);
167+
minify(process, beforeMinifyCallback, options, debugMode, data);
166168
});
167169
}
168170
}
@@ -195,8 +197,11 @@ function findArgumentTo(option, rawArgs, args) {
195197
return value;
196198
}
197199

198-
function minify(process, options, debugMode, data) {
199-
new CleanCSS(options).minify(data, function (errors, minified) {
200+
function minify(process, beforeMinifyCallback, options, debugMode, data) {
201+
var cleanCss = new CleanCSS(options);
202+
203+
beforeMinifyCallback(cleanCss);
204+
cleanCss.minify(data, function (errors, minified) {
200205
var mapFilename;
201206

202207
if (debugMode) {

Diff for: test/binary-test.js

+10
Original file line numberDiff line numberDiff line change
@@ -612,4 +612,14 @@ vows.describe('cleancss')
612612
})
613613
}
614614
})
615+
.addBatch({
616+
'custom CLI': {
617+
'topic': function () {
618+
exec('echo ".block{background-image:url(image.png)}" | ./test/custom-cli/custom-cleancss', this.callback);
619+
},
620+
'outputs transformed url': function (error, stdout) {
621+
assert.equal(stdout, '.block{background-image:url(../valid/path/to/image.png)}');
622+
}
623+
}
624+
})
615625
.export(module);

Diff for: test/custom-cli/custom-cleancss

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env node
2+
3+
var cleanCssCli = require('../../index');
4+
return cleanCssCli(process, function (cleanCss) {
5+
cleanCss.options.level['1'].transform = function (propertyName, propertyValue) {
6+
if (propertyName == 'background-image' && propertyValue.indexOf('../valid/path/to') == -1) {
7+
return propertyValue.replace('url(', 'url(../valid/path/to/');
8+
}
9+
}
10+
});

0 commit comments

Comments
 (0)