0

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.

bambooom
  • 664
  • 6
  • 15
  • Why do you wanna do that in the first place? Could you kindly explain? – h-sifat Sep 18 '21 at 08:57
  • @h-sifat I insert and evaluate `main` function in webpages several times through puppeteer's `await page.evaluate(pageFunction, ...)` API. And it seems that the `a`, `b` functions need to be defined directly inside the main function to be correct. – bambooom Sep 18 '21 at 09:26
  • Oh I see. Sadly I can't help you. I've just started banging my head on the webpack docs few days ago. – h-sifat Sep 18 '21 at 13:04
  • @h-sifat thanks anyway! – bambooom Sep 20 '21 at 15:52

0 Answers0