UNPKG

3.8 kBJavaScriptView Raw
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 {
65 file,
66 traverseFast
67} = _t();
68
69const debug = _debug()("babel:transform:file");
70
71const LARGE_INPUT_SOURCEMAP_THRESHOLD = 1000000;
72
73function* normalizeFile(pluginPasses, options, code, ast) {
74 code = `${code || ""}`;
75
76 if (ast) {
77 if (ast.type === "Program") {
78 ast = file(ast, [], []);
79 } else if (ast.type !== "File") {
80 throw new Error("AST root must be a Program or File node");
81 }
82
83 if (options.cloneInputAst) {
84 ast = (0, _cloneDeep.default)(ast);
85 }
86 } else {
87 ast = yield* (0, _parser.default)(pluginPasses, options, code);
88 }
89
90 let inputMap = null;
91
92 if (options.inputSourceMap !== false) {
93 if (typeof options.inputSourceMap === "object") {
94 inputMap = _convertSourceMap().fromObject(options.inputSourceMap);
95 }
96
97 if (!inputMap) {
98 const lastComment = extractComments(INLINE_SOURCEMAP_REGEX, ast);
99
100 if (lastComment) {
101 try {
102 inputMap = _convertSourceMap().fromComment(lastComment);
103 } catch (err) {
104 debug("discarding unknown inline input sourcemap", err);
105 }
106 }
107 }
108
109 if (!inputMap) {
110 const lastComment = extractComments(EXTERNAL_SOURCEMAP_REGEX, ast);
111
112 if (typeof options.filename === "string" && lastComment) {
113 try {
114 const match = EXTERNAL_SOURCEMAP_REGEX.exec(lastComment);
115
116 const inputMapContent = _fs().readFileSync(_path().resolve(_path().dirname(options.filename), match[1]));
117
118 if (inputMapContent.length > LARGE_INPUT_SOURCEMAP_THRESHOLD) {
119 debug("skip merging input map > 1 MB");
120 } else {
121 inputMap = _convertSourceMap().fromJSON(inputMapContent);
122 }
123 } catch (err) {
124 debug("discarding unknown file input sourcemap", err);
125 }
126 } else if (lastComment) {
127 debug("discarding un-loadable file input sourcemap");
128 }
129 }
130 }
131
132 return new _file.default(options, {
133 code,
134 ast,
135 inputMap
136 });
137}
138
139const INLINE_SOURCEMAP_REGEX = /^[@#]\s+sourceMappingURL=data:(?:application|text)\/json;(?:charset[:=]\S+?;)?base64,(?:.*)$/;
140const EXTERNAL_SOURCEMAP_REGEX = /^[@#][ \t]+sourceMappingURL=([^\s'"`]+)[ \t]*$/;
141
142function extractCommentsFromList(regex, comments, lastComment) {
143 if (comments) {
144 comments = comments.filter(({
145 value
146 }) => {
147 if (regex.test(value)) {
148 lastComment = value;
149 return false;
150 }
151
152 return true;
153 });
154 }
155
156 return [comments, lastComment];
157}
158
159function extractComments(regex, ast) {
160 let lastComment = null;
161 traverseFast(ast, node => {
162 [node.leadingComments, lastComment] = extractCommentsFromList(regex, node.leadingComments, lastComment);
163 [node.innerComments, lastComment] = extractCommentsFromList(regex, node.innerComments, lastComment);
164 [node.trailingComments, lastComment] = extractCommentsFromList(regex, node.trailingComments, lastComment);
165 });
166 return lastComment;
167}
\No newline at end of file