Fixes #845 - fixes web compatibility in 4.0.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 23 Dec 2016 20:43:50 +0000 (21:43 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 23 Dec 2016 20:53:03 +0000 (21:53 +0100)
Why:

* 4.0 calculates stats by default but `process.hrtime` is not
  available inside browsers. However, since we don't need
  sub-ms resolution, `Date.now()` can be used instead.

History.md
lib/clean.js

index 357559b..2f85695 100644 (file)
@@ -20,6 +20,7 @@
 * Fixed issue [#839](https://github.com/jakubpawlowicz/clean-css/issues/839) - allows URIs in import inlining rules.
 * Fixed issue [#840](https://github.com/jakubpawlowicz/clean-css/issues/840) - allows input source map as map object.
 * Fixed issue [#843](https://github.com/jakubpawlowicz/clean-css/issues/843) - regression in selector handling.
+* Fixed issue [#845](https://github.com/jakubpawlowicz/clean-css/issues/845) - web compatibility of 4.0 branch.
 * Fixed issue [#847](https://github.com/jakubpawlowicz/clean-css/issues/847) - regression in handling invalid selectors.
 
 [3.4.23 / 2016-12-17](https://github.com/jakubpawlowicz/clean-css/compare/v3.4.22...v3.4.23)
index 31c6a70..9e28b24 100644 (file)
@@ -73,7 +73,7 @@ CleanCSS.prototype.minify = function (input, callback) {
       efficiency: 0,
       minifiedSize: 0,
       originalSize: 0,
-      startedAt: process.hrtime(),
+      startedAt: Date.now(),
       timeSpent: 0
     },
     errors: [],
@@ -137,10 +137,11 @@ function withMetadata(output, context) {
 }
 
 function calculateStatsFrom(styles, context) {
-  var elapsed = process.hrtime(context.stats.startedAt);
+  var finishedAt = Date.now();
+  var timeSpent = finishedAt - context.stats.startedAt;
 
   delete context.stats.startedAt;
-  context.stats.timeSpent = ~~(elapsed[0] * 1e3 + elapsed[1] / 1e6);
+  context.stats.timeSpent = timeSpent;
   context.stats.efficiency = 1 - styles.length / context.stats.originalSize;
   context.stats.minifiedSize = styles.length;