Skip to content

Commit 4b8ed5c

Browse files
committed
Defer all code splitting to consumers
1 parent 9f3c391 commit 4b8ed5c

File tree

9 files changed

+57
-37
lines changed

9 files changed

+57
-37
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"image-type": "^4.1.0",
2222
"jest": "^24.8.0",
2323
"js-yaml": "^3.13.1",
24+
"lodash.flatmap": "^4.5.0",
2425
"lodash.merge": "^4.6.2",
2526
"markdown-it": "^9.1.0",
2627
"next": "^9.0.3",

pages/docs/[doc].jsx

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
11
import { useState, useEffect } from 'react';
22
import { useRouter } from 'next/router';
33
import template from 'lodash/template';
4+
import markdownit from 'markdown-it';
5+
import flatMap from 'lodash.flatmap';
46

57
import topics from '../../src/topics.json';
68
import ContentBlock from '../../src/ContentBlock';
7-
import { getDoc } from '../../src/utils';
89

910
import prismCss from 'raw-loader!prismjs/themes/prism-tomorrow.css';
1011

12+
const md = new markdownit();
13+
1114
const Page = () => {
1215
const [doc, setDoc] = useState(null);
16+
const [highlight, setHighlight] = useState(false);
17+
1318
const router = useRouter();
1419

20+
async function loadDoc(name) {
21+
const mod = await import(`../../docs/${name}/config.yaml`);
22+
const doc = mod.default;
23+
setDoc(doc);
24+
25+
const contents = Object.values(doc.topics).map(topic => topic.content);
26+
const languageImports = flatMap(contents, content =>
27+
ContentBlock.parseLanguages(content)
28+
).map(lang => import(`prismjs/components/prism-${lang}`));
29+
await Promise.all(languageImports);
30+
setHighlight(true);
31+
}
32+
1533
useEffect(() => {
1634
if (router.query.doc) {
17-
getDoc(router.query.doc).then(doc => setDoc(doc));
35+
loadDoc(router.query.doc);
1836
}
1937
}, [router.query.doc]);
2038

@@ -36,7 +54,10 @@ const Page = () => {
3654
<div key={topic.key}>
3755
<h2>{topic.name}</h2>
3856
{'content' in doc.topics[topic.key] ? (
39-
<ContentBlock content={doc.topics[topic.key].content} />
57+
<ContentBlock
58+
content={doc.topics[topic.key].content}
59+
highlight={highlight}
60+
/>
4061
) : (
4162
'No content'
4263
)}

pages/index.jsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import Link from 'next/link';
22
import { listDocs } from '../src/utils';
33

4-
const Page = ({ docs }) => (
4+
const Page = () => (
55
<div>
66
<h1>Docs</h1>
77
<ul>
8-
{docs.map(doc => (
8+
{listDocs().map(doc => (
99
<li key={doc}>
1010
<Link href="/docs/[doc]" as={`/docs/${doc}`}>
1111
<a>{doc}</a>
@@ -16,6 +16,4 @@ const Page = ({ docs }) => (
1616
</div>
1717
);
1818

19-
Page.getInitialProps = () => listDocs().then(docs => ({ docs }));
20-
2119
export default Page;

src/ContentBlock.jsx

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,20 @@ import markdownit from 'markdown-it';
55

66
import { parseTemplate } from './utils';
77

8-
const toHTML = (content, data = {}, highlight = true) => {
8+
const ContentBlock = ({ content, data, highlight }) => {
9+
const html = ContentBlock.toHTML(content, data, highlight);
10+
return <div dangerouslySetInnerHTML={{ __html: html }} />;
11+
};
12+
13+
ContentBlock.parseLanguages = content => {
14+
const md = new markdownit();
15+
return md
16+
.parse(content)
17+
.filter(token => token.type === 'fence')
18+
.map(token => token.info);
19+
};
20+
21+
ContentBlock.toHTML = (content, data = {}, highlight = false) => {
922
const md = new markdownit({
1023
highlight: (str, lang) => {
1124
if (highlight && lang) {
@@ -23,35 +36,18 @@ const toHTML = (content, data = {}, highlight = true) => {
2336
});
2437

2538
const markdown = parseTemplate(content, data);
26-
27-
if (highlight) {
28-
const languageImports = md
29-
.parse(markdown)
30-
.filter(token => token.type === 'fence')
31-
.map(token => import(`prismjs/components/prism-${token.info}`));
32-
return Promise.all(languageImports).then(() => md.render(markdown));
33-
}
34-
3539
return md.render(markdown);
3640
};
3741

38-
const ContentBlock = ({ content, data }) => {
39-
const [html, setHtml] = useState(toHTML(content, data, false));
40-
41-
useEffect(() => {
42-
toHTML(content, data).then(result => setHtml(result));
43-
}, [content, data]);
44-
45-
return <div dangerouslySetInnerHTML={{ __html: html }} />;
46-
};
47-
4842
ContentBlock.propTypes = {
4943
content: PropTypes.string.isRequired,
5044
data: PropTypes.object,
45+
highlight: PropTypes.bool,
5146
};
5247

5348
ContentBlock.defaultProps = {
5449
data: {},
50+
highlight: false,
5551
};
5652

5753
export default ContentBlock;

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { listDocs, getDoc, parseTemplate } from './utils';
1+
import { listDocs, parseTemplate } from './utils';
22

3-
export { listDocs, getDoc, parseTemplate };
4-
export default { listDocs, getDoc, parseTemplate };
3+
export { listDocs, parseTemplate };
4+
export default { listDocs, parseTemplate };

src/topics.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
{
1111
"key": "client",
12-
"name": "Client reqeusts"
12+
"name": "Client requests"
1313
},
1414
{
1515
"key": "renewal",

src/utils.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import template from 'lodash.template';
22

33
import docs from './docs';
44

5-
export const listDocs = () => Promise.resolve(docs);
6-
export const getDoc = name =>
7-
import(`../docs/${name}/config.yaml`).then(mod => mod.default);
5+
export const listDocs = () => docs;
86

97
export const parseTemplate = (content, data = {}) => {
108
const parse = template(content);

webpack/config.dist.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ module.exports = {
4141
],
4242
},
4343
externals: {
44-
React: 'react',
45-
ReactDOM: 'react-dom',
46-
Prism: 'prismjs',
44+
react: 'react',
45+
'react-dom': 'react-dom',
46+
prismjs: 'prismjs',
47+
'markdown-it': 'markdown-it',
4748
},
4849
};

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4137,6 +4137,11 @@ lodash._reinterpolate@^3.0.0:
41374137
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
41384138
integrity sha1-DM8tiRZq8Ds2Y8eWU4t1rG4RTZ0=
41394139

4140+
lodash.flatmap@^4.5.0:
4141+
version "4.5.0"
4142+
resolved "https://registry.yarnpkg.com/lodash.flatmap/-/lodash.flatmap-4.5.0.tgz#ef8cbf408f6e48268663345305c6acc0b778702e"
4143+
integrity sha1-74y/QI9uSCaGYzRTBcaswLd4cC4=
4144+
41404145
lodash.merge@^4.6.2:
41414146
version "4.6.2"
41424147
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"

0 commit comments

Comments
 (0)