metro-source-map 0.76.6 → 0.77.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/src/generateFunctionMap.js +55 -6
- package/src/generateFunctionMap.js.flow +82 -23
- package/src/source-map.js +22 -20
- package/src/source-map.js.flow +22 -22
- package/rn-babelrc.json +0 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "metro-source-map",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.77.0",
|
|
4
4
|
"description": "🚇 Source map generator for Metro.",
|
|
5
5
|
"main": "src/source-map.js",
|
|
6
6
|
"repository": {
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
"@babel/traverse": "^7.20.0",
|
|
16
16
|
"@babel/types": "^7.20.0",
|
|
17
17
|
"invariant": "^2.2.4",
|
|
18
|
-
"metro-symbolicate": "0.
|
|
18
|
+
"metro-symbolicate": "0.77.0",
|
|
19
19
|
"nullthrows": "^1.1.1",
|
|
20
|
-
"ob1": "0.
|
|
20
|
+
"ob1": "0.77.0",
|
|
21
21
|
"source-map": "^0.5.6",
|
|
22
22
|
"vlq": "^1.0.0"
|
|
23
23
|
},
|
|
@@ -28,6 +28,6 @@
|
|
|
28
28
|
"terser": "^5.15.0"
|
|
29
29
|
},
|
|
30
30
|
"engines": {
|
|
31
|
-
"node": ">=
|
|
31
|
+
"node": ">=18"
|
|
32
32
|
}
|
|
33
33
|
}
|
|
@@ -18,6 +18,7 @@ function _interopRequireDefault(obj) {
|
|
|
18
18
|
}
|
|
19
19
|
const B64Builder = require("./B64Builder");
|
|
20
20
|
const t = require("@babel/types");
|
|
21
|
+
const invariant = require("invariant");
|
|
21
22
|
const nullthrows = require("nullthrows");
|
|
22
23
|
const fsPath = require("path");
|
|
23
24
|
/**
|
|
@@ -47,12 +48,51 @@ function generateFunctionMappingsArray(ast, context) {
|
|
|
47
48
|
});
|
|
48
49
|
return mappings;
|
|
49
50
|
}
|
|
51
|
+
function functionMapBabelPlugin() {
|
|
52
|
+
return {
|
|
53
|
+
// Eagerly traverse the tree on `pre`, before any visitors have run, so
|
|
54
|
+
// that regardless of plugin order we're dealing with the AST before any
|
|
55
|
+
// mutations.
|
|
56
|
+
visitor: {},
|
|
57
|
+
pre: ({ path, metadata, opts }) => {
|
|
58
|
+
const { filename } = nullthrows(opts);
|
|
59
|
+
const encoder = new MappingEncoder();
|
|
60
|
+
const visitor = getFunctionMapVisitor(
|
|
61
|
+
{
|
|
62
|
+
filename,
|
|
63
|
+
},
|
|
64
|
+
(mapping) => encoder.push(mapping)
|
|
65
|
+
);
|
|
66
|
+
invariant(
|
|
67
|
+
path && t.isProgram(path.node),
|
|
68
|
+
"path missing or not a program node"
|
|
69
|
+
);
|
|
70
|
+
// $FlowFixMe[prop-missing] checked above
|
|
71
|
+
// $FlowFixMe[incompatible-type-arg] checked above
|
|
72
|
+
const programPath = path;
|
|
73
|
+
visitor.enter(programPath);
|
|
74
|
+
programPath.traverse({
|
|
75
|
+
Function: visitor,
|
|
76
|
+
Class: visitor,
|
|
77
|
+
});
|
|
78
|
+
visitor.exit(programPath);
|
|
50
79
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
80
|
+
// $FlowFixMe[prop-missing] Babel `File` is not generically typed
|
|
81
|
+
const metroMetadata = metadata;
|
|
82
|
+
const functionMap = encoder.getResult();
|
|
83
|
+
|
|
84
|
+
// Set the result on a metadata property
|
|
85
|
+
if (!metroMetadata.metro) {
|
|
86
|
+
metroMetadata.metro = {
|
|
87
|
+
functionMap,
|
|
88
|
+
};
|
|
89
|
+
} else {
|
|
90
|
+
metroMetadata.metro.functionMap = functionMap;
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function getFunctionMapVisitor(context, pushMapping) {
|
|
56
96
|
const nameStack = [];
|
|
57
97
|
let tailPos = {
|
|
58
98
|
line: 1,
|
|
@@ -96,7 +136,7 @@ function forEachMapping(ast, context, pushMapping) {
|
|
|
96
136
|
const basename = context.filename
|
|
97
137
|
? fsPath.basename(context.filename).replace(/\..+$/, "")
|
|
98
138
|
: null;
|
|
99
|
-
|
|
139
|
+
return {
|
|
100
140
|
enter(path) {
|
|
101
141
|
let name = getNameForPath(path);
|
|
102
142
|
if (basename) {
|
|
@@ -108,6 +148,14 @@ function forEachMapping(ast, context, pushMapping) {
|
|
|
108
148
|
popFrame();
|
|
109
149
|
},
|
|
110
150
|
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Traverses a Babel AST and calls the supplied callback with function name
|
|
155
|
+
* mappings, one at a time.
|
|
156
|
+
*/
|
|
157
|
+
function forEachMapping(ast, context, pushMapping) {
|
|
158
|
+
const visitor = getFunctionMapVisitor(context, pushMapping);
|
|
111
159
|
|
|
112
160
|
// Traversing populates/pollutes the path cache (`traverse.cache.path`) with
|
|
113
161
|
// values missing the `hub` property needed by Babel transformation, so we
|
|
@@ -440,6 +488,7 @@ function positionGreater(x, y) {
|
|
|
440
488
|
return x.line > y.line || (x.line === y.line && x.column > y.column);
|
|
441
489
|
}
|
|
442
490
|
module.exports = {
|
|
491
|
+
functionMapBabelPlugin,
|
|
443
492
|
generateFunctionMap,
|
|
444
493
|
generateFunctionMappingsArray,
|
|
445
494
|
};
|
|
@@ -11,7 +11,9 @@
|
|
|
11
11
|
|
|
12
12
|
'use strict';
|
|
13
13
|
|
|
14
|
+
import type {MetroBabelFileMetadata} from 'metro-babel-transformer';
|
|
14
15
|
import type {FBSourceFunctionMap} from './source-map';
|
|
16
|
+
import type {PluginObj} from '@babel/core';
|
|
15
17
|
import type {NodePath} from '@babel/traverse';
|
|
16
18
|
import type {Node} from '@babel/types';
|
|
17
19
|
|
|
@@ -42,6 +44,7 @@ import {
|
|
|
42
44
|
|
|
43
45
|
const B64Builder = require('./B64Builder');
|
|
44
46
|
const t = require('@babel/types');
|
|
47
|
+
const invariant = require('invariant');
|
|
45
48
|
const nullthrows = require('nullthrows');
|
|
46
49
|
const fsPath = require('path');
|
|
47
50
|
|
|
@@ -55,7 +58,21 @@ type RangeMapping = {
|
|
|
55
58
|
start: Position,
|
|
56
59
|
...
|
|
57
60
|
};
|
|
58
|
-
|
|
61
|
+
type FunctionMapVisitor = {
|
|
62
|
+
enter: (
|
|
63
|
+
path:
|
|
64
|
+
| NodePath<BabelNodeProgram>
|
|
65
|
+
| NodePath<BabelNodeFunction>
|
|
66
|
+
| NodePath<BabelNodeClass>,
|
|
67
|
+
) => void,
|
|
68
|
+
exit: (
|
|
69
|
+
path:
|
|
70
|
+
| NodePath<BabelNodeProgram>
|
|
71
|
+
| NodePath<BabelNodeFunction>
|
|
72
|
+
| NodePath<BabelNodeClass>,
|
|
73
|
+
) => void,
|
|
74
|
+
};
|
|
75
|
+
export type Context = {filename?: ?string, ...};
|
|
59
76
|
|
|
60
77
|
/**
|
|
61
78
|
* Generate a map of source positions to function names. The names are meant to
|
|
@@ -91,15 +108,52 @@ function generateFunctionMappingsArray(
|
|
|
91
108
|
return mappings;
|
|
92
109
|
}
|
|
93
110
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
111
|
+
function functionMapBabelPlugin(): PluginObj<> {
|
|
112
|
+
return {
|
|
113
|
+
// Eagerly traverse the tree on `pre`, before any visitors have run, so
|
|
114
|
+
// that regardless of plugin order we're dealing with the AST before any
|
|
115
|
+
// mutations.
|
|
116
|
+
visitor: {},
|
|
117
|
+
pre: ({path, metadata, opts}) => {
|
|
118
|
+
const {filename} = nullthrows(opts);
|
|
119
|
+
const encoder = new MappingEncoder();
|
|
120
|
+
const visitor = getFunctionMapVisitor({filename}, mapping =>
|
|
121
|
+
encoder.push(mapping),
|
|
122
|
+
);
|
|
123
|
+
invariant(
|
|
124
|
+
path && t.isProgram(path.node),
|
|
125
|
+
'path missing or not a program node',
|
|
126
|
+
);
|
|
127
|
+
// $FlowFixMe[prop-missing] checked above
|
|
128
|
+
// $FlowFixMe[incompatible-type-arg] checked above
|
|
129
|
+
const programPath: NodePath<BabelNodeProgram> = path;
|
|
130
|
+
|
|
131
|
+
visitor.enter(programPath);
|
|
132
|
+
programPath.traverse({
|
|
133
|
+
Function: visitor,
|
|
134
|
+
Class: visitor,
|
|
135
|
+
});
|
|
136
|
+
visitor.exit(programPath);
|
|
137
|
+
|
|
138
|
+
// $FlowFixMe[prop-missing] Babel `File` is not generically typed
|
|
139
|
+
const metroMetadata: MetroBabelFileMetadata = metadata;
|
|
140
|
+
|
|
141
|
+
const functionMap = encoder.getResult();
|
|
142
|
+
|
|
143
|
+
// Set the result on a metadata property
|
|
144
|
+
if (!metroMetadata.metro) {
|
|
145
|
+
metroMetadata.metro = {functionMap};
|
|
146
|
+
} else {
|
|
147
|
+
metroMetadata.metro.functionMap = functionMap;
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function getFunctionMapVisitor(
|
|
100
154
|
context: ?Context,
|
|
101
155
|
pushMapping: RangeMapping => void,
|
|
102
|
-
) {
|
|
156
|
+
): FunctionMapVisitor {
|
|
103
157
|
const nameStack: Array<{loc: BabelNodeSourceLocation, name: string}> = [];
|
|
104
158
|
let tailPos = {line: 1, column: 0};
|
|
105
159
|
let tailName = null;
|
|
@@ -140,30 +194,31 @@ function forEachMapping(
|
|
|
140
194
|
? fsPath.basename(context.filename).replace(/\..+$/, '')
|
|
141
195
|
: null;
|
|
142
196
|
|
|
143
|
-
|
|
144
|
-
enter(
|
|
145
|
-
path:
|
|
146
|
-
| NodePath<BabelNodeProgram>
|
|
147
|
-
| NodePath<BabelNodeFunction>
|
|
148
|
-
| NodePath<BabelNodeClass>,
|
|
149
|
-
) {
|
|
197
|
+
return {
|
|
198
|
+
enter(path) {
|
|
150
199
|
let name = getNameForPath(path);
|
|
151
200
|
if (basename) {
|
|
152
201
|
name = removeNamePrefix(name, basename);
|
|
153
202
|
}
|
|
154
|
-
|
|
155
203
|
pushFrame(name, nullthrows(path.node.loc));
|
|
156
204
|
},
|
|
157
205
|
|
|
158
|
-
exit(
|
|
159
|
-
path:
|
|
160
|
-
| NodePath<BabelNodeProgram>
|
|
161
|
-
| NodePath<BabelNodeFunction>
|
|
162
|
-
| NodePath<BabelNodeClass>,
|
|
163
|
-
): void {
|
|
206
|
+
exit(path) {
|
|
164
207
|
popFrame();
|
|
165
208
|
},
|
|
166
209
|
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Traverses a Babel AST and calls the supplied callback with function name
|
|
214
|
+
* mappings, one at a time.
|
|
215
|
+
*/
|
|
216
|
+
function forEachMapping(
|
|
217
|
+
ast: BabelNode,
|
|
218
|
+
context: ?Context,
|
|
219
|
+
pushMapping: RangeMapping => void,
|
|
220
|
+
) {
|
|
221
|
+
const visitor = getFunctionMapVisitor(context, pushMapping);
|
|
167
222
|
|
|
168
223
|
// Traversing populates/pollutes the path cache (`traverse.cache.path`) with
|
|
169
224
|
// values missing the `hub` property needed by Babel transformation, so we
|
|
@@ -525,4 +580,8 @@ function positionGreater(x: Position, y: Position) {
|
|
|
525
580
|
return x.line > y.line || (x.line === y.line && x.column > y.column);
|
|
526
581
|
}
|
|
527
582
|
|
|
528
|
-
module.exports = {
|
|
583
|
+
module.exports = {
|
|
584
|
+
functionMapBabelPlugin,
|
|
585
|
+
generateFunctionMap,
|
|
586
|
+
generateFunctionMappingsArray,
|
|
587
|
+
};
|
package/src/source-map.js
CHANGED
|
@@ -16,7 +16,10 @@ const composeSourceMaps = require("./composeSourceMaps");
|
|
|
16
16
|
const Consumer = require("./Consumer");
|
|
17
17
|
// We need to export this for `metro-symbolicate`
|
|
18
18
|
const normalizeSourcePath = require("./Consumer/normalizeSourcePath");
|
|
19
|
-
const {
|
|
19
|
+
const {
|
|
20
|
+
functionMapBabelPlugin,
|
|
21
|
+
generateFunctionMap,
|
|
22
|
+
} = require("./generateFunctionMap");
|
|
20
23
|
const Generator = require("./Generator");
|
|
21
24
|
// $FlowFixMe[untyped-import] - source-map
|
|
22
25
|
const SourceMap = require("source-map");
|
|
@@ -148,29 +151,27 @@ function addMappingsForFile(generator, mappings, module, carryOver) {
|
|
|
148
151
|
generator.endFile();
|
|
149
152
|
}
|
|
150
153
|
function addMapping(generator, mapping, carryOver) {
|
|
151
|
-
const n = mapping.length;
|
|
152
154
|
const line = mapping[0] + carryOver;
|
|
153
155
|
// lines start at 1, columns start at 0
|
|
154
156
|
const column = mapping[1];
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
} else {
|
|
172
|
-
throw new Error(`Invalid mapping: [${mapping.join(", ")}]`);
|
|
157
|
+
switch (mapping.length) {
|
|
158
|
+
case 2:
|
|
159
|
+
generator.addSimpleMapping(line, column);
|
|
160
|
+
return;
|
|
161
|
+
case 4:
|
|
162
|
+
generator.addSourceMapping(line, column, mapping[2], mapping[3]);
|
|
163
|
+
return;
|
|
164
|
+
case 5:
|
|
165
|
+
generator.addNamedSourceMapping(
|
|
166
|
+
line,
|
|
167
|
+
column,
|
|
168
|
+
mapping[2],
|
|
169
|
+
mapping[3],
|
|
170
|
+
mapping[4]
|
|
171
|
+
);
|
|
172
|
+
return;
|
|
173
173
|
}
|
|
174
|
+
throw new Error(`Invalid mapping: [${mapping.join(", ")}]`);
|
|
174
175
|
}
|
|
175
176
|
const newline = /\r\n?|\n|\u2028|\u2029/g;
|
|
176
177
|
const countLines = (string) => (string.match(newline) || []).length + 1;
|
|
@@ -182,6 +183,7 @@ module.exports = {
|
|
|
182
183
|
generateFunctionMap,
|
|
183
184
|
fromRawMappings,
|
|
184
185
|
fromRawMappingsNonBlocking,
|
|
186
|
+
functionMapBabelPlugin,
|
|
185
187
|
normalizeSourcePath,
|
|
186
188
|
toBabelSegments,
|
|
187
189
|
toSegmentTuple,
|
package/src/source-map.js.flow
CHANGED
|
@@ -19,7 +19,10 @@ const composeSourceMaps = require('./composeSourceMaps');
|
|
|
19
19
|
const Consumer = require('./Consumer');
|
|
20
20
|
// We need to export this for `metro-symbolicate`
|
|
21
21
|
const normalizeSourcePath = require('./Consumer/normalizeSourcePath');
|
|
22
|
-
const {
|
|
22
|
+
const {
|
|
23
|
+
functionMapBabelPlugin,
|
|
24
|
+
generateFunctionMap,
|
|
25
|
+
} = require('./generateFunctionMap');
|
|
23
26
|
const Generator = require('./Generator');
|
|
24
27
|
// $FlowFixMe[untyped-import] - source-map
|
|
25
28
|
const SourceMap = require('source-map');
|
|
@@ -298,31 +301,27 @@ function addMapping(
|
|
|
298
301
|
mapping: MetroSourceMapSegmentTuple,
|
|
299
302
|
carryOver: number,
|
|
300
303
|
) {
|
|
301
|
-
const n = mapping.length;
|
|
302
304
|
const line = mapping[0] + carryOver;
|
|
303
305
|
// lines start at 1, columns start at 0
|
|
304
306
|
const column = mapping[1];
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
sourceMap[4],
|
|
322
|
-
);
|
|
323
|
-
} else {
|
|
324
|
-
throw new Error(`Invalid mapping: [${mapping.join(', ')}]`);
|
|
307
|
+
switch (mapping.length) {
|
|
308
|
+
case 2:
|
|
309
|
+
generator.addSimpleMapping(line, column);
|
|
310
|
+
return;
|
|
311
|
+
case 4:
|
|
312
|
+
generator.addSourceMapping(line, column, mapping[2], mapping[3]);
|
|
313
|
+
return;
|
|
314
|
+
case 5:
|
|
315
|
+
generator.addNamedSourceMapping(
|
|
316
|
+
line,
|
|
317
|
+
column,
|
|
318
|
+
mapping[2],
|
|
319
|
+
mapping[3],
|
|
320
|
+
mapping[4],
|
|
321
|
+
);
|
|
322
|
+
return;
|
|
325
323
|
}
|
|
324
|
+
throw new Error(`Invalid mapping: [${mapping.join(', ')}]`);
|
|
326
325
|
}
|
|
327
326
|
|
|
328
327
|
const newline = /\r\n?|\n|\u2028|\u2029/g;
|
|
@@ -338,6 +337,7 @@ module.exports = {
|
|
|
338
337
|
generateFunctionMap,
|
|
339
338
|
fromRawMappings,
|
|
340
339
|
fromRawMappingsNonBlocking,
|
|
340
|
+
functionMapBabelPlugin,
|
|
341
341
|
normalizeSourcePath,
|
|
342
342
|
toBabelSegments,
|
|
343
343
|
toSegmentTuple,
|
package/rn-babelrc.json
DELETED