Coder Social home page Coder Social logo

When `require.context` is used in a module that has been discovered through an entry in `resolve.modules`, the keys are duplicated. about webpack HOT 4 CLOSED

cha0s avatar cha0s commented on April 28, 2024
When `require.context` is used in a module that has been discovered through an entry in `resolve.modules`, the keys are duplicated.

from webpack.

Comments (4)

cha0s avatar cha0s commented on April 28, 2024

The duplicates are created at https://github.com/webpack/webpack/blob/main/lib/dependencies/RequireContextPlugin.js#L133

If someone could explain what this code is meant to do, I could probably write a PR.

from webpack.

alexander-akait avatar alexander-akait commented on April 28, 2024

I want to say it is expected, for example:

const cache = {};

function importAll(r) {
  r.keys().forEach((key) => (cache[key] = r(key)));
}

importAll(require.context('../components/', true, /\.js$/));
// At build-time cache will be populated with all required modules.

It means you can import a module using './thing.js' and 'test/things/thing.js', you can solve it by using regexp and exclude duplicate.

This was done intentionally. Changing this logic can break monorepos and will be the breaking change. Anyway feel free to feedback and questions.

from webpack.

cha0s avatar cha0s commented on April 28, 2024

Thanks for the explanation!

There's something about it that just feels wrong. It definitely violated my expectation as a user. Perhaps a note in the documentation?

That being said, it's easy enough to work around with a RegExp. :)

from webpack.

alexander-akait avatar alexander-akait commented on April 28, 2024

@cha0s I am fine with updating docs, do you want to send a PR? Thank you

from webpack.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.