Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export browser-friendly JS files #405

Open
jiribenes opened this issue Mar 4, 2024 · 2 comments · May be fixed by #511
Open

Export browser-friendly JS files #405

jiribenes opened this issue Mar 4, 2024 · 2 comments · May be fixed by #511
Labels

Comments

@jiribenes
Copy link
Contributor

jiribenes commented Mar 4, 2024

Currently, the commonjs export produces the following export at the end:

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:

(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
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:

val exportStatement = js.Assign(RawExpr("module.exports"),
js.Object(exports.map { e => e.name -> e.expr })
)

Alternative 2

Let's use ES 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:

mod.module = js.eval(
s"""|(function() {
| module = { exports: {} };
| ${contents}
| return module.exports
| }).apply(this)""".stripMargin).asInstanceOf[js.Dynamic]

*The only issue is that for Node.JS, all of our files would have to have the .mjs extension...

@b-studios
Copy link
Collaborator

For context: at some point we generated several different module formats (which is the reason why this is still called commonjs

It shouldn't be too hard to first hotfix and then implement Alternative 2.

It might also be relevant that the standalone compilation mode now is whole program. That is, we do not generate separate files that need to be linked when compiling to JS. This is only enabled for the website.

@b-studios
Copy link
Collaborator

Let's please implement Alternative 2 with .mjs file extensions.

marzipankaiser added a commit that referenced this issue May 28, 2024
@jiribenes jiribenes linked a pull request Jun 25, 2024 that will close this issue
7 tasks
marzipankaiser added a commit that referenced this issue Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants