mirror of https://github.com/status-im/metro.git
Fix flow types of sourcemaps
Reviewed By: davidaurelio Differential Revision: D6271757 fbshipit-source-id: abf9c91a4ebd36eb4cd77acf75c40f85fbecd5f9
This commit is contained in:
parent
7b93876541
commit
e7b4e50813
|
@ -19,7 +19,7 @@ const Worker = require('jest-worker').default;
|
||||||
|
|
||||||
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 {RawMappings} from '../lib/SourceMap';
|
import type {MappingsMap} from '../lib/SourceMap';
|
||||||
import type {ResultWithMap} from './worker/minify';
|
import type {ResultWithMap} from './worker/minify';
|
||||||
|
|
||||||
import typeof {
|
import typeof {
|
||||||
|
@ -78,7 +78,7 @@ module.exports = class Transformer {
|
||||||
async minify(
|
async minify(
|
||||||
filename: string,
|
filename: string,
|
||||||
code: string,
|
code: string,
|
||||||
sourceMap: RawMappings,
|
sourceMap: MappingsMap,
|
||||||
): Promise<ResultWithMap> {
|
): Promise<ResultWithMap> {
|
||||||
return await this._worker.minify(filename, code, sourceMap);
|
return await this._worker.minify(filename, code, sourceMap);
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,6 @@
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const asyncify = require('async/asyncify');
|
|
||||||
const constantFolding = require('./constant-folding');
|
const constantFolding = require('./constant-folding');
|
||||||
const extractDependencies = require('./extract-dependencies');
|
const extractDependencies = require('./extract-dependencies');
|
||||||
const inline = require('./inline');
|
const inline = require('./inline');
|
||||||
|
@ -34,7 +33,7 @@ export type TransformedCode = {
|
||||||
code: string,
|
code: string,
|
||||||
dependencies: Array<string>,
|
dependencies: Array<string>,
|
||||||
dependencyOffsets: Array<number>,
|
dependencyOffsets: Array<number>,
|
||||||
map?: ?CompactRawMappings,
|
map: CompactRawMappings,
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TransformArgs<ExtraOptions: {}> = {|
|
export type TransformArgs<ExtraOptions: {}> = {|
|
||||||
|
@ -134,7 +133,7 @@ function transformCode(
|
||||||
: transformed.map;
|
: transformed.map;
|
||||||
|
|
||||||
// Convert the sourcemaps to Compact Raw source maps.
|
// 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;
|
let code = transformed.code;
|
||||||
if (isJson) {
|
if (isJson) {
|
||||||
|
@ -168,7 +167,7 @@ function transformCode(
|
||||||
exports.minify = async function(
|
exports.minify = async function(
|
||||||
filename: string,
|
filename: string,
|
||||||
code: string,
|
code: string,
|
||||||
sourceMap: RawMappings,
|
sourceMap: MappingsMap,
|
||||||
): Promise<ResultWithMap> {
|
): Promise<ResultWithMap> {
|
||||||
try {
|
try {
|
||||||
return minify.withSourceMap(code, sourceMap, filename);
|
return minify.withSourceMap(code, sourceMap, filename);
|
||||||
|
|
|
@ -14,17 +14,7 @@
|
||||||
|
|
||||||
const uglify = require('uglify-es');
|
const uglify = require('uglify-es');
|
||||||
|
|
||||||
const {
|
import type {MappingsMap} from '../../lib/SourceMap';
|
||||||
compactMapping,
|
|
||||||
fromRawMappings,
|
|
||||||
toRawMappings,
|
|
||||||
} = require('../../Bundler/source-map');
|
|
||||||
|
|
||||||
import type {
|
|
||||||
CompactRawMappings,
|
|
||||||
MappingsMap,
|
|
||||||
RawMappings,
|
|
||||||
} from '../../lib/SourceMap';
|
|
||||||
|
|
||||||
export type ResultWithMap = {
|
export type ResultWithMap = {
|
||||||
code: string,
|
code: string,
|
||||||
|
@ -37,15 +27,9 @@ function noSourceMap(code: string): string {
|
||||||
|
|
||||||
function withSourceMap(
|
function withSourceMap(
|
||||||
code: string,
|
code: string,
|
||||||
sourceMap: ?MappingsMap | RawMappings,
|
sourceMap: ?MappingsMap,
|
||||||
filename: string,
|
filename: string,
|
||||||
): ResultWithMap {
|
): ResultWithMap {
|
||||||
if (sourceMap && Array.isArray(sourceMap)) {
|
|
||||||
sourceMap = fromRawMappings([
|
|
||||||
{code, source: code, map: sourceMap, path: filename},
|
|
||||||
]).toMap(undefined, {});
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = minify(code, sourceMap);
|
const result = minify(code, sourceMap);
|
||||||
|
|
||||||
const map: MappingsMap = JSON.parse(result.map);
|
const map: MappingsMap = JSON.parse(result.map);
|
||||||
|
@ -53,19 +37,6 @@ function withSourceMap(
|
||||||
return {code: result.code, map};
|
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) {
|
function minify(inputCode: string, inputMap: ?MappingsMap) {
|
||||||
const result = uglify.minify(inputCode, {
|
const result = uglify.minify(inputCode, {
|
||||||
mangle: {toplevel: true},
|
mangle: {toplevel: true},
|
||||||
|
@ -93,6 +64,5 @@ function minify(inputCode: string, inputMap: ?MappingsMap) {
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
noSourceMap,
|
noSourceMap,
|
||||||
withRawMappings,
|
|
||||||
withSourceMap,
|
withSourceMap,
|
||||||
};
|
};
|
||||||
|
|
|
@ -35,11 +35,7 @@ import type {
|
||||||
} from '../lib/TransformCaching';
|
} from '../lib/TransformCaching';
|
||||||
import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
|
import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
|
||||||
|
|
||||||
type MinifyCode = (
|
import typeof {minify as MinifyCode} from '../JSTransformer/worker';
|
||||||
filePath: string,
|
|
||||||
code: string,
|
|
||||||
map: ?MappingsMap,
|
|
||||||
) => Promise<{code: string, map: ?MappingsMap}>;
|
|
||||||
|
|
||||||
type ContainsTransformerOptions = {+transformer: JSTransformerOptions};
|
type ContainsTransformerOptions = {+transformer: JSTransformerOptions};
|
||||||
|
|
||||||
|
@ -273,7 +269,7 @@ class Resolver {
|
||||||
);
|
);
|
||||||
|
|
||||||
const minified = await this._minifyCode(path, code, sourceMap);
|
const minified = await this._minifyCode(path, code, sourceMap);
|
||||||
const result = await this._postMinifyProcess(minified);
|
const result = await this._postMinifyProcess({...minified});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
code: result.code,
|
code: result.code,
|
||||||
|
|
|
@ -35,7 +35,7 @@ export type CachedResult = {
|
||||||
code: string,
|
code: string,
|
||||||
dependencies: Array<string>,
|
dependencies: Array<string>,
|
||||||
dependencyOffsets: Array<number>,
|
dependencyOffsets: Array<number>,
|
||||||
map?: ?CompactRawMappings,
|
map: CompactRawMappings,
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TransformCacheResult = {|
|
export type TransformCacheResult = {|
|
||||||
|
@ -336,7 +336,7 @@ function readMetadataFileSync(
|
||||||
cachedSourceHash: string,
|
cachedSourceHash: string,
|
||||||
dependencies: Array<string>,
|
dependencies: Array<string>,
|
||||||
dependencyOffsets: Array<number>,
|
dependencyOffsets: Array<number>,
|
||||||
sourceMap: ?CompactRawMappings,
|
sourceMap: CompactRawMappings,
|
||||||
} {
|
} {
|
||||||
const metadataStr = fs.readFileSync(metadataFilePath, 'utf8');
|
const metadataStr = fs.readFileSync(metadataFilePath, 'utf8');
|
||||||
const metadata = tryParseJSON(metadataStr);
|
const metadata = tryParseJSON(metadataStr);
|
||||||
|
|
|
@ -27,7 +27,7 @@ import type {
|
||||||
Options as WorkerOptions,
|
Options as WorkerOptions,
|
||||||
} from '../JSTransformer/worker';
|
} from '../JSTransformer/worker';
|
||||||
import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
|
import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
|
||||||
import type {MappingsMap} from '../lib/SourceMap';
|
import type {CompactRawMappings} from '../lib/SourceMap';
|
||||||
import type {
|
import type {
|
||||||
TransformCache,
|
TransformCache,
|
||||||
GetTransformCacheKey,
|
GetTransformCacheKey,
|
||||||
|
@ -42,7 +42,7 @@ export type ReadResult = {
|
||||||
+code: string,
|
+code: string,
|
||||||
+dependencies: Array<string>,
|
+dependencies: Array<string>,
|
||||||
+dependencyOffsets?: ?Array<number>,
|
+dependencyOffsets?: ?Array<number>,
|
||||||
+map?: ?(MappingsMap | Array<RawMapping>),
|
+map: CompactRawMappings,
|
||||||
+source: string,
|
+source: string,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue