From 4bdc17d4a11f0ba517c6d9124d296458332c536b Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Sat, 3 Jun 2023 15:07:19 -0400 Subject: [PATCH] inline scripts --- package-lock.json | 12 ++++ package.json | 1 + quartz/bootstrap.mjs | 34 ++++++++-- quartz/components/Head.tsx | 14 +--- quartz/components/Header.tsx | 4 +- quartz/components/scripts/darkmode.inline.ts | 3 + quartz/components/types.ts | 8 +++ quartz/index.ts | 2 +- quartz/path.ts | 10 ++- quartz/plugins/emitters/contentPage.tsx | 43 ++++++------ quartz/plugins/index.ts | 70 +++++++++++++++++++- quartz/plugins/transformers/ofm.ts | 6 +- quartz/plugins/types.ts | 2 + quartz/processors/emit.ts | 16 ++--- quartz/processors/filter.ts | 15 +++-- quartz/processors/parse.ts | 5 +- quartz/resources.ts | 1 + quartz/styles/base.scss | 2 + quartz/theme.ts | 8 +-- 19 files changed, 187 insertions(+), 69 deletions(-) create mode 100644 quartz/components/scripts/darkmode.inline.ts create mode 100644 quartz/components/types.ts diff --git a/package-lock.json b/package-lock.json index 0b292c076..e53a3e581 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@napi-rs/simple-git": "^0.1.8", "chalk": "^4.1.2", "cli-spinner": "^0.2.10", + "env-paths": "^3.0.0", "esbuild-sass-plugin": "^2.9.0", "github-slugger": "^2.0.0", "globby": "^13.1.4", @@ -1346,6 +1347,17 @@ "url": "https://github.com/fb55/entities?sponsor=1" } }, + "node_modules/env-paths": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/env-paths/-/env-paths-3.0.0.tgz", + "integrity": "sha512-dtJUTepzMW3Lm/NPxRf3wP4642UWhjL2sQxc+ym2YMj1m/H2zDNQOlezafzkHwn6sMstjHTwG6iQQsctDW/b1A==", + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/esbuild": { "version": "0.17.19", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.17.19.tgz", diff --git a/package.json b/package.json index 32175204c..83ddd542d 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "@napi-rs/simple-git": "^0.1.8", "chalk": "^4.1.2", "cli-spinner": "^0.2.10", + "env-paths": "^3.0.0", "esbuild-sass-plugin": "^2.9.0", "github-slugger": "^2.0.0", "globby": "^13.1.4", diff --git a/quartz/bootstrap.mjs b/quartz/bootstrap.mjs index 4886e3dab..16a0c695c 100755 --- a/quartz/bootstrap.mjs +++ b/quartz/bootstrap.mjs @@ -1,6 +1,7 @@ #!/usr/bin/env node -import { readFileSync } from 'fs' +import { promises, readFileSync } from 'fs' import yargs from 'yargs' +import path from 'path' import { hideBin } from 'yargs/helpers' import esbuild from 'esbuild' import chalk from 'chalk' @@ -61,9 +62,34 @@ yargs(hideBin(process.argv)) jsx: "automatic", jsxImportSource: "preact", external: ["@napi-rs/simple-git", "shiki"], - plugins: [sassPlugin({ - type: 'css-text' - })] + plugins: [ + sassPlugin({ + type: 'css-text' + }), + { + name: 'inline-script-loader', + setup(build) { + build.onLoad({ filter: /\.inline\.(ts|js)$/ }, async (args) => { + let text = await promises.readFile(args.path, 'utf8') + const transpiled = await esbuild.build({ + stdin: { + contents: text, + sourcefile: path.relative(path.resolve('.'), args.path), + }, + write: false, + bundle: true, + platform: "browser", + format: "esm", + }) + const rawMod = transpiled.outputFiles[0].text + return { + contents: rawMod, + loader: 'text', + } + }) + } + } + ] }).catch(err => { console.error(`${chalk.red("Couldn't parse Quartz configuration:")} ${fp}`) console.log(`Reason: ${chalk.grey(err)}`) diff --git a/quartz/components/Head.tsx b/quartz/components/Head.tsx index 868294dde..29050a799 100644 --- a/quartz/components/Head.tsx +++ b/quartz/components/Head.tsx @@ -8,10 +8,9 @@ export interface HeadProps { externalResources: StaticResources } -export default function({ title, description, slug, externalResources }: HeadProps) { +export function Component({ title, description, slug, externalResources }: HeadProps) { const { css, js } = externalResources const baseDir = resolveToRoot(slug) - const stylePath = baseDir + "/index.css" const iconPath = baseDir + "/static/icon.png" const ogImagePath = baseDir + "/static/og-image.png" return @@ -28,16 +27,7 @@ export default function({ title, description, slug, externalResources }: HeadPro - {css.map(href => )} - {js.filter(resource => resource.loadTime === "beforeDOMReady").map(resource =>