Fix flow types of sourcemaps

Reviewed By: davidaurelio

Differential Revision: D6271757

fbshipit-source-id: abf9c91a4ebd36eb4cd77acf75c40f85fbecd5f9
This commit is contained in:
Rafael Oleza 2017-11-08 12:34:05 -08:00 committed by Facebook Github Bot
parent 7b93876541
commit e7b4e50813
6 changed files with 13 additions and 48 deletions

View File

@ -19,7 +19,7 @@ const Worker = require('jest-worker').default;
import type {Options, TransformedCode} from './worker';
import type {LocalPath} from '../node-haste/lib/toLocalPath';
import type {RawMappings} from '../lib/SourceMap';
import type {MappingsMap} from '../lib/SourceMap';
import type {ResultWithMap} from './worker/minify';
import typeof {
@ -78,7 +78,7 @@ module.exports = class Transformer {
async minify(
filename: string,
code: string,
sourceMap: RawMappings,
sourceMap: MappingsMap,
): Promise<ResultWithMap> {
return await this._worker.minify(filename, code, sourceMap);
}

View File

@ -12,7 +12,6 @@
'use strict';
const asyncify = require('async/asyncify');
const constantFolding = require('./constant-folding');
const extractDependencies = require('./extract-dependencies');
const inline = require('./inline');
@ -34,7 +33,7 @@ export type TransformedCode = {
code: string,
dependencies: Array<string>,
dependencyOffsets: Array<number>,
map?: ?CompactRawMappings,
map: CompactRawMappings,
};
export type TransformArgs<ExtraOptions: {}> = {|
@ -134,7 +133,7 @@ function transformCode(
: transformed.map;
// Convert the sourcemaps to Compact Raw source maps.
const map = rawMappings ? rawMappings.map(compactMapping) : null;
const map = rawMappings ? rawMappings.map(compactMapping) : [];
let code = transformed.code;
if (isJson) {
@ -168,7 +167,7 @@ function transformCode(
exports.minify = async function(
filename: string,
code: string,
sourceMap: RawMappings,
sourceMap: MappingsMap,
): Promise<ResultWithMap> {
try {
return minify.withSourceMap(code, sourceMap, filename);

View File

@ -14,17 +14,7 @@
const uglify = require('uglify-es');
const {
compactMapping,
fromRawMappings,
toRawMappings,
} = require('../../Bundler/source-map');
import type {
CompactRawMappings,
MappingsMap,
RawMappings,
} from '../../lib/SourceMap';
import type {MappingsMap} from '../../lib/SourceMap';
export type ResultWithMap = {
code: string,
@ -37,15 +27,9 @@ function noSourceMap(code: string): string {
function withSourceMap(
code: string,
sourceMap: ?MappingsMap | RawMappings,
sourceMap: ?MappingsMap,
filename: string,
): ResultWithMap {
if (sourceMap && Array.isArray(sourceMap)) {
sourceMap = fromRawMappings([
{code, source: code, map: sourceMap, path: filename},
]).toMap(undefined, {});
}
const result = minify(code, sourceMap);
const map: MappingsMap = JSON.parse(result.map);
@ -53,19 +37,6 @@ function withSourceMap(
return {code: result.code, map};
}
function withRawMappings(
code: string,
map: RawMappings,
filename: string,
): {code: string, map: CompactRawMappings} {
const result = withSourceMap(code, map, filename);
return {
code: result.code,
map: toRawMappings(result.map).map(compactMapping),
};
}
function minify(inputCode: string, inputMap: ?MappingsMap) {
const result = uglify.minify(inputCode, {
mangle: {toplevel: true},
@ -93,6 +64,5 @@ function minify(inputCode: string, inputMap: ?MappingsMap) {
module.exports = {
noSourceMap,
withRawMappings,
withSourceMap,
};

View File

@ -35,11 +35,7 @@ import type {
} from '../lib/TransformCaching';
import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
type MinifyCode = (
filePath: string,
code: string,
map: ?MappingsMap,
) => Promise<{code: string, map: ?MappingsMap}>;
import typeof {minify as MinifyCode} from '../JSTransformer/worker';
type ContainsTransformerOptions = {+transformer: JSTransformerOptions};
@ -273,7 +269,7 @@ class Resolver {
);
const minified = await this._minifyCode(path, code, sourceMap);
const result = await this._postMinifyProcess(minified);
const result = await this._postMinifyProcess({...minified});
return {
code: result.code,

View File

@ -35,7 +35,7 @@ export type CachedResult = {
code: string,
dependencies: Array<string>,
dependencyOffsets: Array<number>,
map?: ?CompactRawMappings,
map: CompactRawMappings,
};
export type TransformCacheResult = {|
@ -336,7 +336,7 @@ function readMetadataFileSync(
cachedSourceHash: string,
dependencies: Array<string>,
dependencyOffsets: Array<number>,
sourceMap: ?CompactRawMappings,
sourceMap: CompactRawMappings,
} {
const metadataStr = fs.readFileSync(metadataFilePath, 'utf8');
const metadata = tryParseJSON(metadataStr);

View File

@ -27,7 +27,7 @@ import type {
Options as WorkerOptions,
} from '../JSTransformer/worker';
import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
import type {MappingsMap} from '../lib/SourceMap';
import type {CompactRawMappings} from '../lib/SourceMap';
import type {
TransformCache,
GetTransformCacheKey,
@ -42,7 +42,7 @@ export type ReadResult = {
+code: string,
+dependencies: Array<string>,
+dependencyOffsets?: ?Array<number>,
+map?: ?(MappingsMap | Array<RawMapping>),
+map: CompactRawMappings,
+source: string,
};