Skip to content

Commit 2e6a675

Browse files
authored
perf: reduce bundle size (closes #1607) (#1689)
1 parent e3162f7 commit 2e6a675

File tree

11 files changed

+94
-578
lines changed

11 files changed

+94
-578
lines changed

docs/advanced/making plugins.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ export const ContentPage: QuartzEmitterPlugin = () => {
274274
const allFiles = content.map((c) => c[1].data)
275275
for (const [tree, file] of content) {
276276
const slug = canonicalizeServer(file.data.slug!)
277-
const externalResources = pageResources(slug, resources)
277+
const externalResources = pageResources(slug, file.data, resources)
278278
const componentData: QuartzComponentProps = {
279279
fileData: file.data,
280280
externalResources,

package-lock.json

Lines changed: 52 additions & 547 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
"mdast-util-find-and-replace": "^3.0.1",
6060
"mdast-util-to-hast": "^13.2.0",
6161
"mdast-util-to-string": "^4.0.0",
62-
"mermaid": "^11.4.1",
6362
"micromorph": "^0.4.5",
6463
"pixi.js": "^8.6.6",
6564
"preact": "^10.25.4",

quartz/components/Head.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ export default (() => {
165165
<link rel="stylesheet" href={googleFontHref(cfg.theme)} />
166166
</>
167167
)}
168+
<link rel="preconnect" href="https://cdnjs.cloudflare.com" crossOrigin={"anonymous"} />
168169
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
169170
{/* OG/Twitter meta tags */}
170171
<meta name="og:site_name" content={cfg.pageTitle}></meta>

quartz/components/renderPage.tsx

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import { visit } from "unist-util-visit"
88
import { Root, Element, ElementContent } from "hast"
99
import { GlobalConfiguration } from "../cfg"
1010
import { i18n } from "../i18n"
11+
// @ts-ignore
12+
import mermaidScript from "./scripts/mermaid.inline"
13+
import mermaidStyle from "./styles/mermaid.inline.scss"
14+
import { QuartzPluginData } from "../plugins/vfile"
1115

1216
interface RenderComponents {
1317
head: QuartzComponent
@@ -23,12 +27,13 @@ interface RenderComponents {
2327
const headerRegex = new RegExp(/h[1-6]/)
2428
export function pageResources(
2529
baseDir: FullSlug | RelativeURL,
30+
fileData: QuartzPluginData,
2631
staticResources: StaticResources,
2732
): StaticResources {
2833
const contentIndexPath = joinSegments(baseDir, "static/contentIndex.json")
2934
const contentIndexScript = `const fetchData = fetch("${contentIndexPath}").then(data => data.json())`
3035

31-
return {
36+
const resources: StaticResources = {
3237
css: [
3338
{
3439
content: joinSegments(baseDir, "index.css"),
@@ -48,14 +53,28 @@ export function pageResources(
4853
script: contentIndexScript,
4954
},
5055
...staticResources.js,
51-
{
52-
src: joinSegments(baseDir, "postscript.js"),
53-
loadTime: "afterDOMReady",
54-
moduleType: "module",
55-
contentType: "external",
56-
},
5756
],
5857
}
58+
59+
if (fileData.hasMermaidDiagram) {
60+
resources.js.push({
61+
script: mermaidScript,
62+
loadTime: "afterDOMReady",
63+
moduleType: "module",
64+
contentType: "inline",
65+
})
66+
resources.css.push({ content: mermaidStyle, inline: true })
67+
}
68+
69+
// NOTE: we have to put this last to make sure spa.inline.ts is the last item.
70+
resources.js.push({
71+
src: joinSegments(baseDir, "postscript.js"),
72+
loadTime: "afterDOMReady",
73+
moduleType: "module",
74+
contentType: "external",
75+
})
76+
77+
return resources
5978
}
6079

6180
export function renderPage(

quartz/components/scripts/mermaid.inline.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { removeAllChildren } from "./util"
2-
import mermaid from "mermaid"
32

43
interface Position {
54
x: number
@@ -144,6 +143,7 @@ const cssVars = [
144143
"--codeFont",
145144
] as const
146145

146+
let mermaidImport = undefined
147147
document.addEventListener("nav", async () => {
148148
const center = document.querySelector(".center") as HTMLElement
149149
const nodes = center.querySelectorAll("code.mermaid") as NodeListOf<HTMLElement>
@@ -157,6 +157,12 @@ document.addEventListener("nav", async () => {
157157
{} as Record<(typeof cssVars)[number], string>,
158158
)
159159

160+
mermaidImport ||= await import(
161+
//@ts-ignore
162+
"https://cdnjs.cloudflare.com/ajax/libs/mermaid/11.4.0/mermaid.esm.min.mjs"
163+
)
164+
const mermaid = mermaidImport.default
165+
160166
const darkMode = document.documentElement.getAttribute("saved-theme") === "dark"
161167
mermaid.initialize({
162168
startOnLoad: false,

quartz/plugins/emitters/404.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ export const NotFoundPage: QuartzEmitterPlugin = () => {
3737

3838
const url = new URL(`https://${cfg.baseUrl ?? "example.com"}`)
3939
const path = url.pathname as FullSlug
40-
const externalResources = pageResources(path, resources)
4140
const notFound = i18n(cfg.locale).pages.error.title
4241
const [tree, vfile] = defaultProcessedContent({
4342
slug,
4443
text: notFound,
4544
description: notFound,
4645
frontmatter: { title: notFound, tags: [] },
4746
})
47+
const externalResources = pageResources(path, vfile.data, resources)
4848
const componentData: QuartzComponentProps = {
4949
ctx,
5050
fileData: vfile.data,

quartz/plugins/emitters/contentPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export const ContentPage: QuartzEmitterPlugin<Partial<FullPageLayout>> = (userOp
106106
containsIndex = true
107107
}
108108

109-
const externalResources = pageResources(pathToRoot(slug), resources)
109+
const externalResources = pageResources(pathToRoot(slug), file.data, resources)
110110
const componentData: QuartzComponentProps = {
111111
ctx,
112112
fileData: file.data,

quartz/plugins/emitters/folderPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ export const FolderPage: QuartzEmitterPlugin<Partial<FolderPageOptions>> = (user
106106

107107
for (const folder of folders) {
108108
const slug = joinSegments(folder, "index") as FullSlug
109-
const externalResources = pageResources(pathToRoot(slug), resources)
110109
const [tree, file] = folderDescriptions[folder]
110+
const externalResources = pageResources(pathToRoot(slug), file.data, resources)
111111
const componentData: QuartzComponentProps = {
112112
ctx,
113113
fileData: file.data,

quartz/plugins/emitters/tagPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ export const TagPage: QuartzEmitterPlugin<Partial<TagPageOptions>> = (userOpts)
114114

115115
for (const tag of tags) {
116116
const slug = joinSegments("tags", tag) as FullSlug
117-
const externalResources = pageResources(pathToRoot(slug), resources)
118117
const [tree, file] = tagDescriptions[tag]
118+
const externalResources = pageResources(pathToRoot(slug), file.data, resources)
119119
const componentData: QuartzComponentProps = {
120120
ctx,
121121
fileData: file.data,

0 commit comments

Comments
 (0)