Это как моя команда пошла о включении шрифтов Roboto в нашем WebPack проекте:
Скачать шрифты Roboto и сделать файл CSS в папку шрифтов конкретных
- Создать папку (
/fonts
).
- Скачайте все шрифты Roboto с Font Squirrel и переместите их в
/fonts
.
- Создайте файл CSS (
/fonts/index.css
). Мы получили содержимое для этого файла от this tutorial.
index.css:
* {
font-family: Roboto, sans-serif;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-Regular-webfont.eot');
src: url('Roboto-Regular-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-Regular-webfont.woff') format('woff'),
url('Roboto-Regular-webfont.ttf') format('truetype'),
url('Roboto-Regular-webfont.svg#RobotoRegular') format('svg');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-Italic-webfont.eot');
src: url('Roboto-Italic-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-Italic-webfont.woff') format('woff'),
url('Roboto-Italic-webfont.ttf') format('truetype'),
url('Roboto-Italic-webfont.svg#RobotoItalic') format('svg');
font-weight: normal;
font-style: italic;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-Bold-webfont.eot');
src: url('Roboto-Bold-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-Bold-webfont.woff') format('woff'),
url('Roboto-Bold-webfont.ttf') format('truetype'),
url('Roboto-Bold-webfont.svg#RobotoBold') format('svg');
font-weight: bold;
font-style: normal;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-BoldItalic-webfont.eot');
src: url('Roboto-BoldItalic-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-BoldItalic-webfont.woff') format('woff'),
url('Roboto-BoldItalic-webfont.ttf') format('truetype'),
url('Roboto-BoldItalic-webfont.svg#RobotoBoldItalic') format('svg');
font-weight: bold;
font-style: italic;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-Thin-webfont.eot');
src: url('Roboto-Thin-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-Thin-webfont.woff') format('woff'),
url('Roboto-Thin-webfont.ttf') format('truetype'),
url('Roboto-Thin-webfont.svg#RobotoThin') format('svg');
font-weight: 200;
font-style: normal;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-ThinItalic-webfont.eot');
src: url('Roboto-ThinItalic-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-ThinItalic-webfont.woff') format('woff'),
url('Roboto-ThinItalic-webfont.ttf') format('truetype'),
url('Roboto-ThinItalic-webfont.svg#RobotoThinItalic') format('svg'); (under the Apache Software License).
font-weight: 200;
font-style: italic;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-Light-webfont.eot');
src: url('Roboto-Light-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-Light-webfont.woff') format('woff'),
url('Roboto-Light-webfont.ttf') format('truetype'),
url('Roboto-Light-webfont.svg#RobotoLight') format('svg');
font-weight: 100;
font-style: normal;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-LightItalic-webfont.eot');
src: url('Roboto-LightItalic-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-LightItalic-webfont.woff') format('woff'),
url('Roboto-LightItalic-webfont.ttf') format('truetype'),
url('Roboto-LightItalic-webfont.svg#RobotoLightItalic') format('svg');
font-weight: 100;
font-style: italic;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-Medium-webfont.eot');
src: url('Roboto-Medium-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-Medium-webfont.woff') format('woff'),
url('Roboto-Medium-webfont.ttf') format('truetype'),
url('Roboto-Medium-webfont.svg#RobotoMedium') format('svg');
font-weight: 300;
font-style: normal;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-MediumItalic-webfont.eot');
src: url('Roboto-MediumItalic-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-MediumItalic-webfont.woff') format('woff'),
url('Roboto-MediumItalic-webfont.ttf') format('truetype'),
url('Roboto-MediumItalic-webfont.svg#RobotoMediumItalic') format('svg');
font-weight: 300;
font-style: italic;
}
Используйте модуль файл-загрузчик WebPack для загрузки в файлы шрифтов так WebPack может распознать их
webpack.conf.js:
loaders: [
..., {
test: /\.(woff|woff2|eot|ttf|svg)$/,
loader: 'file?name=fonts/[name].[ext]'
}
]
Импорт файла CSS шрифта в главном входе в приложение
App.js:
import './fonts/index.css';
И это Это. Шрифт по умолчанию для вашего приложения должен быть Roboto.
РЕДАКТИРОВАТЬ: Какие шрифты Roboto действительно используют интерфейс-интерфейс?
Часть этого вопроса определяет право Roboto шрифты для включения в проект, так как шрифты Roboto составляют почти 5 МБ.
В инструкции README инструкции по включению Roboto указывают на: fonts.google.com/?selection.family=Roboto:300,400,500. Здесь 300 = Roboto-Light, 400 = Roboto-Regular и 500 = Roboto-Medium. Они соответствуют весам шрифтов, определенным в typography.js file. Хотя эти три плотности шрифтов учитывают использование почти во всей библиотеке, есть одна ссылка на Regular-Bold в DateDisplay.js. Если вы не используете DatePicker, вы, вероятно, должны быть в безопасности, чтобы опустить это. Стиль шрифта Italics не используется нигде в проекте, кроме стиля уценки GitHub.
Эта информация точна на момент написания этой статьи, но может измениться в будущем.
Спасибо. Да, это * работает *. Но проблема в том, что я создаю [PWA] (https://developers.google.com/web/progressive-web-apps/), что означает, что ** все ** активы загружаются и кэшируются. Только файлы шрифтов суммируются до ** 4,5 мегабайта **. Я надеюсь (надеюсь), что не все стили (тонкие, светлые, средние, ...) действительно нужны MUI, но как узнать ...? –
А теперь я вижу проблему. Я не знаю, что вам нужен другой шрифт помимо Roboto-Regular. Это зависит от того, есть ли какие-либо компоненты, которые используют стиль 'font-weight' или' font-style'. Я постараюсь проверить это сегодня вечером. В любом случае, я нашел [статью о ленивых загрузочных шрифтах для повышения производительности] (https://css-tricks.com/preventing-the-performance-hit-from-custom-fonts/). Не могли бы вы загрузить Roboto-Regular вперёд и lazy-load другие варианты позже? –
Интересная статья.Однако, поскольку это PWA, мне нужно, чтобы все файлы шрифтов были доступны в автономном режиме (это означает, что они должны быть загружены). Я думаю, что нет другого способа удалить ненужные стили (и, следовательно, определить, какие из них не нужны) –