Reading through the loader documentation, I noticed that this.exec is deprecated, but the docs don't mention why it was deprecated or whether there is a different API that should be used in its place. This would be helpful information for loader authors, especially since webpack-maintained loaders that rely on this functionality (e.g.) still appear to use this API.
Sounds reasonable, thanks for pointing this out @smably. Once we figure out the answer to this would you be interested in submitting a pr to resolve this?
@webpack/documentation-team can someone point us to the replacement method or new workflow that replaced this.exec?
Yes, I'd be happy to open a PR to fix this!
You can replace it with the following function:
const Module = require("module");
function exec(code, filename) {
const module = new Module(filename, this);
module.paths = Module._nodeModulePaths(this.context);
module.filename = filename;
module._compile(code, filename);
return module.exports;
}
@smably I know it's been a little while but does the above comment answer your question? Would you still be interested in submitting a PR?
Usage:
const tmpModule = exec.call(this, content, this.resource)
...
// Don't forget
delete require.cache[this.resource]
I've been using floatdrop/require-from-string for this 🤷♂️
This doesn't seem to work?
module.exports = function (content) {
const result = exec.call(this, content, this.resource);
console.log('RESULT', result);
}
const Module = require("module");
function exec(code, filename) {
const module = new Module(filename, this);
module.paths = Module._nodeModulePaths(this.context);
module.filename = filename;
module._compile(code, filename);
return module.exports;
}
It can't handle export default:
ERROR in Error: Child compilation failed:
Module build failed (from ./loaders/js2json-loader.js):
/home/mpen/Projects/crap/aaa-test-project/src/pwa.webmanifest.ts:18
export default manifest;
^^^^^^
SyntaxError: Unexpected token 'export
Most helpful comment
You can replace it with the following function: