Giter Site home page Giter Site logo

Comments (3)

mrjoelkemp avatar mrjoelkemp commented on May 21, 2024 1

Thanks for the thoughts! I wonder if the resolveToContext option is the path forward. I'm currently not using that when resolving files via webpack (there's a 3 argument form of the resolver where the first arg is the context).

If that resolver option were the key, this would get supported via a few changes:

  1. detective-cjs would need to support this special case https://github.com/dependents/node-detective-cjs/blob/4e31629f92bafa3df9be09f5ddb3bae1474156fa/index.js#L13 of require.context. It would then have to save the arguments via a non-string output format (or an encoded string if we want to avoid multiple types for elements of the array of dependencies that precinct returns).
  2. The (encoded) require.context arguments would need to get processed (turning on the resolveToContext flag) (likely within node-filing-cabinet) when we've detected that the coded data from step 1 above.

The painful part of pushing this through filing-cabinet is that the api of that module is meant to return a single string, not an array of dependencies that were generated from a single import (require.context call). As such, it might be worth putting this (either your solution or enhanced-resolve's) into detective-cjs proper.


I'm open to reviewing a pull request that gets the desired functionality in. My hesitation is that require.context and even dynamically requiring files (within a for loop) are not possible with ES6 imports – which we'll all eventually migrate to anyway. Not sure if it makes sense to put in the work for these dynamic requires, but I'm happy to review and merge if others feel strongly.

from node-precinct.

mrjoelkemp avatar mrjoelkemp commented on May 21, 2024

Thanks so much for reporting the issue @brianmhunt. We'd have to add a sniff for require.context in detective-cjs and then call out to either webpack's resolver or roll our own to evaluate require.context to yield all of the files to be required.

Any other ideas for making this work?

from node-precinct.

brianmhunt avatar brianmhunt commented on May 21, 2024

Thanks for the update @mrjoelkemp . I think that's pretty much it. I didn't see any exposed code in Webpack to allow use of require.context externally, and I did a quick search but didn't see any useful npm projects that might've already solved it.

The require.context prototype is:

require.context(directory, useSubdirectories = false, regExp = /^\.\//)

require.context ought to return

  1. a function that, when called, requires the given file (may be relative to the directory); and
  2. have a keys property that is a list of all the filenames that match.

The globbing ought to be pretty straightforward walk + grep.

The function could basically just be equivalent to a require call (possibly with "directory" prefixed to the argument).

Here's a quick mock-up of roughly what I think it'd need to do:

require.context = function(dir, includeSubdirectories, regex) {
    const contextualRequire = (file) => require(path.join(dir, file))
    contextualRequire.keys = someGlobFunction(dir, includeSubdirectories)
    if (regex) {
      contextualRequire.keys = contextualRequire.keys.filter((r) => regex.test(r))
    }
    return contextualRequire
} 

from node-precinct.

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.