Đây là cách đội của tôi đã đi về bao gồm các phông chữ Roboto trong dự án Webpack của chúng tôi:
Tải phông chữ Roboto và thực hiện một tập tin CSS trong một thư mục font-cụ thể
- Tạo một thư mục (
/fonts
).
- Tải xuống tất cả phông chữ Roboto từ Font Squirrel và di chuyển chúng vào
/fonts
.
- Tạo tệp CSS (
/fonts/index.css
). Chúng tôi có nội dung cho tệp này từ 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;
}
Sử dụng các module file-loader webpack tải trong các tập tin phông chữ để webpack có thể nhận ra chúng
webpack.conf.js:
loaders: [
..., {
test: /\.(woff|woff2|eot|ttf|svg)$/,
loader: 'file?name=fonts/[name].[ext]'
}
]
Import các file font css trong mục nhập chính của ứng dụng
App.js:
import './fonts/index.css';
Và đó là nó. Phông chữ mặc định của ứng dụng của bạn giờ đây sẽ là Roboto.
CHỈNH SỬA: Phông chữ Roboto nào thực sự sử dụng Material-UI?
Một phần của câu hỏi này đang xác định ngay Phông chữ Roboto để đưa vào dự án vì toàn bộ phông chữ Roboto gần như là 5MB.
Trong README, hướng dẫn bao gồm điểm Roboto tới: fonts.google.com/?selection.family=Roboto:300,400,500. Ở đây, 300 = Roboto-Light, 400 = Roboto-Regular và 500 = Roboto-Medium. Các giá trị này tương ứng với trọng số phông chữ được xác định trong typography.js file. Trong khi ba trọng số phông chữ này tính đến mức sử dụng gần như toàn bộ thư viện, thì có một tham chiếu đến Regular-Bold trong DateDisplay.js. Nếu bạn không sử dụng DatePicker, có lẽ bạn nên an toàn để bỏ qua điều đó. In nghiêng kiểu chữ không được sử dụng ở bất kỳ đâu trong dự án ngoài kiểu dáng GitHub markdown.
Thông tin này là chính xác tại thời điểm viết bài này, nhưng nó có thể thay đổi trong tương lai.
Cảm ơn. Vâng, đó * hoạt động *. Nhưng vấn đề là, tôi đang tạo [PWA] (https://developers.google.com/web/progressive-web-apps/) có nghĩa là ** tất cả ** nội dung được tải xuống và lưu vào bộ nhớ cache. Chỉ cần các tập tin phông chữ một mình lên tới ** 4,5 megabyte **. Tôi đoán (hy vọng) rằng không phải tất cả các phong cách (Thin, Light, Medium, ...) thực sự cần thiết bởi MUI - nhưng làm thế nào để tìm ra ...? –
Ah, tôi thấy sự cố ngay bây giờ. Tôi không biết rằng bạn cần bất kỳ phông chữ nào khác ngoài Roboto-Regular. Nó phụ thuộc vào nếu có bất kỳ thành phần nào sử dụng kiểu chữ 'font-weight' hoặc' font-style'. Tôi sẽ cố gắng xác minh điều này tối nay. Trong mọi trường hợp, tôi đã tìm thấy [một bài viết về các phông chữ tải chậm để đạt được hiệu suất] (https://css-tricks.com/preventing-the-performance-hit-from-custom-fonts/). Bạn có thể tải Roboto-Regular trả trước và lười biếng tải các biến thể khác sau này? –
Bài viết thú vị.Tuy nhiên, vì đây là một PWA, tôi cần phải có tất cả các tệp phông chữ khả dụng khi ngoại tuyến (nghĩa là chúng phải được tải xuống). Tôi đoán không có cách nào khác để loại bỏ các kiểu không cần thiết (và do đó xác định kiểu nào là không cần thiết) –