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

255
Views
How to write a webpack plugin to replace require function to its source code

Say we have a util.js contains functions a and b:

// util.js
export function a() {
  ...
}

export function b() {
  ...
}

And I require them in index.js:

// index.js
export default function main() {
  const {a, b} = require('./util');
  
  ...
  a(); // use them somewhere
  b(); 
}

But I want to replace the require function to its source code like this before the bundling:

// replaced index.js
export default function main() {
  const a = function () {
    // ... source code from util.js
  };
  const b = function () {
    // ... source code from util.js
  };
  
  ...
  a(); // use them somewhere
  b();
}

I'm not that familiar with webpack plugins and hooks API. Is this possible and if yes, how to do it?

Thanks!

One solution not that clever, do string replace directly by string-replace-loader.

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!