dir.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. function _defaults() {
  7. const data = _interopRequireDefault(require("lodash/defaults"));
  8. _defaults = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _outputFileSync() {
  14. const data = _interopRequireDefault(require("output-file-sync"));
  15. _outputFileSync = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _mkdirp() {
  21. const data = require("mkdirp");
  22. _mkdirp = 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. const filenames = cliOptions.filenames;
  62. function write(_x2, _x3) {
  63. return _write.apply(this, arguments);
  64. }
  65. function _write() {
  66. _write = _asyncToGenerator(function* (src, base) {
  67. let relative = _path().default.relative(base, src);
  68. if (!util.isCompilableExtension(relative, cliOptions.extensions)) {
  69. return false;
  70. }
  71. relative = util.adjustRelative(relative, cliOptions.keepFileExtension);
  72. const dest = getDest(relative, base);
  73. try {
  74. const res = yield util.compile(src, (0, _defaults().default)({
  75. sourceFileName: (0, _slash().default)(_path().default.relative(dest + "/..", src))
  76. }, babelOptions));
  77. if (!res) return false;
  78. if (res.map && babelOptions.sourceMaps && babelOptions.sourceMaps !== "inline") {
  79. const mapLoc = dest + ".map";
  80. res.code = util.addSourceMappingUrl(res.code, mapLoc);
  81. res.map.file = _path().default.basename(relative);
  82. (0, _outputFileSync().default)(mapLoc, JSON.stringify(res.map));
  83. }
  84. (0, _outputFileSync().default)(dest, res.code);
  85. util.chmod(src, dest);
  86. if (cliOptions.verbose) {
  87. console.log(src + " -> " + dest);
  88. }
  89. return true;
  90. } catch (err) {
  91. if (cliOptions.watch) {
  92. console.error(err);
  93. return false;
  94. }
  95. throw err;
  96. }
  97. });
  98. return _write.apply(this, arguments);
  99. }
  100. function getDest(filename, base) {
  101. if (cliOptions.relative) {
  102. return _path().default.join(base, cliOptions.outDir, filename);
  103. }
  104. return _path().default.join(cliOptions.outDir, filename);
  105. }
  106. function handleFile(_x4, _x5) {
  107. return _handleFile.apply(this, arguments);
  108. }
  109. function _handleFile() {
  110. _handleFile = _asyncToGenerator(function* (src, base) {
  111. const written = yield write(src, base);
  112. if (!written && cliOptions.copyFiles) {
  113. const filename = _path().default.relative(base, src);
  114. const dest = getDest(filename, base);
  115. (0, _outputFileSync().default)(dest, _fs().default.readFileSync(src));
  116. util.chmod(src, dest);
  117. }
  118. return written;
  119. });
  120. return _handleFile.apply(this, arguments);
  121. }
  122. function handle(_x6) {
  123. return _handle.apply(this, arguments);
  124. }
  125. function _handle() {
  126. _handle = _asyncToGenerator(function* (filenameOrDir) {
  127. if (!_fs().default.existsSync(filenameOrDir)) return 0;
  128. const stat = _fs().default.statSync(filenameOrDir);
  129. if (stat.isDirectory()) {
  130. const dirname = filenameOrDir;
  131. let count = 0;
  132. const files = util.readdir(dirname, cliOptions.includeDotfiles);
  133. for (const filename of files) {
  134. const src = _path().default.join(dirname, filename);
  135. const written = yield handleFile(src, dirname);
  136. if (written) count += 1;
  137. }
  138. return count;
  139. } else {
  140. const filename = filenameOrDir;
  141. const written = yield handleFile(filename, _path().default.dirname(filename));
  142. return written ? 1 : 0;
  143. }
  144. });
  145. return _handle.apply(this, arguments);
  146. }
  147. if (!cliOptions.skipInitialBuild) {
  148. if (cliOptions.deleteDirOnStart) {
  149. util.deleteDir(cliOptions.outDir);
  150. }
  151. (0, _mkdirp().sync)(cliOptions.outDir);
  152. let compiledFiles = 0;
  153. for (const filename of cliOptions.filenames) {
  154. compiledFiles += yield handle(filename);
  155. }
  156. console.log(`Successfully compiled ${compiledFiles} ${compiledFiles !== 1 ? "files" : "file"} with Babel.`);
  157. }
  158. if (cliOptions.watch) {
  159. const chokidar = util.requireChokidar();
  160. filenames.forEach(function (filenameOrDir) {
  161. const watcher = chokidar.watch(filenameOrDir, {
  162. persistent: true,
  163. ignoreInitial: true,
  164. awaitWriteFinish: {
  165. stabilityThreshold: 50,
  166. pollInterval: 10
  167. }
  168. });
  169. ["add", "change"].forEach(function (type) {
  170. watcher.on(type, function (filename) {
  171. handleFile(filename, filename === filenameOrDir ? _path().default.dirname(filenameOrDir) : filenameOrDir).catch(err => {
  172. console.error(err);
  173. });
  174. });
  175. });
  176. });
  177. }
  178. });
  179. return _ref.apply(this, arguments);
  180. }