title: Compile JS module with Webpack before requiring it inside a loader
body: <p>I'm writing a loader for Webpack and one of its options is a string containing a path to a js file. This js file is then required in my loader because I need to get some data from it. The problem is that I'd like webpack to compile this js file before I require it my loader. The reason for that is that I'm requiring an image in the js file and I'd like to use webpack's require instead of node's require so that I'll get a path to that image.</p>
  <p>Some code might help</p>
  <p>the loader:</p>
  <pre><code>// ...
  
  //assume options.data is './file.js'
  const data = require(options.data);
  
  // ...
  </code></pre>
  <p>file.js</p>
  <pre><code>const img = require('@/assets/my-image.png');
  
  module.exports = { image: img }
  </code></pre>
  <p>This doesn't work because when I do <code>require(options.data)</code> node loads <code>file.js</code> and when it tries to <code>require('@/assets/my-image.png')</code> node throws an error because, obviously, it can't find the module <code>@/assets/my-image.png</code>.</p>
  <p>I could use <code>loadModule</code> to get the processed source code of <code>file.js</code> but I don't know how to specify a javascript loader or something like that in the request parameter of <code>loadModule</code>.</p>
  <p>How can I get webpack to compile <code>file.js</code> before I require it in my module? Is that even possible? If not, any workarounds?</p>