Skip to content

Adds support for the --input-source-map flag. #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ clean-css-cli 4.1 introduces the following changes / features:
--skip-rebase Disable URLs rebasing
--source-map Enables building input's source map
--source-map-inline-sources Enables inlining sources inside source maps
--input-source-map [file] Specifies the path of the input source map file
```

## Compatibility modes
Expand Down
43 changes: 35 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ function cli(process, beforeMinifyCallback) {
.option('--remove-inlined-files', 'Remove files inlined in <source-file ...> or via `@import` statements')
.option('--skip-rebase', 'Disable URLs rebasing')
.option('--source-map', 'Enables building input\'s source map')
.option('--source-map-inline-sources', 'Enables inlining sources inside source maps');
.option('--source-map-inline-sources', 'Enables inlining sources inside source maps')
.option('--input-source-map [file]', 'Specifies the path of the input source map file');

commands.on('--help', function () {
console.log(' Examples:\n');
Expand Down Expand Up @@ -156,14 +157,25 @@ function cli(process, beforeMinifyCallback) {
sourceMapInlineSources: commands.sourceMapInlineSources
};

if (commands.inputSourceMap && !options.sourceMap) {
options.sourceMap = true;
}

if (options.sourceMap && !options.output) {
outputFeedback(['Source maps will not be built because you have not specified an output file.'], true);
options.sourceMap = false;
}

var configurations = {
beforeMinifyCallback: beforeMinifyCallback,
debugMode: debugMode,
removeInlinedFiles: removeInlinedFiles,
inputSourceMap: commands.inputSourceMap
};

// ... and do the magic!
if (commands.args.length > 0) {
minify(process, beforeMinifyCallback, options, debugMode, removeInlinedFiles, expandGlobs(commands.args));
minify(process, options, configurations, expandGlobs(commands.args));
} else {
stdin = process.openStdin();
stdin.setEncoding('utf-8');
Expand All @@ -172,7 +184,7 @@ function cli(process, beforeMinifyCallback) {
data += chunk;
});
stdin.on('end', function () {
minify(process, beforeMinifyCallback, options, debugMode, removeInlinedFiles, data);
minify(process, options, configurations, data);
});
}
}
Expand Down Expand Up @@ -211,15 +223,15 @@ function expandGlobs(paths) {
}, []);
}

function minify(process, beforeMinifyCallback, options, debugMode, removeInlinedFiles, data) {
function minify(process, options, configurations, data) {
var cleanCss = new CleanCSS(options);

applyNonBooleanCompatibilityFlags(cleanCss, options.compatibility);
beforeMinifyCallback(cleanCss);
cleanCss.minify(data, function (errors, minified) {
configurations.beforeMinifyCallback(cleanCss);
cleanCss.minify(data, getSourceMapContent(configurations.inputSourceMap), function (errors, minified) {
var mapFilename;

if (debugMode) {
if (configurations.debugMode) {
console.error('Original: %d bytes', minified.stats.originalSize);
console.error('Minified: %d bytes', minified.stats.minifiedSize);
console.error('Efficiency: %d%', ~~(minified.stats.efficiency * 10000) / 100.0);
Expand All @@ -240,7 +252,7 @@ function minify(process, beforeMinifyCallback, options, debugMode, removeInlined
process.exit(1);
}

if (removeInlinedFiles) {
if (configurations.removeInlinedFiles) {
minified.inlinedStylesheets.forEach(fs.unlinkSync);
}

Expand Down Expand Up @@ -289,6 +301,21 @@ function outputFeedback(messages, isError) {
});
}

function getSourceMapContent(sourceMapPath) {
if (!sourceMapPath || !fs.existsSync(sourceMapPath)) {
return null;
}
var content = null;

try {
content = fs.readFileSync(sourceMapPath).toString();
} catch (e) {
console.error('Failed to read the input source map file.');
}

return content;
}

function output(process, options, minified) {
if (options.output) {
fs.writeFileSync(options.output, minified, 'utf8');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clean-css-cli",
"version": "4.2.0",
"version": "4.3.0-pre",
"description": "A command-line interface to clean-css CSS optimization library",
"scripts": {
"check": "jshint ./bin/cleancss .",
Expand Down
45 changes: 45 additions & 0 deletions test/binary-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,4 +694,49 @@ vows.describe('cleancss')
}
}
})
.addBatch({
'process an input-source-map': pipedContext(fs.readFileSync('./test/fixtures/source-maps/map/styles.css'), '-o ./test/styles.min.css --input-source-map ./test/fixtures/source-maps/map/input.map', {
'enables the source map flag': function() {
assert.isTrue(fs.existsSync('test/styles.min.css'));
assert.isTrue(fs.existsSync('test/styles.min.css.map'));
},
'teardown': function () {
deleteFile('test/styles.min.css');
deleteFile('test/styles.min.css.map');
}
})
})
.addBatch({
'missing an input-source-map': pipedContext(fs.readFileSync('./test/fixtures/source-maps/map/styles.css'), '-o ./test/styles.min.css', {
'does not generate a source map if the parameter is missing': function() {
assert.isTrue(fs.existsSync('test/styles.min.css'));
assert.isFalse(fs.existsSync('test/styles.min.css.map'));
},
'teardown': function () {
deleteFile('test/styles.min.css');
}
})
})
.addBatch({
'content of input-source-map': pipedContext(fs.readFileSync('./test/fixtures/source-maps/map/styles.css'), '-o ./test/styles.min.css --input-source-map ./test/fixtures/source-maps/map/input.map', {
'includes the right content of the source map': function() {
assert.isTrue(fs.existsSync('test/styles.min.css.map'));
var sourceMap = new SourceMapConsumer(fs.readFileSync('./test/styles.min.css.map', 'utf-8'));

assert.deepEqual(
sourceMap.originalPositionFor({ line: 1, column: 1 }),
{
source: 'styles.less',
line: 1,
column: 4,
name: null
}
);
},
'teardown': function () {
deleteFile('test/styles.min.css');
deleteFile('test/styles.min.css.map');
}
})
})
.export(module);
1 change: 1 addition & 0 deletions test/fixtures/source-maps/map/input.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/source-maps/map/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
div > a {
color: blue;
}