-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroccoli-jsx.js
62 lines (54 loc) · 1.95 KB
/
broccoli-jsx.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
'use strict';
/* eslint-env node */
var Filter = require('broccoli-persistent-filter')
// var stringify = require('json-stable-stringify')
var { extractJSXComponents } = require('ember-meta-explorer');
module.exports = JSXFilter;
JSXFilter.prototype = Object.create(Filter.prototype)
JSXFilter.prototype.constructor = JSXFilter
function JSXFilter (inputTree, options) {
if (!options || typeof options !== 'object') {
options = { persist: true };
} else if (typeof options.persist === 'undefined') {
options.persist = true;
}
if (!(this instanceof JSXFilter)) return new JSXFilter(inputTree, options)
Filter.call(this, inputTree, options)
options = options || {}
this.bare = options.bare;
this.options = options;
}
JSXFilter.prototype.extensions = ['jsx','tsx']
JSXFilter.prototype.targetExtension = 'hbs'
JSXFilter.prototype.baseDir = function() {
return __dirname;
};
JSXFilter.prototype.optionsHash = function() {
if (!this._optionsHash) {
this._optionsHash = '{foo: bar}';
}
return this._optionsHash;
};
JSXFilter.prototype.cacheKeyProcessString = function(string, relativePath) {
// return Math.random();
return this.optionsHash() + Filter.prototype.cacheKeyProcessString.call(this, string, relativePath);
};
JSXFilter.prototype.processString = function (string) {
try {
let components = extractJSXComponents(string);
let keys = Object.keys(components);
let firstKey = keys[0];
let declaratedKey = firstKey + '_declarated';
if (components[declaratedKey]) {
return components[declaratedKey];
} else {
return components[firstKey];
}
} catch (err) {
// first_line/first_column properties; pass them on
// err.line = err.location && ((err.location.first_line || 0) + 1)
// err.column = err.location && ((err.location.first_column || 0) + 1)
// err.descriptions = err.description + ' template ' + string;
throw new Error('ember-cli-jsx-templates: Unable to compile ' + string);
}
}