Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next.js: Fix next/font/local usage in babel mode #25045

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import type { LoaderOptions } from '../types';

type LocalFontSrc = string | Array<{ path: string; weight?: string; style?: string }>;

export async function getFontFaceDeclarations(options: LoaderOptions, rootContext: string) {
export async function getFontFaceDeclarations(
options: LoaderOptions,
rootContext: string,
swcMode: boolean
) {
const localFontSrc = options.props.src as LocalFontSrc;

// Parent folder relative to the root context
const parentFolder = path
.dirname(path.join(getProjectRoot(), options.filename))
.replace(rootContext, '');
const parentFolder = swcMode
? path.dirname(path.join(getProjectRoot(), options.filename)).replace(rootContext, '')
: path.dirname(options.filename).replace(rootContext, '');

const { validateData } = require('../utils/local-font-utils');
const { weight, style, variable } = validateData('', options.props);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type FontFaceDeclaration = {

export default async function storybookNextjsFontLoader(this: any) {
const loaderOptions = this.getOptions() as LoaderOptions;
let swcMode = false;
let options;

if (Object.keys(loaderOptions).length > 0) {
Expand All @@ -23,6 +24,7 @@ export default async function storybookNextjsFontLoader(this: any) {
} else {
// handles SWC mode
const importQuery = JSON.parse(this.resourceQuery.slice(1));
swcMode = true;

options = {
filename: importQuery.path,
Expand All @@ -42,7 +44,7 @@ export default async function storybookNextjsFontLoader(this: any) {
}

if (options.source.endsWith('next/font/local') || options.source.endsWith('@next/font/local')) {
fontFaceDeclaration = await getLocalFontFaceDeclarations(options, rootCtx);
fontFaceDeclaration = await getLocalFontFaceDeclarations(options, rootCtx, swcMode);
}

if (typeof fontFaceDeclaration !== 'undefined') {
Expand Down
Loading