1 | 'use strict';
|
2 |
|
3 | var fs = require('fs');
|
4 | var utils = require('./utils');
|
5 | var Filter = require('broccoli-persistent-filter');
|
6 | var crypto = require('crypto');
|
7 | var stringify = require('json-stable-stringify');
|
8 | var stripBom = require('strip-bom');
|
9 |
|
10 | function 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);
|
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 |
|
32 | TemplateCompiler.prototype = Object.create(Filter.prototype);
|
33 | TemplateCompiler.prototype.constructor = TemplateCompiler;
|
34 | TemplateCompiler.prototype.extensions = ['hbs', 'handlebars'];
|
35 | TemplateCompiler.prototype.targetExtension = 'js';
|
36 |
|
37 | TemplateCompiler.prototype.baseDir = function() {
|
38 | return __dirname;
|
39 | };
|
40 |
|
41 | TemplateCompiler.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 |
|
53 | TemplateCompiler.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 |
|
67 | TemplateCompiler.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 |
|
74 | TemplateCompiler.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 |
|
86 | TemplateCompiler.prototype._templateCompilerContents = function() {
|
87 | if (this.options.templateCompilerPath) {
|
88 | return fs.readFileSync(this.options.templateCompilerPath, { encoding: 'utf8' });
|
89 | } else {
|
90 | return '';
|
91 | }
|
92 | };
|
93 |
|
94 | TemplateCompiler.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 |
|
105 | TemplateCompiler.prototype.cacheKeyProcessString = function(string, relativePath) {
|
106 | return this.optionsHash() + Filter.prototype.cacheKeyProcessString.call(this, string, relativePath);
|
107 | };
|
108 |
|
109 | module.exports = TemplateCompiler;
|