From de7f1d90f22bfbaf3ed434168a8a378305a7b6a2 Mon Sep 17 00:00:00 2001 From: Rafael Oleza Date: Sat, 2 Dec 2017 00:25:58 -0800 Subject: [PATCH] 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 --- packages/metro/src/JSTransformer/worker/index.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/metro/src/JSTransformer/worker/index.js b/packages/metro/src/JSTransformer/worker/index.js index 80893617..3ab8f67e 100644 --- a/packages/metro/src/JSTransformer/worker/index.js +++ b/packages/metro/src/JSTransformer/worker/index.js @@ -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 = {| |}; export type TransformResults = { - ast: Ast, + ast: ?Ast, }; export type Transform = ( @@ -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 = {