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

Emit ES modules from JavaScript backend #511

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

jiribenes
Copy link
Contributor

@jiribenes jiribenes commented Jun 25, 2024

Resolves #405

Disclaimer: most of this PR was written by Claude 3.5 Sonnet

TODO

  • test separate compilation (EDIT: I left the separate compilation as is for now)
  • test how this works with the Effekt website
  • test ES modules in the browser
  • run CI
  • fix Windows (see comment below)
  • rewrite complex async/await loader into a simple .then loader
  • tidy up

How to use in the browser

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>My First Effekt Program</title>
  </head>
  <body>
    <script type="module">
      import { main } from './out/$MY_EFFEKT_MODULE.mjs'

      main()
    </script>
  </body>
</html>

Disclaimer: most of this PR was written by Claude 3.5 Sonnet
@jiribenes
Copy link
Contributor Author

On Windows, the hack to load a file dynamically without ES modules doesn't work, because there it's a .mjs file, not a file-ending-less file.

file:///D:/a/effekt/effekt/out/tests/effekt.javascripttests/sideeffects__main.mjs:2
const { pathToFileURL } = require('url');
                          ^
ReferenceError: require is not defined

Instead, we might want to:

  1. investigate whether we really need the pathToFileURL import at all
  2. write the import directly nicely in the .mjs file:
    val jsScript = s"""
      |import { main } from '${mjsFilePath}';
      |main();
    """.stripMargin

Comment on lines 171 to 174
// NOTE: This is a hack since this file cannot use ES imports & exports
// because it doesn't have the .mjs ending. Sigh.
// Also, we add the 'file://' prefix to satisfy Windows.
val jsScript = s"import('file://${mjsFileName}').then(({main}) => { main(); })"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll need to revisit this: the path should be relative (see #566).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just try using file:filename instead 🤔

Suggested change
// NOTE: This is a hack since this file cannot use ES imports & exports
// because it doesn't have the .mjs ending. Sigh.
// Also, we add the 'file://' prefix to satisfy Windows.
val jsScript = s"import('file://${mjsFileName}').then(({main}) => { main(); })"
// NOTE: This is a hack since this file cannot use ES imports & exports
// because it doesn't have the .mjs ending. Sigh.
// Also, we add the 'file:' prefix to satisfy Windows.
val jsScript = s"import('file:${mjsFileName}').then(({main}) => { main(); })"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhoh.

(node:8164) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must be absolute

I'll need to think about this, worstcase we need to dynamically get the current file in JS...

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 this pull request may close these issues.

Export browser-friendly JS files
1 participant