effekt-lang / effekt-lang/effekt
Emit CommonJS in JS backends
- Dominant language
- Scala
- Stars
- 469
- Forks
- 41
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 23
Description
Currently, the `commonjs` export produces the following export at the end:
```js
module.exports = { main: () => main_1394().run() };
```
Unfortunately, browsers use a completely different module system so this produces a crash at runtime (as `module` doesn't exist).
### Alternative 1
Let's hot-fix that by generating the following export (or a similar one) which works both in a browser and in Node.JS:
```js
(typeof module != 'undefined' && module !== null ? module : {}).exports = $MODULE_NAME = {
main: () => main_1435().run()
}
```
[where `$MODULE_NAME` is the name of our module]
🪄 Here's the `sed` call needed
```sh
sed -i'' -e 's/module.exports/(typeof module != "undefined" \&\& module !== null ? module : {}).exports = $MODULE_NAME/g' out/$MYFILE.js
```
Here's the code that would need to be changed:
https://github.com/effekt-lang/effekt/blob/6f8973ae77e4962b67b3cb626142fab7430a6cd8/effekt/shared/src/main/scala/effekt/generator/js/Tree.scala#L46-L48
### Alternative 2
Let's use [ES modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) instead! They have been stable for about 15 years by now and work both in a browser and in Node*.
This way, we could also get rid of the following hack:
https://github.com/effekt-lang/effekt/blob/6f8973ae77e4962b67b3cb626142fab7430a6cd8/effekt/js/src/test/scala/effekt/WebTests.scala#L29-L34
*The only issue is that for Node.JS, all of our files would have to have the `.mjs` extension...
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.