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

Added a way to register JS modules directly #69

Merged
merged 2 commits into from
Nov 30, 2023
Merged

Conversation

WebReflection
Copy link
Contributor

@WebReflection WebReflection commented Nov 28, 2023

This MR provides the ability parse the [js_modules.main] and [js_modules.worker] config entries in a source -> target manner so that it's possible to declare any JS module (as standard ECMAScript Module, aka: ESM) that would be automatically available via polyscript.js_modules reference.

Modules that are on main only and don't have any worker counterpart are fetched on the fly in workers.

Modules that require also companion CSS styles can be defined by providing a .css href and still pointing at the same module name, as that gets resolved lazily as opposite of landing out of the box any CSS that's present in the config.

Example

# file ./modules.toml

[js_modules.main]
"https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet-src.esm.js" = "leaflet"
"https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.css" = "leaflet"
<div id="worker-map"></div>
<script type="pyodide" config="./modules.toml" worker>
    from polyscript.js_modules import leaflet as L

    center = [51.505, -0.09]
    mark = [51.5, -0.09]
    map = L.map('worker-map').setView(center, 13)

    L.tileLayer(
        'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
        {"attribution": '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'}
    ).addTo(map)

    L.marker(mark).addTo(map).bindPopup('Modules on Worker are awesome 🥳').openPopup()
</script>

The wonderful leaflet has been used to demo both main and worker case.

TODOs

  • make this resolution happens
  • write documentation around that resolution - edit this should actually happen in PyScript as we link config details to PyScript config resolution

@WebReflection WebReflection force-pushed the js_modules branch 3 times, most recently from 0ea67c7 to ad1ea8b Compare November 28, 2023 17:40
@WebReflection WebReflection marked this pull request as draft November 29, 2023 09:55
@WebReflection WebReflection force-pushed the js_modules branch 2 times, most recently from 5461fae to 734caf2 Compare November 29, 2023 15:51
@WebReflection WebReflection marked this pull request as ready for review November 29, 2023 16:09
@WebReflection WebReflection merged commit 8993bf7 into main Nov 30, 2023
2 checks passed
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

Successfully merging this pull request may close these issues.

1 participant