webpack: Rule with issuer is not applied when importing via require.context

Bug report

What is the current behavior? Loaders are not applied for files, imported via require.context, if rule has “issuer” option.

If the current behavior is a bug, please provide the steps to reproduce. See reproduction https://github.com/huston007/webpack-issuer-repro

In short: In webpack.config:

rules: [
      {
        test: /\.svg/,
        issuer: /\.js/, // Remove this line and everything works
        loader: 'url-loader'
      }
    ]

a.js:

function importAll (r) {
  r.keys().forEach(r);
}

importAll(require.context('.', false, /\.svg$/));

Build error:

WARNING in ./circle.svg 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><circle cx="8" cy="8" r="8"/></svg>
 @ . sync nonrecursive \.svg$ ./circle.svg
 @ ./a.js

Note that it is doesn’t matter what is inside “issuer” property. It also breaks if using wildcarded require like require('./'+fileName+'.svg')

What is the expected behavior? Loaders are applied in-spite of a way how file is imported: via direct require or via context.

Known workaround It is possible to workaround by inlining loader syntax like: require.context('!file-loader!./', true, /\.svg$/)

Other relevant information: webpack version: 4.35.0 Node.js version: 10.15.0 Operating System: macOS

Maybe related to #6915

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 15
  • Comments: 16 (8 by maintainers)

Most upvoted comments

Issue was closed because of inactivity.

If you think this is still a valid issue, please file a new issue with additional information.

Same problem with import(`./${icon}.svg`) and require(`./${icon}.svg`) My config:

{
  test: /\.svg$/,
  issuer: /\.jsx?$/,
  use: [
    { loader: '@svgr/webpack' },
  ],
},

What is the status on this guys ? For webpack 4.42.0 this is still an issue 😦

i think this is still an issue. there’s also an unreviewed PR #10362 for it.

npm webpack --version yields 8.18.0 but I think I’m also suffering from this bug as it relates to https://github.com/vercel/next.js/discussions/15437