Skip to content

Commit

Permalink
Add typescript babel plugin (gatsbyjs#173)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
danielhusar authored and ChristopherBiscardi committed Oct 24, 2018
1 parent 128e26f commit 268376a
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/gatsby-plugin-mdx/component-with-mdx-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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;

Expand Down

0 comments on commit 268376a

Please sign in to comment.