next-font 1.0.6 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  ## Next Font
2
2
 
3
- Derived from `@next/font`.
3
+ Derived from `@next/font`. Aimed to be used in conjunction with [@next-font/plugin- vite](https://npmjs.com/package/@next-font/plugin-vite)
4
+
5
+ > This package is best used with the [@next-font/plugin-vite](https://npmjs.com/package/@next-font/plugin-vite) package or any plugins under the [@next-font](https://npmjs.com/org/next-font) organization.
4
6
 
5
7
  ### Install
6
8
 
@@ -32,4 +34,18 @@ const myFont = localFont({
32
34
  }) // { className: '...' }
33
35
  ```
34
36
 
37
+ This package also supports accessing the manifest, which contains per-file font information. This can be useful for generating preload or preconnect tags in the `<head>` of your HTML document.
38
+
39
+ ```js
40
+ import nextFontManifest from 'next-font/manifest';
41
+
42
+ // raw manifest
43
+ const manifest = nextFontManifest.manifest;
44
+
45
+ // get fonts with preload enabled
46
+ const preloadableFonts = nextFontManifest.getPreloadableFonts(someFilePath /* ex. import.meta.url */);
47
+ // or skip accessing preloadableFonts and get data for <head>
48
+ const metadata = nextFontManifest.getFontMetadata(someFilePath /* ex. import.meta.url */);
49
+ ```
50
+
35
51
  See the Next.js [API Page](https://nextjs.org/docs/app/api-reference/components/font) for more options.
@@ -1,5 +1,40 @@
1
- import { FontLoader } from 'next/font';
1
+ type FontLoaderOptions = Omit<{
2
+ functionName: string;
3
+ variableName: string;
4
+ data: any[];
5
+ emitFontFile: (content: Buffer, ext: string, preload: boolean, isUsingSizeAdjust?: boolean) => string;
6
+ resolve: (src: string) => string;
7
+ isDev: boolean;
8
+ isServer: boolean;
9
+ loaderContext: any;
10
+ }, 'loaderContext'> & {
11
+ loaderContext: {
12
+ fs?: {
13
+ readFile: (...args: any[]) => Promise<Buffer | Uint8Array | string>;
14
+ };
15
+ error: (message: string) => any;
16
+ };
17
+ cache?: {
18
+ css?: Map<string, string | null>;
19
+ font?: Map<string, string | null>;
20
+ };
21
+ };
22
+ type FontLoader = (options: FontLoaderOptions) => Promise<{
23
+ css: string;
24
+ fallbackFonts?: string[];
25
+ variable?: string;
26
+ adjustFontFallback?: AdjustFontFallback;
27
+ weight?: string;
28
+ style?: string;
29
+ }>;
30
+ type AdjustFontFallback = {
31
+ fallbackFont: string;
32
+ ascentOverride?: string;
33
+ descentOverride?: string;
34
+ lineGapOverride?: string;
35
+ sizeAdjust?: string;
36
+ };
2
37
 
3
- declare const nextFontGoogleFontLoader: FontLoader;
38
+ declare const loader: FontLoader;
4
39
 
5
- export { nextFontGoogleFontLoader as default };
40
+ export { loader as default };
@@ -1,9 +1,9 @@
1
- import{validateGoogleFontFunctionCall as e}from"./validate-google-font-function-call.js";import{getFontAxes as t}from"./get-font-axes.js";import{getGoogleFontsUrl as n}from"./get-google-fonts-url.js";import{nextFontError as r}from"../next-font-error.js";import{findFontFilesInCss as i}from"./find-font-files-in-css.js";import{getFallbackFontOverrideMetrics as o}from"./get-fallback-font-override-metrics.js";import{fetchCSSFromGoogleFonts as c}from"./fetch-css-from-google-fonts.js";import{fetchFontFile as s}from"./fetch-font-file.js";var l,a,u,f={},h={},b={},m=(u||(u=1,!function(e){Object.defineProperty(e,"__esModule",{value:!0});var t={bootstrap:function(){return u},error:function(){return m},event:function(){return v},info:function(){return g},prefixes:function(){return o},ready:function(){return x},trace:function(){return p},wait:function(){return f},warn:function(){return d},warnOnce:function(){return w}};for(var n in t)Object.defineProperty(e,n,{enumerable:!0,get:t[n]});let r=(l||(l=1,!function(e){Object.defineProperty(e,"__esModule",{value:!0});var t,n={bgBlack:function(){return F},bgBlue:function(){return _},bgCyan:function(){return L},bgGreen:function(){return R},bgMagenta:function(){return C},bgRed:function(){return k},bgWhite:function(){return P},bgYellow:function(){return M},black:function(){return g},blue:function(){return w},bold:function(){return u},cyan:function(){return S},dim:function(){return f},gray:function(){return $},green:function(){return p},hidden:function(){return d},inverse:function(){return m},italic:function(){return h},magenta:function(){return y},purple:function(){return j},red:function(){return v},reset:function(){return a},strikethrough:function(){return x},underline:function(){return b},white:function(){return O},yellow:function(){return z}};for(var r in n)Object.defineProperty(e,r,{enumerable:!0,get:n[r]});let{env:i,stdout:o}=(null==(t=globalThis)?void 0:t.process)??{},c=i&&!i.NO_COLOR&&(i.FORCE_COLOR||(null==o?void 0:o.isTTY)&&!i.CI&&"dumb"!==i.TERM),s=(e,t,n,r)=>{let i=e.substring(0,r)+n,o=e.substring(r+t.length),c=o.indexOf(t);return~c?i+s(o,t,n,c):i+o},l=(e,t,n=e)=>c?r=>{let i=""+r,o=i.indexOf(t,e.length);return~o?e+s(i,t,n,o)+t:e+i+t}:String,a=c?e=>`\x1b[0m${e}\x1b[0m`:String,u=l("\x1b[1m","\x1b[22m","\x1b[22m\x1b[1m"),f=l("\x1b[2m","\x1b[22m","\x1b[22m\x1b[2m"),h=l("\x1b[3m","\x1b[23m"),b=l("\x1b[4m","\x1b[24m"),m=l("\x1b[7m","\x1b[27m"),d=l("\x1b[8m","\x1b[28m"),x=l("\x1b[9m","\x1b[29m"),g=l("\x1b[30m","\x1b[39m"),v=l("\x1b[31m","\x1b[39m"),p=l("\x1b[32m","\x1b[39m"),z=l("\x1b[33m","\x1b[39m"),w=l("\x1b[34m","\x1b[39m"),y=l("\x1b[35m","\x1b[39m"),j=l("\x1b[38;2;173;127;168m","\x1b[39m"),S=l("\x1b[36m","\x1b[39m"),O=l("\x1b[37m","\x1b[39m"),$=l("\x1b[90m","\x1b[39m"),F=l("\x1b[40m","\x1b[49m"),k=l("\x1b[41m","\x1b[49m"),R=l("\x1b[42m","\x1b[49m"),M=l("\x1b[43m","\x1b[49m"),_=l("\x1b[44m","\x1b[49m"),C=l("\x1b[45m","\x1b[49m"),L=l("\x1b[46m","\x1b[49m"),P=l("\x1b[47m","\x1b[49m")}(h)),h),i=function(){if(a)return b;a=1;Object.defineProperty(b,"__esModule",{value:!0}),Object.defineProperty(b,"LRUCache",{enumerable:!0,get:function(){return e}});class e{constructor(e,t){this.cache=new Map,this.sizes=new Map,this.totalSize=0,this.maxSize=e,this.calculateSize=t||(()=>1)}set(e,t){if(!e||!t)return;let n=this.calculateSize(t);if(n>this.maxSize)return void console.warn("Single item size exceeds maxSize");this.cache.has(e)&&(this.totalSize-=this.sizes.get(e)||0),this.cache.set(e,t),this.sizes.set(e,n),this.totalSize+=n,this.touch(e)}has(e){return!!e&&(this.touch(e),!!this.cache.get(e))}get(e){if(!e)return;let t=this.cache.get(e);if(void 0!==t)return this.touch(e),t}touch(e){let t=this.cache.get(e);void 0!==t&&(this.cache.delete(e),this.cache.set(e,t),this.evictIfNecessary())}evictIfNecessary(){for(;this.totalSize>this.maxSize&&this.cache.size>0;)this.evictLeastRecentlyUsed()}evictLeastRecentlyUsed(){let e=this.cache.keys().next().value;if(void 0!==e){let t=this.sizes.get(e)||0;this.totalSize-=t,this.cache.delete(e),this.sizes.delete(e)}}reset(){this.cache.clear(),this.sizes.clear(),this.totalSize=0}keys(){return[...this.cache.keys()]}remove(e){this.cache.has(e)&&(this.totalSize-=this.sizes.get(e)||0,this.cache.delete(e),this.sizes.delete(e))}clear(){this.cache.clear(),this.sizes.clear(),this.totalSize=0}get size(){return this.cache.size}get currentSize(){return this.totalSize}}return b}(),o={wait:(0,r.white)((0,r.bold)("○")),error:(0,r.red)((0,r.bold)("⨯")),warn:(0,r.yellow)((0,r.bold)("⚠")),ready:"▲",info:(0,r.white)((0,r.bold)(" ")),event:(0,r.green)((0,r.bold)("✓")),trace:(0,r.magenta)((0,r.bold)("\xbb"))},c={log:"log",warn:"warn",error:"error"};function s(e,...t){(""===t[0]||void 0===t[0])&&1===t.length&&t.shift();let n=e in c?c[e]:"log",r=o[e];0===t.length?console[n](""):1===t.length&&"string"==typeof t[0]?console[n](" "+r+" "+t[0]):console[n](" "+r,...t)}function u(...e){console.log(" "+e.join(" "))}function f(...e){s("wait",...e)}function m(...e){s("error",...e)}function d(...e){s("warn",...e)}function x(...e){s("ready",...e)}function g(...e){s("info",...e)}function v(...e){s("event",...e)}function p(...e){s("trace",...e)}let z=new i.LRUCache(1e4,e=>e.length);function w(...e){let t=e.join(" ");z.has(t)||(z.set(t,t),d(...e))}}(f)),f);let d=new Map,x=new Map,g=/[|\\{}()[\]^$+*?.-]/,v=/[|\\{}()[\]^$+*?.-]/g,p=async({functionName:l,data:a,emitFontFile:u,isDev:f,isServer:h})=>{let{fontFamily:b,weights:p,styles:z,display:w,preload:y,selectedVariableAxes:j,fallback:S,adjustFontFallback:O,variable:$,subsets:F}=e(l,a[0]),k=t(b,p,z,j),R=n(b,k,w),M=O?o(b):void 0,_={fallbackFonts:S,weight:1===p.length&&"variable"!==p[0]?p[0]:void 0,style:1===z.length?z[0]:void 0,variable:$,adjustFontFallback:M};try{let e=d.has(R),t=e?d.get(R):await c(R,b,f).catch(e=>(console.error(e),null));e?d.delete(R):d.set(R,t??null),null==t&&r(`Failed to fetch \`${b}\` from Google Fonts.`),t=t.split("body {",1)[0];let n=i(t,y?F:void 0),o=await Promise.all(n.map(async({googleFontFileUrl:e,preloadFontFile:t})=>{let n=x.has(e),i=n?x.get(e):await s(e,f).catch(e=>(console.error(e),null));n?x.delete(e):x.set(e,i??null),null==i&&r(`Failed to fetch \`${b}\` from Google Fonts.`);let o=/\.(woff|woff2|eot|ttf|otf)$/.exec(e)[1],c=u(i,o,t,!!M);return{googleFontFileUrl:e,selfHostedFileUrl:c}})),l=t;for(let{googleFontFileUrl:e,selfHostedFileUrl:t}of o)l=l.replace(RegExp(g.test(e)?e.replace(v,"\\$&"):e,"g"),t);return{..._,css:l}}catch(e){if(f){h&&m.error(`Failed to download \`${b}\` from Google Fonts. Using fallback font instead.
1
+ import{nextFontError as e}from"../next-font-error.js";import{findFontFilesInCss as t}from"./find-font-files-in-css.js";import{getFontAxes as o}from"./get-font-axes.js";import{getGoogleFontsUrl as l}from"./get-google-fonts-url.js";import{validateGoogleFontFunctionCall as a}from"./validate-google-font-function-call.js";let r=e=>{let t;return()=>(t||(t=e().then(e=>(t=e,e))),t)},n=/[|\\{}()[\]^$+*?.-]/,s=/[|\\{}()[\]^$+*?.-]/g,i=e=>n.test(e)?e.replace(s,"\\$&"):e,f=r(()=>import("./get-fallback-font-override-metrics.js").then(e=>e.default||e)),c=r(()=>import("./fetch-css-from-google-fonts.js").then(e=>e.default||e)),d=r(()=>import("./fetch-font-file.js").then(e=>e.default||e)),u=async({functionName:r,data:n,emitFontFile:s,isDev:u,isServer:g,loaderContext:m,cache:{css:h,font:p}={}})=>{let{fontFamily:w,weights:$,styles:v,display:F,preload:j,selectedVariableAxes:y,fallback:b,adjustFontFallback:x,variable:k,subsets:G}=a(r,n[0]),O=o(w,$,v,y),z=l(w,O,F),A=x?await (async()=>{let{getFallbackFontOverrideMetrics:e}=await f();return e(w)})():void 0,E={fallbackFonts:b,weight:1===$.length&&"variable"!==$[0]?$[0]:void 0,style:1===v.length?v[0]:void 0,variable:k,adjustFontFallback:A};try{let o=h?.has(z),l=null!=h&&o?h.get(z):await (async()=>{let{fetchCSSFromGoogleFonts:e}=await c();return await e(z,w,u).catch(e=>(console.error(e),null))})();o?h?.delete(z):h?.set(z,l??null),null==l&&e(`Failed to fetch \`${w}\` from Google Fonts.`),l=l.split("body {",1)[0];let a=t(l,j?G:void 0),r=await Promise.all(a.map(async({googleFontFileUrl:t,preloadFontFile:o})=>{let l=p?.has(t),a=null!=p&&l?p.get(t):await (async()=>{let{fetchFontFile:e}=await d();return await e(t,u).catch(e=>(console.error(e),null))})();l?p?.delete(t):p?.set(t,a??null),null==a&&e(`Failed to fetch \`${w}\` from Google Fonts.`);let r=/\.(woff|woff2|eot|ttf|otf)$/.exec(t)[1],n=s(a,r,o,!!A);return{googleFontFileUrl:t,selfHostedFileUrl:n}})),n=l;for(let{googleFontFileUrl:e,selfHostedFileUrl:t}of r)n=n.replace(RegExp(i(e),"g"),t);return{...E,css:n}}catch(e){if(u){g&&m.error(`Failed to download \`${w}\` from Google Fonts. Using fallback font instead.
2
2
 
3
3
  ${e.message}}`);let t=`@font-face {
4
- font-family: '${b} Fallback';
5
- src: local("${M?.fallbackFont??"Arial"}");`;return M&&(t+=`
6
- ascent-override:${M.ascentOverride};
7
- descent-override:${M.descentOverride};
8
- line-gap-override:${M.lineGapOverride};
9
- size-adjust:${M.sizeAdjust};`),t+="\n}",{..._,css:t}}throw e}};export{p as default};
4
+ font-family: '${w} Fallback';
5
+ src: local("${A?.fallbackFont??"Arial"}");`;return A&&(t+=`
6
+ ascent-override:${A.ascentOverride};
7
+ descent-override:${A.descentOverride};
8
+ line-gap-override:${A.lineGapOverride};
9
+ size-adjust:${A.sizeAdjust};`),t+="\n}",{...E,css:t}}throw e}};export{u as default};
@@ -0,0 +1,38 @@
1
+ type FontLoaderOptions = Omit<{
2
+ functionName: string;
3
+ variableName: string;
4
+ data: any[];
5
+ emitFontFile: (content: Buffer, ext: string, preload: boolean, isUsingSizeAdjust?: boolean) => string;
6
+ resolve: (src: string) => string;
7
+ isDev: boolean;
8
+ isServer: boolean;
9
+ loaderContext: any;
10
+ }, 'loaderContext'> & {
11
+ loaderContext: {
12
+ fs?: {
13
+ readFile: (...args: any[]) => Promise<Buffer | Uint8Array | string>;
14
+ };
15
+ error: (message: string) => any;
16
+ };
17
+ cache?: {
18
+ css?: Map<string, string | null>;
19
+ font?: Map<string, string | null>;
20
+ };
21
+ };
22
+ type FontLoader = (options: FontLoaderOptions) => Promise<{
23
+ css: string;
24
+ fallbackFonts?: string[];
25
+ variable?: string;
26
+ adjustFontFallback?: AdjustFontFallback;
27
+ weight?: string;
28
+ style?: string;
29
+ }>;
30
+ type AdjustFontFallback = {
31
+ fallbackFont: string;
32
+ ascentOverride?: string;
33
+ descentOverride?: string;
34
+ lineGapOverride?: string;
35
+ sizeAdjust?: string;
36
+ };
37
+
38
+ export type { AdjustFontFallback, FontLoader };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+