See #799 - speeds up serializing code.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Sat, 14 Jan 2017 10:30:04 +0000 (11:30 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Sat, 14 Jan 2017 10:31:56 +0000 (11:31 +0100)
Why:

* Instead of using an `if` branch implements wrapping and tracking
  as a callback with `no-op` implementation when wrapping is disabled.

lib/writer/simple.js
lib/writer/source-maps.js

index ceecd8a..5bb16d1 100644 (file)
@@ -7,13 +7,16 @@ function store(token, serializeContext) {
     token :
     token[1];
 
-  if (serializeContext.format.wrapAt && serializeContext.column + value.length > serializeContext.format.wrapAt) {
+  serializeContext.wrap(value);
+  serializeContext.track(value);
+  serializeContext.output.push(value);
+}
+
+function wrap(value, serializeContext) {
+  if (serializeContext.column + value.length > serializeContext.format.wrapAt) {
     track(lineBreak, serializeContext);
     serializeContext.output.push(lineBreak);
   }
-
-  track(value, serializeContext);
-  serializeContext.output.push(value);
 }
 
 function track(value, serializeContext) {
@@ -32,7 +35,13 @@ function serializeStyles(tokens, context) {
     line: 1,
     output: [],
     spaceAfterClosingBrace: context.options.compatibility.properties.spaceAfterClosingBrace,
-    store: store
+    store: store,
+    track: context.options.format.wrapAt ?
+      function (value) { track(value, serializeContext); } :
+      function () { /* noop */  },
+    wrap: context.options.format.wrapAt ?
+      function (value) { wrap(value, serializeContext); } :
+      function () { /* noop */  }
   };
 
   all(tokens, serializeContext, false);
index c69dc1a..f58db4f 100644 (file)
@@ -15,13 +15,16 @@ function store(element, serializeContext) {
   var value = fromString ? element : element[1];
   var mappings = fromString ? null : element[2];
 
-  if (serializeContext.format.wrapAt && serializeContext.column + value.length > serializeContext.format.wrapAt) {
+  serializeContext.wrap(value);
+  track(value, mappings, serializeContext);
+  serializeContext.output.push(value);
+}
+
+function wrap(value, serializeContext) {
+  if (serializeContext.column + value.length > serializeContext.format.wrapAt) {
     track(lineBreak, false, serializeContext);
     serializeContext.output.push(lineBreak);
   }
-
-  track(value, mappings, serializeContext);
-  serializeContext.output.push(value);
 }
 
 function track(value, mappings, serializeContext) {
@@ -81,7 +84,10 @@ function serializeStylesAndSourceMap(tokens, context) {
     outputMap: new SourceMapGenerator(),
     sourcesContent: context.sourcesContent,
     spaceAfterClosingBrace: context.options.compatibility.properties.spaceAfterClosingBrace,
-    store: store
+    store: store,
+    wrap: context.options.format.wrapAt ?
+      function (value) { wrap(value, serializeContext); } :
+      function () { /* noop */  }
   };
 
   all(tokens, serializeContext, false);