Move option parsing into react-tools proper.
We were doing some preprocessing for module options in the command line. Since we also expose the same API via react-tools (and JSXTransformer), we need to do the same processing from the API. So just move it all to the same place.
Paul O’Shannessy committed
Feb 19, 2015 at 09:46 UTC
112f7aa249c2033276feb704d311620ad652d762
2 files changed
+36
-19
bin/jsx
+6
-9
@@ -27,20 +27,17 @@ require('commoner').version(
27
'--source-map-inline',
28
'Embed inline sourcemap in transformed source'
29
).process(function(id, source) {
30
- var sourceType;
31
- if (this.options.es6module) {
32
- sourceType = 'module';
33
- }
34
- if (this.options.nonStrictEs6module) {
35
- sourceType = 'nonStrictModule';
36
- }
37
-
30
// This is where JSX, ES6, etc. desugaring happens.
31
+ // We don't do any pre-processing of options so that the command line and the
32
+ // JS API both expose the same set of options. We do extract the options that
33
+ // we care about from commoner though so we aren't passing too many things
34
+ // along.
35
var options = {
36
harmony: this.options.harmony,
37
sourceMap: this.options.sourceMapInline,
38
stripTypes: this.options.stripTypes,
43
- sourceType: sourceType,
39
+ es6module: this.options.es6module,
40
+ nonStrictEs6Module: this.options.nonStrictEs6Module
41
};
42
return transform(source, options);
43
});
main.js
+30
-10
@@ -7,35 +7,58 @@ var Buffer = require('buffer').Buffer;
7
8
module.exports = {
9
transform: function(input, options) {
10
+ options = processOptions(options);
11
var output = innerTransform(input, options);
12
var result = output.code;
12
- if (options && options.sourceMap) {
13
+ if (options.sourceMap) {
14
var map = inlineSourceMap(
15
output.sourceMap,
16
input,
16
- options.sourceFilename
17
+ options.filename
18
);
19
result += '\n' + map;
20
}
21
return result;
22
},
23
transformWithDetails: function(input, options) {
24
+ options = processOptions(options);
25
var output = innerTransform(input, options);
26
var result = {};
27
result.code = output.code;
26
- if (options && options.sourceMap) {
28
+ if (options.sourceMap) {
29
result.sourceMap = output.sourceMap.toJSON();
30
}
29
- if (options && options.sourceFilename) {
30
- result.sourceMap.sources = [options.sourceFilename];
31
+ if (options.filename) {
32
+ result.sourceMap.sources = [options.filename];
33
}
34
return result;
35
}
36
};
37
36
-function innerTransform(input, options) {
37
- options = options || {};
38
+/**
39
+ * Only copy the values that we need. We'll do some preprocessing to account for
40
+ * converting command line flags to options that jstransform can actually use.
41
+ */
42
+function processOptions(opts) {
43
+ opts = opts || {};
44
+ var options = {};
45
+
46
+ options.harmony = opts.harmony;
47
+ options.stripTypes = opts.stripTypes;
48
+ options.sourceMap = opts.sourceMap;
49
+ options.filename = opts.sourceFilename;
50
+
51
+ if (opts.es6module) {
52
+ options.sourceType = 'module';
53
+ }
54
+ if (opts.nonStrictEs6Module) {
55
+ options.sourceType = 'nonStrict6Module';
56
+ }
57
58
+ return options;
59
+}
60
+
61
+function innerTransform(input, options) {
62
var visitorSets = ['react'];
63
if (options.harmony) {
64
visitorSets.push('harmony');
@@ -49,9 +72,6 @@ function innerTransform(input, options) {
72
}
73
74
var visitorList = visitors.getVisitorsBySet(visitorSets);
52
- if (options.sourceFilename) {
53
- options.filename = options.sourceFilename;
54
- }
75
return transform(visitorList, input, options);
76
}
77