diff --git a/rollup.config.mjs b/rollup.config.mjs index 2b59aa8..257c635 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -8,6 +8,7 @@ import del from "rollup-plugin-delete"; import dts from "rollup-plugin-dts"; import external from "rollup-plugin-peer-deps-external"; import postcss from "rollup-plugin-postcss"; +import { inlineMalachiteFonts } from "./scripts/inline-malachite-fonts.mjs"; const pkg = createRequire(import.meta.url)("./package.json"); @@ -79,6 +80,7 @@ export default [ inlineDynamicImports: true, }, plugins: [ + inlineMalachiteFonts(), resolve({ preferBuiltins: false, browser: true, diff --git a/scripts/inline-malachite-fonts.mjs b/scripts/inline-malachite-fonts.mjs new file mode 100644 index 0000000..f4b2e0b --- /dev/null +++ b/scripts/inline-malachite-fonts.mjs @@ -0,0 +1,32 @@ +import { existsSync, readFileSync } from 'node:fs'; + +const MALACHITE = /[\\/]@stackone[\\/]malachite[\\/]/; +const WRAPPER = /\.woff2\.js$/; + +// malachite ships each font as a woff2 file beside a wrapper that exports +// `new URL("./.woff2", import.meta.url)`. In the IIFE that URL resolves next to wherever +// the customer serves webcomponent.js, where no font file exists, so each face the page uses +// 404s before malachite falls back to its inlined copy. Point each wrapper at that copy instead. +export const inlineMalachiteFonts = () => ({ + name: 'hub:inline-malachite-fonts', + async resolveId(source, importer, options) { + if (!WRAPPER.test(source)) { + return null; + } + const resolved = await this.resolve(source, importer, { ...options, skipSelf: true }); + if (!resolved || !MALACHITE.test(resolved.id)) { + return resolved; + } + const inlined = resolved.id.replace(WRAPPER, '.woff2.inline.js'); + if (existsSync(inlined)) { + return { ...resolved, id: inlined }; + } + // Before 2.134.1 the wrapper is itself the data URL, and there is nothing to swap. + if (readFileSync(resolved.id, 'utf8').includes('import.meta.url')) { + this.error( + `${resolved.id} has no inlined copy at ${inlined}; webcomponent.js would load the font as a file`, + ); + } + return resolved; + }, +}); diff --git a/scripts/inline-malachite-fonts.test.mjs b/scripts/inline-malachite-fonts.test.mjs new file mode 100644 index 0000000..8e0cd86 --- /dev/null +++ b/scripts/inline-malachite-fonts.test.mjs @@ -0,0 +1,75 @@ +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { dirname, join } from 'node:path'; +import { rollup } from 'rollup'; +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import { inlineMalachiteFonts } from './inline-malachite-fonts.mjs'; + +const WRAPPER = 'export default new URL("./face.woff2", import.meta.url).href;'; +const INLINED = 'export default "data:font/woff2;base64,AAAA";'; + +let root; +const write = (path, code) => { + mkdirSync(dirname(join(root, path)), { recursive: true }); + writeFileSync(join(root, path), code); +}; +const bundle = async (wrapper) => { + write('entry.js', `import face from './${wrapper}'; console.log(face);`); + const build = await rollup({ + input: join(root, 'entry.js'), + plugins: [inlineMalachiteFonts()], + }); + try { + const { output } = await build.generate({ format: 'iife' }); + return output[0].code; + } finally { + await build.close(); + } +}; + +beforeEach(() => { + root = mkdtempSync(join(tmpdir(), 'hub-fonts-')); +}); +afterEach(() => { + rmSync(root, { recursive: true, force: true }); +}); + +describe('inlineMalachiteFonts', () => { + it('bundles the inlined copy of a malachite font instead of its file URL', async () => { + const wrapper = 'node_modules/@stackone/malachite/dist/face.woff2.js'; + write(wrapper, WRAPPER); + write(wrapper.replace('.js', '.inline.js'), INLINED); + + const code = await bundle(wrapper); + + expect(code).toContain('data:font/woff2;base64,AAAA'); + expect(code).not.toContain('face.woff2'); + }); + + it('leaves a font wrapper from another package alone', async () => { + const wrapper = 'node_modules/other/face.woff2.js'; + write(wrapper, WRAPPER); + write(wrapper.replace('.js', '.inline.js'), INLINED); + + const code = await bundle(wrapper); + + expect(code).toContain('face.woff2'); + expect(code).not.toContain('data:font'); + }); + + it('keeps a malachite font that is already a data URL, as before 2.134.1', async () => { + const wrapper = 'node_modules/@stackone/malachite/dist/face.woff2.js'; + write(wrapper, INLINED); + + const code = await bundle(wrapper); + + expect(code).toContain('data:font/woff2;base64,AAAA'); + }); + + it('fails the build when a malachite font has no inlined copy', async () => { + const wrapper = 'node_modules/@stackone/malachite/dist/face.woff2.js'; + write(wrapper, WRAPPER); + + await expect(bundle(wrapper)).rejects.toThrow(/has no inlined copy/); + }); +}); diff --git a/scripts/verify-build.mjs b/scripts/verify-build.mjs index e19928b..30c7fa5 100644 --- a/scripts/verify-build.mjs +++ b/scripts/verify-build.mjs @@ -1,5 +1,6 @@ import { readFileSync } from 'node:fs'; import { createRequire } from 'node:module'; +import { basename } from 'node:path'; const require = createRequire(import.meta.url); const { version } = require('../package.json'); @@ -12,6 +13,14 @@ const BANNER = /^["']use client["'];/; const bundles = ['dist/index.esm.js', 'dist/index.js', 'dist/webcomponent.js']; const withBanner = new Set(['dist/index.esm.js', 'dist/index.js']); +// The IIFE is served on its own, so a font it names as a file 404s beside it. +const selfContained = new Set(['dist/webcomponent.js']); +const FONT_FILE = /[\w.-]+\.(?:woff2?|ttf|otf)(?![\w.-])/g; +// Rollup's IIFE stand-in for import.meta.url: anything resolved against it lands beside the script. +const scriptRelative = (file) => + new RegExp( + `new URL\\(["']${basename(file).replaceAll('.', '\\.')}["'],\\s*document\\.baseURI\\)`, + ); const failures = []; @@ -40,6 +49,16 @@ for (const file of bundles) { if (withBanner.has(file) && !BANNER.test(source)) { failures.push(`${file}: lost its "use client" banner`); } + + if (selfContained.has(file)) { + const fontFiles = new Set(source.match(FONT_FILE)); + if (fontFiles.size > 0) { + failures.push(`${file}: loads ${[...fontFiles].join(', ')} as files`); + } + if (scriptRelative(file).test(source)) { + failures.push(`${file}: uses import.meta.url, which points at the script's own URL`); + } + } } if (failures.length > 0) {