elastic/require-in-the-middle

[question]could this package be used to wrap local files?

Opened this issue · 3 comments

Like I have a Test.js file,
could i change it like this:

const path = require("path");
const { Hook } = require("require-in-the-middle");

// Hook into the Test module
new Hook(["./Test.js"], function (exports, name, basedir) {
  // 在这里对Test模块进行修改或扩展
  console.log("Hooked into Test module:", name);

  // 返回你想要的修改后的exports对象
  return exports;
});

const Test = require("./Test.js");
console.log(`Test`, Test);

I find absolute path is work:

const Hook = require("require-in-the-middle");
// Hook into the Test module
const hook = new Hook(["G:\\xxx\\math\\require_hack\\Test.js"], function (
  exports,
  name,
  basedir
) {
  // 在这里对Test模块进行修改或扩展
  console.log("Hooked into module:", name);
  // 返回你想要的修改后的exports对象
  return exports;
});

const Test = require("./Test");

but it doesnt work when using null or ['./Test.js']

#56

This pr is work for local files, but still not merged into master branch

#duplicate #37