2015-02-19 11:02:56 +08:00
|
|
|
/**
|
|
|
|
* Copyright 2013-2015, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
2013-05-30 03:46:11 +08:00
|
|
|
'use strict';
|
2015-02-13 04:48:11 +08:00
|
|
|
/*eslint-disable no-undef*/
|
2014-03-17 06:38:40 +08:00
|
|
|
var visitors = require('./vendor/fbtransform/visitors');
|
2013-08-20 05:37:25 +08:00
|
|
|
var transform = require('jstransform').transform;
|
2014-11-15 09:58:51 +08:00
|
|
|
var typesSyntax = require('jstransform/visitors/type-syntax');
|
2015-02-19 11:02:56 +08:00
|
|
|
var inlineSourceMap = require('./vendor/inline-source-map');
|
2013-05-30 03:46:11 +08:00
|
|
|
|
|
|
|
module.exports = {
|
2014-01-17 02:39:33 +08:00
|
|
|
transform: function(input, options) {
|
2015-02-20 01:46:25 +08:00
|
|
|
options = processOptions(options);
|
2014-07-07 04:23:50 +08:00
|
|
|
var output = innerTransform(input, options);
|
|
|
|
var result = output.code;
|
2015-02-20 01:46:25 +08:00
|
|
|
if (options.sourceMap) {
|
2014-01-17 02:39:33 +08:00
|
|
|
var map = inlineSourceMap(
|
2014-07-07 04:23:50 +08:00
|
|
|
output.sourceMap,
|
2014-01-17 02:39:33 +08:00
|
|
|
input,
|
2015-02-20 01:46:25 +08:00
|
|
|
options.filename
|
2014-01-17 02:39:33 +08:00
|
|
|
);
|
2014-07-07 04:23:50 +08:00
|
|
|
result += '\n' + map;
|
2014-03-17 06:38:40 +08:00
|
|
|
}
|
2014-07-07 04:23:50 +08:00
|
|
|
return result;
|
2014-07-03 20:20:55 +08:00
|
|
|
},
|
2014-07-11 20:38:28 +08:00
|
|
|
transformWithDetails: function(input, options) {
|
2015-02-20 01:46:25 +08:00
|
|
|
options = processOptions(options);
|
2014-07-07 04:23:50 +08:00
|
|
|
var output = innerTransform(input, options);
|
|
|
|
var result = {};
|
|
|
|
result.code = output.code;
|
2015-02-20 01:46:25 +08:00
|
|
|
if (options.sourceMap) {
|
2014-07-11 20:44:02 +08:00
|
|
|
result.sourceMap = output.sourceMap.toJSON();
|
2014-07-03 20:20:55 +08:00
|
|
|
}
|
2015-02-20 01:46:25 +08:00
|
|
|
if (options.filename) {
|
|
|
|
result.sourceMap.sources = [options.filename];
|
2015-02-17 04:21:51 +08:00
|
|
|
}
|
2014-07-03 20:20:55 +08:00
|
|
|
return result;
|
2013-05-30 03:46:11 +08:00
|
|
|
}
|
|
|
|
};
|
2014-01-17 02:39:33 +08:00
|
|
|
|
2015-02-20 01:46:25 +08:00
|
|
|
/**
|
|
|
|
* Only copy the values that we need. We'll do some preprocessing to account for
|
|
|
|
* converting command line flags to options that jstransform can actually use.
|
|
|
|
*/
|
|
|
|
function processOptions(opts) {
|
|
|
|
opts = opts || {};
|
|
|
|
var options = {};
|
|
|
|
|
|
|
|
options.harmony = opts.harmony;
|
|
|
|
options.stripTypes = opts.stripTypes;
|
|
|
|
options.sourceMap = opts.sourceMap;
|
|
|
|
options.filename = opts.sourceFilename;
|
|
|
|
|
|
|
|
if (opts.es6module) {
|
|
|
|
options.sourceType = 'module';
|
|
|
|
}
|
2015-03-12 05:26:51 +08:00
|
|
|
if (opts.nonStrictEs6module) {
|
|
|
|
options.sourceType = 'nonStrictModule';
|
2015-02-20 01:46:25 +08:00
|
|
|
}
|
2014-07-07 04:23:50 +08:00
|
|
|
|
2015-01-13 06:08:38 +08:00
|
|
|
// Instead of doing any fancy validation, only look for 'es3'. If we have
|
|
|
|
// that, then use it. Otherwise use 'es5'.
|
|
|
|
options.es3 = opts.target === 'es3';
|
|
|
|
options.es5 = !options.es3;
|
|
|
|
|
2015-02-20 01:46:25 +08:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
function innerTransform(input, options) {
|
2014-09-06 04:27:22 +08:00
|
|
|
var visitorSets = ['react'];
|
|
|
|
if (options.harmony) {
|
|
|
|
visitorSets.push('harmony');
|
|
|
|
}
|
2015-01-13 06:08:38 +08:00
|
|
|
|
|
|
|
if (options.es3) {
|
|
|
|
visitorSets.push('es3');
|
|
|
|
}
|
|
|
|
|
2014-09-06 04:27:22 +08:00
|
|
|
if (options.stripTypes) {
|
2014-11-15 09:58:51 +08:00
|
|
|
// Stripping types needs to happen before the other transforms
|
|
|
|
// unfortunately, due to bad interactions. For example,
|
|
|
|
// es6-rest-param-visitors conflict with stripping rest param type
|
|
|
|
// annotation
|
|
|
|
input = transform(typesSyntax.visitorList, input, options).code;
|
2014-05-19 00:51:14 +08:00
|
|
|
}
|
2014-09-06 04:27:22 +08:00
|
|
|
|
|
|
|
var visitorList = visitors.getVisitorsBySet(visitorSets);
|
|
|
|
return transform(visitorList, input, options);
|
2014-05-19 00:51:14 +08:00
|
|
|
}
|