Unverified Commit b5590022 authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

Font loaders next config shape (#41219)

Changes how font loaders are configured in next config, makes more sense since options can be optional. Also adds error for when font loaders are used from within node_modules.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent f83cc0cb
throw new Error('@next/font/google is not correctly setup') throw new Error('@next/font/google is not correctly configured')
throw new Error('@next/font/local is not correctly setup') throw new Error('@next/font/local is not correctly configured')
...@@ -134,7 +134,9 @@ function getBaseSWCOptions({ ...@@ -134,7 +134,9 @@ function getBaseSWCOptions({
fontLoaders: fontLoaders:
nextConfig?.experimental?.fontLoaders && relativeFilePathFromRoot nextConfig?.experimental?.fontLoaders && relativeFilePathFromRoot
? { ? {
fontLoaders: Object.keys(nextConfig.experimental.fontLoaders), fontLoaders: nextConfig.experimental.fontLoaders.map(
({ loader }) => loader
),
relativeFilePathFromRoot, relativeFilePathFromRoot,
} }
: null, : null,
......
...@@ -1202,8 +1202,8 @@ export default async function getBaseWebpackConfig( ...@@ -1202,8 +1202,8 @@ export default async function getBaseWebpackConfig(
const fontLoaderTargets = const fontLoaderTargets =
config.experimental.fontLoaders && config.experimental.fontLoaders &&
Object.keys(config.experimental.fontLoaders).map((fontLoader) => { config.experimental.fontLoaders.map(({ loader }) => {
const resolved = require.resolve(fontLoader) const resolved = require.resolve(loader)
return path.join(resolved, '../target.css') return path.join(resolved, '../target.css')
}) })
......
...@@ -11,6 +11,7 @@ import { ...@@ -11,6 +11,7 @@ import {
getGlobalModuleImportError, getGlobalModuleImportError,
getLocalModuleImportError, getLocalModuleImportError,
getFontLoaderDocumentImportError, getFontLoaderDocumentImportError,
getFontLoaderImportError,
} from './messages' } from './messages'
import { getPostCssPlugins } from './plugins' import { getPostCssPlugins } from './plugins'
...@@ -183,12 +184,10 @@ export const css = curry(async function css( ...@@ -183,12 +184,10 @@ export const css = curry(async function css(
// Resolve the configured font loaders, the resolved files are noop files that next-font-loader will match // Resolve the configured font loaders, the resolved files are noop files that next-font-loader will match
let fontLoaders: [string, string][] | undefined = ctx.experimental.fontLoaders let fontLoaders: [string, string][] | undefined = ctx.experimental.fontLoaders
? Object.entries(ctx.experimental.fontLoaders).map( ? ctx.experimental.fontLoaders.map(({ loader: fontLoader, options }) => [
([fontLoader, fontLoaderOptions]: any) => [ path.join(require.resolve(fontLoader), '../target.css'),
path.join(require.resolve(fontLoader), '../target.css'), options,
fontLoaderOptions, ])
]
)
: undefined : undefined
// Font loaders cannot be imported in _document. // Font loaders cannot be imported in _document.
...@@ -232,6 +231,22 @@ export const css = curry(async function css( ...@@ -232,6 +231,22 @@ export const css = curry(async function css(
], ],
}) })
) )
fns.push(
loader({
oneOf: [
markRemovable({
test: fontLoaderPath,
use: {
loader: 'error-loader',
options: {
reason: getFontLoaderImportError(),
},
},
}),
],
})
)
}) })
// CSS cannot be imported in _document. This comes before everything because // CSS cannot be imported in _document. This comes before everything because
......
...@@ -33,7 +33,13 @@ export function getCustomDocumentError() { ...@@ -33,7 +33,13 @@ export function getCustomDocumentError() {
} }
export function getFontLoaderDocumentImportError() { export function getFontLoaderDocumentImportError() {
return `Font loaders ${chalk.bold('cannot')} be used within ${chalk.cyan( return `Font loader error:\nFont loaders ${chalk.bold(
'pages/_document.js' 'cannot'
)}.` )} be used within ${chalk.cyan('pages/_document.js')}.`
}
export function getFontLoaderImportError() {
return `Font loader error:\nFont loaders ${chalk.bold(
'cannot'
)} be used from within ${chalk.bold('node_modules')}.`
} }
...@@ -406,8 +406,19 @@ const configSchema = { ...@@ -406,8 +406,19 @@ const configSchema = {
type: 'boolean', type: 'boolean',
}, },
fontLoaders: { fontLoaders: {
type: 'object', items: {
}, additionalProperties: false,
properties: {
loader: {
type: 'string',
},
options: {},
},
type: 'object',
required: ['loader'],
},
type: 'array',
} as any,
webVitalsAttribution: { webVitalsAttribution: {
type: 'array', type: 'array',
items: { items: {
......
...@@ -159,7 +159,7 @@ export interface ExperimentalConfig { ...@@ -159,7 +159,7 @@ export interface ExperimentalConfig {
// A list of packages that should be treated as external in the RSC server build // A list of packages that should be treated as external in the RSC server build
serverComponentsExternalPackages?: string[] serverComponentsExternalPackages?: string[]
fontLoaders?: { [fontLoader: string]: any } fontLoaders?: [{ loader: string; options?: any }]
webVitalsAttribution?: Array<typeof WEB_VITALS[number]> webVitalsAttribution?: Array<typeof WEB_VITALS[number]>
} }
......
...@@ -3,8 +3,10 @@ module.exports = { ...@@ -3,8 +3,10 @@ module.exports = {
appDir: true, appDir: true,
legacyBrowsers: false, legacyBrowsers: false,
browsersListForSwc: true, browsersListForSwc: true,
fontLoaders: { fontLoaders: [
'@next/font/local': {}, {
}, loader: '@next/font/local',
},
],
}, },
} }
module.exports = { module.exports = {
experimental: { experimental: {
fontLoaders: { fontLoaders: [
'@next/font/google': { {
subsets: ['latin'], loader: '@next/font/google',
options: { subsets: ['latin'] },
}, },
'@next/font/local': {}, {
}, loader: '@next/font/local',
},
],
}, },
} }
module.exports = { module.exports = {
experimental: { experimental: {
fontLoaders: { fontLoaders: [
'@next/font/google': { {
subsets: ['latin'], loader: '@next/font/google',
options: { subsets: ['latin'] },
}, },
}, ],
}, },
} }
module.exports = { module.exports = {
basePath: '/dashboard', basePath: '/dashboard',
experimental: { experimental: {
fontLoaders: { fontLoaders: [
'@next/font/google': { {
subsets: ['latin'], loader: '@next/font/google',
options: { subsets: ['latin'] },
}, },
}, ],
}, },
} }
module.exports = { module.exports = {
experimental: { experimental: {
fontLoaders: { fontLoaders: [
'@next/font/google': { {
subsets: ['latin'], loader: '@next/font/google',
options: { subsets: ['latin'] },
}, },
}, ],
}, },
} }
module.exports = { module.exports = {
experimental: { experimental: {
fontLoaders: { fontLoaders: [
'@next/font/google': { {
subsets: ['latin'], loader: '@next/font/google',
options: { subsets: ['latin'] },
}, },
'@next/font/local': {}, {
}, loader: '@next/font/local',
},
],
}, },
} }
module.exports = { module.exports = {
experimental: { experimental: {
fontLoaders: { fontLoaders: [
'@next/font/google': { {
subsets: ['latin'], loader: '@next/font/google',
options: { subsets: ['latin'] },
}, },
}, ],
}, },
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment