Skip to content

Commit

Permalink
Add fonts to repo instead of downloading on each build
Browse files Browse the repository at this point in the history
  • Loading branch information
FlamingTempura committed Apr 19, 2024
1 parent 8ef198f commit f2905f7
Show file tree
Hide file tree
Showing 16 changed files with 1,027 additions and 874 deletions.
42 changes: 4 additions & 38 deletions build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ import { join } from 'path';
import { env } from 'process';
import { transform as swc, type Options, type Output } from '@swc/core';
import { generateDtsBundle } from 'dts-bundle-generator';
import {
build,
context,
type OnLoadResult,
type OutputFile,
type Plugin,
} from 'esbuild';
import { build, context, type OutputFile, type Plugin } from 'esbuild';
import sveltePlugin from 'esbuild-svelte';
import prettier from 'prettier';
//@ts-expect-error

Check failure on line 9 in build.ts

View workflow job for this annotation

GitHub Actions / lint

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
Expand Down Expand Up @@ -307,14 +301,11 @@ async function buildWebBundle() {
outfile: join(WEB_PATH, 'bundle.js'),
bundle: true,
write: false,
loader: { '.woff2': 'file' },
keepNames: true,
minify: true,
target: ['esnext'],
plugins: [
sveltePlugin({ preprocess: autoPreprocess() }),
googleFontPlugin,
regexpuPlugin,
],
plugins: [sveltePlugin({ preprocess: autoPreprocess() }), regexpuPlugin],
});

for (const file of outputFiles) {
Expand All @@ -339,12 +330,12 @@ async function serveWeb() {
bundle: true,
sourcemap: true,
write: false,
loader: { '.woff2': 'file' },
plugins: [
sveltePlugin({
preprocess: autoPreprocess(),
compilerOptions: { enableSourcemap: true },
}),
googleFontPlugin,
],
});
const server = await ctx.serve({ servedir: WEB_PATH });
Expand Down Expand Up @@ -375,31 +366,6 @@ const regexpuPlugin: Plugin = {
},
};

// Downloads google fonts and injects them as base64 urls into bundle css
const googleFontPlugin: Plugin = {
name: 'google-font-loader',
setup(build) {
build.onResolve({ filter: /^https?:\/\/fonts\./ }, (args) => ({
path: args.path,
namespace: 'http-url',
}));
build.onLoad(
{ filter: /.*/, namespace: 'http-url' },
async (args): Promise<OnLoadResult> => {
const res = await fetch(args.path, {
headers: {
// ensures google responds with woff2 fonts
'User-Agent': 'Mozilla/5.0 Firefox/90.0',
},
});
const contents = Buffer.from(await res.arrayBuffer());
const loader = args.path.endsWith('.woff2') ? 'dataurl' : 'css';
return { contents, loader };
},
);
},
};

/**
* swc converts js syntax to support older browsers. ESBuild can kinda do this
* but only for more recent browsers. swc is also far easier to configure than
Expand Down
Binary file added docs/IBMPlexSans-Light-SATUMA4V.woff2
Binary file not shown.
Binary file added docs/IBMPlexSans-Medium-ILA6RNXQ.woff2
Binary file not shown.
Binary file added docs/Inconsolata-Regular-CEGDCP7X.woff2
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/bundle.css

Large diffs are not rendered by default.

Loading

0 comments on commit f2905f7

Please sign in to comment.