Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
pass parameter to variable which assigned require.context

When require.context of webpack is assigned to variable, how this variable can work receiveing parameter, like 'key'? For example,(in webpack document)

var context = require.context('components', true, /\.html$/);

// this 'context' variable receive parameter 'localA'
var context = require.context('locales', true, /\.json$/, 'lazy');
context('localeA').then((locale) => {
  // do something with locale
});

In this code, I can't understand why context variable can receive parameter 'localeA'. I looked in webpack documents, but I couldn't find how variable which is assigned to require.context can receive the parameter.

See another example...

var req = require.context("../someDir", true, /^(.*\.(js$))[^.]*$/igm);
req.keys().forEach(function(key){
    req(key);
});
// or just: req.keys().forEach(req)

webpack require every file in directory

the var req already returned context which fits conditions specified in parameters. then, why this code recall 'req(key)' with 'key' parameter which represents element of array(context) agian? Does this code mean => require.context(element of context = each file)? I can't understand this recall either..

  • In summary, my question is..
  1. How variable which is assignd to require.context can accept parameter. in webpack document, require.context can accept 4 parameter, =>
require.context(
  (directory: String),
  (includeSubdirs: Boolean) /* optional, default true */,
  (filter: RegExp) /* optional, default /^\.\/.*$/, any file */,
  (mode: String) /* optional, 'sync' | 'eager' | 'weak' | 'lazy' | 'lazy-once', default 'sync' */
);

but in the conde i can't understand, the parameter which is sent to variable(is assigned to 'require.context') is key (element of context array)

  1. why they recall require.cnotext in require.context.keys().foreach(), var req = require.context() already returned context array(files fit conditions),then why this code repeats require.context(key) in foreach() block?
var req = require.context("../someDir", true, /^(.*\.(js$))[^.]*$/igm);
// => return context array


req.keys().forEach(function(key){
    req(key);
// => means 'require.context(key)?
// why repeat require.context(each file)?
});
// or just: req.keys().forEach(req)

Please help me to understand this..

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!