From 5f58ae0e8404f0e0ddaca077750857f6f5c066a9 Mon Sep 17 00:00:00 2001 From: Rafael Oleza Date: Wed, 22 Nov 2017 13:29:40 -0800 Subject: [PATCH] Metro reads AST from transformers if available Reviewed By: BYK Differential Revision: D6396330 fbshipit-source-id: 10539a1cce81b090a781df7d75515d52ece9ef9c --- .../src/JSTransformer/worker/index.js | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/metro-bundler/src/JSTransformer/worker/index.js b/packages/metro-bundler/src/JSTransformer/worker/index.js index 0bebcce7..f8603a70 100644 --- a/packages/metro-bundler/src/JSTransformer/worker/index.js +++ b/packages/metro-bundler/src/JSTransformer/worker/index.js @@ -14,6 +14,7 @@ const constantFolding = require('./constant-folding'); const extractDependencies = require('./extract-dependencies'); +const generate = require('babel-generator').default; const inline = require('./inline'); const minify = require('./minify'); @@ -117,7 +118,7 @@ function transformCode( ? [] : [[inline.plugin, options], [constantFolding.plugin, options]]; - const transformed = transformer.transform({ + const result = transformer.transform({ filename, localPath, options: options.transform, @@ -125,6 +126,23 @@ function transformCode( src: sourceCode, }); + // If we receive AST from the transformer, serialize it into code/map. + const transformed = result.ast + ? generate( + result.ast, + { + code: false, + comments: false, + compact: false, + filename: localPath, + retainLines: false, + sourceFileName: filename, + sourceMaps: true, + }, + sourceCode, + ) + : {code: result.code, map: result.map}; + // If the transformer returns standard sourcemaps, we need to transform them // to rawMappings so we can process them correctly. const rawMappings =