options.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = parseArgv;
  6. function _fs() {
  7. const data = require("fs");
  8. _fs = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function commander() {
  14. const data = _interopRequireWildcard(require("commander"), true);
  15. commander = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _core() {
  21. const data = require("@babel/core");
  22. _core = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function glob() {
  28. const data = _interopRequireWildcard(require("glob"), true);
  29. glob = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. var _util = require("./util.js");
  35. function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
  36. const program = commander().default.program;
  37. program.option("-f, --filename [filename]", "The filename to use when reading from stdin. This will be used in source-maps, errors etc.");
  38. program.option("--presets [list]", "A comma-separated list of preset names.", collect);
  39. program.option("--plugins [list]", "A comma-separated list of plugin names.", collect);
  40. program.option("--config-file [path]", "Path to a .babelrc file to use.");
  41. program.option("--env-name [name]", "The name of the 'env' to use when loading configs and plugins. " + "Defaults to the value of BABEL_ENV, or else NODE_ENV, or else 'development'.");
  42. program.option("--root-mode [mode]", "The project-root resolution mode. " + "One of 'root' (the default), 'upward', or 'upward-optional'.");
  43. program.option("--source-type [script|module]", "");
  44. program.option("--no-babelrc", "Whether or not to look up .babelrc and .babelignore files.");
  45. program.option("--ignore [list]", "List of glob paths to **not** compile.", collect);
  46. program.option("--only [list]", "List of glob paths to **only** compile.", collect);
  47. program.option("--no-highlight-code", "Enable or disable ANSI syntax highlighting of code frames. (on by default)");
  48. program.option("--no-comments", "Write comments to generated output. (true by default)");
  49. program.option("--retain-lines", "Retain line numbers. This will result in really ugly code.");
  50. program.option("--compact [true|false|auto]", "Do not include superfluous whitespace characters and line terminators.", booleanify);
  51. program.option("--minified", "Save as many bytes when printing. (false by default)");
  52. program.option("--auxiliary-comment-before [string]", "Print a comment before any injected non-user code.");
  53. program.option("--auxiliary-comment-after [string]", "Print a comment after any injected non-user code.");
  54. program.option("-s, --source-maps [true|false|inline|both]", "", booleanify, undefined);
  55. program.option("--source-map-target [string]", "Set `file` on returned source map.");
  56. program.option("--source-file-name [string]", "Set `sources[0]` on returned source map.");
  57. program.option("--source-root [filename]", "The root from which all sources are relative.");
  58. {
  59. program.option("--module-root [filename]", "Optional prefix for the AMD module formatter that will be prepended to the filename on module definitions.");
  60. program.option("-M, --module-ids", "Insert an explicit id for modules.");
  61. program.option("--module-id [string]", "Specify a custom name for module ids.");
  62. }
  63. program.option("-x, --extensions [extensions]", "List of extensions to compile when a directory has been the input. [" + _core().DEFAULT_EXTENSIONS.join() + "]", collect);
  64. program.option("--keep-file-extension", "Preserve the file extensions of the input files.");
  65. program.option("-w, --watch", "Recompile files on changes.");
  66. program.option("--skip-initial-build", "Do not compile files before watching.");
  67. program.option("-o, --out-file [out]", "Compile all input files into a single file.");
  68. program.option("-d, --out-dir [out]", "Compile an input directory of modules into an output directory.");
  69. program.option("--relative", "Compile into an output directory relative to input directory or file. Requires --out-dir [out]");
  70. program.option("-D, --copy-files", "When compiling a directory copy over non-compilable files.");
  71. program.option("--include-dotfiles", "Include dotfiles when compiling and copying non-compilable files.");
  72. program.option("--no-copy-ignored", "Exclude ignored files when copying non-compilable files.");
  73. program.option("--verbose", "Log everything. This option conflicts with --quiet");
  74. program.option("--quiet", "Don't log anything. This option conflicts with --verbose");
  75. program.option("--delete-dir-on-start", "Delete the out directory before compilation.");
  76. program.option("--out-file-extension [string]", "Use a specific extension for the output files");
  77. program.version("7.28.3" + " (@babel/core " + _core().version + ")");
  78. program.usage("[options] <files ...>");
  79. program.action(() => {});
  80. function parseArgv(args) {
  81. program.parse(args);
  82. const opts = program.opts();
  83. const errors = [];
  84. let filenames = program.args.reduce(function (globbed, input) {
  85. let files = glob().sync(input);
  86. if (!files.length) files = [input];
  87. globbed.push(...files);
  88. return globbed;
  89. }, []);
  90. filenames = Array.from(new Set(filenames));
  91. filenames.forEach(function (filename) {
  92. if (!_fs().existsSync(filename)) {
  93. errors.push(filename + " does not exist");
  94. }
  95. });
  96. if (opts.outDir && !filenames.length) {
  97. errors.push("--out-dir requires filenames");
  98. }
  99. if (opts.outFile && opts.outDir) {
  100. errors.push("--out-file and --out-dir cannot be used together");
  101. }
  102. if (opts.relative && !opts.outDir) {
  103. errors.push("--relative requires --out-dir usage");
  104. }
  105. if (opts.watch) {
  106. if (!opts.outFile && !opts.outDir) {
  107. errors.push("--watch requires --out-file or --out-dir");
  108. }
  109. if (!filenames.length) {
  110. errors.push("--watch requires filenames");
  111. }
  112. }
  113. if (opts.skipInitialBuild && !opts.watch) {
  114. errors.push("--skip-initial-build requires --watch");
  115. }
  116. if (opts.deleteDirOnStart && !opts.outDir) {
  117. errors.push("--delete-dir-on-start requires --out-dir");
  118. }
  119. if (opts.verbose && opts.quiet) {
  120. errors.push("--verbose and --quiet cannot be used together");
  121. }
  122. if (!opts.outDir && filenames.length === 0 && typeof opts.filename !== "string" && opts.babelrc !== false) {
  123. errors.push("stdin compilation requires either -f/--filename [filename] or --no-babelrc");
  124. }
  125. if (opts.keepFileExtension && opts.outFileExtension) {
  126. errors.push("--out-file-extension cannot be used with --keep-file-extension");
  127. }
  128. if (errors.length) {
  129. console.error("babel:");
  130. errors.forEach(function (e) {
  131. console.error(" " + e);
  132. });
  133. return null;
  134. }
  135. const babelOptions = {
  136. presets: opts.presets,
  137. plugins: opts.plugins,
  138. rootMode: opts.rootMode,
  139. configFile: opts.configFile,
  140. envName: opts.envName,
  141. sourceType: opts.sourceType,
  142. ignore: opts.ignore,
  143. only: opts.only,
  144. retainLines: opts.retainLines,
  145. compact: opts.compact,
  146. minified: opts.minified,
  147. auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
  148. auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
  149. sourceMaps: opts.sourceMaps,
  150. sourceFileName: opts.sourceFileName,
  151. sourceRoot: opts.sourceRoot,
  152. babelrc: opts.babelrc === true ? undefined : opts.babelrc,
  153. highlightCode: opts.highlightCode === true ? undefined : opts.highlightCode,
  154. comments: opts.comments === true ? undefined : opts.comments
  155. };
  156. {
  157. Object.assign(babelOptions, {
  158. moduleRoot: opts.moduleRoot,
  159. moduleIds: opts.moduleIds,
  160. moduleId: opts.moduleId
  161. });
  162. }
  163. for (const key of Object.keys(babelOptions)) {
  164. if (babelOptions[key] === undefined) {
  165. delete babelOptions[key];
  166. }
  167. }
  168. return {
  169. babelOptions,
  170. cliOptions: {
  171. filename: opts.filename,
  172. filenames,
  173. extensions: opts.extensions,
  174. keepFileExtension: opts.keepFileExtension,
  175. outFileExtension: opts.outFileExtension,
  176. watch: opts.watch,
  177. skipInitialBuild: opts.skipInitialBuild,
  178. outFile: opts.outFile,
  179. outDir: opts.outDir,
  180. relative: opts.relative,
  181. copyFiles: opts.copyFiles,
  182. copyIgnored: opts.copyFiles && opts.copyIgnored,
  183. includeDotfiles: opts.includeDotfiles,
  184. verbose: opts.verbose,
  185. quiet: opts.quiet,
  186. deleteDirOnStart: opts.deleteDirOnStart,
  187. sourceMapTarget: opts.sourceMapTarget
  188. }
  189. };
  190. }
  191. function booleanify(val) {
  192. if (val === "true" || val === "1") {
  193. return true;
  194. }
  195. if (val === "false" || val === "0" || val === "") {
  196. return false;
  197. }
  198. return val;
  199. }
  200. function collect(value, previousValue) {
  201. if (typeof value !== "string") return previousValue;
  202. const values = value.split(",");
  203. if (previousValue) {
  204. previousValue.push(...values);
  205. return previousValue;
  206. }
  207. return values;
  208. }
  209. //# sourceMappingURL=options.js.map