elastic / require-in-the-middle

Module to hook into the Node.js require function
MIT License
168 stars 26 forks source link

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

Open radiorz opened 1 year ago

radiorz commented 1 year ago

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);
radiorz commented 1 year ago

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']

radiorz commented 1 year ago

56

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

radiorz commented 1 year ago

duplicate #37