1"use strict"; 2 3Object.defineProperty(exports, "__esModule", { 4 value: true 5}); 6exports.default = normalizeFile; 7 8function _fs() { 9 const data = require("fs"); 10 11 _fs = function () { 12 return data; 13 }; 14 15 return data; 16} 17 18function _path() { 19 const data = require("path"); 20 21 _path = function () { 22 return data; 23 }; 24 25 return data; 26} 27 28function _debug() { 29 const data = require("debug"); 30 31 _debug = function () { 32 return data; 33 }; 34 35 return data; 36} 37 38function t() { 39 const data = require("@babel/types"); 40 41 t = function () { 42 return data; 43 }; 44 45 return data; 46} 47 48function _convertSourceMap() { 49 const data = require("convert-source-map"); 50 51 _convertSourceMap = function () { 52 return data; 53 }; 54 55 return data; 56} 57 58var _file = require("./file/file"); 59 60var _parser = require("../parser"); 61 62var _cloneDeep = require("./util/clone-deep"); 63 64const debug = _debug()("babel:transform:file"); 65 66const LARGE_INPUT_SOURCEMAP_THRESHOLD = 1000000; 67 68function* normalizeFile(pluginPasses, options, code, ast) { 69 code = `${code || ""}`; 70 71 if (ast) { 72 if (ast.type === "Program") { 73 ast = t().file(ast, [], []); 74 } else if (ast.type !== "File") { 75 throw new Error("AST root must be a Program or File node"); 76 } 77 78 if (options.cloneInputAst) { 79 ast = (0, _cloneDeep.default)(ast); 80 } 81 } else { 82 ast = yield* (0, _parser.default)(pluginPasses, options, code); 83 } 84 85 let inputMap = null; 86 87 if (options.inputSourceMap !== false) { 88 if (typeof options.inputSourceMap === "object") { 89 inputMap = _convertSourceMap().fromObject(options.inputSourceMap); 90 } 91 92 if (!inputMap) { 93 const lastComment = extractComments(INLINE_SOURCEMAP_REGEX, ast); 94 95 if (lastComment) { 96 try { 97 inputMap = _convertSourceMap().fromComment(lastComment); 98 } catch (err) { 99 debug("discarding unknown inline input sourcemap", err); 100 } 101 } 102 } 103 104 if (!inputMap) { 105 const lastComment = extractComments(EXTERNAL_SOURCEMAP_REGEX, ast); 106 107 if (typeof options.filename === "string" && lastComment) { 108 try { 109 const match = EXTERNAL_SOURCEMAP_REGEX.exec(lastComment); 110 111 const inputMapContent = _fs().readFileSync(_path().resolve(_path().dirname(options.filename), match[1])); 112 113 if (inputMapContent.length > LARGE_INPUT_SOURCEMAP_THRESHOLD) { 114 debug("skip merging input map > 1 MB"); 115 } else { 116 inputMap = _convertSourceMap().fromJSON(inputMapContent); 117 } 118 } catch (err) { 119 debug("discarding unknown file input sourcemap", err); 120 } 121 } else if (lastComment) { 122 debug("discarding un-loadable file input sourcemap"); 123 } 124 } 125 } 126 127 return new _file.default(options, { 128 code, 129 ast, 130 inputMap 131 }); 132} 133 134const INLINE_SOURCEMAP_REGEX = /^[@#]\s+sourceMappingURL=data:(?:application|text)\/json;(?:charset[:=]\S+?;)?base64,(?:.*)$/; 135const EXTERNAL_SOURCEMAP_REGEX = /^[@#][ \t]+sourceMappingURL=([^\s'"`]+)[ \t]*$/; 136 137function extractCommentsFromList(regex, comments, lastComment) { 138 if (comments) { 139 comments = comments.filter(({ 140 value 141 }) => { 142 if (regex.test(value)) { 143 lastComment = value; 144 return false; 145 } 146 147 return true; 148 }); 149 } 150 151 return [comments, lastComment]; 152} 153 154function extractComments(regex, ast) { 155 let lastComment = null; 156 t().traverseFast(ast, node => { 157 [node.leadingComments, lastComment] = extractCommentsFromList(regex, node.leadingComments, lastComment); 158 [node.innerComments, lastComment] = extractCommentsFromList(regex, node.innerComments, lastComment); 159 [node.trailingComments, lastComment] = extractCommentsFromList(regex, node.trailingComments, lastComment); 160 }); 161 return lastComment; 162}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.