Do not return a promise from transformers unless we have to

Reviewed By: mjesun

Differential Revision: D6405568

fbshipit-source-id: 09463442aecdb844e18498c3b3ce169d2915fe77
This commit is contained in:
Burak Yigit Kaya 2017-11-27 02:19:37 -08:00 committed by Facebook Github Bot
parent 091f8560f2
commit 319220d870
1 changed files with 54 additions and 31 deletions

View File

@ -85,40 +85,15 @@ export type Data = {
transformFileEndLogEntry: LogEntry, transformFileEndLogEntry: LogEntry,
}; };
async function transformCode( function postTransform(
transformer: Transformer<*>,
filename: string, filename: string,
localPath: LocalPath, localPath: LocalPath,
sourceCode: string, sourceCode: string,
isScript: boolean, isScript: boolean,
options: Options, options: Options,
): Promise<Data> { transformFileStartLogEntry: LogEntry,
const isJson = filename.endsWith('.json'); ast: Ast,
) {
if (isJson) {
sourceCode = 'module.exports=' + sourceCode;
}
const transformFileStartLogEntry = {
action_name: 'Transforming file',
action_phase: 'start',
file_name: filename,
log_entry_label: 'Transforming file',
start_timestamp: process.hrtime(),
};
const plugins = options.dev
? []
: [[inline.plugin, options], [constantFolding.plugin, options]];
const {ast} = await transformer.transform({
filename,
localPath,
options,
plugins,
src: sourceCode,
});
const timeDelta = process.hrtime(transformFileStartLogEntry.start_timestamp); const timeDelta = process.hrtime(transformFileStartLogEntry.start_timestamp);
const duration_ms = Math.round((timeDelta[0] * 1e9 + timeDelta[1]) / 1e6); const duration_ms = Math.round((timeDelta[0] * 1e9 + timeDelta[1]) / 1e6);
const transformFileEndLogEntry = { const transformFileEndLogEntry = {
@ -179,6 +154,54 @@ async function transformCode(
}; };
} }
function transformCode(
transformer: Transformer<*>,
filename: string,
localPath: LocalPath,
sourceCode: string,
isScript: boolean,
options: Options,
): Data | Promise<Data> {
const isJson = filename.endsWith('.json');
if (isJson) {
sourceCode = 'module.exports=' + sourceCode;
}
const transformFileStartLogEntry = {
action_name: 'Transforming file',
action_phase: 'start',
file_name: filename,
log_entry_label: 'Transforming file',
start_timestamp: process.hrtime(),
};
const plugins = options.dev
? []
: [[inline.plugin, options], [constantFolding.plugin, options]];
const transformResult = transformer.transform({
filename,
localPath,
options,
plugins,
src: sourceCode,
});
const postTransformArgs = [
filename,
localPath,
sourceCode,
isScript,
options,
transformFileStartLogEntry,
];
return typeof transformResult.then === 'function'
? transformResult.then(({ast}) => postTransform(...postTransformArgs, ast))
: postTransform(...postTransformArgs, transformResult.ast);
}
exports.minify = async function( exports.minify = async function(
filename: string, filename: string,
code: string, code: string,
@ -197,14 +220,14 @@ exports.minify = async function(
} }
}; };
exports.transformAndExtractDependencies = async function( exports.transformAndExtractDependencies = function(
transform: string, transform: string,
filename: string, filename: string,
localPath: LocalPath, localPath: LocalPath,
sourceCode: string, sourceCode: string,
isScript: boolean, isScript: boolean,
options: Options, options: Options,
): Promise<Data> { ): Data | Promise<Data> {
// $FlowFixMe: impossible to type a dynamic require. // $FlowFixMe: impossible to type a dynamic require.
const transformModule: Transformer<*> = require(transform); const transformModule: Transformer<*> = require(transform);