From 83b41a0dd5bd8f86e332a4f793907708ff44bc1d Mon Sep 17 00:00:00 2001 From: Daniel Husar Date: Tue, 23 Oct 2018 23:28:16 -0700 Subject: [PATCH] Add typescript babel plugin (#173) Add typescript plugin only for `.ts` and `.tsx` files We can safely assume that anyone building a typescript gatsby site is using something like: https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-typescript, so we don't need to force the dependency on anyone not using typescript --- packages/gatsby-plugin-mdx/component-with-mdx-scope.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-plugin-mdx/component-with-mdx-scope.js b/packages/gatsby-plugin-mdx/component-with-mdx-scope.js index 1063d5645fec1..32c763136a3ed 100644 --- a/packages/gatsby-plugin-mdx/component-with-mdx-scope.js +++ b/packages/gatsby-plugin-mdx/component-with-mdx-scope.js @@ -3,6 +3,7 @@ const crypto = require("crypto"); const path = require("path"); const babel = require("@babel/core"); const syntaxObjRestSpread = require("@babel/plugin-syntax-object-rest-spread"); +const typescriptPlugin = require("@babel/plugin-transform-typescript"); const debug = require("debug")("gatsby-mdx:component-with-mdx-scope"); const slash = require("slash"); @@ -18,12 +19,18 @@ module.exports = function componentWithMDXScope( if (typeof codeScopeAbsPaths === "string") { codeScopeAbsPaths = [codeScopeAbsPaths]; } + const isTS = absWrapperPath.endsWith('.ts'); + const isTSX = absWrapperPath.endsWith('.tsx'); // hoist pageQuery and any other named exports const OGWrapper = fs.readFileSync(absWrapperPath, "utf-8"); const instance = new BabelPluginPluckExports(); + const plugins = [instance.plugin, syntaxObjRestSpread]; + if (isTS || isTSX) { + plugins.push([typescriptPlugin, { isTSX }]); + } babel.transform(OGWrapper, { - plugins: [instance.plugin, syntaxObjRestSpread], + plugins, presets: [require("@babel/preset-react")] }).code;