Skip to content

Commit c0e12b5

Browse files
committed
add fonts loaders + webpack resolve.roots
see webpack-contrib/css-loader#1256
1 parent 00c1e8d commit c0e12b5

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

packages/docusaurus/src/webpack/base.ts

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
getCustomBabelConfigFilePath,
2020
getMinimizer,
2121
} from './utils';
22+
import {STATIC_ASSETS_DIR_NAME} from '../constants';
2223

2324
const CSS_REGEX = /\.css$/;
2425
const CSS_MODULE_REGEX = /\.module\.css$/;
@@ -91,6 +92,14 @@ export function createBaseConfig(
9192
resolve: {
9293
extensions: ['.wasm', '.mjs', '.js', '.jsx', '.ts', '.tsx', '.json'],
9394
symlinks: true,
95+
roots: [
96+
// Allow resolution of url("/fonts/xyz.ttf") by webpack
97+
// See https://webpack.js.org/configuration/resolve/#resolveroots
98+
// See https://github.com/webpack-contrib/css-loader/issues/1256
99+
path.join(siteDir, STATIC_ASSETS_DIR_NAME),
100+
siteDir,
101+
process.cwd(),
102+
],
94103
alias: {
95104
'@site': siteDir,
96105
'@generated': generatedFilesDir,
@@ -152,6 +161,7 @@ export function createBaseConfig(
152161
module: {
153162
rules: [
154163
fileLoaderUtils.rules.images(),
164+
fileLoaderUtils.rules.fonts(),
155165
fileLoaderUtils.rules.media(),
156166
fileLoaderUtils.rules.svg(),
157167
fileLoaderUtils.rules.otherAssets(),

packages/docusaurus/src/webpack/utils.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ export function compile(config: Configuration[]): Promise<Stats.ToJsonOutput> {
235235
});
236236
}
237237

238-
type AssetFolder = 'images' | 'files' | 'medias';
238+
type AssetFolder = 'images' | 'files' | 'fonts' | 'medias';
239239

240240
// Inspired by https://github.com/gatsbyjs/gatsby/blob/8e6e021014da310b9cc7d02e58c9b3efe938c665/packages/gatsby/src/utils/webpack-utils.ts#L447
241241
export function getFileLoaderUtils(): Record<string, any> {
@@ -291,6 +291,13 @@ export function getFileLoaderUtils(): Record<string, any> {
291291
};
292292
},
293293

294+
fonts: (): RuleSetRule => {
295+
return {
296+
use: [loaders.url({folder: 'fonts'})],
297+
test: /\.(woff|woff2|eot|ttf|otf)$/,
298+
};
299+
},
300+
294301
/**
295302
* Loads audio and video and inlines them via a data URI if they are below
296303
* the size threshold

0 commit comments

Comments
 (0)