Webpack does not rewrite resource URLs (stylus)

I am trying to rewrite my resource paths from a specific path ( ~assets/myimage.png) to the correct directory (in my case /assets/).

I told my webpack to write all the images in a folder inside the public. Images are spelled correctly, however the css assembly url is not adjusted accordingly.

This is the rule that I use for my stylus files.

use: ExtractTextPlugin.extract({
  fallback: {
    loader: require.resolve("style-loader"),
    options: {
      hmr: false
    }
  },
  use: [
    {
      loader: require.resolve("css-loader")
    },
    {
      loader: require.resolve("stylus-loader")
    }
  ]
})

and the one that I use for assets

{
    test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
    loader: require.resolve("url-loader"),
    options: {
      limit: 100,
      name: "static/media/[name].[hash:8].[ext]"
    }
},

My alias that resolves the correct directory is:

assets: path.resolve(__dirname, "../public")

and the public path is set as /assets/

publicPath: publicPath + "assets/",

Since I am using ExtractTextPlugin, the style loader cannot be used. When using the style loader only it works, however in my use case I need a css file like/style.css

Any ideas on this?

+4
3

file-loader .

test: /\.(png|jpg|gif)$/,
        use: [
          {
            loader: 'file-loader',
            options: {}  
          }
        ]
      }
    ]
0

CSS file-loader/url-loader URL- .

stylus-loader, CSS resolve-url-loader:

module.exports = {
  module: {
    loaders: [
      {
        test   : /\.css$/,
        loaders: ['style-loader', 'css-loader', 'resolve-url-loader']
      }, {
        test   : /\.styl$/,
        loaders: ['style-loader', 'css-loader', 'resolve-url-loader', 'stylus-loader?sourceMap']
      }
    ]
  }
};
0

This is just a thought, but can your pseudonym be mistaken? You wrote

assets: path.resolve(__dirname, "../public")

but regarding the fact that your project structure looks something like this.

--project_root/
 |--public
 |--src
 |--webpack.conf.js

he should be

assets: path.resolve(__dirname, "public") //without the "folder up" operator
0
source

Source: https://habr.com/ru/post/1688920/


All Articles