Skip to content

Context dependencies should support resource queries #5730

Closed

Description

@anuraaga

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

There is no way to pass a resource query when using context dependencies via require.context. In other words, this.resourceQuery will never be set inside a loader when using require.context.

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

What is the expected behavior?

this.resourceQuery should be set in a loader for something like the following:

const ctx = require.context('images?foo=bar', false, /\.jpg$/);
ctx('./a.jpg');

I have a test case and a proposed fix in my fork here - anuraaga#1
If the approach seems reasonable, I would be happy to move the PR to this repo for tweaks/submission, otherwise someone more familiar with the webpack design should probably take over as I just stumbled to that solution :)

If this is a feature request, what is motivation or use case for changing the behavior?

Some loaders (e.g. sharp-loader) use this.resourceQuery to enable certain behavior. This functionality cannot be used with context dependencies due to having no way to set a resource query. There doesn't seem to be any reason context dependencies should not support this feature as the only difference compared to a normal dependency is multiple files are resolved at the same time and placed in a map.

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.

All

Activity

  1. sokra commented on Sep 28, 2017

    @sokra
    Member

    Yep, send a PR

  2. added
    help wanted
    on Feb 21, 2018
  3. ogonkov commented on May 29, 2018

    @ogonkov

    Provided PR doesn't seems working for imports like this

    ../foo/${bar}.foo?my-query

    Should i make PR to support queries like the one above?

  4. ogonkov commented on May 29, 2018

    @ogonkov

    resourceQuery make it way to regExp here, is it expected behaviour, @sokra ?

  5. webpack-bot commented on Nov 28, 2018

    @webpack-bot
    Contributor

    This issue had no activity for at least half a year.

    It's subject to automatic issue closing if there is no activity in the next 15 days.

  6. added
    status: stale
    on Nov 28, 2018
  7. ogonkov commented on Nov 28, 2018

    @ogonkov

    I believe it still an issue

  8. removed
    status: stale
    on Nov 28, 2018
  9. webpack-bot commented on May 29, 2019

    @webpack-bot
    Contributor

    This issue had no activity for at least half a year.

    It's subject to automatic issue closing if there is no activity in the next 15 days.

  10. added
    status: stale
    on May 29, 2019
  11. alexander-akait commented on May 29, 2019

    @alexander-akait
    Member

    @ogonkov Problem still exists?

  12. removed
    status: stale
    on May 29, 2019
  13. 14 remaining items

  14. removed
    status: stale
    on May 16, 2020
  15. added
    status: stale
    on Aug 15, 2020
  16. removed
    status: stale
    on Aug 31, 2020
  17. jclow-sift commented on Nov 12, 2020

    @jclow-sift

    So just to clarify, the original request is specifically about require.context not import or import() The latter two have worked with resource queries for a while, but the require function returned by require.context has no facility for passing the resource query along.

    So, for example, let's say we want to write a rule to use a different svg loader based on the resource query...something like this:

    {
      test: /\.svg$/,
      resourceQuery: /sprite/,
      use: ['svg-sprite-loader', 'svgo-loader'],
    },
    

    With that rule, this import of foo.svg ends up using that set of loaders:
    import './foo.svg?sprite';

    but something like this does not:

    const requireSprites = require.context('./?sprite', false, /\.svg$/);
    requireSprites.keys().forEach(requireSprites);
    
  18. jclow-sift commented on Nov 13, 2020

    @jclow-sift

    For anybody landing here after struggling with this and require.context, take a look at https://webpack.js.org/concepts/loaders/#inline for possible solutions

  19. added
    status: stale
    on Feb 12, 2021
  20. removed
    status: stale
    on Mar 1, 2021
  21. alexander-akait commented on Mar 15, 2021

    @alexander-akait
    Member

    Original problem was resolved, if you still have a problem, please create a new issue, other issue #12900, test is preferred over resourceQuery, so if you have intersection for test, you need specify resourceQuery for both rules, i.e. resourceQuery: /raw/, and resourceQuery: /^(?!\?raw$).*/,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wanted

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions