0

Resolve-url-loader没有解析我的css 文件中的图像/字体。我的文件夹结构是这样的:

src 
   public
      images
           -banner-bg.jpg
      stylesheets
         css
           -basic.css with background: url(../../images/banner-bg.jpg)
         scss
           -main.scss with @import "../css/basic.css";

输出文件夹:

dist
  public
     images
           -banner-bg.jpg
     stylesheets
           -main-output.css

图像和字体在开发服务器中显示正常,并在生产中正确复制。main-output.css -检查时 - 显示

background:url(public/images/banner-bg.jpg

而不是预期的

background:url(../images/banner-bg.jpg)

浏览器控制台显示错误:

GET file: ///home/ustrd/Documents/myproject12/dist/public/stylesheets/public/images/banner-bg.jpg net:: ERR_FILE_NOT_FOUND

plugins: [
   new MiniCssExtractPlugin({
     filename: path.join("public", "stylesheets", "[name].css")
   })
 ],
 module: {
   rules: [
     {
       test: /\.(css|scss)$/,
       use: [
         MiniCssExtractPlugin.loader, 
         {
           loader: "css-loader",
           options: {
             sourceMap: true,
             importLoaders: 2
           }
         },
         {
           loader: "postcss-loader",
           options: {
             ident: "postcss",
             plugins: () => [new PostcssPresetEnv()]
           }
         },
         {
           loader: "resolve-url-loader",
           options: { sourceMap: true }
         },
         { loader: "sass-loader", options: { sourceMap: true } } 
       ]
     },

任何想法如何解决这个问题?

4

1 回答 1

0

我猜那resolve-url-loader不知道你的决赛的位置,main-output.css并希望它在dist. 您可以尝试使用自定义join函数来修复路径:

{
  loader: "resolve-url-loader",
  options: {
    sourceMap: true,
    join: (uri, base) => path.join('../..', base, uri);
  }
},

这应该输出如下内容:

background:url(../../public/images/banner-bg.jpg)

虽然没有测试过。检查它的工作原理以获取一些详细信息。另请查看 css-loader url设置。

更改 的位置可能更容易main-output.css

new MiniCssExtractPlugin({
     filename: "[name].css"
})
于 2019-06-17T21:52:30.410 回答