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: Update validateData function for next/font compatibility #25061

Merged
merged 3 commits into from
Dec 1, 2023
Merged
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
@@ -1,4 +1,3 @@
/* eslint-disable import/no-mutable-exports */
import dedent from 'ts-dedent';

type FontOptions = {
Expand All @@ -22,30 +21,56 @@ type FontOptions = {
}>;
};

let validateData: (functionName: string, fontData: any) => FontOptions;
const trials = [
{
module: '@next/font/dist/local/utils',
exportName: 'validateData',
description: 'Support @next/font prior to v13.2.5',
},
{
module: '@next/font/dist/local/validate-local-font-function-call',
exportName: 'validateLocalFontFunctionCall',
description: 'Support @next/font since v13.2.5',
},
{
module: 'next/dist/compiled/@next/font/dist/local/utils',
exportName: 'validateData',
description: 'Support next/font prior to v13.2.4',
},
{
module: 'next/dist/compiled/@next/font/dist/local/validate-local-font-function-call',
exportName: 'validateLocalFontFunctionCall',
description: 'Support next/font since v13.2.4',
},
];

// Support @next/font
try {
const fontUtils = require('@next/font/dist/local/utils');
validateData = fontUtils.validateData;
} catch (_) {
// Support next/font prior to v13.2.4
try {
const fontUtils = require('next/dist/compiled/@next/font/dist/local/utils');
validateData = fontUtils.validateData;
} catch (__) {
// Support next/font since v13.2.4
const validateData: (functionName: string, fontData: any) => FontOptions = (() => {
// eslint-disable-next-line no-restricted-syntax
for (const { module, exportName } of trials) {
try {
validateData =
require('next/dist/compiled/@next/font/dist/local/validate-local-font-function-call').validateLocalFontFunctionCall;
} catch (e) {
throw new Error(dedent`
We are unable to load the helper functions to use next/font/local.
Please downgrade Next.js to version 13.2.4 to continue to use next/font/local in Storybook.
Feel free to open a Github Issue!
`);
const loadedModule = require(module);
if (exportName in loadedModule) {
return loadedModule[exportName];
}
} catch {
// Continue to the next trial
}
}
}

// Generate the dynamic error message
const errorDetails = trials
.map(
(trial) =>
`- ${trial.description}: tries to import '${trial.exportName}' from '${trial.module}'`
)
.join('\n');

throw new Error(dedent`
We were unable to load the helper functions to use next/font/local. The code attempted the following scenarios:
${errorDetails}

Please check your Next.js version and the module paths. If you resolve this issue for a version or setup not covered, consider contributing by updating the 'trials' array and making a pull request.
`);
})();

export { validateData };
Loading