웹 팩 및 글꼴 페이스로 글꼴 로드
CSS를 사용하여 @font-face
폰트는 로딩되지 않습니다.이것은 제 디렉토리 구조입니다.
ㅇㅇㅇㅇ에서webpack.config.js
을 사용하다
var path = require('path');
var webpack = require('webpack');
module.exports = {
devtool: 'eval',
entry: [
"./index.js"
],
output: {
path: __dirname+"/build",
filename: "main.js"
},
plugins: [
new webpack.NoErrorsPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
],
resolve: {
extensions: ['', '.js', '.jsx']
},
module: {
loaders: [
{ test: /\.js$/, loaders: ['react-hot', 'babel-loader'], exclude: /node_modules/ },
{ test: /\.jsx?$/, loaders: ['react-hot', 'babel-loader'], exclude: /node_modules/ },
{ test: /\.svg$/, loader: "raw" },
{ test: /\.css$/, loader: "style-loader!css-loader" },
{ test: /\.(eot|svg|ttf|woff|woff2)$/, loader: 'file?name=src/css/[name].[ext]'}
]
}
};
CSS 파일에는 다음과 같은 것이 있습니다.
@font-face {
font-family: 'Darkenstone';
src: url('./Darkenstone.woff') format('woff');
}
body {
background-color: green;
font-size: 24px;
font-family: 'Darkenstone';
}
제 index.js
라이선스:
import './src/css/master.css';
모든 것이 동작하지만, 폰트가 로드되지 않는다.
여러 가지 시도를 한 끝에 다음 로더가 작업을 완료했습니다.파일 로더 대신 url-loader를 사용했는데 url-loader가 설치되어 있어야 합니다.
{ test: /\.(png|woff|woff2|eot|ttf|svg)$/, loader: 'url-loader?limit=100000' }
Web pack 4에서는, 다음과 같이 문제를 해결했습니다(diff).
{
test: /\.svg$/,
use: ['svg-loader']
},
{
test: /\.(eot|woff|woff2|svg|ttf)([\?]?.*)$/,
use: ['file-loader']
}
{ test: /\.(png|woff|woff2|eot|ttf|svg)$/, use: ['url-loader?limit=100000'] }
는 그것을 제거해야만 했다.svg-loader
★★★★★★★★★★★★★★★★★」file-loader
에 url-loader
★★★app.scss
을 사용하다
$fa-font-path: '~font-awesome/fonts';
@import '~font-awesome/scss/font-awesome';
$icon-font-path: "~bootstrap-sass/assets/fonts/bootstrap/";
@import '~bootstrap-sass/assets/stylesheets/bootstrap';
그리고 내 안에app.js
.app.scss
:
import './app.scss';
변경 후 웹 팩 설정은 다음과 같습니다.
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpackConfig = require('./webpack.config');
module.exports = {
entry: {
app: './client/app/app.js'
},
devtool: 'source-map',
mode: 'development',
plugins: [
new HtmlWebpackPlugin({
title: 'Development',
template: 'client/index.html'
})
],
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.(sa|sc|c)ss$/,
use: [
'style-loader',
'css-loader',
'sass-loader',
],
},
{ test: /\.(png|woff|woff2|eot|ttf|svg)$/, use: ['url-loader?limit=100000'] }
]
}
};
Webpack의 설명서에 의하면, 다음의 정보를 갱신할 필요가 있습니다.webpack.config.js
글꼴 파일을 처리합니다.츠키노
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
}
웹 팩 ★★★★★★webpack.config.js
을 사용하다
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource',
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
],
},
};
그런 다음 글꼴을 항목 파일로 가져와야 합니다. 경우, 「 」./src/index.js
경우, 글꼴을 하여 로더를 할 수 @font-face
로컬 ★★url(...)
디렉티브는 이미지와 마찬가지로 웹 팩에 의해 픽업됩니다.
Webpack 4.x.x에서 최신 5.52.1로 업데이트 했을 때 글꼴 면에 문제가 발생했습니다.
보보 of of의 .woff / .woff2 / .ttf ...
이렇게 생겼죠.
export default __webpack_public_path__ + "glyphicons-halflings-regular.woff2";
은 방금 전에 것을 것이었습니다.file-loader
번 한 것 .파일이 여러 번 처리되어 오류가 발생한 것 같습니다.
이전:
...
module: {
rules: [
{
test: /\.(png|jpg|gif|ico|woff|woff2|ttf|svg|eot)$/,
use: {
loader: 'file-loader',
options: {
name: '[name].[ext]',
useRelativePath: true
}
}
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: { url: false }
}
]
}
]
}
...
그 후:
...
module: {
rules: [
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: { url: false }
}
]
}
]
}
...
상세한 것에 대하여는, https://webpack.js.org/guides/asset-modules/ 를 참조해 주세요.
나도 같은 문제를 겪고 있었다.제 경우 폰트의 'src'가 src=srcobject Module]이 되었습니다.
웹 팩에서 esModule을 비활성화하는 것이 해결책이었습니다.
{
test: /\.(png|jpe?g|gif|svg|ttf|woff|otf)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[contenthash].[ext]',
outputPath: 'static/img',
esModule: false // <- here
}
}
]
}
자세한 내용은 여기를 참조하십시오.
이걸 알아내는 데 시간이 좀 걸려서 앞으로 도움이 될 것 같아서 여기에 글을 올렸습니다.노드 js / webpack에서 일반 css를 사용하는 경우 이 솔루션이 도움이 됩니다.
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"],
},
},
},
{
test: /\.html$/,
exclude: /node_modules/,
use: "html-loader",
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: "asset/resource",
generator: {
filename: "assets/[name][ext][query]",
},
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: "asset/resource",
generator: {
filename: "fonts/[name][ext][query]",
},
},
],
},
.js 파일에 필요한 Import문을 추가합니다.
// images references in the manifest
import "../../assets/icon-16.png";
import "../../assets/icon-32.png";
import "../../assets/icon-64.png";
import "../../assets/icon-80.png";
import "../../assets/icon-25.png";
import "../../assets/icon-48.png";
//fonts referenced in the css
import "../../Fonts/opensans-regular-webfont.eot";
import "../../Fonts/opensans-regular-webfont.svg";
import "../../Fonts/opensans-regular-webfont.ttf";
import "../../Fonts/opensans-regular-webfont.woff";
import "../../Fonts/opensans-regular-webfont.woff2";
import "../../Fonts/OpenSans-Regular.ttf";
그런 다음 작성한 dist/fonts 파일을 가리키도록 .css 파일을 업데이트하기만 하면 됩니다.
@font-face {
font-family: 'open_sansregular';
src: url('fonts/opensans-regular-webfont.eot');
src: url('fonts/Fonts/opensans-regular-webfont.eot?#iefix') format('embedded-opentype'), url('fonts/opensans-regular-webfont.woff2') format('woff2'), url('fonts/opensans-regular-webfont.woff') format('woff'), url('fonts/opensans-regular-webfont.ttf') format('truetype'), url('fonts/opensans-regular-webfont.svg#open_sansregular') format('svg');
font-weight: normal;
font-style: normal;
}
html,
body {
font-family: open_sansregular !important
}
그리고 프로젝트를 구축합니다.
로더를 다음으로 변경해 보십시오.
{test: /\.(eot|svg|ttf|woff|woff2)$/, loader: 'file?name=[name].[ext]'}
publicPath: 'build/'
고객님께output
열쇠.
webpack.config.ts에서는 다음 두 가지가 필요했습니다.웹 팩은 이 코드의 글꼴 파일을 처리합니다.
module: {
rules: [
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
}
]
}
그리고 @font-face 및 url()을 이해하기 위한 웹 팩용 css-loader와 DOM에 css를 삽입하기 위한 style-loader가 필요했습니다.
module: {
rules: [
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
{
test: /\.css$/i,
use: [
'style-loader',
'css-loader'
]
},
]
}
그런 다음 src add 아래의 index.css 파일에서 (내 글꼴은 Exo라고 불렸습니다)
@font-face {
font-family: "Exo";
src: local(Exo), // This names it to use in css
url('./fonts/exo-v20-latin/exo-v20-latin-regular.woff2') format('woff2'),
url('./fonts/exo-v20-latin/exo-v20-latin-regular.woff') format('woff');
font-weight: normal;
font-style: normal;
}
html, body {
font-family: Arial, Helvetica, sans-serif;
}
woff 포맷은 최신 브라우저에 모두 대응하고 있기 때문에 추천합니다.
반드시 css 파일을 메인 앱인덱스에 추가해 주세요.js:
import './index.css';
그런 다음 이러한 파일이 빌드/거리/공용 폴더(빌드 폴더 이름을 지정한 폴더)에 저장되도록 빌드할 수 있습니다.
npm run build
또는
yarn run build
이건 나한테 효과가 있었어.잘 됐으면 좋겠네요!
웹팩 버전을 4.20.2에서 4.42.0으로 넘겼더니 코드가 원하는 글꼴을 로드하기 시작했습니다.
const URL_LOADER_LIMIT = 8192
module.exports = () => ({
module: {
rules: [
{
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'url-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/',
limit: URL_LOADER_LIMIT
}
}
]
},
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
use: [
{
loader: 'url-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/',
limit: URL_LOADER_LIMIT,
mimetype: 'application/font-woff'
}
}
]
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'url-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/',
limit: URL_LOADER_LIMIT,
mimetype: 'application/octet-stream'
}
}
]
},
{
test: /\.mp3$/,
use: ['file-loader']
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'url-loader',
options: {
outputPath: 'images/',
name: '[name].[ext]',
limit: URL_LOADER_LIMIT,
mimetype: 'image/svg+xml'
}
}
]
},
{
test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'url-loader',
options: {
outputPath: 'images/',
name: '[name].[ext]',
limit: URL_LOADER_LIMIT
}
}
]
}
]
}
})
언급URL : https://stackoverflow.com/questions/45489897/load-fonts-with-webpack-and-font-face
'programing' 카테고리의 다른 글
Angular를 사용한 W3C 검증JS 디렉티브 (0) | 2023.03.28 |
---|---|
어떻게 내 angular.js ng-model에 json을 로드하지? (0) | 2023.03.28 |
spring-boot에 application.properties 파일을 여러 개 추가하는 방법 (0) | 2023.03.28 |
NameService에 대한 공급자 각도 없음 (0) | 2023.03.28 |
페이지의 모든 AJAX 요청에 "훅" 추가 (0) | 2023.03.28 |