css @font-face 的 Webpack 配置?当前设置将 url() 更改为一个很长的字符串
Webpack configuration for css @font-face? Current setup changes url() to a very long string
我正在使用 webpack 5.4.0
捆绑和最小化我的 css
这没问题,除了 fonts.css
文件,最初只是这样:
@font-face {
font-family: 'Kulim Park';
font-style: normal;
font-weight: 400;
src: url('../fonts/kulim-park-v2-latin/kulim-park-v2-latin-regular.eot');
src: local(''), url('../fonts/kulim-park-v2-latin/kulim-park-v2-latin-regular.eot?#iefix') format('embedded-opentype'),
url('../fonts/kulim-park-v2-latin/kulim-park-v2-latin-regular.woff2') format('woff2'),
url('../fonts/kulim-park-v2-latin/kulim-park-v2-latin-regular.woff') format('woff'),
url('../fonts/kulim-park-v2-latin/kulim-park-v2-latin-regular.ttf') format('truetype'),
url('../fonts/kulim-park-v2-latin/kulim-park-v2-latin-regular.svg#KulimPark') format('svg');
}
@font-face {
font-family: 'Staatliches';
font-style: normal;
font-weight: 400;
src: url('../fonts/staatliches-v5-latin/staatliches-v5-latin-regular.eot');
src: local('Staatliches Regular'), local('Staatliches-Regular'),
url('../fonts/staatliches-v5-latin/staatliches-v5-latin-regular.eot?#iefix') format('embedded-opentype'),
url('../fonts/staatliches-v5-latin/staatliches-v5-latin-regular.woff2') format('woff2'),
url('../fonts/staatliches-v5-latin/staatliches-v5-latin-regular.woff') format('woff'),
url('../fonts/staatliches-v5-latin/staatliches-v5-latin-regular.ttf') format('truetype'),
url('../fonts/staatliches-v5-latin/staatliches-v5-latin-regular.svg#Staatliches') format('svg');
}
最终的包是公里数,url()
被转换成这个(在开发中,在生产中“正确”部分最小化)
@font-face {
font-family: 'Kulim Park';
font-style: normal;
font-weight: 400;
src: url(data:application/vnd.ms-fontobject;base64,kEoAAIxJAAACAAIABAAAAAAAAAAAAAAAAAABAJABAAAAAExQJwAAgEsAAAAAAAAAAAAAAJMAACAAAAAA3HSrPQAAAAAAAAAAAAAAAAAAAAAAABQASwB1AGwAaQBtACAAUABhAHIAawAAAA4AUgBlAGcAdQBsAGEA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QBzlsKiaRfkty8BRALQC3AJcBQARwFCAgw3bqAiAE/M/lBwG5s8RnNv230X6n6rat65hAxsIGYXBNfdko2hjtlx (much longer)
webpack配置哪里出错了? (我粘贴了相关部分)
const fastGlob = require('fast-glob')
const path = require('path')
const webpack = require('webpack')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
const isDev = process.env.NODE_ENV !== 'production'
const filename = isDev ? '[name]' : '[contenthash].[name]'
const entries = {
css: [
path.resolve(__dirname, 'src/_assets/styles/base.css'),
path.resolve(__dirname, 'src/_assets/styles/fonts.css'),
path.resolve(__dirname, 'src/_assets/styles/site.css'),
path.resolve(__dirname, 'src/_assets/styles/atomic.css'),
path.resolve(__dirname, 'src/_assets/styles/theme.css'),
],
}
module.exports = {
mode: isDev ? 'development' : 'production',
stats: {
colors: true,
preset: 'minimal',
},
performance: { hints: isDev ? 'warning' : false },
devtool: isDev ? 'cheap-module-source-map' : 'source-map',
entry: entries,
output: {
path: path.resolve(__dirname, '_build/_assets'),
publicPath: '/_assets/',
},
plugins: [
new CompressionPlugin({
filename: '[path][base].gz',
algorithm: 'gzip',
test: /\.(js|css|html|svg)$/,
threshold: 10240,
minRatio: 0.8,
}),
new webpack.ids.HashedModuleIdsPlugin(),
new ManifestPlugin(),
new RemoveEmptyScriptsPlugin(),
new MiniCssExtractPlugin({
filename: `${filename}.css`,
}),
],
...(!isDev && {
optimization: {
nodeEnv: 'production',
mangleWasmImports: true,
removeAvailableModules: true,
minimizer: [
new TerserPlugin(),
new CssMinimizerPlugin({
minimizerOptions: {
preset: [
'default',
{
discardComments: { removeAll: true },
discardUnused: { removeAll: true },
},
],
},
}),
],
},
}),
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [{ loader: 'babel-loader' }],
},
{
test: /\.css$/i,
exclude: /node_modules/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {},
},
{
loader: 'css-loader',
options: {
sourceMap: isDev,
},
},
],
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
use: [{ loader: 'url-loader' }],
},
],
},
resolve: {
alias: {
assets: path.resolve(__dirname, 'src/_assets'),
},
},
}
因为您使用 url-loader
作为字体。使用 webpack 5,你可以使用 Asset Modules for loading fonts:
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
我正在使用 webpack 5.4.0
捆绑和最小化我的 css这没问题,除了 fonts.css
文件,最初只是这样:
@font-face {
font-family: 'Kulim Park';
font-style: normal;
font-weight: 400;
src: url('../fonts/kulim-park-v2-latin/kulim-park-v2-latin-regular.eot');
src: local(''), url('../fonts/kulim-park-v2-latin/kulim-park-v2-latin-regular.eot?#iefix') format('embedded-opentype'),
url('../fonts/kulim-park-v2-latin/kulim-park-v2-latin-regular.woff2') format('woff2'),
url('../fonts/kulim-park-v2-latin/kulim-park-v2-latin-regular.woff') format('woff'),
url('../fonts/kulim-park-v2-latin/kulim-park-v2-latin-regular.ttf') format('truetype'),
url('../fonts/kulim-park-v2-latin/kulim-park-v2-latin-regular.svg#KulimPark') format('svg');
}
@font-face {
font-family: 'Staatliches';
font-style: normal;
font-weight: 400;
src: url('../fonts/staatliches-v5-latin/staatliches-v5-latin-regular.eot');
src: local('Staatliches Regular'), local('Staatliches-Regular'),
url('../fonts/staatliches-v5-latin/staatliches-v5-latin-regular.eot?#iefix') format('embedded-opentype'),
url('../fonts/staatliches-v5-latin/staatliches-v5-latin-regular.woff2') format('woff2'),
url('../fonts/staatliches-v5-latin/staatliches-v5-latin-regular.woff') format('woff'),
url('../fonts/staatliches-v5-latin/staatliches-v5-latin-regular.ttf') format('truetype'),
url('../fonts/staatliches-v5-latin/staatliches-v5-latin-regular.svg#Staatliches') format('svg');
}
最终的包是公里数,url()
被转换成这个(在开发中,在生产中“正确”部分最小化)
@font-face {
font-family: 'Kulim Park';
font-style: normal;
font-weight: 400;
src: url(data:application/vnd.ms-fontobject;base64,kEoAAIxJAAACAAIABAAAAAAAAAA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CbD9RqoP9BhkiYBKz3QJNBwOGJywcLtwdsV+Cxj1YSS5NgDsfzBEYExgp6Q+7B4YM2lO1gwqdVGyh2B2Ap5ypYXoFPIUyek71HYxKfDeBKgSQDPsIPQDRxHcPCncAYQBzlsKiaRfkty8BRALQC3AJcBQARwFCAgw3bqAiAE/M/lBwG5s8RnNv230X6n6rat65hAxsIGYXBNfdko2hjtlx (much longer)
webpack配置哪里出错了? (我粘贴了相关部分)
const fastGlob = require('fast-glob')
const path = require('path')
const webpack = require('webpack')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
const isDev = process.env.NODE_ENV !== 'production'
const filename = isDev ? '[name]' : '[contenthash].[name]'
const entries = {
css: [
path.resolve(__dirname, 'src/_assets/styles/base.css'),
path.resolve(__dirname, 'src/_assets/styles/fonts.css'),
path.resolve(__dirname, 'src/_assets/styles/site.css'),
path.resolve(__dirname, 'src/_assets/styles/atomic.css'),
path.resolve(__dirname, 'src/_assets/styles/theme.css'),
],
}
module.exports = {
mode: isDev ? 'development' : 'production',
stats: {
colors: true,
preset: 'minimal',
},
performance: { hints: isDev ? 'warning' : false },
devtool: isDev ? 'cheap-module-source-map' : 'source-map',
entry: entries,
output: {
path: path.resolve(__dirname, '_build/_assets'),
publicPath: '/_assets/',
},
plugins: [
new CompressionPlugin({
filename: '[path][base].gz',
algorithm: 'gzip',
test: /\.(js|css|html|svg)$/,
threshold: 10240,
minRatio: 0.8,
}),
new webpack.ids.HashedModuleIdsPlugin(),
new ManifestPlugin(),
new RemoveEmptyScriptsPlugin(),
new MiniCssExtractPlugin({
filename: `${filename}.css`,
}),
],
...(!isDev && {
optimization: {
nodeEnv: 'production',
mangleWasmImports: true,
removeAvailableModules: true,
minimizer: [
new TerserPlugin(),
new CssMinimizerPlugin({
minimizerOptions: {
preset: [
'default',
{
discardComments: { removeAll: true },
discardUnused: { removeAll: true },
},
],
},
}),
],
},
}),
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [{ loader: 'babel-loader' }],
},
{
test: /\.css$/i,
exclude: /node_modules/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {},
},
{
loader: 'css-loader',
options: {
sourceMap: isDev,
},
},
],
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
use: [{ loader: 'url-loader' }],
},
],
},
resolve: {
alias: {
assets: path.resolve(__dirname, 'src/_assets'),
},
},
}
因为您使用 url-loader
作为字体。使用 webpack 5,你可以使用 Asset Modules for loading fonts:
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},