From 70dadc63f942ab342df183d5c4df57083260b513 Mon Sep 17 00:00:00 2001 From: Rafael Oleza Date: Thu, 23 Nov 2017 03:04:56 -0800 Subject: [PATCH] Update Readme + change default transform Reviewed By: cpojer Differential Revision: D6401596 fbshipit-source-id: 8928abb825933edd84c508a4f04f50c748725722 --- README.md | 14 ++++++++------ packages/metro-bundler/src/defaultTransform.js | 8 +++++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 9003a603..ada0812b 100644 --- a/README.md +++ b/README.md @@ -137,14 +137,16 @@ The JavaScript transformer (`transformModulePath`) is the place where JS code wi ## Method `transform(module)` -Mandatory method that will transform code. The object received has information about the module being transformed (e.g its path, code...) and the returned object has to contain a `code` key that is the result of the transform. The default transformer shipped is a pretty good place to start: +Mandatory method that will transform code. The object received has information about the module being transformed (e.g its path, code...) and the returned object has to contain an `ast` key that is the AST representation of the transformed code. The default shipped transformer does the bare minimum amount of work by just parsing the code to AST: ```js -module.exports.transform = file => { - return { - code: file.src, - }; -}); +const babylon = require('babylon'); + +module.exports.transform = (file: {filename: string, src: string}) => { + const ast = babylon.parse(code, {sourceType: 'module'}); + + return {ast}; +}; ``` If you would like to plug-in babel, you can simply do that by passing the code to it: diff --git a/packages/metro-bundler/src/defaultTransform.js b/packages/metro-bundler/src/defaultTransform.js index a8ddc002..2020ed6f 100644 --- a/packages/metro-bundler/src/defaultTransform.js +++ b/packages/metro-bundler/src/defaultTransform.js @@ -11,4 +11,10 @@ */ 'use strict'; -module.exports.transform = (file: {src: string}) => ({code: file.src}); +const babylon = require('babylon'); + +module.exports.transform = (file: {filename: string, src: string}) => { + const ast = babylon.parse(file.src, {sourceType: 'module'}); + + return {ast}; +};