-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcra.js
33 lines (26 loc) · 792 Bytes
/
cra.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'use strict'
const matter = require('gray-matter')
const stringifyObject = require('stringify-object')
const mdx = require('@mdx-js/mdx')
const babel = require('@babel/core')
function parseFrontMatter(src) {
const { content, data } = matter(src)
return `export const frontMatter = ${stringifyObject(
data,
)};
${content}`
}
function createTransformer(src) {
const withFrontMatter = parseFrontMatter(src)
const jsx = mdx.sync(withFrontMatter)
const toTransform = `import {mdx} from '@mdx-js/react';${jsx}`
return babel.transform(toTransform, {
presets: [require.resolve('babel-preset-react-app')],
babelrc: false,
configFile: false,
filename: 'null',
}).code
}
module.exports = {
process: createTransformer,
}