Unify source map types

Summary: deduplicates / unifies types for source maps across the code base

Reviewed By: jeanlauliac

Differential Revision: D4955924

fbshipit-source-id: 25cb71031dce835dd7d2bc1c27d6b20050906e81
This commit is contained in:
David Aurelio 2017-04-28 12:25:44 -07:00 committed by Facebook Github Bot
parent ff3f15b9ad
commit 2da9c66c78
15 changed files with 74 additions and 88 deletions

View File

@ -20,8 +20,9 @@ const debug = require('debug')('RNP:Bundle');
const invariant = require('fbjs/lib/invariant'); const invariant = require('fbjs/lib/invariant');
const {fromRawMappings} = require('./source-map'); const {fromRawMappings} = require('./source-map');
const {isMappingsMap} = require('../lib/SourceMap');
import type {SourceMap, CombinedSourceMap, MixedSourceMap} from '../lib/SourceMap'; import type {IndexMap, MappingsMap, SourceMap} from '../lib/SourceMap';
import type {GetSourceOptions, FinalizeOptions} from './BundleBase'; import type {GetSourceOptions, FinalizeOptions} from './BundleBase';
export type Unbundle = { export type Unbundle = {
@ -206,7 +207,7 @@ class Bundle extends BundleBase {
* that makes use of of the `sections` field to combine sourcemaps by adding * that makes use of of the `sections` field to combine sourcemaps by adding
* an offset. This is supported only by Chrome for now. * an offset. This is supported only by Chrome for now.
*/ */
_getCombinedSourceMaps(options: {excludeSource?: boolean}): CombinedSourceMap { _getCombinedSourceMaps(options: {excludeSource?: boolean}): IndexMap {
const result = { const result = {
version: 3, version: 3,
file: this._getSourceMapFile(), file: this._getSourceMapFile(),
@ -215,22 +216,22 @@ class Bundle extends BundleBase {
let line = 0; let line = 0;
this.getModules().forEach(module => { this.getModules().forEach(module => {
let map = module.map == null || module.virtual invariant(
!Array.isArray(module.map),
`Unexpected raw mappings for ${module.sourcePath}`,
);
let map: SourceMap = module.map == null || module.virtual
? generateSourceMapForVirtualModule(module) ? generateSourceMapForVirtualModule(module)
: module.map; : module.map;
invariant(
!Array.isArray(map),
`Unexpected raw mappings for ${module.sourcePath}`,
);
if (options.excludeSource && 'sourcesContent' in map) { if (options.excludeSource && isMappingsMap(map)) {
map = {...map, sourcesContent: []}; map = {...map, sourcesContent: []};
} }
result.sections.push({ result.sections.push({
offset: {line, column: 0}, offset: {line, column: 0},
map: (map: MixedSourceMap), map: map,
}); });
line += module.code.split('\n').length; line += module.code.split('\n').length;
}); });
@ -238,7 +239,7 @@ class Bundle extends BundleBase {
return result; return result;
} }
getSourceMap(options: {excludeSource?: boolean}): MixedSourceMap { getSourceMap(options: {excludeSource?: boolean}): SourceMap {
this.assertFinalized(); this.assertFinalized();
return this._sourceMapFormat === 'indexed' return this._sourceMapFormat === 'indexed'
@ -314,7 +315,7 @@ class Bundle extends BundleBase {
} }
} }
function generateSourceMapForVirtualModule(module): SourceMap { function generateSourceMapForVirtualModule(module): MappingsMap {
// All lines map 1-to-1 // All lines map 1-to-1
let mappings = 'AAAA;'; let mappings = 'AAAA;';

View File

@ -13,7 +13,7 @@
const B64Builder = require('./B64Builder'); const B64Builder = require('./B64Builder');
import type {SourceMap} from 'babel-core'; import type {MappingsMap} from '../../lib/SourceMap';
/** /**
* Generates a source map from raw mappings. * Generates a source map from raw mappings.
@ -39,7 +39,7 @@ class Generator {
names: IndexedSet; names: IndexedSet;
source: number; source: number;
sources: Array<string>; sources: Array<string>;
sourcesContent: Array<string>; sourcesContent: Array<?string>;
constructor() { constructor() {
this.builder = new B64Builder(); this.builder = new B64Builder();
@ -141,7 +141,7 @@ class Generator {
/** /**
* Return the source map as object. * Return the source map as object.
*/ */
toMap(file?: string): SourceMap { toMap(file?: string): MappingsMap {
return { return {
version: 3, version: 3,
file, file,

View File

@ -21,7 +21,7 @@ const util = require('util');
const workerFarm = require('worker-farm'); const workerFarm = require('worker-farm');
import type {Data as TransformData, Options as TransformOptions} from './worker/worker'; import type {Data as TransformData, Options as TransformOptions} from './worker/worker';
import type {SourceMap} from '../lib/SourceMap'; import type {MappingsMap} from '../lib/SourceMap';
// Avoid memory leaks caused in workers. This number seems to be a good enough number // Avoid memory leaks caused in workers. This number seems to be a good enough number
// to avoid any memory leak while not slowing down initial builds. // to avoid any memory leak while not slowing down initial builds.
@ -62,8 +62,8 @@ class Transformer {
minify: ( minify: (
filename: string, filename: string,
code: string, code: string,
sourceMap: SourceMap, sourceMap: MappingsMap,
) => Promise<{code: string, map: SourceMap}>; ) => Promise<{code: string, map: MappingsMap}>;
constructor(transformModulePath: string, maxWorkerCount: number) { constructor(transformModulePath: string, maxWorkerCount: number) {
invariant(path.isAbsolute(transformModulePath), 'transform module path should be absolute'); invariant(path.isAbsolute(transformModulePath), 'transform module path should be absolute');

View File

@ -13,7 +13,7 @@
const babel = require('babel-core'); const babel = require('babel-core');
import type {Ast, SourceMap} from 'babel-core'; import type {Ast, SourceMap as MappingsMap} from 'babel-core';
const t = babel.types; const t = babel.types;
const Conditional = { const Conditional = {
@ -73,7 +73,7 @@ const plugin = {
function constantFolding(filename: string, transformResult: { function constantFolding(filename: string, transformResult: {
ast: Ast, ast: Ast,
code?: ?string, code?: ?string,
map: ?SourceMap, map: ?MappingsMap,
}) { }) {
return babel.transformFromAst(transformResult.ast, transformResult.code, { return babel.transformFromAst(transformResult.ast, transformResult.code, {
filename, filename,

View File

@ -14,7 +14,7 @@
const babel = require('babel-core'); const babel = require('babel-core');
const invariant = require('fbjs/lib/invariant'); const invariant = require('fbjs/lib/invariant');
import type {Ast, SourceMap} from 'babel-core'; import type {Ast, SourceMap as MappingsMap} from 'babel-core';
const t = babel.types; const t = babel.types;
const React = {name: 'React'}; const React = {name: 'React'};
@ -164,12 +164,12 @@ function checkRequireArgs(args, dependencyId) {
type AstResult = { type AstResult = {
ast: Ast, ast: Ast,
code: ?string, code: ?string,
map: ?SourceMap, map: ?MappingsMap,
}; };
function inline( function inline(
filename: string, filename: string,
transformResult: {ast?: ?Ast, code: string, map: ?SourceMap}, transformResult: {ast?: ?Ast, code: string, map: ?MappingsMap},
options: {+dev: boolean, +platform: string}, options: {+dev: boolean, +platform: string},
): AstResult { ): AstResult {
const code = transformResult.code; const code = transformResult.code;

View File

@ -18,13 +18,13 @@ const invariant = require('fbjs/lib/invariant');
const minify = require('./minify'); const minify = require('./minify');
import type {LogEntry} from '../../Logger/Types'; import type {LogEntry} from '../../Logger/Types';
import type {Ast, SourceMap} from 'babel-core'; import type {Ast, SourceMap as MappingsMap} from 'babel-core';
export type TransformedCode = { export type TransformedCode = {
code: string, code: string,
dependencies: Array<string>, dependencies: Array<string>,
dependencyOffsets: Array<number>, dependencyOffsets: Array<number>,
map?: ?SourceMap, map?: ?MappingsMap,
}; };
type Transformer = { type Transformer = {
@ -32,7 +32,7 @@ type Transformer = {
filename: string, filename: string,
sourceCode: string, sourceCode: string,
options: ?{}, options: ?{},
) => {ast: ?Ast, code: string, map: ?SourceMap} ) => {ast: ?Ast, code: string, map: ?MappingsMap}
}; };
export type TransformOptions = {| export type TransformOptions = {|

View File

@ -10,37 +10,15 @@
*/ */
'use strict'; 'use strict';
import type {FBSourceMap, IndexMapSection, IndexMap} from '../../lib/SourceMap';
export type {FBSourceMap};
type CreateIndexMapOptions = {| type CreateIndexMapOptions = {|
file?: string, file?: string,
sections?: Array<IndexMapSection> sections?: Array<IndexMapSection>
|}; |};
type IndexMap = MapBase & {
sections: Array<IndexMapSection>,
};
type IndexMapSection = {
map: IndexMap | MappingsMap,
offset: {line: number, column: number},
};
type MapBase = {
// always the first entry in the source map entry object per
// https://fburl.com/source-map-spec#heading=h.qz3o9nc69um5
version: 3,
file?: string,
};
type MappingsMap = MapBase & {
mappings: string,
names: Array<string>,
sourceRoot?: string,
sources: Array<string>,
sourcesContent?: Array<?string>,
};
export type SourceMap = IndexMap | MappingsMap;
exports.createIndexMap = (opts?: CreateIndexMapOptions): IndexMap => ({ exports.createIndexMap = (opts?: CreateIndexMapOptions): IndexMap => ({
version: 3, version: 3,
file: opts && opts.file, file: opts && opts.file,

View File

@ -10,7 +10,7 @@
*/ */
'use strict'; 'use strict';
import type {SourceMap} from './output/source-map'; import type {MappingsMap, SourceMap} from '../lib/SourceMap';
import type {Ast} from 'babel-core'; import type {Ast} from 'babel-core';
import type {Console} from 'console'; import type {Console} from 'console';
@ -102,7 +102,7 @@ type ResolveOptions = {
export type TransformerResult = {| export type TransformerResult = {|
ast: ?Ast, ast: ?Ast,
code: string, code: string,
map: ?SourceMap, map: ?MappingsMap,
|}; |};
export type Transformer = { export type Transformer = {
@ -111,7 +111,7 @@ export type Transformer = {
filename: string, filename: string,
options: ?{}, options: ?{},
plugins?: Array<string | Object | [string | Object, any]>, plugins?: Array<string | Object | [string | Object, any]>,
) => {ast: ?Ast, code: string, map: ?SourceMap} ) => {ast: ?Ast, code: string, map: ?MappingsMap}
}; };
export type TransformResult = {| export type TransformResult = {|

View File

@ -18,14 +18,14 @@ const pathJoin = require('path').join;
import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse'; import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse';
import type Module, {HasteImpl, TransformCode} from '../node-haste/Module'; import type Module, {HasteImpl, TransformCode} from '../node-haste/Module';
import type {SourceMap} from '../lib/SourceMap'; import type {MappingsMap} from '../lib/SourceMap';
import type {Options as JSTransformerOptions} from '../JSTransformer/worker/worker'; import type {Options as JSTransformerOptions} from '../JSTransformer/worker/worker';
import type {Reporter} from '../lib/reporting'; import type {Reporter} from '../lib/reporting';
import type {GetTransformCacheKey} from '../lib/TransformCache'; import type {GetTransformCacheKey} from '../lib/TransformCache';
import type {GlobalTransformCache} from '../lib/GlobalTransformCache'; import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
type MinifyCode = (filePath: string, code: string, map: SourceMap) => type MinifyCode = (filePath: string, code: string, map: MappingsMap) =>
Promise<{code: string, map: SourceMap}>; Promise<{code: string, map: MappingsMap}>;
type ContainsTransformerOptions = {+transformer: JSTransformerOptions} type ContainsTransformerOptions = {+transformer: JSTransformerOptions}
@ -195,7 +195,7 @@ class Resolver {
resolutionResponse: ResolutionResponse<Module, T>, resolutionResponse: ResolutionResponse<Module, T>,
module: Module, module: Module,
name: string, name: string,
map: SourceMap, map: MappingsMap,
code: string, code: string,
meta?: { meta?: {
dependencyOffsets?: Array<number>, dependencyOffsets?: Array<number>,
@ -227,8 +227,8 @@ class Resolver {
} }
minifyModule( minifyModule(
{path, code, map}: {path: string, code: string, map: SourceMap}, {path, code, map}: {path: string, code: string, map: MappingsMap},
): Promise<{code: string, map: SourceMap}> { ): Promise<{code: string, map: MappingsMap}> {
return this._minifyCode(path, code, map); return this._minifyCode(path, code, map);
} }

View File

@ -20,7 +20,7 @@ const {LazyPromise, LockingPromise} = require('./util');
const {fork} = require('child_process'); const {fork} = require('child_process');
export type {SourceMap as SourceMap}; export type {SourceMap as SourceMap};
import type {MixedSourceMap as SourceMap} from '../../lib/SourceMap'; import type {SourceMap} from '../../lib/SourceMap';
export type Stack = Array<{file: string, lineNumber: number, column: number}>; export type Stack = Array<{file: string, lineNumber: number, column: number}>;
export type Symbolicate = export type Symbolicate =

View File

@ -12,9 +12,9 @@
'use strict'; 'use strict';
import type {RawMapping} from '../Bundler/source-map'; import type {RawMapping} from '../Bundler/source-map';
import type {MixedSourceMap} from './SourceMap'; import type {SourceMap} from './SourceMap';
type SourceMapOrMappings = MixedSourceMap | Array<RawMapping>; type SourceMapOrMappings = SourceMap | Array<RawMapping>;
type Metadata = { type Metadata = {
dependencies?: ?Array<string>, dependencies?: ?Array<string>,

View File

@ -11,23 +11,28 @@
'use strict'; 'use strict';
import type {SourceMap as BabelSourceMap} from 'babel-core'; import type {SourceMap as MappingsMap} from 'babel-core';
export type SourceMap = BabelSourceMap; export type IndexMapSection = {
map: SourceMap,
export type CombinedSourceMap = { offset: {line: number, column: number},
version: number,
file?: string,
sections: Array<{
offset: {line: number, column: number},
map: MixedSourceMap,
}>,
}; };
type FBExtensions = {x_facebook_offsets?: Array<number>}; type FBExtensions = {x_facebook_offsets: Array<number>};
export type MixedSourceMap export type {MappingsMap};
= SourceMap export type IndexMap = {
| CombinedSourceMap file?: string,
| (SourceMap & FBExtensions) mappings?: void, // avoids SourceMap being a disjoint union
| (CombinedSourceMap & FBExtensions); sections: Array<IndexMapSection>,
version: number,
};
export type SourceMap = IndexMap | MappingsMap;
export type FBSourceMap = (IndexMap & FBExtensions) | (MappingsMap & FBExtensions);
function isMappingsMap(map: SourceMap)/*: %checks*/ {
return map.mappings !== undefined;
}
exports.isMappingsMap = isMappingsMap;

View File

@ -21,7 +21,7 @@ const terminal = require('../lib/terminal');
const writeFileAtomicSync = require('write-file-atomic').sync; const writeFileAtomicSync = require('write-file-atomic').sync;
import type {Options as TransformOptions} from '../JSTransformer/worker/worker'; import type {Options as TransformOptions} from '../JSTransformer/worker/worker';
import type {SourceMap} from './SourceMap'; import type {MappingsMap} from './SourceMap';
import type {Reporter} from './reporting'; import type {Reporter} from './reporting';
type CacheFilePaths = {transformedCode: string, metadata: string}; type CacheFilePaths = {transformedCode: string, metadata: string};
@ -94,7 +94,7 @@ export type CachedResult = {
code: string, code: string,
dependencies: Array<string>, dependencies: Array<string>,
dependencyOffsets: Array<number>, dependencyOffsets: Array<number>,
map?: ?SourceMap, map?: ?MappingsMap,
}; };
export type TransformCacheResult = {| export type TransformCacheResult = {|
@ -281,7 +281,7 @@ function readMetadataFileSync(
cachedSourceHash: string, cachedSourceHash: string,
dependencies: Array<string>, dependencies: Array<string>,
dependencyOffsets: Array<number>, dependencyOffsets: Array<number>,
sourceMap: ?SourceMap, sourceMap: ?MappingsMap,
} { } {
const metadataStr = fs.readFileSync(metadataFilePath, 'utf8'); const metadataStr = fs.readFileSync(metadataFilePath, 'utf8');
let metadata; let metadata;

View File

@ -13,10 +13,12 @@
const path = require('path'); const path = require('path');
import type {MixedSourceMap} from './SourceMap'; const {isMappingsMap} = require('./SourceMap');
function relativizeSourceMapInternal(sourceMap: any, sourcesRoot: string) { import type {SourceMap} from './SourceMap';
if (sourceMap.sections) {
function relativizeSourceMapInternal(sourceMap: SourceMap, sourcesRoot: string) {
if (!isMappingsMap(sourceMap)) {
for (let i = 0; i < sourceMap.sections.length; i++) { for (let i = 0; i < sourceMap.sections.length; i++) {
relativizeSourceMapInternal(sourceMap.sections[i].map, sourcesRoot); relativizeSourceMapInternal(sourceMap.sections[i].map, sourcesRoot);
} }
@ -27,7 +29,7 @@ function relativizeSourceMapInternal(sourceMap: any, sourcesRoot: string) {
} }
} }
function relativizeSourceMap(sourceMap: MixedSourceMap, sourcesRoot?: string): MixedSourceMap { function relativizeSourceMap(sourceMap: SourceMap, sourcesRoot?: string): SourceMap {
if (!sourcesRoot) { if (!sourcesRoot) {
return sourceMap; return sourceMap;
} }

View File

@ -24,7 +24,7 @@ const {join: joinPath, relative: relativePath, extname} = require('path');
import type {TransformedCode, Options as TransformOptions} from '../JSTransformer/worker/worker'; import type {TransformedCode, Options as TransformOptions} from '../JSTransformer/worker/worker';
import type {GlobalTransformCache} from '../lib/GlobalTransformCache'; import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
import type {SourceMap} from '../lib/SourceMap'; import type {MappingsMap} from '../lib/SourceMap';
import type {GetTransformCacheKey} from '../lib/TransformCache'; import type {GetTransformCacheKey} from '../lib/TransformCache';
import type {ReadTransformProps} from '../lib/TransformCache'; import type {ReadTransformProps} from '../lib/TransformCache';
import type {Reporter} from '../lib/reporting'; import type {Reporter} from '../lib/reporting';
@ -35,7 +35,7 @@ export type ReadResult = {
+code: string, +code: string,
+dependencies: Array<string>, +dependencies: Array<string>,
+dependencyOffsets?: ?Array<number>, +dependencyOffsets?: ?Array<number>,
+map?: ?SourceMap, +map?: ?MappingsMap,
+source: string, +source: string,
}; };