UNPKG

3.21 kBJavaScriptView Raw
1'use strict';
2
3var fs = require('fs');
4var utils = require('./utils');
5var Filter = require('broccoli-persistent-filter');
6var crypto = require('crypto');
7var stringify = require('json-stable-stringify');
8var stripBom = require('strip-bom');
9
10function TemplateCompiler (inputTree, _options) {
11 if (!(this instanceof TemplateCompiler)) {
12 return new TemplateCompiler(inputTree, _options);
13 }
14
15 var options = _options || {};
16 if (!options.hasOwnProperty('persist')) {
17 options.persist = true;
18 }
19
20 Filter.call(this, inputTree, options); // this._super()
21
22 this.options = options || {};
23 this.inputTree = inputTree;
24
25 this.precompile = this.options.templateCompiler.precompile;
26 this.registerPlugin = this.options.templateCompiler.registerPlugin;
27
28 this.registerPlugins();
29 this.initializeFeatures();
30}
31
32TemplateCompiler.prototype = Object.create(Filter.prototype);
33TemplateCompiler.prototype.constructor = TemplateCompiler;
34TemplateCompiler.prototype.extensions = ['hbs', 'handlebars'];
35TemplateCompiler.prototype.targetExtension = 'js';
36
37TemplateCompiler.prototype.baseDir = function() {
38 return __dirname;
39};
40
41TemplateCompiler.prototype.registerPlugins = function registerPlugins() {
42 var plugins = this.options.plugins;
43
44 if (plugins) {
45 for (var type in plugins) {
46 for (var i = 0, l = plugins[type].length; i < l; i++) {
47 this.registerPlugin(type, plugins[type][i]);
48 }
49 }
50 }
51};
52
53TemplateCompiler.prototype.initializeFeatures = function initializeFeatures() {
54 var EmberENV = this.options.EmberENV;
55 var FEATURES = this.options.FEATURES;
56 var templateCompiler = this.options.templateCompiler;
57
58 if (FEATURES) {
59 console.warn('Using `options.FEATURES` with ember-cli-htmlbars is deprecated. Please provide the full EmberENV as options.EmberENV instead.');
60 EmberENV = EmberENV || {};
61 EmberENV.FEATURES = FEATURES;
62 }
63
64 utils.initializeEmberENV(templateCompiler, EmberENV);
65};
66
67TemplateCompiler.prototype.processString = function (string, relativePath) {
68 return 'export default ' + utils.template(this.options.templateCompiler, stripBom(string), {
69 contents: string,
70 moduleName: relativePath
71 }) + ';';
72};
73
74TemplateCompiler.prototype._buildOptionsForHash = function() {
75 var strippedOptions = {};
76
77 for (var key in this.options) {
78 if (key !== 'templateCompiler') {
79 strippedOptions[key] = this.options[key];
80 }
81 }
82
83 return strippedOptions;
84};
85
86TemplateCompiler.prototype._templateCompilerContents = function() {
87 if (this.options.templateCompilerPath) {
88 return fs.readFileSync(this.options.templateCompilerPath, { encoding: 'utf8' });
89 } else {
90 return '';
91 }
92};
93
94TemplateCompiler.prototype.optionsHash = function() {
95 if (!this._optionsHash) {
96 this._optionsHash = crypto.createHash('md5')
97 .update(stringify(this._buildOptionsForHash()), 'utf8')
98 .update(stringify(this._templateCompilerContents()), 'utf8')
99 .digest('hex');
100 }
101
102 return this._optionsHash;
103};
104
105TemplateCompiler.prototype.cacheKeyProcessString = function(string, relativePath) {
106 return this.optionsHash() + Filter.prototype.cacheKeyProcessString.call(this, string, relativePath);
107};
108
109module.exports = TemplateCompiler;