Skip to content

Commit 112f7aa

Browse files
committed
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.
1 parent f1288d1 commit 112f7aa

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

bin/jsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,17 @@ require('commoner').version(
2727
'--source-map-inline',
2828
'Embed inline sourcemap in transformed source'
2929
).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-
3830
// 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.
3935
var options = {
4036
harmony: this.options.harmony,
4137
sourceMap: this.options.sourceMapInline,
4238
stripTypes: this.options.stripTypes,
43-
sourceType: sourceType,
39+
es6module: this.options.es6module,
40+
nonStrictEs6Module: this.options.nonStrictEs6Module
4441
};
4542
return transform(source, options);
4643
});

main.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,58 @@ var Buffer = require('buffer').Buffer;
77

88
module.exports = {
99
transform: function(input, options) {
10+
options = processOptions(options);
1011
var output = innerTransform(input, options);
1112
var result = output.code;
12-
if (options && options.sourceMap) {
13+
if (options.sourceMap) {
1314
var map = inlineSourceMap(
1415
output.sourceMap,
1516
input,
16-
options.sourceFilename
17+
options.filename
1718
);
1819
result += '\n' + map;
1920
}
2021
return result;
2122
},
2223
transformWithDetails: function(input, options) {
24+
options = processOptions(options);
2325
var output = innerTransform(input, options);
2426
var result = {};
2527
result.code = output.code;
26-
if (options && options.sourceMap) {
28+
if (options.sourceMap) {
2729
result.sourceMap = output.sourceMap.toJSON();
2830
}
29-
if (options && options.sourceFilename) {
30-
result.sourceMap.sources = [options.sourceFilename];
31+
if (options.filename) {
32+
result.sourceMap.sources = [options.filename];
3133
}
3234
return result;
3335
}
3436
};
3537

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+
}
3857

58+
return options;
59+
}
60+
61+
function innerTransform(input, options) {
3962
var visitorSets = ['react'];
4063
if (options.harmony) {
4164
visitorSets.push('harmony');
@@ -49,9 +72,6 @@ function innerTransform(input, options) {
4972
}
5073

5174
var visitorList = visitors.getVisitorsBySet(visitorSets);
52-
if (options.sourceFilename) {
53-
options.filename = options.sourceFilename;
54-
}
5575
return transform(visitorList, input, options);
5676
}
5777

0 commit comments

Comments
 (0)