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

248
Views
Generating a main script and a some libraries/modules with Webpack 5

I'm trying to build a web application made one main entry point (index.js) and a number of modules that I want to load dynamically.

Something like:

index.js
document.getElementById('button1').addEventListener('click', async function(){
  const mod = await import('module1.js');
  mod.run();
});
module1.js
export function run() {
  console.log("Hello from module1");
}

Is it possible to build a bundle for each of these files using Webpack 5?

What I tried

output.library (but it builds everything as libraries)
  1. It seems that Webpack lets me set multiple entry points.

  2. However to expose all of module1.js's exports I need to set output.library.

  3. But if I do, then index.js is built as a library too, since there can be only one output field.

Multiple configurations (but it breaks the dev server)

I could configure Webpack to build both my main script and the modules using multiple configurations, but if I try this, then the webpack-dev-server tries to start two instances and fails.

It prints errors like:

<e> [webpack-dev-middleware] ConcurrentCompilationError: You ran Webpack twice. Each instance only supports a single concurrent compilation at a time.

What else can I try?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Found an answer: the correct way to do this is to use a dynamic import. By default Webpack places the dynamically-imported resource in different files.

More documentation:

  • Code Splitting
  • Lazy Loading
  • Dynamic expressions in import()
about 4 years ago · Juan Pablo Isaza Report
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!