gpt4 book ai didi

javascript - Webpack 无法解析相对路径

转载 作者:行者123 更新时间:2023-12-03 03:53:59 25 4
gpt4 key购买 nike

我有这个文件夹结构

enter image description here

这是我的 webpack.config.js

var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
entry: {
bundle: ['./src/index.js',
'./assets/style.less']
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'web/dist'),
publicPath: path.resolve('/webpackdemo/web/dist/')
},
module: {
rules: [
{
test: /\.less$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: ["css-loader", "resolve-url-loader", "less-loader"]
})
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
loader: 'file-loader?name=/fonts/[name].[ext]'
}
]
},
plugins: [
new ExtractTextPlugin('style.css'),
new CleanWebpackPlugin(['web/dist'])
]
};

Assets /style.less

@import url(http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,300,500,600,700&subset=latin,latin-ext);

@import "linearicons/linearicons.less";

body{
font-family: "Open Sans", sans-serif;
font-size: 12px;
}

然后是 asset/linearicon/linearicon.less

@font-face {
font-family: 'linearicons';
src:url('fonts/Linearicons-Free.eot?w118d');
src:url('fonts/Linearicons-Free.eot?#iefixw118d') format('embedded-opentype'),
url('fonts/Linearicons-Free.woff2?w118d') format('woff2'),
url('fonts/Linearicons-Free.woff?w118d') format('woff'),
url('fonts/Linearicons-Free.ttf?w118d') format('truetype'),
url('fonts/Linearicons-Free.svg?w118d#Linearicons-Free') format('svg');
font-weight: normal;
font-style: normal;
}

问题在于,由于 url("fonts/Linearicon-*),编译会以错误结束。

ERROR in ./node_modules/css-loader!./node_modules/resolve-url-loader?root=./../!./node_modules/less-loader/dist/cjs.js!./assets/style.less Module not found: Error: Can't resolve './linearicons/fonts/Linearicons-Free.eot?w118d' in '/home/wwwhome/webpackdemo/assets' @ ./node_modules/css-loader!./node_modules/resolve-url-loader?root=./../!./node_modules/less-loader/dist/cjs.js!./assets/style.less 6:173-230 @ ./assets/style.less @ multi ./src/index.js ./assets/style.less

对于编译器来说,它与文件夹“linearicons”相关,但我想始终在 Assets /字体中解析它。我可以更改 url('../fonts/Linearicons-Free.eot?w118d') 中的所有网址,但这并不方便,因为这是具有数百个更少文件的更大主题的一部分。我尝试向resolve-url-loader?root=assets/fonts 添加一个根参数,但没有成功。

有什么想法吗?

谢谢

最佳答案

您可以为此添加resolve.alias

var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
entry: {
bundle: ['./src/index.js',
'./assets/style.less']
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'web/dist'),
publicPath: path.resolve('/webpackdemo/web/dist/')
},
module: {
rules: [
{
test: /\.less$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: ["css-loader", "resolve-url-loader", "less-loader"]
})
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
loader: 'file-loader?name=/fonts/[name].[ext]'
}
]
},
plugins: [
new ExtractTextPlugin('style.css'),
new CleanWebpackPlugin(['web/dist'])
],
resolve: {
alias: {
linearicons: path.resolve(__dirname, 'fonts/')
}
}
};

关于javascript - Webpack 无法解析相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45035591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com