I'm looking for a way to access classes inside a compiled webpack bundle after using new Function() to execute the bundle.
For example, I have
export class TestClass {
constructor() {
console.log('Success');
}
}
Which webpack compiles to:
/******/ (() => { // webpackBootstrap
/******/ "use strict";
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it uses a non-standard name for the exports (exports).
(() => {
var exports = __webpack_exports__;
/*!*********************!*\
!*** ./src/test.ts ***!
\*********************/
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.TestClass = void 0;
class TestClass {
constructor() {
console.log('Success');
}
}
exports.TestClass = TestClass;
})();
/******/
})()
;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7QUFBQSxNQUFhLFNBQVM7SUFDbkI7UUFDRyxPQUFPLENBQUMsR0FBRyxDQUFDLFNBQVMsQ0FBQyxDQUFDO0lBQzFCLENBQUM7Q0FDSDtBQUpELDhCQUlDIiwic291cmNlcyI6WyJ3ZWJwYWNrOi8vc3RyYXRlZ3kvc3JjL1VzZXJzL2FkbWluL2NvZGUvYm90bWFpbi9zdHJhdGVneS9zcmMvdGVzdC50cyJdLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgY2xhc3MgVGVzdENsYXNzIHtcbiAgIGNvbnN0cnVjdG9yKCkge1xuICAgICAgY29uc29sZS5sb2coJ1N1Y2Nlc3MnKTtcbiAgIH1cbn0iXSwibmFtZXMiOltdLCJzb3VyY2VSb290IjoiIn0=
If I load this bundle from the file system and execute it using new Function(), how can I then access TestClass?
I would like to do something like:
const code = await fs.readFile(path, 'utf8')
const rawModule = new Function(code)();
const instance = new rawModule.TestClass();
I basically need to access __webpack_exports__. Is there some webpack loader or other way I can do this?