fix(v2): move metadata export after compiling MDX to avoid weird MDX parsing error. (#2105)

* fix(v2): move metadata export to mdx-loader to prevent any weird mdx parsing

* refactor

* nits

* nits

* nits
This commit is contained in:
Endi 2019-12-11 16:33:47 +07:00 committed by GitHub
parent 1f0eb37e19
commit ace93c5a14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 65 deletions

View file

@ -6,6 +6,7 @@
*/
const {getOptions} = require('loader-utils');
const {readFile} = require('fs-extra');
const mdx = require('@mdx-js/mdx');
const emoji = require('remark-emoji');
const slug = require('remark-slug');
@ -43,11 +44,25 @@ module.exports = async function(fileString) {
return callback(err);
}
let exportStr = `export const frontMatter = ${stringifyObject(data)};`;
// Read metadata for this MDX and export it
if (options.metadataPath && typeof options.metadataPath === 'function') {
const metadataPath = options.metadataPath(this.resourcePath);
if (metadataPath) {
// Add as dependency of this loader result so that we can recompile if metadata is changed
this.addDependency(metadataPath);
const metadata = await readFile(metadataPath, 'utf8');
exportStr += `\nexport const metadata = ${metadata};`;
}
}
const code = `
import React from 'react';
import { mdx } from '@mdx-js/react';
export const frontMatter = ${stringifyObject(data)};
${exportStr}
${result}
`;