question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Equivalent of require.context from Webpack?

See original GitHub issue

Choose one: 🙋 feature request?

🔦 Context

What I’m trying to do is import all images (> 100 images) from a subdirectory, using a regexp. It was possible using webpack require.context (code sample below). I have seen #511 which talks about webpack require.context. This feature was very important (for me atleast) since it saves a tons of lines instead of importing all your assets line by line.

💻 Code Sample

I have a utility function which automatically require all my assets depending of the subdir and regexp I pass as parameters. This is a code sample that work on the latest version of webpack (3.10.0).

// require-all.js
function requireAll(r) {
  const dirContent = r.keys().map(r);
  const obj = {};

  dirContent.map((value) => {
    let name = value.substring(value.lastIndexOf('/') + 1, value.indexOf('.'));
    obj[name] = value;
  });

  return obj;
}

export { requireAll };

// foo.js
import { requireAll } from './require-all';

const images = requireAll(require.context('../../assets/', true, /\.png$/));
const audios = requireAll(require.context('../../assets/', true, /\.mp3$/));

Which throw an error because require.context doesn’t exist with Parcel and is specific to webpack:

Uncaught TypeError: require.context is not a function

Any plans on supporting or introduce the equivalent of require.context?

🌍 Your Environment

Software Version(s)
Parcel 1.5.1
Node 8.9.2
npm/Yarn yarn@1.3.2
Operating System MacOS High Sierra 10.13.3

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

20reactions
TotomInccommented, Apr 12, 2018

I’ve made a small util function to retrieve all images from a folder that I was using in my old projects:

// Util
function requireAll(r) {
  const dirContent = r.keys().map(r);
  const obj = {};

  dirContent.map((value) => {
    let name = value.substring((value.indexOf('img/') + 4), value.indexOf('.', 3));
    obj[name] = value;
  });

  return obj;
}

export default requireAll;

// Usage
const images = requireAll(require.context('./assets/', false, /\.png$/));

This should get you started on how to retrieve files from a folder, including its sub-folder.

More info about require.context here.

2reactions
lbguilhermecommented, Feb 9, 2018

You should be able to do const images = require('../../assets/*.png');. But I just tested it and seems like it caused some unrelated bug.

So what I can say is that the feature is there, but it has issues. Anyway, can you try that?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency Management - webpack
You can create your own context with the require.context() function. It allows you to pass in a directory to search, a flag indicating...
Read more >
What is `require.context`? - webpack - Stack Overflow
require.context is a special feature supported by webpack's compiler that allows you to get all matching modules starting from some base ...
Read more >
Use Webpack and require.context to load HTML files
Now we can load HTML files, we can set about using require.context() to load in some files within a folder and process their...
Read more >
require-context - npm
Require with an expression.. Latest version: 1.1.0, last published: 5 years ago. Start using require-context in your project by running `npm ...
Read more >
How does Webpack's require.context() work? : r/javascript
They're using require.context from Webpack. What i can't understand it how it can be called as a function ( req(filename) ) and be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found