59bf9e429bec55feb6e76bdeb649b69a07c3299173166dd4293ca08afb88a7a54f18f7955bb58a549ab5a645e6761fd9307e0a5f164a5aa2450301bfaf75ff 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = normalizeFile;
  6. function _fs() {
  7. const data = require("fs");
  8. _fs = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _path() {
  14. const data = require("path");
  15. _path = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _debug() {
  21. const data = require("debug");
  22. _debug = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _t() {
  28. const data = require("@babel/types");
  29. _t = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _convertSourceMap() {
  35. const data = require("convert-source-map");
  36. _convertSourceMap = function () {
  37. return data;
  38. };
  39. return data;
  40. }
  41. var _file = require("./file/file.js");
  42. var _index = require("../parser/index.js");
  43. var _cloneDeep = require("./util/clone-deep.js");
  44. const {
  45. file,
  46. traverseFast
  47. } = _t();
  48. const debug = _debug()("babel:transform:file");
  49. const INLINE_SOURCEMAP_REGEX = /^[@#]\s+sourceMappingURL=data:(?:application|text)\/json;(?:charset[:=]\S+?;)?base64,.*$/;
  50. const EXTERNAL_SOURCEMAP_REGEX = /^[@#][ \t]+sourceMappingURL=([^\s'"`]+)[ \t]*$/;
  51. function* normalizeFile(pluginPasses, options, code, ast) {
  52. code = `${code || ""}`;
  53. if (ast) {
  54. if (ast.type === "Program") {
  55. ast = file(ast, [], []);
  56. } else if (ast.type !== "File") {
  57. throw new Error("AST root must be a Program or File node");
  58. }
  59. if (options.cloneInputAst) {
  60. ast = (0, _cloneDeep.default)(ast);
  61. }
  62. } else {
  63. ast = yield* (0, _index.default)(pluginPasses, options, code);
  64. }
  65. let inputMap = null;
  66. if (options.inputSourceMap !== false) {
  67. if (typeof options.inputSourceMap === "object") {
  68. inputMap = _convertSourceMap().fromObject(options.inputSourceMap);
  69. }
  70. if (!inputMap) {
  71. const lastComment = extractComments(INLINE_SOURCEMAP_REGEX, ast);
  72. if (lastComment) {
  73. try {
  74. inputMap = _convertSourceMap().fromComment("//" + lastComment);
  75. } catch (err) {
  76. {
  77. debug("discarding unknown inline input sourcemap");
  78. }
  79. }
  80. }
  81. }
  82. if (!inputMap) {
  83. const lastComment = extractComments(EXTERNAL_SOURCEMAP_REGEX, ast);
  84. if (typeof options.filename === "string" && lastComment) {
  85. try {
  86. const match = EXTERNAL_SOURCEMAP_REGEX.exec(lastComment);
  87. const inputMapContent = _fs().readFileSync(_path().resolve(_path().dirname(options.filename), match[1]), "utf8");
  88. inputMap = _convertSourceMap().fromJSON(inputMapContent);
  89. } catch (err) {
  90. debug("discarding unknown file input sourcemap", err);
  91. }
  92. } else if (lastComment) {
  93. debug("discarding un-loadable file input sourcemap");
  94. }
  95. }
  96. }
  97. return new _file.default(options, {
  98. code,
  99. ast: ast,
  100. inputMap
  101. });
  102. }
  103. function extractCommentsFromList(regex, comments, lastComment) {
  104. if (comments) {
  105. comments = comments.filter(({
  106. value
  107. }) => {
  108. if (regex.test(value)) {
  109. lastComment = value;
  110. return false;
  111. }
  112. return true;
  113. });
  114. }
  115. return [comments, lastComment];
  116. }
  117. function extractComments(regex, ast) {
  118. let lastComment = null;
  119. traverseFast(ast, node => {
  120. [node.leadingComments, lastComment] = extractCommentsFromList(regex, node.leadingComments, lastComment);
  121. [node.innerComments, lastComment] = extractCommentsFromList(regex, node.innerComments, lastComment);
  122. [node.trailingComments, lastComment] = extractCommentsFromList(regex, node.trailingComments, lastComment);
  123. });
  124. return lastComment;
  125. }
  126. 0 && 0;
  127. //# sourceMappingURL=normalize-file.js.map