file.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. function _convertSourceMap() {
  7. const data = _interopRequireDefault(require("convert-source-map"));
  8. _convertSourceMap = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _defaults() {
  14. const data = _interopRequireDefault(require("lodash/defaults"));
  15. _defaults = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _sourceMap() {
  21. const data = _interopRequireDefault(require("source-map"));
  22. _sourceMap = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _slash() {
  28. const data = _interopRequireDefault(require("slash"));
  29. _slash = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _path() {
  35. const data = _interopRequireDefault(require("path"));
  36. _path = function () {
  37. return data;
  38. };
  39. return data;
  40. }
  41. function _fs() {
  42. const data = _interopRequireDefault(require("fs"));
  43. _fs = function () {
  44. return data;
  45. };
  46. return data;
  47. }
  48. var util = _interopRequireWildcard(require("./util"));
  49. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  50. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  51. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  52. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  53. function _default(_x) {
  54. return _ref.apply(this, arguments);
  55. }
  56. function _ref() {
  57. _ref = _asyncToGenerator(function* ({
  58. cliOptions,
  59. babelOptions
  60. }) {
  61. function buildResult(fileResults) {
  62. const map = new (_sourceMap().default.SourceMapGenerator)({
  63. file: cliOptions.sourceMapTarget || _path().default.basename(cliOptions.outFile || "") || "stdout",
  64. sourceRoot: babelOptions.sourceRoot
  65. });
  66. let code = "";
  67. let offset = 0;
  68. for (const result of fileResults) {
  69. if (!result) continue;
  70. code += result.code + "\n";
  71. if (result.map) {
  72. const consumer = new (_sourceMap().default.SourceMapConsumer)(result.map);
  73. const sources = new Set();
  74. consumer.eachMapping(function (mapping) {
  75. if (mapping.source != null) sources.add(mapping.source);
  76. map.addMapping({
  77. generated: {
  78. line: mapping.generatedLine + offset,
  79. column: mapping.generatedColumn
  80. },
  81. source: mapping.source,
  82. original: mapping.source == null ? null : {
  83. line: mapping.originalLine,
  84. column: mapping.originalColumn
  85. }
  86. });
  87. });
  88. sources.forEach(source => {
  89. const content = consumer.sourceContentFor(source, true);
  90. if (content !== null) {
  91. map.setSourceContent(source, content);
  92. }
  93. });
  94. offset = code.split("\n").length - 1;
  95. }
  96. }
  97. if (babelOptions.sourceMaps === "inline" || !cliOptions.outFile && babelOptions.sourceMaps) {
  98. code += "\n" + _convertSourceMap().default.fromObject(map).toComment();
  99. }
  100. return {
  101. map: map,
  102. code: code
  103. };
  104. }
  105. function output(fileResults) {
  106. const result = buildResult(fileResults);
  107. if (cliOptions.outFile) {
  108. if (babelOptions.sourceMaps && babelOptions.sourceMaps !== "inline") {
  109. const mapLoc = cliOptions.outFile + ".map";
  110. result.code = util.addSourceMappingUrl(result.code, mapLoc);
  111. _fs().default.writeFileSync(mapLoc, JSON.stringify(result.map));
  112. }
  113. _fs().default.writeFileSync(cliOptions.outFile, result.code);
  114. } else {
  115. process.stdout.write(result.code + "\n");
  116. }
  117. }
  118. function readStdin() {
  119. return new Promise((resolve, reject) => {
  120. let code = "";
  121. process.stdin.setEncoding("utf8");
  122. process.stdin.on("readable", function () {
  123. const chunk = process.stdin.read();
  124. if (chunk !== null) code += chunk;
  125. });
  126. process.stdin.on("end", function () {
  127. resolve(code);
  128. });
  129. process.stdin.on("error", reject);
  130. });
  131. }
  132. function stdin() {
  133. return _stdin.apply(this, arguments);
  134. }
  135. function _stdin() {
  136. _stdin = _asyncToGenerator(function* () {
  137. const code = yield readStdin();
  138. const res = yield util.transform(cliOptions.filename, code, (0, _defaults().default)({
  139. sourceFileName: "stdin"
  140. }, babelOptions));
  141. output([res]);
  142. });
  143. return _stdin.apply(this, arguments);
  144. }
  145. function walk(_x2) {
  146. return _walk.apply(this, arguments);
  147. }
  148. function _walk() {
  149. _walk = _asyncToGenerator(function* (filenames) {
  150. const _filenames = [];
  151. filenames.forEach(function (filename) {
  152. if (!_fs().default.existsSync(filename)) return;
  153. const stat = _fs().default.statSync(filename);
  154. if (stat.isDirectory()) {
  155. const dirname = filename;
  156. util.readdirForCompilable(filename, cliOptions.includeDotfiles, cliOptions.extensions).forEach(function (filename) {
  157. _filenames.push(_path().default.join(dirname, filename));
  158. });
  159. } else {
  160. _filenames.push(filename);
  161. }
  162. });
  163. const results = yield Promise.all(_filenames.map(function () {
  164. var _ref2 = _asyncToGenerator(function* (filename) {
  165. let sourceFilename = filename;
  166. if (cliOptions.outFile) {
  167. sourceFilename = _path().default.relative(_path().default.dirname(cliOptions.outFile), sourceFilename);
  168. }
  169. sourceFilename = (0, _slash().default)(sourceFilename);
  170. try {
  171. return yield util.compile(filename, (0, _defaults().default)({
  172. sourceFileName: sourceFilename,
  173. sourceMaps: babelOptions.sourceMaps === "inline" ? true : babelOptions.sourceMaps
  174. }, babelOptions));
  175. } catch (err) {
  176. if (!cliOptions.watch) {
  177. throw err;
  178. }
  179. console.error(err);
  180. return null;
  181. }
  182. });
  183. return function (_x4) {
  184. return _ref2.apply(this, arguments);
  185. };
  186. }()));
  187. output(results);
  188. });
  189. return _walk.apply(this, arguments);
  190. }
  191. function files(_x3) {
  192. return _files.apply(this, arguments);
  193. }
  194. function _files() {
  195. _files = _asyncToGenerator(function* (filenames) {
  196. if (!cliOptions.skipInitialBuild) {
  197. yield walk(filenames);
  198. }
  199. if (cliOptions.watch) {
  200. const chokidar = util.requireChokidar();
  201. chokidar.watch(filenames, {
  202. persistent: true,
  203. ignoreInitial: true,
  204. awaitWriteFinish: {
  205. stabilityThreshold: 50,
  206. pollInterval: 10
  207. }
  208. }).on("all", function (type, filename) {
  209. if (!util.isCompilableExtension(filename, cliOptions.extensions)) {
  210. return;
  211. }
  212. if (type === "add" || type === "change") {
  213. if (cliOptions.verbose) {
  214. console.log(type + " " + filename);
  215. }
  216. walk(filenames).catch(err => {
  217. console.error(err);
  218. });
  219. }
  220. });
  221. }
  222. });
  223. return _files.apply(this, arguments);
  224. }
  225. if (cliOptions.filenames.length) {
  226. yield files(cliOptions.filenames);
  227. } else {
  228. yield stdin();
  229. }
  230. });
  231. return _ref.apply(this, arguments);
  232. }