-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
67 lines (55 loc) · 1.56 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
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
const fs = require('fs')
const path = require('path')
const through = require('through2')
const JSXParser = require('./lib/parse.js')
const JSXCodeGenerator = require('./lib/codegen.js')
function fromString(code, o = {}) {
const options = Object.assign({
spreadFn: `Object.assign`,
unknownTagPattern: `{tag}`,
passUnknownTagsToFactory: false,
unknownTagsAsString: false,
arrayChildren: true,
ecmaVersion: 8
}, o)
if (options.factory === undefined) {
throw new Error('Missing options.factory function name.')
}
const parseOptions = {
ecmaVersion: options.ecmaVersion
}
const node = JSXParser.parse(code, parseOptions)
const codegen = new JSXCodeGenerator(code, node, options)
return codegen.generate()
}
function fromFile(path, o = {}) {
return fromString(fs.readFileSync(path, 'utf8'), o)
}
function browserifyTransform(filename, options) {
return browserifyTransform.configure(options)(filename)
}
browserifyTransform.configure = function (options) {
if (typeof options.extensions === 'undefined') {
options.extensions = ['.js', '.jsx', '.es', '.es6']
}
return function (filename) {
if (!~options.extensions.indexOf(path.extname(filename))) {
return through()
}
var data = "";
return through(function (chunk, enc, next) {
data += chunk.toString('utf8')
next()
}, function (next) {
try {
this.push(fromString(data, options))
next()
} catch (err) {
next(err)
}
})
}
}
module.exports = {
fromString, fromFile, browserifyTransform
}