Support null transformer AST output

Summary: This fixes https://github.com/facebook/metro/issues/99, which causes issues when metro tries to build files that are ignored by babel (if babel transformer ignores a file, it returns a null AST, which makes the collectDependencies logic break).

Reviewed By: mjesun

Differential Revision: D6466878

fbshipit-source-id: b5030e03775b982958a0b9204f4efccc8940ae4d
This commit is contained in:
Rafael Oleza 2017-12-02 00:25:58 -08:00 committed by Facebook Github Bot
parent 5d76d340e5
commit de7f1d90f2
1 changed files with 7 additions and 2 deletions

View File

@ -14,6 +14,7 @@
const JsFileWrapping = require('../../ModuleGraph/worker/JsFileWrapping');
const babylon = require('babylon');
const collectDependencies = require('../../ModuleGraph/worker/collect-dependencies');
const constantFolding = require('./constant-folding');
const generate = require('babel-generator').default;
@ -43,7 +44,7 @@ export type TransformArgs<ExtraOptions: {}> = {|
|};
export type TransformResults = {
ast: Ast,
ast: ?Ast,
};
export type Transform<ExtraOptions: {}> = (
@ -92,8 +93,12 @@ function postTransform(
isScript: boolean,
options: Options,
transformFileStartLogEntry: LogEntry,
ast: Ast,
receivedAst: ?Ast,
): Data {
// Transformers can ouptut null ASTs (if they ignore the file). In that case
// we need to parse the module source code to get their AST.
const ast = receivedAst || babylon.parse(sourceCode, {sourceType: 'module'});
const timeDelta = process.hrtime(transformFileStartLogEntry.start_timestamp);
const duration_ms = Math.round((timeDelta[0] * 1e9 + timeDelta[1]) / 1e6);
const transformFileEndLogEntry = {