webpack: assets modules won't work for inline html

Bug report

What is the current behavior?

I know I can import using js, but how can I import in an HTML file or CSS file in Webpack 5?

doing this in css/sass:

background: url("../../images/image.png");

or this in html template:

<img class="logo-img" src="images/favicon.svg" alt="website logo" />

doesn’t work. I just get broken links. What am I missing for these asset modules? I’ve tried them all in all combos with all file extensions. I’ve been adding and deleting stuff but here’s what I have latest:

CleanShot 2020-11-10 at 22 21 35@2x

If the current behavior is a bug, please provide the steps to reproduce.

  module: {
    rules: [
      {
        test: /\.txt$/,
        type: 'asset/source'
      },
      {
        test: /\.(?:ico|gif|png|jpg|jpeg)$/i,
        type: 'asset/resource'
      },
      {
        test: /\.(woff(2)?|eot|ttf|otf|svg|)$/,
        type: 'asset/inline'
      },
      {
        test: /\.js$/, exclude: /node_modules/,
        use: ['babel-loader']
      },
      {
        test: /\.(scss|css)$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              sourceMap: true,
              importLoaders: 1
            }
          },
          {
            loader: 'postcss-loader',
            options: {
              sourceMap: true
            }
          },
          {
            loader: 'sass-loader',
            options: {
              sourceMap: true
            }
          },
        ],
      },
    ],
  },

What is the expected behavior?

The image shows up on the page.

Other relevant information: webpack version: 5.4.0 Node.js version: 14.4.0 Operating System: macOS latest version

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 3
  • Comments: 21 (19 by maintainers)

Most upvoted comments

The latest alpha of the html-webpack-plugin has partial support for asset/ressource:

https://github.com/jantimon/html-webpack-plugin/blob/next/examples/javascript/webpack.config.js

publicPath: "auto" doesn’t work in node.js and html-webpack-plugin runs the child compiler bundle in node.js. Within node.js webpack can’t figure out the public path. I think it should be fixable by using a relative public path in the child compiler publicPath: publicPath === "auto" ? "" : publicPath

Sorry I don’t know why webpack-cli is no longer supporting codesandbox - I’ll create an issue…

You can download a codesandbox here: export-zip

I fixed the codesandbox thanks to your latest cli release