Skip to content

Commit

Permalink
Fix ES module event plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismaltby committed Mar 16, 2024
1 parent 72318e2 commit a56a336
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/lib/project/loadScriptEventHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ export type ScriptEventHandlers = Dictionary<ScriptEventHandler>;
const vm = new NodeVM({
timeout: 1000,
sandbox: {},
compiler: (code: string) => {
// Convert es6 style modules to commonjs
let moduleCode = code;
moduleCode = code.replace(/(^|\n)(\S\s)*export /g, "");
if (moduleCode.indexOf("module.exports") === -1) {
const moduleExports =
code
.match(/export [a-z]* [a-zA-Z_$][0-9a-zA-Z_$]*]*/g)
?.map((c) => c.replace(/.* /, "")) ?? [];
moduleCode += `\nmodule.exports = { ${moduleExports.join(", ")} };`;
}
return moduleCode;
},
require: {
mock: {
"./helpers": eventHelpers,
Expand Down

0 comments on commit a56a336

Please sign in to comment.