2024-02-08 07:52:55 +00:00
|
|
|
import { FilePath, FullSlug, joinSegments } from "../../util/path"
|
2023-07-25 04:54:47 +00:00
|
|
|
import { QuartzEmitterPlugin } from "../types"
|
2023-07-24 01:20:43 +00:00
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
import spaRouterScript from "../../components/scripts/spa.inline"
|
|
|
|
// @ts-ignore
|
|
|
|
import popoverScript from "../../components/scripts/popover.inline"
|
2023-10-14 20:45:56 +00:00
|
|
|
import styles from "../../styles/custom.scss"
|
2023-07-24 01:20:43 +00:00
|
|
|
import popoverStyle from "../../components/styles/popover.scss"
|
2023-08-17 05:04:15 +00:00
|
|
|
import { BuildCtx } from "../../util/ctx"
|
2023-07-24 01:20:43 +00:00
|
|
|
import { QuartzComponent } from "../../components/types"
|
2023-08-17 05:04:15 +00:00
|
|
|
import { googleFontHref, joinStyles } from "../../util/theme"
|
2023-08-07 05:07:08 +00:00
|
|
|
import { Features, transform } from "lightningcss"
|
2023-12-28 16:02:04 +00:00
|
|
|
import { transform as transpile } from "esbuild"
|
2024-01-18 18:56:14 +00:00
|
|
|
import { write } from "./helpers"
|
2024-02-09 15:07:32 +00:00
|
|
|
import DepGraph from "../../depgraph"
|
2023-07-24 01:20:43 +00:00
|
|
|
|
|
|
|
type ComponentResources = {
|
|
|
|
css: string[]
|
|
|
|
beforeDOMLoaded: string[]
|
|
|
|
afterDOMLoaded: string[]
|
|
|
|
}
|
|
|
|
|
2023-07-24 07:04:01 +00:00
|
|
|
function getComponentResources(ctx: BuildCtx): ComponentResources {
|
2023-07-24 01:20:43 +00:00
|
|
|
const allComponents: Set<QuartzComponent> = new Set()
|
2023-07-24 07:04:01 +00:00
|
|
|
for (const emitter of ctx.cfg.plugins.emitters) {
|
|
|
|
const components = emitter.getQuartzComponents(ctx)
|
2023-07-24 01:20:43 +00:00
|
|
|
for (const component of components) {
|
|
|
|
allComponents.add(component)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const componentResources = {
|
|
|
|
css: new Set<string>(),
|
|
|
|
beforeDOMLoaded: new Set<string>(),
|
|
|
|
afterDOMLoaded: new Set<string>(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const component of allComponents) {
|
|
|
|
const { css, beforeDOMLoaded, afterDOMLoaded } = component
|
|
|
|
if (css) {
|
|
|
|
componentResources.css.add(css)
|
|
|
|
}
|
|
|
|
if (beforeDOMLoaded) {
|
|
|
|
componentResources.beforeDOMLoaded.add(beforeDOMLoaded)
|
|
|
|
}
|
|
|
|
if (afterDOMLoaded) {
|
|
|
|
componentResources.afterDOMLoaded.add(afterDOMLoaded)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
css: [...componentResources.css],
|
|
|
|
beforeDOMLoaded: [...componentResources.beforeDOMLoaded],
|
|
|
|
afterDOMLoaded: [...componentResources.afterDOMLoaded],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 16:02:04 +00:00
|
|
|
async function joinScripts(scripts: string[]): Promise<string> {
|
2023-07-24 01:20:43 +00:00
|
|
|
// wrap with iife to prevent scope collision
|
2023-12-28 16:02:04 +00:00
|
|
|
const script = scripts.map((script) => `(function () {${script}})();`).join("\n")
|
|
|
|
|
|
|
|
// minify with esbuild
|
|
|
|
const res = await transpile(script, {
|
|
|
|
minify: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
return res.code
|
2023-07-24 01:20:43 +00:00
|
|
|
}
|
|
|
|
|
2024-03-10 00:42:23 +00:00
|
|
|
function addGlobalPageResources(ctx: BuildCtx, componentResources: ComponentResources) {
|
2023-07-24 01:20:43 +00:00
|
|
|
const cfg = ctx.cfg.configuration
|
|
|
|
|
|
|
|
// popovers
|
|
|
|
if (cfg.enablePopovers) {
|
|
|
|
componentResources.afterDOMLoaded.push(popoverScript)
|
|
|
|
componentResources.css.push(popoverStyle)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cfg.analytics?.provider === "google") {
|
|
|
|
const tagId = cfg.analytics.tagId
|
|
|
|
componentResources.afterDOMLoaded.push(`
|
2024-03-10 00:42:23 +00:00
|
|
|
const gtagScript = document.createElement("script")
|
|
|
|
gtagScript.src = "https://www.googletagmanager.com/gtag/js?id=${tagId}"
|
|
|
|
gtagScript.async = true
|
|
|
|
document.head.appendChild(gtagScript)
|
|
|
|
|
2023-07-24 01:20:43 +00:00
|
|
|
window.dataLayer = window.dataLayer || [];
|
|
|
|
function gtag() { dataLayer.push(arguments); }
|
2023-12-19 07:13:37 +00:00
|
|
|
gtag("js", new Date());
|
|
|
|
gtag("config", "${tagId}", { send_page_view: false });
|
2024-01-18 18:56:14 +00:00
|
|
|
|
2023-12-19 07:13:37 +00:00
|
|
|
document.addEventListener("nav", () => {
|
|
|
|
gtag("event", "page_view", {
|
2023-07-24 01:20:43 +00:00
|
|
|
page_title: document.title,
|
|
|
|
page_location: location.href,
|
|
|
|
});
|
|
|
|
});`)
|
|
|
|
} else if (cfg.analytics?.provider === "plausible") {
|
2023-12-28 16:49:35 +00:00
|
|
|
const plausibleHost = cfg.analytics.host ?? "https://plausible.io"
|
|
|
|
componentResources.afterDOMLoaded.push(`
|
|
|
|
const plausibleScript = document.createElement("script")
|
|
|
|
plausibleScript.src = "${plausibleHost}/js/script.manual.js"
|
|
|
|
plausibleScript.setAttribute("data-domain", location.hostname)
|
|
|
|
plausibleScript.defer = true
|
|
|
|
document.head.appendChild(plausibleScript)
|
|
|
|
|
|
|
|
window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }
|
|
|
|
|
|
|
|
document.addEventListener("nav", () => {
|
|
|
|
plausible("pageview")
|
|
|
|
})
|
|
|
|
`)
|
2023-09-04 04:28:57 +00:00
|
|
|
} else if (cfg.analytics?.provider === "umami") {
|
|
|
|
componentResources.afterDOMLoaded.push(`
|
|
|
|
const umamiScript = document.createElement("script")
|
2024-02-27 16:05:28 +00:00
|
|
|
umamiScript.src = "${cfg.analytics.host ?? "https://analytics.umami.is"}/script.js"
|
2023-09-16 02:17:20 +00:00
|
|
|
umamiScript.setAttribute("data-website-id", "${cfg.analytics.websiteId}")
|
2023-09-04 04:28:57 +00:00
|
|
|
umamiScript.async = true
|
2024-01-18 18:56:14 +00:00
|
|
|
|
2023-09-04 04:28:57 +00:00
|
|
|
document.head.appendChild(umamiScript)
|
|
|
|
`)
|
2024-03-07 03:44:34 +00:00
|
|
|
} else if (cfg.analytics?.provider === "goatcounter") {
|
|
|
|
componentResources.afterDOMLoaded.push(`
|
|
|
|
const goatcounterScript = document.createElement("script")
|
|
|
|
goatcounterScript.src = "${cfg.analytics.scriptSrc ?? "https://gc.zgo.at/count.js"}"
|
|
|
|
goatcounterScript.async = true
|
|
|
|
goatcounterScript.setAttribute("data-goatcounter",
|
|
|
|
"https://${cfg.analytics.websiteId}.${cfg.analytics.host ?? "goatcounter.com"}/count")
|
|
|
|
document.head.appendChild(goatcounterScript)
|
|
|
|
`)
|
2024-04-09 01:43:09 +00:00
|
|
|
} else if (cfg.analytics?.provider === "posthog") {
|
|
|
|
componentResources.afterDOMLoaded.push(`
|
|
|
|
const posthogScript = document.createElement("script")
|
|
|
|
posthogScript.innerHTML= \`!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags getFeatureFlag getFeatureFlagPayload reloadFeatureFlags group updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures getActiveMatchingSurveys getSurveys onSessionId".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
|
|
|
|
posthog.init('${cfg.analytics.apiKey}',{api_host:'${cfg.analytics.host ?? "https://app.posthog.com"}'})\`
|
|
|
|
document.head.appendChild(posthogScript)
|
|
|
|
`)
|
2024-05-06 16:30:21 +00:00
|
|
|
} else if (cfg.analytics?.provider === "tinylytics") {
|
|
|
|
const siteId = cfg.analytics.siteId
|
|
|
|
componentResources.afterDOMLoaded.push(`
|
|
|
|
const tinylyticsScript = document.createElement("script")
|
|
|
|
tinylyticsScript.src = "https://tinylytics.app/embed/${siteId}.js"
|
|
|
|
tinylyticsScript.defer = true
|
|
|
|
document.head.appendChild(tinylyticsScript)
|
|
|
|
`)
|
2023-07-24 01:20:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cfg.enableSPA) {
|
|
|
|
componentResources.afterDOMLoaded.push(spaRouterScript)
|
|
|
|
} else {
|
|
|
|
componentResources.afterDOMLoaded.push(`
|
2024-02-02 04:07:14 +00:00
|
|
|
window.spaNavigate = (url, _) => window.location.assign(url)
|
|
|
|
window.addCleanup = () => {}
|
|
|
|
const event = new CustomEvent("nav", { detail: { url: document.body.dataset.slug } })
|
|
|
|
document.dispatchEvent(event)
|
|
|
|
`)
|
2023-07-24 01:20:43 +00:00
|
|
|
}
|
2023-07-25 04:54:47 +00:00
|
|
|
}
|
|
|
|
|
2024-03-10 00:42:23 +00:00
|
|
|
// This emitter should not update the `resources` parameter. If it does, partial
|
|
|
|
// rebuilds may not work as expected.
|
|
|
|
export const ComponentResources: QuartzEmitterPlugin = () => {
|
2023-07-25 04:54:47 +00:00
|
|
|
return {
|
|
|
|
name: "ComponentResources",
|
|
|
|
getQuartzComponents() {
|
|
|
|
return []
|
|
|
|
},
|
2024-03-10 00:42:23 +00:00
|
|
|
async getDependencyGraph(_ctx, _content, _resources) {
|
|
|
|
return new DepGraph<FilePath>()
|
2024-02-09 15:07:32 +00:00
|
|
|
},
|
2024-03-10 00:42:23 +00:00
|
|
|
async emit(ctx, _content, _resources): Promise<FilePath[]> {
|
2024-02-08 07:52:55 +00:00
|
|
|
const promises: Promise<FilePath>[] = []
|
|
|
|
const cfg = ctx.cfg.configuration
|
2023-07-25 04:54:47 +00:00
|
|
|
// component specific scripts and styles
|
|
|
|
const componentResources = getComponentResources(ctx)
|
2024-02-08 07:52:55 +00:00
|
|
|
let googleFontsStyleSheet = ""
|
2024-03-10 00:42:23 +00:00
|
|
|
if (cfg.theme.fontOrigin === "local") {
|
2023-07-25 04:54:47 +00:00
|
|
|
// let the user do it themselves in css
|
2024-03-10 00:42:23 +00:00
|
|
|
} else if (cfg.theme.fontOrigin === "googleFonts" && !cfg.theme.cdnCaching) {
|
|
|
|
// when cdnCaching is true, we link to google fonts in Head.tsx
|
|
|
|
let match
|
|
|
|
|
|
|
|
const fontSourceRegex = /url\((https:\/\/fonts.gstatic.com\/s\/[^)]+\.(woff2|ttf))\)/g
|
|
|
|
|
|
|
|
googleFontsStyleSheet = await (
|
|
|
|
await fetch(googleFontHref(ctx.cfg.configuration.theme))
|
|
|
|
).text()
|
|
|
|
|
|
|
|
while ((match = fontSourceRegex.exec(googleFontsStyleSheet)) !== null) {
|
|
|
|
// match[0] is the `url(path)`, match[1] is the `path`
|
|
|
|
const url = match[1]
|
|
|
|
// the static name of this file.
|
|
|
|
const [filename, ext] = url.split("/").pop()!.split(".")
|
|
|
|
|
|
|
|
googleFontsStyleSheet = googleFontsStyleSheet.replace(
|
|
|
|
url,
|
|
|
|
`https://${cfg.baseUrl}/static/fonts/${filename}.ttf`,
|
|
|
|
)
|
|
|
|
|
|
|
|
promises.push(
|
|
|
|
fetch(url)
|
|
|
|
.then((res) => {
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error(`Failed to fetch font`)
|
|
|
|
}
|
|
|
|
return res.arrayBuffer()
|
|
|
|
})
|
|
|
|
.then((buf) =>
|
|
|
|
write({
|
|
|
|
ctx,
|
|
|
|
slug: joinSegments("static", "fonts", filename) as FullSlug,
|
|
|
|
ext: `.${ext}`,
|
|
|
|
content: Buffer.from(buf),
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
)
|
2024-02-08 07:52:55 +00:00
|
|
|
}
|
2023-07-25 04:54:47 +00:00
|
|
|
}
|
|
|
|
|
2024-02-17 18:34:46 +00:00
|
|
|
// important that this goes *after* component scripts
|
|
|
|
// as the "nav" event gets triggered here and we should make sure
|
|
|
|
// that everyone else had the chance to register a listener for it
|
2024-03-10 00:42:23 +00:00
|
|
|
addGlobalPageResources(ctx, componentResources)
|
2023-07-25 04:54:47 +00:00
|
|
|
|
2024-02-08 07:52:55 +00:00
|
|
|
const stylesheet = joinStyles(
|
|
|
|
ctx.cfg.configuration.theme,
|
|
|
|
googleFontsStyleSheet,
|
2024-02-17 18:34:46 +00:00
|
|
|
...componentResources.css,
|
2024-02-08 07:52:55 +00:00
|
|
|
styles,
|
|
|
|
)
|
2023-12-28 16:02:04 +00:00
|
|
|
const [prescript, postscript] = await Promise.all([
|
|
|
|
joinScripts(componentResources.beforeDOMLoaded),
|
|
|
|
joinScripts(componentResources.afterDOMLoaded),
|
|
|
|
])
|
|
|
|
|
2024-02-08 07:52:55 +00:00
|
|
|
promises.push(
|
2024-01-18 18:56:14 +00:00
|
|
|
write({
|
|
|
|
ctx,
|
2023-08-19 22:52:25 +00:00
|
|
|
slug: "index" as FullSlug,
|
2023-07-25 04:54:47 +00:00
|
|
|
ext: ".css",
|
|
|
|
content: transform({
|
|
|
|
filename: "index.css",
|
|
|
|
code: Buffer.from(stylesheet),
|
2023-07-26 04:10:37 +00:00
|
|
|
minify: true,
|
2023-08-08 00:34:38 +00:00
|
|
|
targets: {
|
|
|
|
safari: (15 << 16) | (6 << 8), // 15.6
|
2023-08-08 04:51:06 +00:00
|
|
|
ios_saf: (15 << 16) | (6 << 8), // 15.6
|
2023-08-08 00:34:38 +00:00
|
|
|
edge: 115 << 16,
|
|
|
|
firefox: 102 << 16,
|
|
|
|
chrome: 109 << 16,
|
|
|
|
},
|
2023-08-07 05:07:33 +00:00
|
|
|
include: Features.MediaQueries,
|
2023-07-25 04:54:47 +00:00
|
|
|
}).code.toString(),
|
|
|
|
}),
|
2024-01-18 18:56:14 +00:00
|
|
|
write({
|
|
|
|
ctx,
|
2023-08-19 22:52:25 +00:00
|
|
|
slug: "prescript" as FullSlug,
|
2023-07-25 04:54:47 +00:00
|
|
|
ext: ".js",
|
|
|
|
content: prescript,
|
|
|
|
}),
|
2024-01-18 18:56:14 +00:00
|
|
|
write({
|
|
|
|
ctx,
|
2023-08-19 22:52:25 +00:00
|
|
|
slug: "postscript" as FullSlug,
|
2023-07-25 04:54:47 +00:00
|
|
|
ext: ".js",
|
|
|
|
content: postscript,
|
|
|
|
}),
|
2024-02-08 07:52:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return await Promise.all(promises)
|
2023-07-25 04:54:47 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|