forked from gatsbyjs/gatsby
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby-plugin-mdx): Use babel plugin to remove export keyword
For very large MDX documents babel will deopt styling. This results in variations in whitespace that can't be handled by the original regex for stripping the export keyword. This replaces that functionality with a plugin. - ChristopherBiscardi/gatsby-mdx#411 - https://github.com/mdx-js/mdx#618
- Loading branch information
Showing
3 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
packages/gatsby-plugin-mdx/utils/babel-plugin-remove-export-keywords/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module.exports = function removeExportKeywords() { | ||
return { | ||
visitor: { | ||
ExportNamedDeclaration(path) { | ||
const declaration = path.node.declaration; | ||
|
||
// Ignore "export { Foo as default }"" syntax | ||
if (declaration) { | ||
path.replaceWith(declaration); | ||
} | ||
} | ||
} | ||
}; | ||
}; |
35 changes: 35 additions & 0 deletions
35
packages/gatsby-plugin-mdx/utils/babel-plugin-remove-export-keywords/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const babel = require("@babel/core"); | ||
|
||
const plugin = require("."); | ||
|
||
const testContents = ` | ||
export const foo = 'bar'; | ||
/** @jsx mdx*/ export const _frontmatter = { | ||
title: "Here's a title with the word export" | ||
}; | ||
const MDXContent = function () {}; | ||
export { MDXContent as default }; | ||
`; | ||
const expectedResults = `const foo = 'bar'; | ||
/** @jsx mdx*/ | ||
const _frontmatter = { | ||
title: "Here's a title with the word export" | ||
}; | ||
const MDXContent = function () {}; | ||
export { MDXContent as default };` | ||
|
||
describe("babel-plugin-remove-export-keyword", () => { | ||
test("removes all export keywords", () => { | ||
const result = babel.transform(testContents, { | ||
configFile: false, | ||
plugins: [plugin], | ||
presets: [require("@babel/preset-react")] | ||
}); | ||
|
||
expect(result.code).toEqual(expectedResults) | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters