From 005bbfd027b5bfa5c0b0d773c4691a7e7fb177b6 Mon Sep 17 00:00:00 2001 From: Avi Goldman Date: Thu, 13 Sep 2018 16:27:54 -0700 Subject: [PATCH] Move the gather exports babel plugin to its own package (#121) * new package * added tests * use new package --- packages/gatsby-plugin-mdx/package.json | 2 +- .../utils/babel-plugin-gather-exports.js | 34 ------------------- .../utils/extract-exports.js | 8 ++--- 3 files changed, 5 insertions(+), 39 deletions(-) delete mode 100644 packages/gatsby-plugin-mdx/utils/babel-plugin-gather-exports.js diff --git a/packages/gatsby-plugin-mdx/package.json b/packages/gatsby-plugin-mdx/package.json index 4d12c6750d3aa..6d029a582e7b8 100644 --- a/packages/gatsby-plugin-mdx/package.json +++ b/packages/gatsby-plugin-mdx/package.json @@ -13,6 +13,7 @@ }, "dependencies": { "@babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.54", + "babel-plugin-gather-exports": "0.0.1", "babel-plugin-pluck-exports": "0.0.1", "babel-plugin-pluck-imports": "0.0.1", "debug": "^3.1.0", @@ -20,7 +21,6 @@ "fs-extra": "^7.0.0", "gray-matter": "^4.0.1", "htmltojsx": "^0.3.0", - "json5": "^1.0.1", "lodash": "^4.17.10", "mdast-util-to-string": "^1.0.4", "mdast-util-toc": "^2.0.1", diff --git a/packages/gatsby-plugin-mdx/utils/babel-plugin-gather-exports.js b/packages/gatsby-plugin-mdx/utils/babel-plugin-gather-exports.js deleted file mode 100644 index db554b7f6fc7a..0000000000000 --- a/packages/gatsby-plugin-mdx/utils/babel-plugin-gather-exports.js +++ /dev/null @@ -1,34 +0,0 @@ -const generate = require("babel-generator").default; -const JSON5 = require("json5"); - -module.exports = () => { - let results = {}; - - const plugin = () => { - const visitor = { - ExportNamedDeclaration(path) { - const declaration = path.node.declaration; - if ( - declaration.type === "VariableDeclaration" && - declaration.kind === "const" - ) { - declaration.declarations.forEach(declarator => { - try { - results[declarator.id.name] = JSON5.parse( - generate(declarator.init).code - ); - } catch (e) { - // why was this empty? - } - }); - } - } - }; - - return { visitor }; - }; - - plugin.results = () => results; - - return plugin; -}; diff --git a/packages/gatsby-plugin-mdx/utils/extract-exports.js b/packages/gatsby-plugin-mdx/utils/extract-exports.js index c0ced37bd3e86..3cf31d40e48d7 100644 --- a/packages/gatsby-plugin-mdx/utils/extract-exports.js +++ b/packages/gatsby-plugin-mdx/utils/extract-exports.js @@ -1,17 +1,17 @@ const babel = require("@babel/core"); const babelReact = require("@babel/preset-react"); const objRestSpread = require("@babel/plugin-proposal-object-rest-spread"); -const gatherExportsGenerator = require("./babel-plugin-gather-exports"); +const BabelPluginGatherExports = require("babel-plugin-gather-exports"); // grab all the export values module.exports = code => { - const gatherExports = gatherExportsGenerator(); + const instance = new BabelPluginGatherExports(); babel.transform(code, { presets: [babelReact], - plugins: [gatherExports, objRestSpread] + plugins: [instance.plugin, objRestSpread] }); - const exportedVariables = gatherExports.results(); + const exportedVariables = instance.state.exports; // grab the frontmatter const {