Giter VIP home page Giter VIP logo

Comments (9)

sontek avatar sontek commented on March 29, 2024 5

I'm with @FezVrasta, is this not supported anymore?

from webpack.

sokra avatar sokra commented on March 29, 2024 1

Sounds good and usually I would do so. It would be no problem for enhanced-require but for webpack. Webpack has to parse this function by statical code analysis so it has some restrictions. I. e. a function cannot be a closure, an object cannot contain references to variables. I think passing primitive values make this more clear.

require.context("../abc", { a: "text" }); // ok
require.context("../abc", { a: variable }); // bad
require.context("../abc", function(name) { return name.substr(2, 4) == "aa"}); // ok
require.context("../abc", function(name) { return name.substr(2, 4) == start}); // bad

require.context("../abc", true, /^[a-z]+$/); // ok
require.context("../abc", true, regExp); // bad

from webpack.

rdrey avatar rdrey commented on March 29, 2024

Sorry, I'm not sure whether this belonged here, in enhanced-require or enhanced-resolve.

I'm reading the enhanced-resolve docs and maybe this could be done with a postprocess: {context: fn}?

from webpack.

rdrey avatar rdrey commented on March 29, 2024

No'pe, that's a dead end. I thought I could modify the context in the postprocess method, but it only supplies me with the context's path.

from webpack.

sokra avatar sokra commented on March 29, 2024

require.context is currently really unflexible... It includes all files that are requirable with a string added to the context string. This means if you can write ./B.js it's included. webpackOptions.extensions is used to match possible files.

This is required for compatibility. I.e. require("../components/" + expr)

Current problems for using it for other intents:

  • Subdirectories are included. No opt-out possible.
  • No file filters.
  • enhanced-require do not include subdirectories. (Bug)

You can solve all these problems with a custom loader, but would be more pretty if you could use require.context.

So the idea is to add two more parameters to require.context in one of the next webpack versions to make it more useable:

require.context(context: String, recursive: Boolean = true, regExp: RegExp = null)

  • recusive include subdirectories.
  • regExp filter included files. You promise that the string you pass to the generated function will match (after the "./").

An example

var fn = require.context("../components", false, /^[^\.]*$/);

This would include the correct files.

Any comments/ideas?

from webpack.

rdrey avatar rdrey commented on March 29, 2024

Thanks, yeah here are some comments:

  1. I like how you usually pass in options objects, since they make handling default/optional/named arguments easier to handle.
  2. If the third parameter could take a filter function, it would be a bit more flexible than with just RegExp objects.

I might look at how to do this tomorrow, I've spent all of today outside in the sun. :)

from webpack.

sokra avatar sokra commented on March 29, 2024

This is sheduled for 0.9...

from webpack.

sokra avatar sokra commented on March 29, 2024

Done

from webpack.

FezVrasta avatar FezVrasta commented on March 29, 2024

@sokra sorry if I'm writing in a so old issue but I'm facing issues with the proposed syntax of the post above.

I thought I could be able to do

require.context('../../app', name => ['app.js', 'reducers.js', 'routes.js', 'store.js'].indexOf(name) === -1)

to exclude the files listed in the array, but it doesn't seem to work at all.

Am I missing something or this feature has been removed?

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.