-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
build-components.js
57 lines (50 loc) · 1.39 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
50
51
52
53
54
55
56
57
const fs = require('fs')
const glob = require('glob')
const {
articleMetadata,
commentaryMetadata,
supplementMetadata,
} = require('./utils/mdx.js')
glob.sync('./components/mdx/page-components.js').forEach((f) => {
if (fs.rmSync) return fs.rmSync(f)
})
const buildComponents = () => {
const componentMapping = [
...articleMetadata,
...commentaryMetadata,
...supplementMetadata,
].map(({ parentId, folder, id, components = [] }) => ({
id,
imports: components.map(({ name, src, exportName }) => ({
name,
exportName,
src: src
.replace(/^\.\//, `../../${folder}/${parentId ?? 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 article'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()