Skip to content

Commit

Permalink
[FIX] Require logic is faulty for compiled apps (#40)
Browse files Browse the repository at this point in the history
* Fix require logic for compiled apps

* Fix typo in comment
  • Loading branch information
d-gubert committed Oct 6, 2021
1 parent bef8e49 commit ffc0ed6
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/compiler/AppsEngineValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ export class AppsEngineValidator {

const result = vm.runInContext(compilationResult.files[`${ filename }.js` as keyof ICompilerResult['files']].compiled, context);

return result || exports;
/**
* `result` will contain ONLY the result of the last line evaluated
* in the script by `vm.runInContext`, and NOT the full `exports` object.
*
* However, we need to handle this case due to backwards compatibility,
* since the main class file might export a class with an unknown name,
* which was supported in the early versions of the Apps-Engine.
*
* So here, if we find that the required file exports ONLY ONE property,
* which is what happens in the case of the main class file, we can return
* the `result`; otherwise, we return the full `exports` object.
*/
if (Object.keys(exports).length === 1) {
return result;
}
return exports;
}
}

0 comments on commit ffc0ed6

Please sign in to comment.