Skip to content

Commit 2bb3a24

Browse files
Merge pull request #71 from pyscript/issue-70
Fix #70 - Use the config base URL to resolve modules
2 parents 8d44c6b + 8e1e01e commit 2bb3a24

File tree

12 files changed

+34
-10
lines changed

12 files changed

+34
-10
lines changed

docs/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ In *polyscript*, this is possible by defining one or more `[js_modules.X]` field
266266

267267
### js_modules config example
268268

269+
**TOML**
270+
269271
```toml
270272
[js_modules.main]
271273
# this modules work best on main
@@ -281,6 +283,8 @@ In *polyscript*, this is possible by defining one or more `[js_modules.X]` field
281283
"https://cdn.jsdelivr.net/npm/worker-only" = "worker_only"
282284
```
283285

286+
**JSON**
287+
284288
```js
285289
{
286290
"js_modules": {
@@ -297,6 +301,8 @@ In *polyscript*, this is possible by defining one or more `[js_modules.X]` field
297301
}
298302
```
299303

304+
**Python**
305+
300306
```html
301307
<!-- main case -->
302308
<script type="pyodide" config="./that.toml">

docs/core.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

esm/interpreter/_utils.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,14 @@ const RUNNING_IN_WORKER = typeof document === UNDEFINED;
173173
export const fetchJSModules = ({ main, worker }) => {
174174
const promises = [];
175175
if (worker && RUNNING_IN_WORKER) {
176-
for (const [source, name] of entries(worker))
176+
for (let [source, name] of entries(worker)) {
177+
source = absoluteURL(source, base.get(worker));
177178
promises.push(importJS(source, name));
179+
}
178180
}
179181
if (main && !RUNNING_IN_WORKER) {
180-
for (const [source, name] of entries(main)) {
182+
for (let [source, name] of entries(main)) {
183+
source = absoluteURL(source, base.get(main));
181184
if (isCSS(source)) importCSS(source);
182185
else promises.push(importJS(source, name));
183186
}

esm/interpreters.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export const interpreter = new Proxy(new Map(), {
3838
const value = config?.[entry];
3939
if (value) base.set(value, baseURL);
4040
}
41+
for (const entry of ['main', 'worker']) {
42+
const value = config?.js_modules?.[entry];
43+
if (value) base.set(value, baseURL);
44+
}
4145
return engine(module, config, url);
4246
});
4347
},

esm/worker/_template.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import * as JSON from '@ungap/structured-clone/json';
88
import coincident from 'coincident/window';
99

10-
import { assign, create, createFunction, createOverload, createResolved, dispatch, entries, isCSS, js_modules } from '../utils.js';
10+
import { absoluteURL, assign, create, createFunction, createOverload, createResolved, dispatch, entries, isCSS, js_modules } from '../utils.js';
11+
import { base } from '../interpreter/_utils.js';
1112
import { configs, registry } from '../interpreters.js';
1213
import { getRuntime, getRuntimeID } from '../loader.js';
1314
import { patch, polluteJS, js as jsHooks, code as codeHooks } from '../hooks.js';
@@ -144,8 +145,9 @@ add('message', ({ data: { options, config: baseURL, code, hooks } }) => {
144145
js_modules: new Proxy(globalThis[js_modules], {
145146
get(map, name) {
146147
if (!map.has(name) && mainModules) {
147-
for (const [source, module] of entries(mainModules)) {
148+
for (let [source, module] of entries(mainModules)) {
148149
if (module !== name) continue;
150+
source = absoluteURL(source, base.get(mainModules));
149151
if (isCSS(source)) sync.importCSS(source);
150152
else {
151153
sync.importJS(source, name);

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<li><a href="/test/plugins/index.html">custom tags</a></li>
3636
<li><a href="/test/async-events/">async events</a></li>
3737
<li><a href="/test/multi-turtle/">multi turtle</a></li>
38+
<li><a href="/test/modules.html">js_modules</a></li>
3839
</ul>
3940
</body>
4041
</html>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,6 @@
8383
"sticky-module": "^0.1.1"
8484
},
8585
"worker": {
86-
"blob": "sha256-qCANJhVOyA+B1ms4kuhKj6mC9rErzLpD95XDILaiP+0="
86+
"blob": "sha256-cvr5TwJH3FX7KekdE0ALuJNaSIhhi7xaK7rHUQ0FNxE="
8787
}
8888
}

test/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ const polyscript = require("../cjs");
167167
{ from: "utils" },
168168
{ from: "/utils", files: ["c.py"] },
169169
],
170+
js_modules: {
171+
main: { "./modules.js": "random_js" }
172+
}
170173
});
171174
patchFetch(() =>
172175
Promise.resolve({

test/modules.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ <h3>Main</h3>
2222
# needed to fix pyodide proxies
2323
from pyodide.ffi import to_js
2424

25-
from polyscript.js_modules import leaflet as L
25+
from polyscript.js_modules import random_js, leaflet as L
26+
print(random_js.default)
2627

2728
center = to_js([51.505, -0.09])
2829
mark = to_js([51.5, -0.09])
@@ -41,7 +42,9 @@ <h3>Main</h3>
4142
<h3>Worker</h3>
4243
<div id="worker-map"></div>
4344
<script type="pyodide" config="./modules.toml" worker>
44-
from polyscript.js_modules import leaflet as L
45+
from polyscript.js_modules import random_js, leaflet as L
46+
47+
print(random_js.default)
4548

4649
center = [51.505, -0.09]
4750
mark = [51.5, -0.09]

0 commit comments

Comments
 (0)