Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for font loaders (#40746)
For some context: [https://vercel.slack.com/archives/CGU8HUTUH/p1662124179102509](https://vercel.slack.com/archives/CGU8HUTUH/p1662124179102509) Continuation of #40221 and #40227 Adds `experimental.fontLoaders`. SWC next-font-loaders (#40221) transforms font loader (e.g. #40227) call expressions into an import with the function call arguments as a query. The imports will be matched by `next-font-loader`. It runs the configured font loaders - emits font files and returns CSS. Exports are added, and the font-family is made locally scoped. The returned CSS is turned into a CSS module with `css-loader` which lets you consume the font-family. `FontLoaderManifestPlugin` creates a manifest of the preloaded font files for each entrypoint. Preload/preconnect are then added in `_document.tsx` if any font files were found for that path. Co-authored-by: JJ Kasper <jj@jjsweb.site>
- Loading branch information
Showing
54 changed files
with
1,994 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/next/build/webpack/config/blocks/css/loaders/font-loader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { webpack } from 'next/dist/compiled/webpack/webpack' | ||
import { ConfigurationContext } from '../../../utils' | ||
import { getClientStyleLoader } from './client' | ||
import { cssFileResolve } from './file-resolve' | ||
|
||
export function getFontLoader( | ||
ctx: ConfigurationContext, | ||
postcss: any, | ||
fontLoaderOptions: any | ||
): webpack.RuleSetUseItem[] { | ||
const loaders: webpack.RuleSetUseItem[] = [] | ||
|
||
if (ctx.isClient) { | ||
// Add appropriate development mode or production mode style | ||
// loader | ||
loaders.push( | ||
getClientStyleLoader({ | ||
isAppDir: !!ctx.experimental.appDir, | ||
isDevelopment: ctx.isDevelopment, | ||
assetPrefix: ctx.assetPrefix, | ||
}) | ||
) | ||
} | ||
|
||
loaders.push({ | ||
loader: require.resolve('../../../../loaders/css-loader/src'), | ||
options: { | ||
postcss, | ||
importLoaders: 1, | ||
// Use CJS mode for backwards compatibility: | ||
esModule: false, | ||
url: (url: string, resourcePath: string) => | ||
cssFileResolve(url, resourcePath, ctx.experimental.urlImports), | ||
import: (url: string, _: any, resourcePath: string) => | ||
cssFileResolve(url, resourcePath, ctx.experimental.urlImports), | ||
modules: { | ||
// Do not transform class names (CJS mode backwards compatibility): | ||
exportLocalsConvention: 'asIs', | ||
// Server-side (Node.js) rendering support: | ||
exportOnlyLocals: ctx.isServer, | ||
// Disallow global style exports so we can code-split CSS and | ||
// not worry about loading order. | ||
mode: 'pure', | ||
getLocalIdent: ( | ||
_context: any, | ||
_localIdentName: any, | ||
exportName: string, | ||
_options: any, | ||
meta: any | ||
) => { | ||
// hash from next-font-loader | ||
return `__${exportName}_${meta.fontFamilyHash}` | ||
}, | ||
}, | ||
fontLoader: true, | ||
}, | ||
}) | ||
|
||
loaders.push({ | ||
loader: 'next-font-loader', | ||
options: { | ||
isServer: ctx.isServer, | ||
assetPrefix: ctx.assetPrefix, | ||
fontLoaderOptions, | ||
postcss, | ||
}, | ||
}) | ||
|
||
return loaders | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.