Skip to content

Commit 2a2eb29

Browse files
Fixes #1 - option to remove inlined files.
Why: * `minified.inlinedStylesheets` provides a list of inlined files, but, unlike in API, there is no way to say we want to remove these files; * `--remove-inlined-files` (defaults to off) enables such behavior.
1 parent e877d27 commit 2a2eb29

File tree

5 files changed

+122
-3
lines changed

5 files changed

+122
-3
lines changed

History.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
==================
33

44
* Bumps clean-css dependency to 4.1.x.
5+
* Fixed issue [#1](https://github.com/jakubpawlowicz/clean-css-cli/issues/1) - option to remove inlined files.
56
* Fixed issue [#2](https://github.com/jakubpawlowicz/clean-css-cli/issues/2) - glob matching source paths.
67
* Fixed issue [#7](https://github.com/jakubpawlowicz/clean-css-cli/issues/7) - using CLI as a module.
78

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ clean-css-cli 4.0 introduces some breaking changes:
8484
-O <n> [optimizations] Turn on level <n> optimizations; optionally accepts a list of fine-grained options, defaults to `1`, see examples below
8585
--inline [rules] Enables inlining for listed sources (defaults to `local`)
8686
--inline-timeout [seconds] Per connection timeout when fetching remote stylesheets (defaults to 5 seconds)
87+
--remove-inlined-files Remove files inlined in <source-file ...> or via `@import` statements
8788
--skip-rebase Disable URLs rebasing
8889
--source-map Enables building input's source map
8990
--source-map-inline-sources Enables inlining sources inside source maps

index.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function cli(process, beforeMinifyCallback) {
1010
var buildVersion = JSON.parse(packageConfig).version;
1111
var fromStdin;
1212
var debugMode;
13+
var removeInlinedFiles;
1314
var options;
1415
var stdin;
1516
var data;
@@ -27,6 +28,7 @@ function cli(process, beforeMinifyCallback) {
2728
.option('-O <n> [optimizations]', 'Turn on level <n> optimizations; optionally accepts a list of fine-grained options, defaults to `1`, see examples below', function (val) { return Math.abs(parseInt(val)); })
2829
.option('--inline [rules]', 'Enables inlining for listed sources (defaults to `local`)')
2930
.option('--inline-timeout [seconds]', 'Per connection timeout when fetching remote stylesheets (defaults to 5 seconds)', parseFloat)
31+
.option('--remove-inlined-files', 'Remove files inlined in <source-file ...> or via `@import` statements')
3032
.option('--skip-rebase', 'Disable URLs rebasing')
3133
.option('--source-map', 'Enables building input\'s source map')
3234
.option('--source-map-inline-sources', 'Enables inlining sources inside source maps');
@@ -134,6 +136,8 @@ function cli(process, beforeMinifyCallback) {
134136

135137
// Now coerce commands into CleanCSS configuration...
136138
debugMode = commands.debug;
139+
removeInlinedFiles = commands.removeInlinedFiles;
140+
137141
options = {
138142
compatibility: commands.compatibility,
139143
format: commands.format,
@@ -156,7 +160,7 @@ function cli(process, beforeMinifyCallback) {
156160

157161
// ... and do the magic!
158162
if (commands.args.length > 0) {
159-
minify(process, beforeMinifyCallback, options, debugMode, expandGlobs(commands.args));
163+
minify(process, beforeMinifyCallback, options, debugMode, removeInlinedFiles, expandGlobs(commands.args));
160164
} else {
161165
stdin = process.openStdin();
162166
stdin.setEncoding('utf-8');
@@ -165,7 +169,7 @@ function cli(process, beforeMinifyCallback) {
165169
data += chunk;
166170
});
167171
stdin.on('end', function () {
168-
minify(process, beforeMinifyCallback, options, debugMode, data);
172+
minify(process, beforeMinifyCallback, options, debugMode, removeInlinedFiles, data);
169173
});
170174
}
171175
}
@@ -204,7 +208,7 @@ function expandGlobs(paths) {
204208
}, []);
205209
}
206210

207-
function minify(process, beforeMinifyCallback, options, debugMode, data) {
211+
function minify(process, beforeMinifyCallback, options, debugMode, removeInlinedFiles, data) {
208212
var cleanCss = new CleanCSS(options);
209213

210214
beforeMinifyCallback(cleanCss);
@@ -232,6 +236,10 @@ function minify(process, beforeMinifyCallback, options, debugMode, data) {
232236
process.exit(1);
233237
}
234238

239+
if (removeInlinedFiles) {
240+
minified.inlinedStylesheets.forEach(fs.unlinkSync);
241+
}
242+
235243
if (minified.sourceMap) {
236244
mapFilename = path.basename(options.output) + '.map';
237245
output(process, options, minified.styles + '/*# sourceMappingURL=' + mapFilename + ' */');

test/binary-test.js

+45
Original file line numberDiff line numberDiff line change
@@ -636,4 +636,49 @@ vows.describe('cleancss')
636636
})
637637
}
638638
})
639+
.addBatch({
640+
'removing inlined stylesheets - off': {
641+
'topic': function() {
642+
var self = this;
643+
644+
exec('cp test/fixtures/reset.css test/fixtures/reset-removing.css', function () {
645+
exec('__DIRECT__=1 ./bin/cleancss test/fixtures/reset-removing.css', self.callback);
646+
});
647+
},
648+
'keeps the file': function () {
649+
assert.isTrue(fs.existsSync('test/fixtures/reset-removing.css'));
650+
},
651+
'teardown': function () {
652+
deleteFile('test/fixtures/reset-removing.css');
653+
}
654+
}
655+
})
656+
.addBatch({
657+
'removing inlined stylesheets - on': {
658+
'topic': function() {
659+
var self = this;
660+
661+
exec('cp test/fixtures/reset.css test/fixtures/reset-removing.css', function () {
662+
exec('__DIRECT__=1 ./bin/cleancss --remove-inlined-files test/fixtures/reset-removing.css', self.callback);
663+
});
664+
},
665+
'removes the file': function () {
666+
assert.isFalse(fs.existsSync('test/fixtures/reset-removing.css'));
667+
}
668+
}
669+
})
670+
.addBatch({
671+
'removing inlined stylesheets - on via @import': {
672+
'topic': function() {
673+
var self = this;
674+
675+
exec('cp test/fixtures/reset.css test/fixtures/reset-removing.css', function () {
676+
exec('echo "@import \'test/fixtures/reset-removing.css\';" | ./bin/cleancss --remove-inlined-files', self.callback);
677+
});
678+
},
679+
'removes the file': function () {
680+
assert.isFalse(fs.existsSync('test/fixtures/reset-removing.css'));
681+
}
682+
}
683+
})
639684
.export(module);

test/fixtures/reset-duplicate.css

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*reset*/
2+
html, body, div, span, applet, object, iframe,
3+
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
4+
a, abbr, acronym, address, big, cite, code,
5+
del, dfn, em, font, img, ins, kbd, q, s, samp,
6+
small, strike, strong, sub, sup, tt, var,
7+
dl, dt, dd, ol, ul, li,
8+
fieldset, form, label, legend,
9+
table, caption, tbody, tfoot, thead, tr, th, td {
10+
margin: 0;
11+
padding: 0;
12+
border: 0;
13+
outline: 0;
14+
font-weight: inherit;
15+
font-style: inherit;
16+
font-size: 100%;
17+
font-family: inherit;
18+
vertical-align: baseline;
19+
}
20+
/* remember to define focus styles! */
21+
:focus {
22+
outline: 0;
23+
}
24+
body {
25+
line-height: 1;
26+
color: black;
27+
background: white;
28+
}
29+
ol, ul {
30+
list-style: none;
31+
}
32+
/* tables still need 'cellspacing="0"' in the markup */
33+
table {
34+
border-collapse: separate;
35+
border-spacing: 0;
36+
}
37+
caption, th, td {
38+
text-align: left;
39+
font-weight: normal;
40+
}
41+
blockquote:before, blockquote:after,
42+
q:before, q:after {
43+
content: "";
44+
}
45+
blockquote, q {
46+
quotes: "" "";
47+
}
48+
.clear {
49+
clear:both;
50+
display: inline-block;
51+
}
52+
.clear:after, .container:after {
53+
content: ".";
54+
display: block;
55+
height: 0;
56+
clear: both;
57+
visibility: hidden;
58+
}
59+
* html .clear {
60+
height: 1%;
61+
}
62+
.clear {
63+
display: block;
64+
}

0 commit comments

Comments
 (0)