Bringing clarity to the Mapping types

Reviewed By: rafeca

Differential Revision: D6702118

fbshipit-source-id: 0fc99454713042117ce41ddfe1d3f84c95265d74
This commit is contained in:
Peter van der Zee 2018-01-11 04:27:23 -08:00 committed by Facebook Github Bot
parent 70858589ff
commit 3ee6e5bfb5
16 changed files with 56 additions and 56 deletions

View File

@ -10,7 +10,7 @@
* @format * @format
*/ */
type _SourceMap = { type _BabelSourceMap = {
file?: string, file?: string,
mappings: string, mappings: string,
names: Array<string>, names: Array<string>,
@ -108,19 +108,19 @@ type TransformResult = {
ast: Ast, ast: Ast,
code: ?string, code: ?string,
ignored: boolean, ignored: boolean,
map: ?_SourceMap, map: ?_BabelSourceMap,
}; };
// https://github.com/babel/babel/blob/master/packages/babel-generator/src/buffer.js#L42 // https://github.com/babel/babel/blob/master/packages/babel-generator/src/buffer.js#L42
type GeneratorResult = { type GeneratorResult = {
code: string, code: string,
map: ?_SourceMap, map: ?_BabelSourceMap,
rawMappings: ?Array<_RawMapping>, rawMappings: ?Array<_RawMapping>,
}; };
type VisitFn = <State>(path: Object, state: State) => any; type VisitFn = <State>(path: Object, state: State) => any;
declare module 'babel-core' { declare module 'babel-core' {
declare type Plugins = _Plugins; declare type Plugins = _Plugins;
declare type SourceMap = _SourceMap; declare type BabelSourceMap = _BabelSourceMap;
declare type Ast = {}; declare type Ast = {};
declare type TransformOptions = _TransformOptions; declare type TransformOptions = _TransformOptions;
declare function transform( declare function transform(

View File

@ -14,7 +14,7 @@
const B64Builder = require('./B64Builder'); const B64Builder = require('./B64Builder');
import type {SourceMap as MappingsMap} from 'babel-core'; import type {BabelSourceMap} from 'babel-core';
/** /**
* Generates a source map from raw mappings. * Generates a source map from raw mappings.
@ -149,7 +149,7 @@ class Generator {
/** /**
* Return the source map as object. * Return the source map as object.
*/ */
toMap(file?: string, options: {excludeSource?: boolean}): MappingsMap { toMap(file?: string, options: {excludeSource?: boolean}): BabelSourceMap {
let content; let content;
if (options && options.excludeSource) { if (options && options.excludeSource) {

View File

@ -15,12 +15,11 @@
const Generator = require('./Generator'); const Generator = require('./Generator');
const SourceMap = require('source-map'); const SourceMap = require('source-map');
import type {SourceMap as MappingsMap} from 'babel-core'; import type {BabelSourceMap} from 'babel-core';
import type {RawMapping as BabelRawMapping} from 'babel-generator'; import type {RawMapping as BabelRawMapping} from 'babel-generator';
import type {RawMapping as CompactRawMapping} from 'source-map'; import type {RawMapping as UnknownSourceMapMappingType} from 'source-map';
export type {SourceMap as MappingsMap} from 'babel-core'; export type UnknownSourceMapMappingTypes = Array<UnknownSourceMapMappingType>;
export type CompactRawMappings = Array<CompactRawMapping>;
export type RawMappings = Array<BabelRawMapping>; export type RawMappings = Array<BabelRawMapping>;
type GeneratedCodeMapping = [number, number]; type GeneratedCodeMapping = [number, number];
@ -50,8 +49,8 @@ export type IndexMap = {
}; };
export type FBIndexMap = IndexMap & FBExtensions; export type FBIndexMap = IndexMap & FBExtensions;
export type MetroSourceMap = IndexMap | MappingsMap; export type MetroSourceMap = IndexMap | BabelSourceMap;
export type FBSourceMap = FBIndexMap | (MappingsMap & FBExtensions); export type FBSourceMap = FBIndexMap | (BabelSourceMap & FBExtensions);
/** /**
* Creates a source map from modules with "raw mappings", i.e. an array of * Creates a source map from modules with "raw mappings", i.e. an array of
@ -91,7 +90,7 @@ function fromRawMappings(
* Transforms a standard source map object into a Raw Mappings object, to be * Transforms a standard source map object into a Raw Mappings object, to be
* used across the bundler. * used across the bundler.
*/ */
function toRawMappings(sourceMap: MappingsMap): RawMappings { function toRawMappings(sourceMap: BabelSourceMap): RawMappings {
const rawMappings = []; const rawMappings = [];
new SourceMap.SourceMapConsumer(sourceMap).eachMapping(map => { new SourceMap.SourceMapConsumer(sourceMap).eachMapping(map => {

View File

@ -32,9 +32,9 @@ import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
import type {TransformCache} from '../lib/TransformCaching'; import type {TransformCache} from '../lib/TransformCaching';
import type {Reporter} from '../lib/reporting'; import type {Reporter} from '../lib/reporting';
import type {HasteImpl} from '../node-haste/Module'; import type {HasteImpl} from '../node-haste/Module';
import type {BabelSourceMap} from 'babel-core';
import type { import type {
CompactRawMappings, UnknownSourceMapMappingTypes,
MappingsMap,
MetroSourceMap as SourceMap, MetroSourceMap as SourceMap,
} from 'metro-source-map'; } from 'metro-source-map';
@ -68,8 +68,8 @@ export type GetTransformOptions = (
export type PostMinifyProcess = ({ export type PostMinifyProcess = ({
code: string, code: string,
map: ?MappingsMap, map: ?BabelSourceMap,
}) => {code: string, map: ?MappingsMap}; }) => {code: string, map: ?BabelSourceMap};
export type PostProcessBundleSourcemap = ({ export type PostProcessBundleSourcemap = ({
code: Buffer | string, code: Buffer | string,
@ -255,8 +255,8 @@ class Bundler {
async minifyModule( async minifyModule(
path: string, path: string,
code: string, code: string,
map: CompactRawMappings, map: UnknownSourceMapMappingTypes,
): Promise<{code: string, map: CompactRawMappings}> { ): Promise<{code: string, map: UnknownSourceMapMappingTypes}> {
const sourceMap = fromRawMappings([{code, source: code, map, path}]).toMap( const sourceMap = fromRawMappings([{code, source: code, map, path}]).toMap(
undefined, undefined,
{}, {},

View File

@ -29,7 +29,7 @@ import type DependencyGraph from '../node-haste/DependencyGraph';
import type Module from '../node-haste/Module'; import type Module from '../node-haste/Module';
import type {Options as BundleOptions, MainOptions} from './'; import type {Options as BundleOptions, MainOptions} from './';
import type {DependencyEdges} from './traverseDependencies'; import type {DependencyEdges} from './traverseDependencies';
import type {CompactRawMappings} from 'metro-source-map'; import type {UnknownSourceMapMappingTypes} from 'metro-source-map';
export type DeltaEntryType = export type DeltaEntryType =
| 'asset' | 'asset'
@ -41,7 +41,7 @@ export type DeltaEntryType =
export type DeltaEntry = {| export type DeltaEntry = {|
+code: string, +code: string,
+id: number, +id: number,
+map: CompactRawMappings, +map: UnknownSourceMapMappingTypes,
+name: string, +name: string,
+path: string, +path: string,
+source: string, +source: string,
@ -515,7 +515,7 @@ class DeltaTransformer extends EventEmitter {
): Promise<{ ): Promise<{
+code: string, +code: string,
+dependencies: Array<string>, +dependencies: Array<string>,
+map: CompactRawMappings, +map: UnknownSourceMapMappingTypes,
+source: string, +source: string,
}> { }> {
return await module.read( return await module.read(

View File

@ -28,7 +28,7 @@ import type DeltaTransformer, {
DeltaEntry, DeltaEntry,
DeltaTransformResponse, DeltaTransformResponse,
} from './DeltaTransformer'; } from './DeltaTransformer';
import type {MappingsMap} from 'metro-source-map'; import type {BabelSourceMap} from 'babel-core';
export type Options = BundleOptions & { export type Options = BundleOptions & {
deltaBundleId: ?string, deltaBundleId: ?string,
@ -86,7 +86,7 @@ async function fullSourceMap(
async function fullSourceMapObject( async function fullSourceMapObject(
deltaBundler: DeltaBundler, deltaBundler: DeltaBundler,
options: Options, options: Options,
): Promise<MappingsMap> { ): Promise<BabelSourceMap> {
const {modules} = await _getAllModules(deltaBundler, options); const {modules} = await _getAllModules(deltaBundler, options);
return fromRawMappings(modules).toMap(undefined, { return fromRawMappings(modules).toMap(undefined, {

View File

@ -17,9 +17,9 @@ const {Logger} = require('metro-core');
const debug = require('debug')('Metro:JStransformer'); const debug = require('debug')('Metro:JStransformer');
const Worker = require('jest-worker').default; const Worker = require('jest-worker').default;
import type {BabelSourceMap} from 'babel-core';
import type {Options, TransformedCode} from './worker'; import type {Options, TransformedCode} from './worker';
import type {LocalPath} from '../node-haste/lib/toLocalPath'; import type {LocalPath} from '../node-haste/lib/toLocalPath';
import type {MappingsMap} from 'metro-source-map';
import type {ResultWithMap} from './worker/minify'; import type {ResultWithMap} from './worker/minify';
import typeof {minify as Minify, transform as Transform} from './worker'; import typeof {minify as Minify, transform as Transform} from './worker';
@ -74,7 +74,7 @@ module.exports = class Transformer {
async minify( async minify(
filename: string, filename: string,
code: string, code: string,
sourceMap: MappingsMap, sourceMap: BabelSourceMap,
): Promise<ResultWithMap> { ): Promise<ResultWithMap> {
return await this._worker.minify(filename, code, sourceMap); return await this._worker.minify(filename, code, sourceMap);
} }

View File

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

View File

@ -27,7 +27,8 @@ const path = require('path');
const {compactMapping} = require('metro-source-map'); const {compactMapping} = require('metro-source-map');
import type {LogEntry} from 'metro-core/src/Logger'; import type {LogEntry} from 'metro-core/src/Logger';
import type {CompactRawMappings, MappingsMap} from 'metro-source-map'; import type {BabelSourceMap} from 'babel-core';
import type {UnknownSourceMapMappingTypes} from 'metro-source-map';
import type {LocalPath} from '../../node-haste/lib/toLocalPath'; import type {LocalPath} from '../../node-haste/lib/toLocalPath';
import type {ResultWithMap} from './minify'; import type {ResultWithMap} from './minify';
import type {Ast, Plugins as BabelPlugins} from 'babel-core'; import type {Ast, Plugins as BabelPlugins} from 'babel-core';
@ -35,7 +36,7 @@ import type {Ast, Plugins as BabelPlugins} from 'babel-core';
export type TransformedCode = { export type TransformedCode = {
code: string, code: string,
dependencies: $ReadOnlyArray<string>, dependencies: $ReadOnlyArray<string>,
map: CompactRawMappings, map: UnknownSourceMapMappingTypes,
}; };
export type TransformArgs<ExtraOptions: {}> = {| export type TransformArgs<ExtraOptions: {}> = {|
@ -219,7 +220,7 @@ function transformCode(
function minifyCode( function minifyCode(
filename: string, filename: string,
code: string, code: string,
sourceMap: MappingsMap, sourceMap: BabelSourceMap,
): ResultWithMap | Promise<ResultWithMap> { ): ResultWithMap | Promise<ResultWithMap> {
try { try {
return minify.withSourceMap(code, sourceMap, filename); return minify.withSourceMap(code, sourceMap, filename);

View File

@ -16,7 +16,8 @@ const babel = require('babel-core');
const inlinePlatform = require('./inline-platform'); const inlinePlatform = require('./inline-platform');
const invariant = require('fbjs/lib/invariant'); const invariant = require('fbjs/lib/invariant');
import type {Ast, SourceMap as MappingsMap} from 'babel-core'; import type {Ast} from 'babel-core';
import type {BabelSourceMap} from 'babel-core';
const t = babel.types; const t = babel.types;
const env = {name: 'env'}; const env = {name: 'env'};
@ -104,12 +105,12 @@ const plugin = () => inlinePlugin;
type AstResult = { type AstResult = {
ast: Ast, ast: Ast,
code: ?string, code: ?string,
map: ?MappingsMap, map: ?BabelSourceMap,
}; };
function inline( function inline(
filename: string, filename: string,
transformResult: {ast?: ?Ast, code: string, map: ?MappingsMap}, transformResult: {ast?: ?Ast, code: string, map: ?BabelSourceMap},
options: {+dev: boolean, +platform: ?string}, options: {+dev: boolean, +platform: ?string},
): AstResult { ): AstResult {
const code = transformResult.code; const code = transformResult.code;

View File

@ -14,11 +14,11 @@
const uglify = require('uglify-es'); const uglify = require('uglify-es');
import type {MappingsMap} from 'metro-source-map'; import type {BabelSourceMap} from 'babel-core';
export type ResultWithMap = { export type ResultWithMap = {
code: string, code: string,
map: MappingsMap, map: BabelSourceMap,
}; };
function noSourceMap(code: string): string { function noSourceMap(code: string): string {
@ -27,17 +27,17 @@ function noSourceMap(code: string): string {
function withSourceMap( function withSourceMap(
code: string, code: string,
sourceMap: ?MappingsMap, sourceMap: ?BabelSourceMap,
filename: string, filename: string,
): ResultWithMap { ): ResultWithMap {
const result = minify(code, sourceMap); const result = minify(code, sourceMap);
const map: MappingsMap = JSON.parse(result.map); const map: BabelSourceMap = JSON.parse(result.map);
map.sources = [filename]; map.sources = [filename];
return {code: result.code, map}; return {code: result.code, map};
} }
function minify(inputCode: string, inputMap: ?MappingsMap) { function minify(inputCode: string, inputMap: ?BabelSourceMap) {
const result = uglify.minify(inputCode, { const result = uglify.minify(inputCode, {
mangle: {toplevel: true}, mangle: {toplevel: true},
output: { output: {

View File

@ -12,12 +12,9 @@
'use strict'; 'use strict';
import type {Ast} from 'babel-core'; import type {Ast} from 'babel-core';
import type {BabelSourceMap} from 'babel-core';
import type {Console} from 'console'; import type {Console} from 'console';
import type { import type {FBSourceMap, MetroSourceMap as SourceMap} from 'metro-source-map';
FBSourceMap,
MappingsMap,
MetroSourceMap as SourceMap,
} from 'metro-source-map';
export type {Transformer} from '../JSTransformer/worker'; export type {Transformer} from '../JSTransformer/worker';
@ -37,7 +34,7 @@ type Dependency = {|
export type File = {| export type File = {|
code: string, code: string,
map: ?MappingsMap, map: ?BabelSourceMap,
path: string, path: string,
type: CodeFileTypes, type: CodeFileTypes,
|}; |};
@ -144,7 +141,7 @@ type ResolveOptions = {
export type TransformerResult = {| export type TransformerResult = {|
ast: ?Ast, ast: ?Ast,
code: string, code: string,
map: ?MappingsMap, map: ?BabelSourceMap,
|}; |};
export type TransformResultDependency = {| export type TransformResultDependency = {|
@ -164,7 +161,7 @@ export type TransformResult = {|
code: string, code: string,
dependencies: $ReadOnlyArray<TransformResultDependency>, dependencies: $ReadOnlyArray<TransformResultDependency>,
dependencyMapName?: string, dependencyMapName?: string,
map: ?MappingsMap, map: ?BabelSourceMap,
|}; |};
export type TransformResults = {[string]: TransformResult}; export type TransformResults = {[string]: TransformResult};

View File

@ -23,7 +23,8 @@ const optimizeDependencies = require('./optimizeDependencies');
const sourceMap = require('source-map'); const sourceMap = require('source-map');
import type {TransformedSourceFile, TransformResult} from '../types.flow'; import type {TransformedSourceFile, TransformResult} from '../types.flow';
import type {MappingsMap, MetroSourceMap as SourceMap} from 'metro-source-map'; import type {BabelSourceMap} from 'babel-core';
import type {MetroSourceMap as SourceMap} from 'metro-source-map';
import type {PostMinifyProcess} from '../../Bundler/index.js'; import type {PostMinifyProcess} from '../../Bundler/index.js';
export type OptimizationOptions = {| export type OptimizationOptions = {|
@ -112,7 +113,7 @@ function mergeSourceMaps(
file: string, file: string,
originalMap: SourceMap, originalMap: SourceMap,
secondMap: SourceMap, secondMap: SourceMap,
): MappingsMap { ): BabelSourceMap {
const merged = new sourceMap.SourceMapGenerator(); const merged = new sourceMap.SourceMapGenerator();
const inputMap = new sourceMap.SourceMapConsumer(originalMap); const inputMap = new sourceMap.SourceMapConsumer(originalMap);
new sourceMap.SourceMapConsumer(secondMap).eachMapping(mapping => { new sourceMap.SourceMapConsumer(secondMap).eachMapping(mapping => {

View File

@ -24,7 +24,7 @@ const writeFileAtomicSync = require('write-file-atomic').sync;
import type {Options as WorkerOptions} from '../JSTransformer/worker'; import type {Options as WorkerOptions} from '../JSTransformer/worker';
import type {Reporter} from './reporting'; import type {Reporter} from './reporting';
import type {LocalPath} from '../node-haste/lib/toLocalPath'; import type {LocalPath} from '../node-haste/lib/toLocalPath';
import type {CompactRawMappings} from 'metro-source-map'; import type {UnknownSourceMapMappingTypes} from 'metro-source-map';
type CacheFilePaths = {transformedCode: string, metadata: string}; type CacheFilePaths = {transformedCode: string, metadata: string};
export type GetTransformCacheKey = (options: {}) => string; export type GetTransformCacheKey = (options: {}) => string;
@ -34,7 +34,7 @@ const CACHE_SUB_DIR = 'cache';
export type CachedResult = { export type CachedResult = {
code: string, code: string,
dependencies: $ReadOnlyArray<string>, dependencies: $ReadOnlyArray<string>,
map: CompactRawMappings, map: UnknownSourceMapMappingTypes,
}; };
export type TransformCacheResult = ?CachedResult; export type TransformCacheResult = ?CachedResult;
@ -324,7 +324,7 @@ function readMetadataFileSync(
cachedResultHash: string, cachedResultHash: string,
cachedSourceHash: string, cachedSourceHash: string,
dependencies: Array<string>, dependencies: Array<string>,
sourceMap: CompactRawMappings, sourceMap: UnknownSourceMapMappingTypes,
} { } {
const metadataStr = fs.readFileSync(metadataFilePath, 'utf8'); const metadataStr = fs.readFileSync(metadataFilePath, 'utf8');
const metadata = tryParseJSON(metadataStr); const metadata = tryParseJSON(metadataStr);

View File

@ -35,12 +35,12 @@ import type {Reporter} from '../lib/reporting';
import type DependencyGraphHelpers from './DependencyGraph/DependencyGraphHelpers'; import type DependencyGraphHelpers from './DependencyGraph/DependencyGraphHelpers';
import type ModuleCache from './ModuleCache'; import type ModuleCache from './ModuleCache';
import type {LocalPath} from './lib/toLocalPath'; import type {LocalPath} from './lib/toLocalPath';
import type {CompactRawMappings} from 'metro-source-map'; import type {UnknownSourceMapMappingTypes} from 'metro-source-map';
export type ReadResult = { export type ReadResult = {
+code: string, +code: string,
+dependencies: $ReadOnlyArray<string>, +dependencies: $ReadOnlyArray<string>,
+map: CompactRawMappings, +map: UnknownSourceMapMappingTypes,
+source: string, +source: string,
}; };

View File

@ -15,10 +15,10 @@ const invariant = require('fbjs/lib/invariant');
import type {RamModule} from '../../../DeltaBundler/Serializers'; import type {RamModule} from '../../../DeltaBundler/Serializers';
import type {ModuleGroups, ModuleTransportLike} from '../../types.flow'; import type {ModuleGroups, ModuleTransportLike} from '../../types.flow';
import type {BabelSourceMap} from 'babel-core';
import type { import type {
FBIndexMap, FBIndexMap,
IndexMap, IndexMap,
MappingsMap,
MetroSourceMap as SourceMap, MetroSourceMap as SourceMap,
} from 'metro-source-map'; } from 'metro-source-map';
@ -29,7 +29,7 @@ const countLines = (string: string) => (string.match(newline) || []).length + 1;
function lineToLineSourceMap( function lineToLineSourceMap(
source: string, source: string,
filename: string = '', filename: string = '',
): MappingsMap { ): BabelSourceMap {
// The first line mapping in our package is the base64vlq code for zeros (A). // The first line mapping in our package is the base64vlq code for zeros (A).
const firstLine = 'AAAA;'; const firstLine = 'AAAA;';