bul*_*ain 10 javascript webpack svg-sprite webpack-4
我在设置 web-pack 4 和 svg-sprite-loader 以将 svg 图标呈现为背景图像时遇到问题。我正在遵循 svg-sprite-loader 官方文档中的这些说明(https://github.com/kisenka/svg-sprite-loader/tree/master/examples/extract-mode)。
我已经成功地在我的 dist 文件夹中创建了 sprite.svg 文件,并将其用作我在 html 中使用标签的参考。但是,我也尝试将 src/images/icons 文件夹中的 svg 图标用于这样的背景图像:
background: url('../images/icons/upload_icon.svg') 10% 50% no-repeat;
Run Code Online (Sandbox Code Playgroud)
这样做时,webpack 会正确编译,但会在 dist css 文件中创建它:
background: url([object Module]) 10% 50% no-repeat;
Run Code Online (Sandbox Code Playgroud)
任何帮助都会很棒。
这是我的 webpack 配置文件:
const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const SpriteLoaderPlugin = require("svg-sprite-loader/plugin");
module.exports = {
mode: "development",
devtool: "source-map",
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "dist")
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
sourceMap: true
}
}
},
{
// scss configuration
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader"
},
{
loader: "postcss-loader"
},
{
loader: "sass-loader",
options: {
sourceMap: true
}
}
]
},
{
// html configuration
test: /\.html$/,
use: {
loader: "html-loader"
}
},
{
// images configuration
test: /\.(jpg|jpeg|gif|png|woff|woff2)$/,
use: [
{
loader: "file-loader",
options: {
name: "[path][name].[ext]"
}
}
]
},
{
test: /\.svg$/,
use: [
{
loader: "svg-sprite-loader",
options: {
extract: true,
spriteFilename: "sprite.svg"
}
}
]
}
]
},
plugins: [
// all plugins used for compiling by webpack
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
title: "Style Guide",
template: path.resolve(__dirname, "src", "index.html")
}),
new MiniCssExtractPlugin({
filename: "app.css"
}),
new SpriteLoaderPlugin()
]
};
Run Code Online (Sandbox Code Playgroud)
Pat*_*yOK 14
添加esModule: false到文件加载器选项对我来说很有用。
{
test: /\.(jpg|png|gif|svg)$/,
use: {
loader: 'file-loader',
options: {
name: "[name].[ext]",
outputPath: "img",
esModule: false
}
},
Run Code Online (Sandbox Code Playgroud)
你必须通过esModule: false的svg-sprite-loader选项。
顺便说一句(它与 esModule 无关):使用MiniCssExtractPlugin你不能提取 svg sprite。一小时前我遇到了这个问题..
几个小时后,我成功地让这件事开始工作,感谢@WimmDeveloper 为我指明了正确的方向。与之前的 webpack 配置文件相比,主要变化是我在 svg-sprite-loader 选项中添加了 esModule: false ,并将 MiniCssExtractPlugin 替换为 extract-text-webpack-plugin。请注意,这个解决方案并不理想,因为这个 webpack 插件已被弃用。
这是我的完整 webpack 配置文件:
const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const SpriteLoaderPlugin = require("svg-sprite-loader/plugin");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
mode: "development",
devtool: "source-map",
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "dist")
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
sourceMap: true
}
}
},
{
test: /\.(s*)css$/,
use: ExtractTextPlugin.extract({
use: ["css-loader", "postcss-loader", "sass-loader"]
})
},
{
// html configuration
test: /\.html$/,
use: {
loader: "html-loader"
}
},
{
test: /\.svg$/,
use: [
{
loader: "svg-sprite-loader",
options: {
esModule: false,
extract: true,
spriteFilename: "sprite.svg"
}
}
]
},
{
// files configuration
test: /\.(jpg|jpeg|gif|png|woff|woff2)$/,
use: [
{
loader: "file-loader",
options: {
name: "[path][name].[ext]"
}
}
]
}
]
},
plugins: [
// all plugins used for compiling by webpack
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
title: "Style Guide",
template: path.resolve(__dirname, "src", "index.html")
}),
new ExtractTextPlugin({ filename: "app.css" }),
new SpriteLoaderPlugin()
]
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8072 次 |
| 最近记录: |