Repository: ember-cli/ember-cli
Pull request #6460 (2016-11-23 22:33:42)

Refactor processTemplate.
Simple change to unlock broader usage of this function.

Turbo87: this changes the lazy to eager require() though. was that intended?
nathanhammond: The lazy require in this case I'm _pretty sure_ didn't net us much because I believe that code path gets hit nearly every time. I [originally had it still lazy](https://github.com/ember-cli/ember-cli/pull/6449/commits/5d2b1b16f80beb2e7712a4339e4cc53431a338f2) but I elected against that.

Don't have strong opinions.
Turbo87: > I believe that code path gets hit nearly every time

does `ember help` render templates?
stefanpenner: seems good, mind adding 1 quick unit test covering this function (now that it is extracted). Then :shipit: 
kellyselden: > does ember help render templates?

I'm also curious if this regresses `ember help` speed.
stefanpenner: > I'm also curious if this regresses ember help speed.

@kellyselden say more. The above PR looks like a extraction, I wouldn't expect a difference.
kellyselden: @stefanpenner I believe moving the `require`s to lazily loaded instead of eagerly was to let `ember h` and `ember v` run faster. At first glance, this appears to revert some of that.

Patch:
diff --git a/lib/models/file-info.js b/lib/models/file-info.js
index c9e514f68..746ddebb8 100644
--- a/lib/models/file-info.js
+++ b/lib/models/file-info.js
@@ -10,15 +10,6 @@ var EOL          = require('os').EOL;
 var isBinaryFile = require('isbinaryfile').sync;
 var canEdit      = require('../utilities/open-editor').canEdit;
 
-function processTemplate(content, context) {
-  var options = {
-    evaluate:    /<%([\s\S]+?)%>/g,
-    interpolate: /<%=([\s\S]+?)%>/g,
-    escape:      /<%-([\s\S]+?)%>/g
-  };
-  return require('lodash.template')(content, options)(context);
-}
-
 function diffHighlight(line) {
   if (line[0] === '+') {
     return chalk.green(line);
@@ -107,7 +98,7 @@ FileInfo.prototype.render = function() {
           return content;
         } else {
           try {
-            return processTemplate(content.toString(), context);
+            return require('../utilities/process-template')(content.toString(), context);
           } catch (err) {
             err.message += ' (Error in blueprint template: ' + path + ')';
             throw err;
diff --git a/lib/utilities/process-template.js b/lib/utilities/process-template.js
new file mode 100644
index 000000000..37fde6196
--- /dev/null
+++ b/lib/utilities/process-template.js
@@ -0,0 +1,10 @@
+var _template = require('lodash.template');
+
+module.exports = function processTemplate(content, context) {
+  var options = {
+    evaluate:    /<%([\s\S]+?)%>/g,
+    interpolate: /<%=([\s\S]+?)%>/g,
+    escape:      /<%-([\s\S]+?)%>/g
+  };
+  return _template(content, options)(context);
+}


Test patch:
diff --git a/tests/unit/utilities/process-template-test.js b/tests/unit/utilities/process-template-test.js
new file mode 100644
index 000000000..09f4d008a
--- /dev/null
+++ b/tests/unit/utilities/process-template-test.js
@@ -0,0 +1,17 @@
+'use strict';
+
+var expect = require('chai').expect;
+var processTemplate = require('../../../lib/utilities/process-template');
+
+describe('process-template', function() {
+  it('successfully transforms a template', function() {
+
+    expect(processTemplate('hello <%= user %>!', { 'user': 'fred' }))
+      .to.be.equal('hello fred!');
+    expect(processTemplate('<b><%- value %></b>', { 'value': '<script>' }))
+      .to.be.equal('<b>&lt;script&gt;</b>');
+    expect(processTemplate('<% users.forEach(function(user) { %><li><%- user %></li><% }); %>', { 'users': ['fred', 'barney'] }))
+      .to.be.equal('<li>fred</li><li>barney</li>');
+
+  });
+});