1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = normalizeFile;
|
7 |
|
8 | function _fs() {
|
9 | const data = require("fs");
|
10 |
|
11 | _fs = function () {
|
12 | return data;
|
13 | };
|
14 |
|
15 | return data;
|
16 | }
|
17 |
|
18 | function _path() {
|
19 | const data = require("path");
|
20 |
|
21 | _path = function () {
|
22 | return data;
|
23 | };
|
24 |
|
25 | return data;
|
26 | }
|
27 |
|
28 | function _debug() {
|
29 | const data = require("debug");
|
30 |
|
31 | _debug = function () {
|
32 | return data;
|
33 | };
|
34 |
|
35 | return data;
|
36 | }
|
37 |
|
38 | function _t() {
|
39 | const data = require("@babel/types");
|
40 |
|
41 | _t = function () {
|
42 | return data;
|
43 | };
|
44 |
|
45 | return data;
|
46 | }
|
47 |
|
48 | function _convertSourceMap() {
|
49 | const data = require("convert-source-map");
|
50 |
|
51 | _convertSourceMap = function () {
|
52 | return data;
|
53 | };
|
54 |
|
55 | return data;
|
56 | }
|
57 |
|
58 | var _file = require("./file/file");
|
59 |
|
60 | var _parser = require("../parser");
|
61 |
|
62 | var _cloneDeep = require("./util/clone-deep");
|
63 |
|
64 | const {
|
65 | file,
|
66 | traverseFast
|
67 | } = _t();
|
68 |
|
69 | const debug = _debug()("babel:transform:file");
|
70 |
|
71 | const LARGE_INPUT_SOURCEMAP_THRESHOLD = 3000000;
|
72 |
|
73 | function* 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: ast,
|
135 | inputMap
|
136 | });
|
137 | }
|
138 |
|
139 | const INLINE_SOURCEMAP_REGEX = /^[@#]\s+sourceMappingURL=data:(?:application|text)\/json;(?:charset[:=]\S+?;)?base64,(?:.*)$/;
|
140 | const EXTERNAL_SOURCEMAP_REGEX = /^[@#][ \t]+sourceMappingURL=([^\s'"`]+)[ \t]*$/;
|
141 |
|
142 | function 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 |
|
159 | function 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 | }
|
168 |
|
169 | 0 && 0; |
\ | No newline at end of file |