Skip to content

Commit ffc0ed6

Browse files
authored
[FIX] Require logic is faulty for compiled apps (#40)
* Fix require logic for compiled apps * Fix typo in comment
1 parent bef8e49 commit ffc0ed6

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

src/compiler/AppsEngineValidator.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@ export class AppsEngineValidator {
112112

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

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

0 commit comments

Comments
 (0)