From 81a500cd673e495cf4ce7fe0680c6a766cf4987e Mon Sep 17 00:00:00 2001 From: Rafael Oleza Date: Fri, 12 Jan 2018 07:39:46 -0800 Subject: [PATCH] Tweak default transformer to do some very minimal babel transformation Reviewed By: jeanlauliac Differential Revision: D6711526 fbshipit-source-id: 03c6aea85fb6d13e92e458a46bf4a3db1268aa11 --- packages/metro/src/defaultTransform.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/metro/src/defaultTransform.js b/packages/metro/src/defaultTransform.js index 2020ed6f..12e706a4 100644 --- a/packages/metro/src/defaultTransform.js +++ b/packages/metro/src/defaultTransform.js @@ -11,10 +11,27 @@ */ 'use strict'; -const babylon = require('babylon'); +const babel = require('babel-core'); -module.exports.transform = (file: {filename: string, src: string}) => { - const ast = babylon.parse(file.src, {sourceType: 'module'}); +import type {TransformOptions} from './JSTransformer/worker'; +import type {Plugins as BabelPlugins} from 'babel-core'; - return {ast}; +type Params = { + filename: string, + options: TransformOptions, + plugins?: BabelPlugins, + src: string, +}; + +module.exports.transform = ({filename, options, plugins, src}: Params) => { + const OLD_BABEL_ENV = process.env.BABEL_ENV; + process.env.BABEL_ENV = options.dev ? 'development' : 'production'; + + try { + const {ast} = babel.transform(src, {filename, code: false, plugins}); + + return {ast}; + } finally { + process.env.BABEL_ENV = OLD_BABEL_ENV; + } };