Skip to content

Commit ae0e1a5

Browse files
See #61 - fixes invalid rebasing when --output is a directory.
In such cases there `rebaseTo` option passed to `clean-css` is just the output directory.
1 parent b437782 commit ae0e1a5

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

History.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[5.3.0 / 2021-xx-xx](https://github.com/jakubpawlowicz/clean-css-cli/compare/5.2...HEAD)
22
==================
33

4+
* Fixed issue [#61](https://github.com/jakubpawlowicz/clean-css-cli/issues/61) - source maps, rebasing, and batch processing.
45
* Fixed issue [#65](https://github.com/jakubpawlowicz/clean-css-cli/issues/65) - batch processing with output path.
56

67
[5.2.2 / 2021-03-19](https://github.com/jakubpawlowicz/clean-css-cli/compare/v5.2.1...v5.2.2)

index.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ function cli(process, beforeMinifyCallback) {
9292
level: { 1: true },
9393
output: inputOptions.output,
9494
rebase: inputOptions.withRebase ? true : false,
95-
rebaseTo: inputOptions.withRebase && ('output' in inputOptions) && inputOptions.output.length > 0 ?
96-
path.dirname(path.resolve(inputOptions.output)) :
97-
(inputOptions.withRebase ? process.cwd() : undefined),
95+
rebaseTo: undefined,
9896
sourceMap: inputOptions.sourceMap,
9997
sourceMapInlineSources: inputOptions.sourceMapInlineSources
10098
};
@@ -124,6 +122,18 @@ function cli(process, beforeMinifyCallback) {
124122
fs.mkdirSync(options.output, {recursive: true});
125123
}
126124

125+
if (inputOptions.withRebase && ('output' in inputOptions) && inputOptions.output.length > 0) {
126+
if (isDirectory(path.resolve(inputOptions.output))) {
127+
options.rebaseTo = path.resolve(inputOptions.output);
128+
} else {
129+
options.rebaseTo = path.dirname(path.resolve(inputOptions.output));
130+
}
131+
} else {
132+
if (inputOptions.withRebase) {
133+
options.rebaseTo = process.cwd();
134+
}
135+
}
136+
127137
var configurations = {
128138
batchSuffix: inputOptions.batchSuffix,
129139
beforeMinifyCallback: beforeMinifyCallback,
@@ -148,6 +158,18 @@ function cli(process, beforeMinifyCallback) {
148158
}
149159
}
150160

161+
function isDirectory(path) {
162+
try {
163+
return fs.statSync(path).isDirectory();
164+
} catch (e) {
165+
if (e.code == 'ENOENT') {
166+
return false;
167+
} else {
168+
throw e;
169+
}
170+
}
171+
}
172+
151173
function findArgumentTo(option, rawArgs, args) {
152174
var value = true;
153175
var optionAt = rawArgs.indexOf(option);

test/binary-test.js

+21
Original file line numberDiff line numberDiff line change
@@ -948,4 +948,25 @@ vows.describe('cleancss')
948948
}
949949
})
950950
})
951+
.addBatch({
952+
'batch processing with source maps, rebase and output as a path': binaryContext('-b ./test/fixtures-batch-9/partials-relative/\\*\\*/included.css --source-map --with-rebase -o test/fixtures-batch-9-output', {
953+
'setup': function () {
954+
execSync('cp -fr test/fixtures test/fixtures-batch-9');
955+
},
956+
'does not raise an error': function (error, stdout, stderr) {
957+
assert.equal(stderr, '');
958+
},
959+
'rebases output correctly': function () {
960+
var minimized = fs.readFileSync('./test/fixtures-batch-9-output/extra/included-min.css', 'utf-8');
961+
assert.equal(minimized, 'a{background:url(../fixtures-batch-9/partials/extra/down.gif) 0 0 no-repeat}\n/*# sourceMappingURL=included-min.css.map */');
962+
},
963+
'creates source map files': function () {
964+
assert.isTrue(fs.existsSync('test/fixtures-batch-9-output/extra/included-min.css.map'));
965+
},
966+
'teardown': function () {
967+
execSync('rm -fr test/fixtures-batch-9');
968+
execSync('rm -fr test/fixtures-batch-9-output');
969+
}
970+
})
971+
})
951972
.export(module);

0 commit comments

Comments
 (0)