Skip to content

🎥 Make videos programmatically with React

Share Google Fonts ESM loader across font entry points

Alec Wattsf9ae695a1870
diff --git a/packages/google-fonts/bundle.ts b/packages/google-fonts/bundle.ts
index 662d7a9f40..fecc93ab50 100644
--- a/packages/google-fonts/bundle.ts
+++ b/packages/google-fonts/bundle.ts
@@ -12,7 +12,14 @@ import {filteredFonts} from './scripts/filtered-fonts';
import {removeWhitespace} from './scripts/utils';
const output = await build({
- entrypoints: filteredFonts.map((p) => `src/${removeWhitespace(p.family)}.ts`),
+ entrypoints: [
+ ...filteredFonts.map((p) => `src/${removeWhitespace(p.family)}.ts`),
+ 'src/from-info.ts',
+ ],
+ // Share the loader and its font-face cache across families and from-info.
+ splitting: true,
+ // Consumer bundlers handle preloading. Keep these chunks usable over HTTP.
+ modulePreload: false,
external: [
'remotion',
'remotion/no-react',
@@ -20,7 +27,7 @@ const output = await build({
'react/jsx-runtime',
'react/jsx-dev-runtime',
],
- naming: '[name].mjs',
+ naming: {entry: '[name].mjs', chunk: '[name]-[hash].mjs'},
});
for (const file of output.outputs) {
@@ -95,7 +102,8 @@ await buildPackage({
},
],
formats: {
- esm: 'build',
+ // Already built together with the font families above.
+ esm: 'do-nothing',
cjs: 'use-tsc',
},
external: 'dependencies',
diff --git a/packages/it-tests/src/monorepo/esm.test.ts b/packages/it-tests/src/monorepo/esm.test.ts
index a6913bbad9..df49f46bef 100644
--- a/packages/it-tests/src/monorepo/esm.test.ts
+++ b/packages/it-tests/src/monorepo/esm.test.ts
@@ -52,3 +52,42 @@ test('Should be able to "import" frontend in node', async () => {
),
).toThrow();
});
+
+test('Google font ESM entries share loaded font faces with from-info', () => {
+ execSync(`node --input-type=module -e "
+ import assert from 'node:assert/strict';
+ import {getAvailableFonts} from '@remotion/google-fonts';
+ import {getInfo, loadFont, loadVariableFont} from '@remotion/google-fonts/Montserrat';
+ import {loadFontFromInfo, loadVariableFontFromInfo} from '@remotion/google-fonts/from-info';
+
+ const added = new Set();
+ const document = {fonts: {add: (face) => added.add(face)}};
+ let constructed = 0;
+ globalThis.FontFace = class {
+ constructor() { constructed++; }
+ load() { return Promise.resolve(this); }
+ };
+ globalThis.fetch = async () => new Response(new Uint8Array([0]));
+
+ const options = {document, weights: ['400'], subsets: ['latin']};
+ const info = getInfo();
+ assert.equal(info.fontFamily, 'Montserrat');
+ await Promise.all([
+ loadFont('normal', options).waitUntilDone(),
+ loadFontFromInfo(info, 'normal', options).waitUntilDone(),
+ ]);
+ assert.equal(constructed, 1);
+ assert.equal(added.size, 1);
+
+ const fromPicker = await getAvailableFonts().find((font) => font.importName === 'Montserrat').load();
+ await fromPicker.loadFont('normal', options).waitUntilDone();
+ assert.equal(constructed, 1);
+
+ await Promise.all([
+ loadVariableFont('normal', options).waitUntilDone(),
+ loadVariableFontFromInfo(info, 'normal', options).waitUntilDone(),
+ ]);
+ assert.equal(constructed, 2);
+ assert.equal(added.size, 2);
+ "`);
+});