transform.cjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. "use strict";
  2. function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
  3. function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
  4. const cloneDeep = require("clone-deep");
  5. const path = require("path");
  6. const fs = require("fs");
  7. const babel = require("./babel-core.cjs");
  8. const registerCache = require("./cache.cjs");
  9. const nmRE = escapeRegExp(path.sep + "node_modules" + path.sep);
  10. function escapeRegExp(string) {
  11. return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");
  12. }
  13. let cache;
  14. let transformOpts;
  15. function setOptions(opts) {
  16. if (opts.cache === false && cache) {
  17. registerCache.clear();
  18. cache = null;
  19. } else if (opts.cache !== false && !cache) {
  20. registerCache.load();
  21. cache = registerCache.get();
  22. }
  23. delete opts.cache;
  24. delete opts.extensions;
  25. transformOpts = Object.assign({}, opts, {
  26. caller: Object.assign({
  27. name: "@babel/register"
  28. }, opts.caller || {})
  29. });
  30. let {
  31. cwd = "."
  32. } = transformOpts;
  33. cwd = transformOpts.cwd = path.resolve(cwd);
  34. if (transformOpts.ignore === undefined && transformOpts.only === undefined) {
  35. const cwdRE = escapeRegExp(cwd);
  36. transformOpts.only = [new RegExp("^" + cwdRE, "i")];
  37. transformOpts.ignore = [new RegExp(`^${cwdRE}(?:${path.sep}.*)?${nmRE}`, "i")];
  38. }
  39. }
  40. function transform(_x, _x2) {
  41. return _transform.apply(this, arguments);
  42. }
  43. function _transform() {
  44. _transform = _asyncToGenerator(function* (input, filename) {
  45. const opts = yield babel.loadOptionsAsync(Object.assign({
  46. sourceRoot: path.dirname(filename) + path.sep
  47. }, cloneDeep(transformOpts), {
  48. filename
  49. }));
  50. if (opts === null) return null;
  51. const {
  52. cached,
  53. store
  54. } = cacheLookup(opts, filename);
  55. if (cached) return cached;
  56. const {
  57. code,
  58. map
  59. } = yield babel.transformAsync(input, Object.assign({}, opts, {
  60. sourceMaps: opts.sourceMaps === undefined ? "both" : opts.sourceMaps,
  61. ast: false
  62. }));
  63. return store({
  64. code,
  65. map
  66. });
  67. });
  68. return _transform.apply(this, arguments);
  69. }
  70. module.exports = {
  71. setOptions,
  72. transform
  73. };
  74. {
  75. module.exports.transformSync = function (input, filename) {
  76. const opts = new babel.OptionManager().init(Object.assign({
  77. sourceRoot: path.dirname(filename) + path.sep
  78. }, cloneDeep(transformOpts), {
  79. filename
  80. }));
  81. if (opts === null) return null;
  82. const {
  83. cached,
  84. store
  85. } = cacheLookup(opts, filename);
  86. if (cached) return cached;
  87. const {
  88. code,
  89. map
  90. } = babel.transformSync(input, Object.assign({}, opts, {
  91. sourceMaps: opts.sourceMaps === undefined ? "both" : opts.sourceMaps,
  92. ast: false
  93. }));
  94. return store({
  95. code,
  96. map
  97. });
  98. };
  99. }
  100. const id = value => value;
  101. function cacheLookup(opts, filename) {
  102. if (!cache) return {
  103. cached: null,
  104. store: id
  105. };
  106. let cacheKey = `${JSON.stringify(opts)}:${babel.version}`;
  107. const env = babel.getEnv();
  108. if (env) cacheKey += `:${env}`;
  109. const cached = cache[cacheKey];
  110. const fileMtime = +fs.statSync(filename).mtime;
  111. if (cached && cached.mtime === fileMtime) {
  112. return {
  113. cached: cached.value,
  114. store: id
  115. };
  116. }
  117. return {
  118. cached: null,
  119. store(value) {
  120. cache[cacheKey] = {
  121. value,
  122. mtime: fileMtime
  123. };
  124. registerCache.setDirty();
  125. return value;
  126. }
  127. };
  128. }
  129. //# sourceMappingURL=transform.cjs.map