mirror of https://github.com/status-im/metro.git
packager worker: enable possibility to add Flow annotations
Reviewed By: matryoshcow Differential Revision: D4160456 fbshipit-source-id: bbc2420126d8e8b725c0b7b359b35f09dfe8c8d6
This commit is contained in:
parent
cedf70f702
commit
2bf0c037ec
|
@ -6,99 +6,11 @@
|
||||||
* LICENSE file in the root directory of this source tree. An additional grant
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||||||
* of patent rights can be found in the PATENTS file in the same directory.
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const constantFolding = require('./constant-folding');
|
const path = require('path');
|
||||||
const extractDependencies = require('./extract-dependencies');
|
require('../../../../babelRegisterOnly')([
|
||||||
const inline = require('./inline');
|
path.resolve(path.join(__dirname, '../../**/*')),
|
||||||
const minify = require('./minify');
|
]);
|
||||||
|
module.exports = require('./worker');
|
||||||
function makeTransformParams(filename, sourceCode, options) {
|
|
||||||
if (filename.endsWith('.json')) {
|
|
||||||
sourceCode = 'module.exports=' + sourceCode;
|
|
||||||
}
|
|
||||||
return {filename, sourceCode, options};
|
|
||||||
}
|
|
||||||
|
|
||||||
function transformCode(transform, filename, sourceCode, options, callback) {
|
|
||||||
const params = makeTransformParams(filename, sourceCode, options.transform);
|
|
||||||
const isJson = filename.endsWith('.json');
|
|
||||||
|
|
||||||
const transformFileStartLogEntry = {
|
|
||||||
action_name: 'Transforming file',
|
|
||||||
action_phase: 'start',
|
|
||||||
file_name: filename,
|
|
||||||
log_entry_label: 'Transforming file',
|
|
||||||
start_timestamp: process.hrtime(),
|
|
||||||
};
|
|
||||||
|
|
||||||
transform(params, (error, transformed) => {
|
|
||||||
if (error) {
|
|
||||||
callback(error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var code, map;
|
|
||||||
if (options.minify) {
|
|
||||||
const optimized =
|
|
||||||
constantFolding(filename, inline(filename, transformed, options));
|
|
||||||
code = optimized.code;
|
|
||||||
map = optimized.map;
|
|
||||||
} else {
|
|
||||||
code = transformed.code;
|
|
||||||
map = transformed.map;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isJson) {
|
|
||||||
code = code.replace(/^\w+\.exports=/, '');
|
|
||||||
} else {
|
|
||||||
// Remove shebang
|
|
||||||
code = code.replace(/^#!.*/, '');
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = isJson || options.extern
|
|
||||||
? {dependencies: [], dependencyOffsets: []}
|
|
||||||
: extractDependencies(code);
|
|
||||||
|
|
||||||
const timeDelta = process.hrtime(transformFileStartLogEntry.start_timestamp);
|
|
||||||
const duration_ms = Math.round((timeDelta[0] * 1e9 + timeDelta[1]) / 1e6);
|
|
||||||
const transformFileEndLogEntry = {
|
|
||||||
action_name: 'Transforming file',
|
|
||||||
action_phase: 'end',
|
|
||||||
file_name: filename,
|
|
||||||
duration_ms: duration_ms,
|
|
||||||
log_entry_label: 'Transforming file',
|
|
||||||
};
|
|
||||||
|
|
||||||
result.code = code;
|
|
||||||
result.map = map;
|
|
||||||
|
|
||||||
return callback(null, {
|
|
||||||
result,
|
|
||||||
transformFileStartLogEntry,
|
|
||||||
transformFileEndLogEntry,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.transformAndExtractDependencies = (
|
|
||||||
transform,
|
|
||||||
filename,
|
|
||||||
sourceCode,
|
|
||||||
options,
|
|
||||||
callback
|
|
||||||
) => {
|
|
||||||
transformCode(require(transform), filename, sourceCode, options || {}, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.minify = (filename, code, sourceMap, callback) => {
|
|
||||||
var result;
|
|
||||||
try {
|
|
||||||
result = minify(filename, code, sourceMap);
|
|
||||||
} catch (error) {
|
|
||||||
callback(error);
|
|
||||||
}
|
|
||||||
callback(null, result);
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.transformCode = transformCode; // for easier testing
|
|
||||||
|
|
|
@ -0,0 +1,141 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2016-present, Facebook, Inc.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the BSD-style license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
*
|
||||||
|
* @flow
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const constantFolding = require('./constant-folding');
|
||||||
|
const extractDependencies = require('./extract-dependencies');
|
||||||
|
const inline = require('./inline');
|
||||||
|
const minify = require('./minify');
|
||||||
|
|
||||||
|
function makeTransformParams(filename, sourceCode, options) {
|
||||||
|
if (filename.endsWith('.json')) {
|
||||||
|
sourceCode = 'module.exports=' + sourceCode;
|
||||||
|
}
|
||||||
|
return {filename, sourceCode, options};
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TransformedCode = {
|
||||||
|
code: string,
|
||||||
|
dependencies: Array<string>,
|
||||||
|
dependencyOffsets: Array<number>,
|
||||||
|
map?: ?{},
|
||||||
|
};
|
||||||
|
|
||||||
|
type Transform = (params: {
|
||||||
|
filename: string,
|
||||||
|
sourceCode: string,
|
||||||
|
options: ?{},
|
||||||
|
}) => mixed;
|
||||||
|
|
||||||
|
type Options = {transform?: {}};
|
||||||
|
|
||||||
|
type Callback = (
|
||||||
|
error: ?Error,
|
||||||
|
data: ?{
|
||||||
|
result: TransformedCode,
|
||||||
|
transformFileStartLogEntry: {},
|
||||||
|
transformFileEndLogEntry: {},
|
||||||
|
},
|
||||||
|
) => mixed;
|
||||||
|
|
||||||
|
function transformCode(
|
||||||
|
transform: Transform,
|
||||||
|
filename: string,
|
||||||
|
sourceCode: string,
|
||||||
|
options: Options,
|
||||||
|
callback: Callback,
|
||||||
|
) {
|
||||||
|
const params = makeTransformParams(filename, sourceCode, options.transform);
|
||||||
|
const isJson = filename.endsWith('.json');
|
||||||
|
|
||||||
|
const transformFileStartLogEntry = {
|
||||||
|
action_name: 'Transforming file',
|
||||||
|
action_phase: 'start',
|
||||||
|
file_name: filename,
|
||||||
|
log_entry_label: 'Transforming file',
|
||||||
|
start_timestamp: process.hrtime(),
|
||||||
|
};
|
||||||
|
|
||||||
|
transform(params, (error, transformed) => {
|
||||||
|
if (error) {
|
||||||
|
callback(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var code, map;
|
||||||
|
if (options.minify) {
|
||||||
|
const optimized =
|
||||||
|
constantFolding(filename, inline(filename, transformed, options));
|
||||||
|
code = optimized.code;
|
||||||
|
map = optimized.map;
|
||||||
|
} else {
|
||||||
|
code = transformed.code;
|
||||||
|
map = transformed.map;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isJson) {
|
||||||
|
code = code.replace(/^\w+\.exports=/, '');
|
||||||
|
} else {
|
||||||
|
// Remove shebang
|
||||||
|
code = code.replace(/^#!.*/, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
const depsResult = isJson || options.extern
|
||||||
|
? {dependencies: [], dependencyOffsets: []}
|
||||||
|
: extractDependencies(code);
|
||||||
|
|
||||||
|
const timeDelta = process.hrtime(transformFileStartLogEntry.start_timestamp);
|
||||||
|
const duration_ms = Math.round((timeDelta[0] * 1e9 + timeDelta[1]) / 1e6);
|
||||||
|
const transformFileEndLogEntry = {
|
||||||
|
action_name: 'Transforming file',
|
||||||
|
action_phase: 'end',
|
||||||
|
file_name: filename,
|
||||||
|
duration_ms: duration_ms,
|
||||||
|
log_entry_label: 'Transforming file',
|
||||||
|
};
|
||||||
|
|
||||||
|
return callback(null, {
|
||||||
|
result: {...depsResult, code, map},
|
||||||
|
transformFileStartLogEntry,
|
||||||
|
transformFileEndLogEntry,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.transformAndExtractDependencies = (
|
||||||
|
transform: string,
|
||||||
|
filename: string,
|
||||||
|
sourceCode: string,
|
||||||
|
options: ?Options,
|
||||||
|
callback: Callback,
|
||||||
|
) => {
|
||||||
|
/* $FlowFixMe: impossible to type a dynamic require */
|
||||||
|
const transformModule = require(transform);
|
||||||
|
transformCode(transformModule, filename, sourceCode, options || {}, callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.minify = (
|
||||||
|
filename: string,
|
||||||
|
code: string,
|
||||||
|
sourceMap: string,
|
||||||
|
callback: (error: ?Error, result: mixed) => mixed,
|
||||||
|
) => {
|
||||||
|
var result;
|
||||||
|
try {
|
||||||
|
result = minify(filename, code, sourceMap);
|
||||||
|
} catch (error) {
|
||||||
|
callback(error);
|
||||||
|
}
|
||||||
|
callback(null, result);
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.transformCode = transformCode; // for easier testing
|
|
@ -21,17 +21,11 @@ const jsonStableStringify = require('json-stable-stringify');
|
||||||
|
|
||||||
const {join: joinPath, relative: relativePath, extname} = require('path');
|
const {join: joinPath, relative: relativePath, extname} = require('path');
|
||||||
|
|
||||||
|
import type {TransformedCode} from '../JSTransformer/worker/worker';
|
||||||
import type Cache from './Cache';
|
import type Cache from './Cache';
|
||||||
import type ModuleCache from './ModuleCache';
|
import type ModuleCache from './ModuleCache';
|
||||||
import type FastFs from './fastfs';
|
import type FastFs from './fastfs';
|
||||||
|
|
||||||
type TransformedCode = {
|
|
||||||
code: string,
|
|
||||||
dependencies: Array<string>,
|
|
||||||
dependencyOffsets: Array<number>,
|
|
||||||
map?: ?{},
|
|
||||||
};
|
|
||||||
|
|
||||||
type ReadResult = {
|
type ReadResult = {
|
||||||
code?: string,
|
code?: string,
|
||||||
dependencies?: ?Array<string>,
|
dependencies?: ?Array<string>,
|
||||||
|
|
Loading…
Reference in New Issue