-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build-components.js
49 lines (42 loc) · 1.23 KB
/
build-components.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const fs = require('fs')
const glob = require('glob')
const { postMetadata } = require('./utils/mdx.js')
glob.sync('./components/mdx/page-components.js').forEach((f) => {
if (fs.rmSync) return fs.rmSync(f)
})
const buildComponents = () => {
const componentMapping = postMetadata.map(({ id, components = [] }) => ({
id,
imports: components.map(({ name, src, exportName }) => ({
name,
exportName,
src: src
.replace(/^\.\//, `../../posts/${id}/`)
.replace('../../components/', '../'),
})),
}))
const file = `
import dynamic from 'next/dynamic'
// NOTE: This is a dynamically generated file based on the config specified under the
// \`components\` key in each post's frontmatter.
const components = {
${componentMapping
.map(
({ id, imports }) => `
'${id}': {${imports
.map(
({ name, src, exportName }) =>
`${name}: dynamic(() => import('${src}').then((mod) => mod.${
exportName ?? name
} || mod.default))`
)
.join(',')}}
`
)
.join(',')}
}
export default components
`
fs.writeFileSync('./components/mdx/page-components.js', file)
}
buildComponents()