## How to optimize multiple files?
-It can be done either by passing an array of paths, or, when sources are already available, a hash:
+It can be done either by passing an array of paths, or, when sources are already available, a hash or an array of hashes:
```js
new CleanCSS().minify(['path/to/file/one', 'path/to/file/two']);
});
```
+```js
+new CleanCSS().minify([
+ {'path/to/file/one': {styles: 'contents of file one'}},
+ {'path/to/file/two': {styles: 'contents of file two'}}
+]);
+```
+
+Passing an array of hashes allows you to explicitly specify the order in which the input files are concatenated. Whereas when you use a single hash the order is determined by the [traversal order of object properties](http://2ality.com/2015/10/property-traversal-order-es6.html).
+
Important note - any `@import` rules already present in the hash will be resolved in memory.
## How to process remote `@import`s correctly?
}
function fromArray(input, context, callback) {
- var inputAsImports = input.reduce(function (accumulator, uri) {
- var normalizedUri = normalizeUri(uri);
+ var inputAsImports = input.reduce(function (accumulator, uriOrHash) {
+ if (typeof uriOrHash === 'string') {
+ return addStringSource(uriOrHash, accumulator);
+ } else {
+ return addHashSource(uriOrHash, context, accumulator);
+ }
- accumulator.push(restoreAsImport(normalizedUri));
- return accumulator;
}, []);
return fromStyles(inputAsImports.join(''), context, { inline: ['all'] }, callback);
}
function fromHash(input, context, callback) {
+ var inputAsImports = addHashSource(input, context, []);
+ return fromStyles(inputAsImports.join(''), context, { inline: ['all'] }, callback);
+}
+
+function addStringSource(input, imports) {
+ imports.push(restoreAsImport(normalizeUri(input)));
+ return imports;
+}
+
+function addHashSource(input, context, imports) {
var uri;
var normalizedUri;
var source;
- var inputAsImports = [];
for (uri in input) {
source = input[uri];
normalizedUri = normalizeUri(uri);
- inputAsImports.push(restoreAsImport(normalizedUri));
+ imports.push(restoreAsImport(normalizedUri));
context.sourcesContent[normalizedUri] = source.styles;
}
}
- return fromStyles(inputAsImports.join(''), context, { inline: ['all'] }, callback);
+ return imports;
}
function normalizeUri(uri) {
}
}
}
+ },
+ 'accepts a list of source files as array of hashes': {
+ 'topic': function () {
+ return new CleanCSS().minify([
+ sourcesAsHash(['test/fixtures/partials/one.css']),
+ sourcesAsHash(['test/fixtures/partials/three.css'])
+ ]);
+ },
+ 'should give right output': function (minified) {
+ assert.equal(minified.styles, '.one{color:red}.three{background-image:url(test/fixtures/partials/extra/down.gif)}');
+ }
}
}).export(module);