Webpack - less-loader 中用于构建和生产的不同字体路径 css
Webpack - different font path for build and production css in the less-loader
我正在尝试使用 webpack 从 LESS 创建一个 CSS。我的源文件夹具有以下层次结构:
fonts
desyrel
desyrel_-webfont.woff
less
ds-handwritten.less
less文件内容包含相对字体路径,制作时会用到
@charset "utf-8";
@font-face {
font-family: 'desyrelregular';
src: url('/www/fonts/desyrel/desyrel_-webfont.woff') format('woff');
font-weight: normal;
font-style: normal;
font-display: swap;
}
//DS hand written
.ds-hw { font-family: 'desyrelregular', sans-serif !important; }
我的入口脚本如下:
import 'ds-less/ds-handwritten.less';
当我尝试 运行 webpack 脚本时,我收到以下错误:
Module build failed (from
./node_modules/mini-css-extract-plugin/dist/loader.js):
ModuleBuildError: Module build failed (from
./node_modules/css-loader/dist/cjs.js): Error: Can't resolve
'/www/fonts/desyrel/desyrel_-webfont.woff' in ''
如何通知 webpack,字体 '/www/fonts/desyrel/desyrel_-webfont.woff' 的相对路径指向源路径中的字体:'src/fonts/desyrel/desyrel_-webfont.woff'?
我尝试结合 file-loader 和 resolve-url-loader 来处理这个问题(根据官方文档 https://webpack.js.org/loaders/sass-loader/#problems-with-url 的提示),但没有成功。
module: {
rules: [
{
test: /\.(ttf|eot|woff|woff2|svg)$/,
include: path.resolve(__dirname, './src/fonts'),
use: {
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'www/fonts/',
esModule: false
},
},
},
{
test: /\.less$/,
exclude: /node_modules/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
},
{
loader: 'resolve-url-loader',
},
{
loader: 'less-loader',
options: {
lessOptions: {
strictMath: true,
},
}
},
],
},
]
},
解决方法是:
- 忽略 CSS-loader 中的 url(url:错误)
- 避免用 resolve-url-loader
解析 urls
module: {
rules: [
{
test: /\.(ttf|eot|woff|woff2|svg)$/,
include: path.resolve(__dirname, './src/fonts'),
use: {
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/',
esModule: false
},
},
},
{
test: /\.less$/,
exclude: /node_modules/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
options: {
url: false,
},
},
{
loader: 'less-loader',
options: {
lessOptions: {
strictMath: true,
},
}
},
],
},
]
},
我正在尝试使用 webpack 从 LESS 创建一个 CSS。我的源文件夹具有以下层次结构:
fonts desyrel desyrel_-webfont.woff less ds-handwritten.less
less文件内容包含相对字体路径,制作时会用到
@charset "utf-8";
@font-face {
font-family: 'desyrelregular';
src: url('/www/fonts/desyrel/desyrel_-webfont.woff') format('woff');
font-weight: normal;
font-style: normal;
font-display: swap;
}
//DS hand written
.ds-hw { font-family: 'desyrelregular', sans-serif !important; }
我的入口脚本如下:
import 'ds-less/ds-handwritten.less';
当我尝试 运行 webpack 脚本时,我收到以下错误:
Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js): ModuleBuildError: Module build failed (from ./node_modules/css-loader/dist/cjs.js): Error: Can't resolve '/www/fonts/desyrel/desyrel_-webfont.woff' in ''
如何通知 webpack,字体 '/www/fonts/desyrel/desyrel_-webfont.woff' 的相对路径指向源路径中的字体:'src/fonts/desyrel/desyrel_-webfont.woff'?
我尝试结合 file-loader 和 resolve-url-loader 来处理这个问题(根据官方文档 https://webpack.js.org/loaders/sass-loader/#problems-with-url 的提示),但没有成功。
module: {
rules: [
{
test: /\.(ttf|eot|woff|woff2|svg)$/,
include: path.resolve(__dirname, './src/fonts'),
use: {
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'www/fonts/',
esModule: false
},
},
},
{
test: /\.less$/,
exclude: /node_modules/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
},
{
loader: 'resolve-url-loader',
},
{
loader: 'less-loader',
options: {
lessOptions: {
strictMath: true,
},
}
},
],
},
]
},
解决方法是:
- 忽略 CSS-loader 中的 url(url:错误)
- 避免用 resolve-url-loader 解析 urls
module: {
rules: [
{
test: /\.(ttf|eot|woff|woff2|svg)$/,
include: path.resolve(__dirname, './src/fonts'),
use: {
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/',
esModule: false
},
},
},
{
test: /\.less$/,
exclude: /node_modules/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
options: {
url: false,
},
},
{
loader: 'less-loader',
options: {
lessOptions: {
strictMath: true,
},
}
},
],
},
]
},