Skip to content
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
24 changes: 17 additions & 7 deletions code/frameworks/nextjs/src/font/babel/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,16 @@ export function getVariableMetasBySpecifier(
return undefined;
}

if (!types.isIdentifier(declaration.init.callee)) {
return undefined;
}

if (
(!types.isIdentifier(declaration.init.callee) ||
specifier.type !== 'ImportSpecifier' ||
(specifier.type !== 'ImportSpecifier' ||
specifier.imported.type !== 'Identifier' ||
declaration.init.callee.name !== specifier.imported.name) &&
(!types.isIdentifier(declaration.init.callee) ||
specifier.type !== 'ImportDefaultSpecifier' ||
(declaration.init.callee.name !== specifier.imported.name &&
declaration.init.callee.name !== specifier.local.name)) &&
(specifier.type !== 'ImportDefaultSpecifier' ||
declaration.init.callee.name !== specifier.local.name)
) {
return undefined;
Expand All @@ -311,8 +314,15 @@ export function getVariableMetasBySpecifier(

const identifierName = declaration.id.name;
const properties = convertNodeToJSON(types, options);
const functionName = declaration.init.callee.name;

let functionName = declaration.init.callee.name;
if (
specifier.type === 'ImportSpecifier' &&
specifier.imported &&
specifier.imported.type === 'Identifier' &&
declaration.init.callee.name !== specifier.imported.name
) {
functionName = specifier.imported.name;
Comment thread
ndelangen marked this conversation as resolved.
}
return { identifierName, properties, functionName };
})
.filter(isDefined);
Expand Down
14 changes: 12 additions & 2 deletions code/frameworks/nextjs/src/font/babel/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { transform } from '@babel/core';
import TransformFontImports from '.';

const example = `
import { Inter, Roboto } from 'next/font/google'
import { Inter, Lora as FontLora, Roboto } from 'next/font/google'
import localFont from 'next/font/local'

const myFont = localFont({ src: './my-font.woff2' })
Expand All @@ -12,6 +12,10 @@ const roboto = Roboto({
weight: '400',
})

const lora = FontLora({
weight: '400',
})

const inter = Inter({
subsets: ['latin'],
});
Expand All @@ -20,7 +24,7 @@ const randomObj = {}
`;

const exampleLegacy = `
import { Inter, Roboto } from '@next/font/google'
import { Inter, Lora as FontLora, Roboto } from '@next/font/google'
import localFont from '@next/font/local'

const myFont = localFont({ src: './my-font.woff2' })
Expand All @@ -29,6 +33,10 @@ const roboto = Roboto({
weight: '400',
})

const lora = FontLora({
weight: '400',
})

const inter = Inter({
subsets: ['latin'],
});
Expand All @@ -40,6 +48,7 @@ it('should transform next/font AST properly', () => {
const { code } = transform(example, { plugins: [TransformFontImports] })!;
expect(code).toMatchInlineSnapshot(`
"import inter from \\"storybook-nextjs-font-loader?{\\\\\\"source\\\\\\":\\\\\\"next/font/google\\\\\\",\\\\\\"props\\\\\\":{\\\\\\"subsets\\\\\\":[\\\\\\"latin\\\\\\"]},\\\\\\"fontFamily\\\\\\":\\\\\\"Inter\\\\\\",\\\\\\"filename\\\\\\":\\\\\\"\\\\\\"}!next/font/google\\";
import lora from \\"storybook-nextjs-font-loader?{\\\\\\"source\\\\\\":\\\\\\"next/font/google\\\\\\",\\\\\\"props\\\\\\":{\\\\\\"weight\\\\\\":\\\\\\"400\\\\\\"},\\\\\\"fontFamily\\\\\\":\\\\\\"Lora\\\\\\",\\\\\\"filename\\\\\\":\\\\\\"\\\\\\"}!next/font/google\\";
import roboto from \\"storybook-nextjs-font-loader?{\\\\\\"source\\\\\\":\\\\\\"next/font/google\\\\\\",\\\\\\"props\\\\\\":{\\\\\\"weight\\\\\\":\\\\\\"400\\\\\\"},\\\\\\"fontFamily\\\\\\":\\\\\\"Roboto\\\\\\",\\\\\\"filename\\\\\\":\\\\\\"\\\\\\"}!next/font/google\\";
import myFont from \\"storybook-nextjs-font-loader?{\\\\\\"source\\\\\\":\\\\\\"next/font/local\\\\\\",\\\\\\"props\\\\\\":{\\\\\\"src\\\\\\":\\\\\\"./my-font.woff2\\\\\\"},\\\\\\"fontFamily\\\\\\":\\\\\\"localFont\\\\\\",\\\\\\"filename\\\\\\":\\\\\\"\\\\\\"}!next/font/local\\";
const randomObj = {};"
Expand All @@ -50,6 +59,7 @@ it('should transform @next/font AST properly', () => {
const { code } = transform(exampleLegacy, { plugins: [TransformFontImports] })!;
expect(code).toMatchInlineSnapshot(`
"import inter from \\"storybook-nextjs-font-loader?{\\\\\\"source\\\\\\":\\\\\\"@next/font/google\\\\\\",\\\\\\"props\\\\\\":{\\\\\\"subsets\\\\\\":[\\\\\\"latin\\\\\\"]},\\\\\\"fontFamily\\\\\\":\\\\\\"Inter\\\\\\",\\\\\\"filename\\\\\\":\\\\\\"\\\\\\"}!@next/font/google\\";
import lora from \\"storybook-nextjs-font-loader?{\\\\\\"source\\\\\\":\\\\\\"@next/font/google\\\\\\",\\\\\\"props\\\\\\":{\\\\\\"weight\\\\\\":\\\\\\"400\\\\\\"},\\\\\\"fontFamily\\\\\\":\\\\\\"Lora\\\\\\",\\\\\\"filename\\\\\\":\\\\\\"\\\\\\"}!@next/font/google\\";
import roboto from \\"storybook-nextjs-font-loader?{\\\\\\"source\\\\\\":\\\\\\"@next/font/google\\\\\\",\\\\\\"props\\\\\\":{\\\\\\"weight\\\\\\":\\\\\\"400\\\\\\"},\\\\\\"fontFamily\\\\\\":\\\\\\"Roboto\\\\\\",\\\\\\"filename\\\\\\":\\\\\\"\\\\\\"}!@next/font/google\\";
import myFont from \\"storybook-nextjs-font-loader?{\\\\\\"source\\\\\\":\\\\\\"@next/font/local\\\\\\",\\\\\\"props\\\\\\":{\\\\\\"src\\\\\\":\\\\\\"./my-font.woff2\\\\\\"},\\\\\\"fontFamily\\\\\\":\\\\\\"localFont\\\\\\",\\\\\\"filename\\\\\\":\\\\\\"\\\\\\"}!@next/font/local\\";
const randomObj = {};"
Expand Down