-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
ember-addon-main.js
143 lines (112 loc) · 4.1 KB
/
ember-addon-main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict';
var path = require('path');
var checker = require('ember-cli-version-checker');
var utils = require('./utils');
var hashForDep = require('hash-for-dep');
module.exports = {
name: 'ember-cli-htmlbars',
init: function() {
if (this._super.init) { this._super.init.apply(this, arguments); }
checker.assertAbove(this, '0.1.2');
},
parentRegistry: null,
shouldSetupRegistryInIncluded: function() {
return !checker.isAbove(this, '0.2.0');
},
setupPreprocessorRegistry: function(type, registry) {
// ensure that broccoli-ember-hbs-template-compiler is not processing hbs files
registry.remove('template', 'broccoli-ember-hbs-template-compiler');
registry.add('template', {
name: 'ember-cli-htmlbars',
ext: 'hbs',
_addon: this,
toTree: function(tree) {
var htmlbarsOptions = this._addon.htmlbarsOptions();
return require('./index')(tree, htmlbarsOptions);
},
precompile: function(string) {
var htmlbarsOptions = this._addon.htmlbarsOptions();
var templateCompiler = htmlbarsOptions.templateCompiler;
return utils.template(templateCompiler, string);
}
});
if (type === 'parent') {
this.parentRegistry = registry;
}
},
included: function (app) {
this._super.included.apply(this, arguments);
if (this.shouldSetupRegistryInIncluded()) {
this.setupPreprocessorRegistry('parent', app.registry);
}
},
projectConfig: function () {
return this.project.config(process.env.EMBER_ENV);
},
templateCompilerPath: function() {
var config = this.projectConfig();
var templateCompilerPath = config['ember-cli-htmlbars'] && config['ember-cli-htmlbars'].templateCompilerPath;
var ember = this.project.findAddonByName('ember-core');
if (ember) {
return ember.absolutePaths.templateCompiler;
} else if (!templateCompilerPath) {
templateCompilerPath = this.project.bowerDirectory + '/ember/ember-template-compiler';
}
var absolutePath = path.resolve(this.project.root, templateCompilerPath);
if (path.extname(absolutePath) === '') {
absolutePath += '.js';
}
return absolutePath;
},
htmlbarsOptions: function() {
var projectConfig = this.projectConfig() || {};
var EmberENV = projectConfig.EmberENV || {};
var templateCompilerPath = this.templateCompilerPath();
// ensure we get a fresh templateCompilerModuleInstance per ember-addon
// instance NOTE: this is a quick hack, and will only work as long as
// templateCompilerPath is a single file bundle
//
// (╯°□°)╯︵ ɹǝqɯǝ
//
// we will also fix this in ember for future releases
delete require.cache[templateCompilerPath];
global.EmberENV = EmberENV; // Needed for eval time feature flag checks
var pluginInfo = this.astPlugins();
var htmlbarsOptions = {
isHTMLBars: true,
EmberENV: EmberENV,
templateCompiler: require(templateCompilerPath),
templateCompilerPath: templateCompilerPath,
plugins: {
ast: pluginInfo.plugins
},
pluginCacheKey: pluginInfo.cacheKeys
};
delete require.cache[templateCompilerPath];
delete global.Ember;
delete global.EmberENV;
return htmlbarsOptions;
},
astPlugins: function() {
var pluginWrappers = this.parentRegistry.load('htmlbars-ast-plugin');
var plugins = [];
var cacheKeys = [];
for (var i = 0; i < pluginWrappers.length; i++) {
var wrapper = pluginWrappers[i];
plugins.push(wrapper.plugin);
if (typeof wrapper.baseDir === 'function') {
var pluginHashForDep = hashForDep(wrapper.baseDir());
cacheKeys.push(pluginHashForDep);
} else {
// support for ember-cli < 2.2.0
var log = this.ui.writeDeprecateLine || this.ui.writeLine;
log.call(this.ui, 'ember-cli-htmlbars is opting out of caching due to an AST plugin that does not provide a caching strategy: `' + wrapper.name + '`.');
cacheKeys.push((new Date()).getTime() + '|' + Math.random());
}
}
return {
plugins: plugins,
cacheKeys: cacheKeys
};
}
};