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

Support type="module" scripts #1068

Open
curran opened this issue Feb 22, 2025 · 1 comment
Open

Support type="module" scripts #1068

curran opened this issue Feb 22, 2025 · 1 comment

Comments

@curran
Copy link
Collaborator

curran commented Feb 22, 2025

When we split up an HTML file into script tags with type="module", the runtime environment doesn't seem to be working properly.

Example: https://vizhub.com/curran/3d-fractal-tree/60c90d44f42c4f1585bd3cac59f22cfc?edit=files&file=index.html&tabs=js%2Fground.js%7Eindex.html

This code should work correctly:

!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Fractal 3D Tree</title>
    <script type="importmap">
      {
        "imports": {
          "three": "https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js",
          "three/addons/": "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/"
        }
      }
    </script>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <canvas id="three-canvas"></canvas>
    <script type="module" src="js/main.js"></script>
  </body>
</html>

It doesn't pull in the main.js.

@curran
Copy link
Collaborator Author

curran commented Feb 22, 2025

The problem:

  • index.html imports an ES module like this: <script type="module" src="js/main.js"></script> - so far OK
  • js/main.js imports from another JS file like import { foo } from './foo.js' - that's when it fails, because Magic Sandbox does not support ES modules properly

Potential solution:

  • Extend the V2 runtime environment to use <script type="module> in the main entry point
  • It has to be backwards compatible, so we could introduce a config option or something to switch from using browser globals to use proper ESM via importmap
  • Started on this in https://github.com/vizhub-core/vizhub/pull/1009/files

@curran curran changed the title Support type="module" scripts in Magic Sandbox Support type="module" scripts Feb 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant