@next/font
Advanced tools
Comparing version 12.3.2-canary.24 to 12.3.2-canary.25
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// @ts-ignore | ||
const node_fetch_1 = __importDefault(require("next/dist/compiled/node-fetch")); | ||
// @ts-ignore | ||
const font_utils_1 = require("next/dist/server/font-utils"); | ||
const utils_1 = require("./utils"); | ||
const cssCache = new Map(); | ||
const fontCache = new Map(); | ||
const downloadGoogleFonts = async ({ functionName, data, config, emitFontFile, }) => { | ||
@@ -19,3 +16,10 @@ var _a, _b; | ||
const url = (0, utils_1.getUrl)(fontFamily, fontAxes, display); | ||
const fontFaceDeclarations = await (0, utils_1.fetchCSSFromGoogleFonts)(url, fontFamily); | ||
let cachedCssRequest = cssCache.get(url); | ||
const fontFaceDeclarations = cachedCssRequest !== null && cachedCssRequest !== void 0 ? cachedCssRequest : (await (0, utils_1.fetchCSSFromGoogleFonts)(url, fontFamily)); | ||
if (!cachedCssRequest) { | ||
cssCache.set(url, fontFaceDeclarations); | ||
} | ||
else { | ||
cssCache.delete(url); | ||
} | ||
// Find font files to download | ||
@@ -42,9 +46,9 @@ const fontFiles = []; | ||
const downloadedFiles = await Promise.all(fontFiles.map(async ({ googleFontFileUrl, preloadFontFile }) => { | ||
let fontFileBuffer; | ||
if (process.env.NEXT_FONT_GOOGLE_MOCKED_RESPONSES) { | ||
fontFileBuffer = Buffer.from(googleFontFileUrl); | ||
let cachedFontRequest = fontCache.get(googleFontFileUrl); | ||
const fontFileBuffer = cachedFontRequest !== null && cachedFontRequest !== void 0 ? cachedFontRequest : (await (0, utils_1.fetchFontFile)(googleFontFileUrl)); | ||
if (!cachedFontRequest) { | ||
fontCache.set(googleFontFileUrl, fontFileBuffer); | ||
} | ||
else { | ||
const arrayBuffer = await (0, node_fetch_1.default)(googleFontFileUrl).then((r) => r.arrayBuffer()); | ||
fontFileBuffer = Buffer.from(arrayBuffer); | ||
fontCache.delete(googleFontFileUrl); | ||
} | ||
@@ -51,0 +55,0 @@ const ext = /\.(woff|woff2|eot|ttf|otf)$/.exec(googleFontFileUrl)[1]; |
@@ -0,1 +1,3 @@ | ||
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
declare type FontOptions = { | ||
@@ -15,3 +17,4 @@ fontFamily: string; | ||
export declare function fetchCSSFromGoogleFonts(url: string, fontFamily: string): Promise<any>; | ||
export declare function fetchFontFile(url: string): Promise<Buffer>; | ||
export declare function getFontAxes(fontFamily: string, weight: string, style: string, selectedVariableAxes?: string[]): [string, string][]; | ||
export {}; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getFontAxes = exports.fetchCSSFromGoogleFonts = exports.getUrl = exports.validateData = void 0; | ||
exports.getFontAxes = exports.fetchFontFile = exports.fetchCSSFromGoogleFonts = exports.getUrl = exports.validateData = void 0; | ||
// @ts-ignore | ||
@@ -100,2 +100,10 @@ const node_fetch_1 = __importDefault(require("next/dist/compiled/node-fetch")); | ||
exports.fetchCSSFromGoogleFonts = fetchCSSFromGoogleFonts; | ||
async function fetchFontFile(url) { | ||
if (process.env.NEXT_FONT_GOOGLE_MOCKED_RESPONSES) { | ||
return Buffer.from(url); | ||
} | ||
const arrayBuffer = await (0, node_fetch_1.default)(url).then((r) => r.arrayBuffer()); | ||
return Buffer.from(arrayBuffer); | ||
} | ||
exports.fetchFontFile = fetchFontFile; | ||
function getFontAxes(fontFamily, weight, style, selectedVariableAxes) { | ||
@@ -102,0 +110,0 @@ const allAxes = font_data_json_1.default[fontFamily].axes; |
@@ -9,16 +9,12 @@ import type { FontModule } from 'next/font'; | ||
style?: string; | ||
adjustFontFallback?: 'Arial' | 'Times New Roman' | false; | ||
fallback?: string[]; | ||
preload?: boolean; | ||
variable?: CssVariable; | ||
fontStretch?: string; | ||
fontVariant?: string; | ||
fontFeatureSettings?: string; | ||
fontVariationSettings?: string; | ||
ascentOverride?: string; | ||
descentOverride?: string; | ||
lineGapOverride?: string; | ||
sizeAdjust?: string; | ||
adjustFontFallback?: 'Arial' | 'Times New Roman' | false; | ||
declarations?: Array<{ | ||
prop: string; | ||
value: string; | ||
}>; | ||
}; | ||
export default function localFont(options: LocalFont): FontModule; | ||
export {}; |
@@ -15,3 +15,3 @@ "use strict"; | ||
var _a, _b; | ||
const { family, src, ext, format, display, weight, style, fallback, preload, variable, ascentOverride, descentOverride, lineGapOverride, fontStretch, fontFeatureSettings, sizeAdjust, adjustFontFallback, } = (0, utils_1.validateData)(functionName, data); | ||
const { family, src, ext, format, display, weight, style, fallback, preload, variable, adjustFontFallback, declarations, } = (0, utils_1.validateData)(functionName, data); | ||
const resolved = await resolve(src); | ||
@@ -47,2 +47,5 @@ const fileBuffer = await (0, util_1.promisify)(fs.readFile)(resolved); | ||
const fontFaceProperties = [ | ||
...(declarations | ||
? declarations.map(({ prop, value }) => [prop, value]) | ||
: []), | ||
['font-family', `'${(_b = fontMetadata === null || fontMetadata === void 0 ? void 0 : fontMetadata.familyName) !== null && _b !== void 0 ? _b : family}'`], | ||
@@ -53,10 +56,2 @@ ['src', `url(${fontUrl}) format('${format}')`], | ||
...(style ? [['font-style', style]] : []), | ||
...(ascentOverride ? [['ascent-override', ascentOverride]] : []), | ||
...(descentOverride ? [['descent-override', descentOverride]] : []), | ||
...(lineGapOverride ? [['line-gap-override', lineGapOverride]] : []), | ||
...(fontStretch ? [['font-stretch', fontStretch]] : []), | ||
...(fontFeatureSettings | ||
? [['font-feature-settings', fontFeatureSettings]] | ||
: []), | ||
...(sizeAdjust ? [['size-adjust', sizeAdjust]] : []), | ||
]; | ||
@@ -63,0 +58,0 @@ const css = `@font-face { |
@@ -12,13 +12,9 @@ declare type FontOptions = { | ||
variable?: string; | ||
ascentOverride?: string; | ||
descentOverride?: string; | ||
fontStretch?: string; | ||
fontVariant?: string; | ||
fontFeatureSettings?: string; | ||
fontVariationSettings?: string; | ||
lineGapOverride?: string; | ||
sizeAdjust?: string; | ||
adjustFontFallback?: string | false; | ||
declarations?: Array<{ | ||
prop: string; | ||
value: string; | ||
}>; | ||
}; | ||
export declare function validateData(functionName: string, data: any): FontOptions; | ||
export {}; |
@@ -18,3 +18,3 @@ "use strict"; | ||
} | ||
let { src, display = 'optional', weight, style, fallback, preload = true, variable, ascentOverride, descentOverride, fontStretch, fontVariant, fontFeatureSettings, fontVariationSettings, lineGapOverride, sizeAdjust, adjustFontFallback, } = data[0] || {}; | ||
let { src, display = 'optional', weight, style, fallback, preload = true, variable, adjustFontFallback, declarations, } = data[0] || {}; | ||
if (!allowedDisplayValues.includes(display)) { | ||
@@ -31,2 +31,15 @@ throw new Error(`Invalid display value \`${display}\`.\nAvailable display values: ${formatValues(allowedDisplayValues)}`); | ||
const family = /.+\/(.+?)\./.exec(src)[1]; | ||
if (Array.isArray(declarations)) { | ||
declarations.forEach((declaration) => { | ||
if ([ | ||
'font-family', | ||
'src', | ||
'font-display', | ||
'font-weight', | ||
'font-style', | ||
].includes(declaration === null || declaration === void 0 ? void 0 : declaration.prop)) { | ||
throw new Error(`Invalid declaration prop: \`${declaration.prop}\``); | ||
} | ||
}); | ||
} | ||
return { | ||
@@ -43,13 +56,6 @@ family, | ||
variable, | ||
ascentOverride, | ||
descentOverride, | ||
fontStretch, | ||
fontVariant, | ||
fontFeatureSettings, | ||
fontVariationSettings, | ||
lineGapOverride, | ||
sizeAdjust, | ||
adjustFontFallback, | ||
declarations, | ||
}; | ||
} | ||
exports.validateData = validateData; |
{ | ||
"name": "@next/font", | ||
"version": "12.3.2-canary.24", | ||
"version": "12.3.2-canary.25", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "url": "vercel/next.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2058610
49256