Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle "simple" (missing origin) source mappings in minifier output #928

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -8,10 +8,6 @@ Array [
"line": 1,
},
"name": null,
"original": Object {
"column": null,
"line": null,
},
"source": null,
},
Object {
Expand Down Expand Up @@ -56,10 +52,6 @@ Array [
"line": 12,
},
"name": null,
"original": Object {
"column": null,
"line": null,
},
"source": null,
},
Object {
Expand All @@ -80,10 +72,6 @@ Array [
"line": 25,
},
"name": null,
"original": Object {
"column": null,
"line": null,
},
"source": null,
},
Object {
Expand Down
59 changes: 42 additions & 17 deletions packages/metro-source-map/src/source-map.js
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @flow strict-local
* @format
* @oncall react_native
*/
Expand All @@ -21,6 +21,7 @@ const Consumer = require('./Consumer');
const normalizeSourcePath = require('./Consumer/normalizeSourcePath');
const {generateFunctionMap} = require('./generateFunctionMap');
const Generator = require('./Generator');
// $FlowFixMe[untyped-import] - source-map
const SourceMap = require('source-map');

export type {IConsumer};
Expand Down Expand Up @@ -85,6 +86,15 @@ export type IndexMap = {

export type MixedSourceMap = IndexMap | BasicSourceMap;

type SourceMapConsumerMapping = {
generatedLine: number,
generatedColumn: number,
originalLine: ?number,
originalColumn: ?number,
source: ?string,
name: ?string,
};

function fromRawMappingsImpl(
isBlocking: boolean,
onDone: Generator => void,
Expand Down Expand Up @@ -205,20 +215,33 @@ function toBabelSegments(
): Array<BabelSourceMapSegment> {
const rawMappings: Array<BabelSourceMapSegment> = [];

new SourceMap.SourceMapConsumer(sourceMap).eachMapping(map => {
rawMappings.push({
generated: {
line: map.generatedLine,
column: map.generatedColumn,
},
original: {
line: map.originalLine,
column: map.originalColumn,
},
source: map.source,
name: map.name,
});
});
new SourceMap.SourceMapConsumer(sourceMap).eachMapping(
(map: SourceMapConsumerMapping) => {
rawMappings.push(
map.originalLine == null || map.originalColumn == null
? {
generated: {
line: map.generatedLine,
column: map.generatedColumn,
},
source: map.source,
name: map.name,
}
: {
generated: {
line: map.generatedLine,
column: map.generatedColumn,
},
original: {
line: map.originalLine,
column: map.originalColumn,
},
source: map.source,
name: map.name,
},
);
},
);

return rawMappings;
}
Expand Down Expand Up @@ -274,11 +297,13 @@ function addMapping(
if (n === 2) {
generator.addSimpleMapping(line, column);
} else if (n === 4) {
const sourceMap: SourceMapping = (mapping: any);
// $FlowIssue[invalid-tuple-arity] Arity is ensured by conidition on length
const sourceMap: SourceMapping = mapping;

generator.addSourceMapping(line, column, sourceMap[2], sourceMap[3]);
} else if (n === 5) {
const sourceMap: SourceMappingWithName = (mapping: any);
// $FlowIssue[invalid-tuple-arity] Arity is ensured by conidition on length
const sourceMap: SourceMappingWithName = mapping;

generator.addNamedSourceMapping(
line,
Expand Down