-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
41 lines (32 loc) · 1.3 KB
/
index.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
var reactTools = require('react-tools')
var path = require('path')
var loaderUtils = require('loader-utils')
var escapeStringRegexp = require('escape-string-regexp');
module.exports = function (content) {
this.cacheable()
var query = loaderUtils.parseQuery(this.query);
var reactToolsOptions = {
harmony: query.harmony == undefined ? true : query.harmony
}
if (query.target) reactToolsOptions.target = query.target;
var identifier = escapeStringRegexp(query.identifier || "React.jsx");
var that = this;
var replace = function (match, jsx) {
try {
var reactCode = reactTools.transform(jsx, reactToolsOptions)
}
catch (ex) {
that.emitError('Problem transforming the following:\n' + jsx + '\n\n' + ex)
return match;
}
return '(' + reactCode + ')'
};
var dollarBraceReplace = function (match, jsx) {
jsx = jsx.replace(/\$\{([^]*?)\}/gm, "{$1}");
return replace(match, jsx);
};
return content
.replace(new RegExp(identifier + '\\(\\s*?`([^`\\\\]*(\\\\.[^`\\\\]*)*)`\\s*?\\)', 'gm'), dollarBraceReplace) // using template strings
.replace(new RegExp(identifier + '\\(\\/\\*((.|[\\r\\n])*?)\\*\\/\\)', 'gm'), replace) // using multiline comments
.replace(/\/\*jsx\*\/((.|[\r\n])*?)\/\*jsx\*\//gm, replace); // using jsx comments
}