|
1 |
| -import { dedent, define } from 'polyscript/exports'; |
| 1 | +import { dedent, define } from "polyscript/exports"; |
2 | 2 |
|
3 |
| -import { stdlib } from '../core.js'; |
4 |
| -import { configDetails } from '../config.js'; |
5 |
| -import { getText } from '../fetch.js'; |
| 3 | +import { stdlib } from "../core.js"; |
| 4 | +import { configDetails } from "../config.js"; |
| 5 | +import { getText } from "../fetch.js"; |
6 | 6 |
|
7 | 7 | const hooks = {
|
8 |
| - main: { |
9 |
| - onReady: async (wrap, script) => { |
10 |
| - if (script.hasAttribute('config')) { |
11 |
| - const value = script.getAttribute('config'); |
12 |
| - const { json, toml, text } = configDetails(value); |
13 |
| - let config = {}; |
14 |
| - if (json) |
15 |
| - config = JSON.parse(text); |
16 |
| - else if (toml) { |
17 |
| - const { parse } = await import( |
18 |
| - /* webpackIgnore: true */ "../3rd-party/toml.js" |
19 |
| - ); |
20 |
| - config = parse(text); |
21 |
| - } |
22 |
| - if (config.packages) { |
23 |
| - const micropip = wrap.interpreter.pyimport('micropip'); |
24 |
| - await micropip.install(config.packages, { keep_going: true }); |
25 |
| - micropip.destroy(); |
26 |
| - } |
27 |
| - } |
| 8 | + main: { |
| 9 | + onReady: async (wrap, script) => { |
| 10 | + if (script.hasAttribute("config")) { |
| 11 | + const value = script.getAttribute("config"); |
| 12 | + const { json, toml, text } = configDetails(value); |
| 13 | + let config = {}; |
| 14 | + if (json) config = JSON.parse(text); |
| 15 | + else if (toml) { |
| 16 | + const { parse } = await import( |
| 17 | + /* webpackIgnore: true */ "../3rd-party/toml.js" |
| 18 | + ); |
| 19 | + config = parse(text); |
| 20 | + } |
| 21 | + if (config.packages) { |
| 22 | + const micropip = wrap.interpreter.pyimport("micropip"); |
| 23 | + await micropip.install(config.packages, { |
| 24 | + keep_going: true, |
| 25 | + }); |
| 26 | + micropip.destroy(); |
| 27 | + } |
| 28 | + } |
28 | 29 |
|
29 |
| - wrap.interpreter.registerJsModule("_pyscript", { |
30 |
| - PyWorker() { |
31 |
| - throw new Error('Unable to use PyWorker in py-game scripts'); |
32 |
| - }, |
33 |
| - js_import: (...urls) => Promise.all(urls.map((url) => import(url))), |
34 |
| - get target() { |
35 |
| - return script.id; |
36 |
| - }, |
37 |
| - }); |
| 30 | + wrap.interpreter.registerJsModule("_pyscript", { |
| 31 | + PyWorker() { |
| 32 | + throw new Error( |
| 33 | + "Unable to use PyWorker in py-game scripts", |
| 34 | + ); |
| 35 | + }, |
| 36 | + js_import: (...urls) => |
| 37 | + Promise.all(urls.map((url) => import(url))), |
| 38 | + get target() { |
| 39 | + return script.id; |
| 40 | + }, |
| 41 | + }); |
38 | 42 |
|
39 |
| - await wrap.interpreter.runPythonAsync(stdlib); |
| 43 | + await wrap.interpreter.runPythonAsync(stdlib); |
40 | 44 |
|
41 |
| - let code = dedent(script.textContent); |
42 |
| - if (script.src) |
43 |
| - code = await fetch(script.src).then(getText); |
44 |
| - |
45 |
| - const target = script.getAttribute('target') || 'canvas'; |
46 |
| - const canvas = document.getElementById(target); |
47 |
| - wrap.interpreter.canvas.setCanvas2D(canvas); |
48 |
| - await wrap.interpreter.runPythonAsync(code); |
49 |
| - }, |
50 |
| - }, |
| 45 | + let code = dedent(script.textContent); |
| 46 | + if (script.src) code = await fetch(script.src).then(getText); |
| 47 | + |
| 48 | + const target = script.getAttribute("target") || "canvas"; |
| 49 | + const canvas = document.getElementById(target); |
| 50 | + wrap.interpreter.canvas.setCanvas2D(canvas); |
| 51 | + await wrap.interpreter.runPythonAsync(code); |
| 52 | + }, |
| 53 | + }, |
51 | 54 | };
|
52 | 55 |
|
53 |
| -define('py-game', { |
54 |
| - config: { packages: ['pygame-ce'] }, |
55 |
| - configURL: new URL('./config.txt', location.href).href, |
56 |
| - interpreter: 'pyodide', |
57 |
| - env: 'py-game', |
58 |
| - hooks, |
| 56 | +define("py-game", { |
| 57 | + config: { packages: ["pygame-ce"] }, |
| 58 | + configURL: new URL("./config.txt", location.href).href, |
| 59 | + interpreter: "pyodide", |
| 60 | + env: "py-game", |
| 61 | + hooks, |
59 | 62 | });
|
0 commit comments