cli.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Copyright 2013-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. var assign = require('object-assign');
  10. var transform = require('./simple').transform;
  11. require('commoner').version(
  12. require('../package.json').version
  13. ).resolve(function(id) {
  14. return this.readModuleP(id);
  15. }).option(
  16. '--react',
  17. 'Turns on the React JSX and React displayName transforms'
  18. ).option(
  19. '--es6',
  20. 'Turns on available ES6 transforms'
  21. ).option(
  22. '--es7',
  23. 'Turns on available ES7 transforms'
  24. ).option(
  25. '--harmony',
  26. 'Shorthand to enable all ES6 and ES7 transforms'
  27. ).option(
  28. '--utility',
  29. 'Turns on available utility transforms'
  30. ).option(
  31. '--target [version]',
  32. 'Specify your target version of ECMAScript. Valid values are "es3" and ' +
  33. '"es5". The default is "es5". "es3" will avoid uses of defineProperty and ' +
  34. 'will quote reserved words. WARNING: "es5" is not properly supported, even ' +
  35. 'with the use of es5shim, es5sham. If you need to support IE8, use "es3".',
  36. 'es5'
  37. ).option(
  38. '--strip-types',
  39. 'Strips out type annotations.'
  40. ).option(
  41. '--es6module',
  42. 'Parses the file as a valid ES6 module. ' +
  43. '(Note that this means implicit strict mode)'
  44. ).option(
  45. '--non-strict-es6module',
  46. 'Parses the file as an ES6 module, except disables implicit strict-mode. ' +
  47. '(This is useful if you\'re porting non-ES6 modules to ES6, but haven\'t ' +
  48. 'yet verified that they are strict-mode safe yet)'
  49. ).option(
  50. '--source-map-inline',
  51. 'Embed inline sourcemap in transformed source'
  52. ).option(
  53. '--source-filename',
  54. 'Filename to use when generating the inline sourcemap. Will default to ' +
  55. 'filename when processing files'
  56. ).process(function(id, source) {
  57. // This is where JSX, ES6, etc. desugaring happens.
  58. // We don't do any pre-processing of options so that the command line and the
  59. // JS API both expose the same set of options. We will set the sourceFilename
  60. // to something more correct than "source.js".
  61. var options;
  62. if (id !== '<stdin>') {
  63. options = assign({sourceFilename: id + '.js'}, this.options);
  64. } else {
  65. options = this.options;
  66. }
  67. var result = transform(source, options);
  68. return result.code;
  69. });