Permalink
Browse files

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
  • Loading branch information...
davidaurelio authored and facebook-github-bot committed Apr 28, 2017
1 parent 6202e02 commit 58ba7fc07518afdacb5363b6449fadfedc0ad6d7
View
@@ -8,12 +8,13 @@
*/
type _SourceMap = {
version: number,
file?: string,
sources: Array<string>,
names: Array<string>,
mappings: string,
sourcesContent?: Array<string>,
names: Array<string>,
sourceRoot?: string,
sources: Array<string>,
sourcesContent?: Array<?string>,
version: number,
};
// based on babylon v6.13.1
@@ -17,7 +17,8 @@ const meta = require('./meta');
const relativizeSourceMap = require('../../../packager/src//lib/relativizeSourceMap');
const writeFile = require('./writeFile');
import type Bundle from '../../../packager/src//Bundler/Bundle';
import type Bundle from '../../../packager/src/Bundler/Bundle';
import type {SourceMap} from '../../../packager/src/lib/SourceMap';
import type {OutputOptions, RequestOptions} from '../types.flow';
function buildBundle(packagerClient: Server, requestOptions: RequestOptions) {
@@ -31,7 +32,7 @@ function buildBundle(packagerClient: Server, requestOptions: RequestOptions) {
function createCodeWithMap(bundle: Bundle, dev: boolean, sourceMapSourcesRoot?: string): * {
const map = bundle.getSourceMap({dev});
const sourceMap = relativizeSourceMap(
typeof map === 'string' ? JSON.parse(map) : map,
typeof map === 'string' ? (JSON.parse(map): SourceMap) : map,
sourceMapSourcesRoot);
return {
code: bundle.getSource({dev}),
@@ -12,14 +12,15 @@
const invariant = require('fbjs/lib/invariant');
import type {ModuleGroups, ModuleTransportLike, SourceMap} from '../../types.flow';
import type {IndexMap, MappingsMap, SourceMap} from '../../../../packager/src/lib/SourceMap';
import type {ModuleGroups, ModuleTransportLike} from '../../types.flow';
const newline = /\r\n?|\n|\u2028|\u2029/g;
// fastest implementation
const countLines = (string: string) => (string.match(newline) || []).length + 1;
function lineToLineSourceMap(source: string, filename: string = ''): SourceMap {
function lineToLineSourceMap(source: string, filename: string = ''): MappingsMap {
// The first line mapping in our package is the base64vlq code for zeros (A).
const firstLine = 'AAAA;';
@@ -52,19 +53,12 @@ function combineSourceMaps({
moduleGroups,
modules,
withCustomOffsets,
}: CombineSourceMapsOptions): SourceMap {
let offsets;
}: CombineSourceMapsOptions): IndexMap {
const offsets = [];
const sections = [];
const sourceMap: Object = {
file: '',
sections,
version: 3,
};
if (withCustomOffsets) {
offsets = sourceMap.x_facebook_offsets = [];
}
const sourceMap: IndexMap = withCustomOffsets
? {sections, version: 3, x_facebook_offsets: offsets}
: {sections, version: 3};
let line = 0;
modules.forEach(moduleTransport => {
@@ -13,9 +13,9 @@
import type Bundle from '../../packager/src/Bundler/Bundle';
import type {Unbundle} from '../../packager/src/Bundler/Bundle';
import type ModuleTransport from '../../packager/src/lib/ModuleTransport';
import type {MixedSourceMap} from '../../packager/src/lib/SourceMap';
import type {FBSourceMap, SourceMap} from '../../packager/src/lib/SourceMap';
export type {Bundle, ModuleTransport, MixedSourceMap as SourceMap, Unbundle};
export type {Bundle, FBSourceMap, ModuleTransport, SourceMap, Unbundle};
export type ModuleGroups = {|
groups: Map<number, Set<number>>,
@@ -20,8 +20,9 @@ const debug = require('debug')('RNP:Bundle');
const invariant = require('fbjs/lib/invariant');
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';
export type Unbundle = {
@@ -206,7 +207,7 @@ class Bundle extends BundleBase {
* that makes use of of the `sections` field to combine sourcemaps by adding
* an offset. This is supported only by Chrome for now.
*/
_getCombinedSourceMaps(options: {excludeSource?: boolean}): CombinedSourceMap {
_getCombinedSourceMaps(options: {excludeSource?: boolean}): IndexMap {
const result = {
version: 3,
file: this._getSourceMapFile(),
@@ -215,30 +216,30 @@ class Bundle extends BundleBase {
let line = 0;
this.getModules().forEach(module => {
let map = module.map == null || module.virtual
? generateSourceMapForVirtualModule(module)
: module.map;
invariant(
!Array.isArray(map),
!Array.isArray(module.map),
`Unexpected raw mappings for ${module.sourcePath}`,
);
let map: SourceMap = module.map == null || module.virtual
? generateSourceMapForVirtualModule(module)
: module.map;
if (options.excludeSource && 'sourcesContent' in map) {
if (options.excludeSource && isMappingsMap(map)) {
map = {...map, sourcesContent: []};
}
result.sections.push({
offset: {line, column: 0},
map: (map: MixedSourceMap),
map: map,
});
line += module.code.split('\n').length;
});
return result;
}
getSourceMap(options: {excludeSource?: boolean}): MixedSourceMap {
getSourceMap(options: {excludeSource?: boolean}): SourceMap {
this.assertFinalized();
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
let mappings = 'AAAA;';
@@ -13,7 +13,7 @@
const B64Builder = require('./B64Builder');
import type {SourceMap} from 'babel-core';
import type {MappingsMap} from '../../lib/SourceMap';
/**
* Generates a source map from raw mappings.
@@ -39,7 +39,7 @@ class Generator {
names: IndexedSet;
source: number;
sources: Array<string>;
sourcesContent: Array<string>;
sourcesContent: Array<?string>;
constructor() {
this.builder = new B64Builder();
@@ -141,7 +141,7 @@ class Generator {
/**
* Return the source map as object.
*/
toMap(file?: string): SourceMap {
toMap(file?: string): MappingsMap {
return {
version: 3,
file,
@@ -21,7 +21,7 @@ const util = require('util');
const workerFarm = require('worker-farm');
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
// to avoid any memory leak while not slowing down initial builds.
@@ -62,8 +62,8 @@ class Transformer {
minify: (
filename: string,
code: string,
sourceMap: SourceMap,
) => Promise<{code: string, map: SourceMap}>;
sourceMap: MappingsMap,
) => Promise<{code: string, map: MappingsMap}>;
constructor(transformModulePath: string, maxWorkerCount: number) {
invariant(path.isAbsolute(transformModulePath), 'transform module path should be absolute');
@@ -13,7 +13,7 @@
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 Conditional = {
@@ -73,7 +73,7 @@ const plugin = {
function constantFolding(filename: string, transformResult: {
ast: Ast,
code?: ?string,
map: ?SourceMap,
map: ?MappingsMap,
}) {
return babel.transformFromAst(transformResult.ast, transformResult.code, {
filename,
@@ -14,7 +14,7 @@
const babel = require('babel-core');
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 React = {name: 'React'};
@@ -164,12 +164,12 @@ function checkRequireArgs(args, dependencyId) {
type AstResult = {
ast: Ast,
code: ?string,
map: ?SourceMap,
map: ?MappingsMap,
};
function inline(
filename: string,
transformResult: {ast?: ?Ast, code: string, map: ?SourceMap},
transformResult: {ast?: ?Ast, code: string, map: ?MappingsMap},
options: {+dev: boolean, +platform: string},
): AstResult {
const code = transformResult.code;
@@ -18,21 +18,21 @@ const invariant = require('fbjs/lib/invariant');
const minify = require('./minify');
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 = {
code: string,
dependencies: Array<string>,
dependencyOffsets: Array<number>,
map?: ?SourceMap,
map?: ?MappingsMap,
};
type Transformer = {
transform: (
filename: string,
sourceCode: string,
options: ?{},
) => {ast: ?Ast, code: string, map: ?SourceMap}
) => {ast: ?Ast, code: string, map: ?MappingsMap}
};
export type TransformOptions = {|
@@ -10,37 +10,15 @@
*/
'use strict';
import type {FBSourceMap, IndexMapSection, IndexMap} from '../../lib/SourceMap';
export type {FBSourceMap};
type CreateIndexMapOptions = {|
file?: string,
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 => ({
version: 3,
file: opts && opts.file,
@@ -10,7 +10,7 @@
*/
'use strict';
import type {SourceMap} from './output/source-map';
import type {MappingsMap, SourceMap} from '../lib/SourceMap';
import type {Ast} from 'babel-core';
import type {Console} from 'console';
@@ -102,7 +102,7 @@ type ResolveOptions = {
export type TransformerResult = {|
ast: ?Ast,
code: string,
map: ?SourceMap,
map: ?MappingsMap,
|};
export type Transformer = {
@@ -111,7 +111,7 @@ export type Transformer = {
filename: string,
options: ?{},
plugins?: Array<string | Object | [string | Object, any]>,
) => {ast: ?Ast, code: string, map: ?SourceMap}
) => {ast: ?Ast, code: string, map: ?MappingsMap}
};
export type TransformResult = {|
@@ -18,14 +18,14 @@ const pathJoin = require('path').join;
import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse';
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 {Reporter} from '../lib/reporting';
import type {GetTransformCacheKey} from '../lib/TransformCache';
import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
type MinifyCode = (filePath: string, code: string, map: SourceMap) =>
Promise<{code: string, map: SourceMap}>;
type MinifyCode = (filePath: string, code: string, map: MappingsMap) =>
Promise<{code: string, map: MappingsMap}>;
type ContainsTransformerOptions = {+transformer: JSTransformerOptions}
@@ -195,7 +195,7 @@ class Resolver {
resolutionResponse: ResolutionResponse<Module, T>,
module: Module,
name: string,
map: SourceMap,
map: MappingsMap,
code: string,
meta?: {
dependencyOffsets?: Array<number>,
@@ -227,8 +227,8 @@ class Resolver {
}
minifyModule(
{path, code, map}: {path: string, code: string, map: SourceMap},
): Promise<{code: string, map: SourceMap}> {
{path, code, map}: {path: string, code: string, map: MappingsMap},
): Promise<{code: string, map: MappingsMap}> {
return this._minifyCode(path, code, map);
}
@@ -20,7 +20,7 @@ const {LazyPromise, LockingPromise} = require('./util');
const {fork} = require('child_process');
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 Symbolicate =
Oops, something went wrong.

0 comments on commit 58ba7fc

Please sign in to comment.