Allow pyscript package to contain multiple files (#1309)

Followup to pyscript#1232. Closes pyscript#1226.

Use node to make a manifest of the src/python dir and then use an esbuild
plugin to resolve an import called `pyscript_python_package.esbuild_injected.json`
to an object indicating the directories and files in the package folder.
This object is then used to govern runtime installation of the package.
This commit is contained in:
Hood Chatham
2023-03-29 07:31:14 -07:00
committed by GitHub
parent 3ae4b3c4de
commit 26f07246e1
4 changed files with 93 additions and 20 deletions

View File

@@ -17,12 +17,17 @@ import { SplashscreenPlugin } from './plugins/splashscreen';
import { ImportmapPlugin } from './plugins/importmap';
import { StdioDirector as StdioDirector } from './plugins/stdiodirector';
import type { PyProxy } from 'pyodide';
import * as Synclink from 'synclink';
// eslint-disable-next-line
// @ts-ignore
import pyscript from './python/pyscript/__init__.py';
import { robustFetch } from './fetch';
import { RemoteInterpreter } from './remote_interpreter';
import { robustFetch } from './fetch';
import * as Synclink from 'synclink';
// pyscript_package is injected from src/python by bundlePyscriptPythonPlugin in
// esbuild.js
// @ts-ignore
import python_package from 'pyscript_python_package.esbuild_injected.json';
declare const python_package: { dirs: string[]; files: [string, string] };
const logger = getLogger('pyscript/main');
@@ -268,9 +273,13 @@ export class PyScriptApp {
// compatible with the old behavior.
logger.info('importing pyscript');
// Save and load pyscript.py from FS
await interpreter.mkdirTree('/home/pyodide/pyscript');
await interpreter.writeFile('pyscript/__init__.py', pyscript as string);
// Write pyscript package into file system
for (const dir of python_package.dirs) {
await interpreter._remote.FS.mkdir('/home/pyodide/' + dir);
}
for (const [path, value] of python_package.files) {
await interpreter._remote.FS.writeFile('/home/pyodide/' + path, value);
}
//Refresh the module cache so Python consistently finds pyscript module
await interpreter._remote.invalidate_module_path_cache();