8000 Allow pyscript package to contain multiple files · hoodmane/pyscript@e304a09 · GitHub
[go: up one dir, main page]

Skip to content

Commit e304a09

Browse files
committed
Allow pyscript package to contain multiple files
Followup to pyscript#1232. Closes pyscript#1226. Use node to make a manifest of the src/python dir and then use terser to inject it into the bundle as a variable called pyscript_package. This means we need to always use the terser plugin even when not minifying. In the non-minify case, we disable terser minification and mangling and enable terser beautification. Note that we bundle mangled versions of many upstream npm dependencies, so even in debug/nonminified builds, these do not include symbol names.
1 parent 41a8d80 commit e304a09

File tree

2 files changed

+56
-21
lines changed

2 files changed

+56
-21
lines changed

pyscriptjs/rollup.config.js

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,31 @@ import css from 'rollup-plugin-css-only';
77
import serve from 'rollup-plugin-serve';
88
import { string } from 'rollup-plugin-string';
99
import copy from 'rollup-plugin-copy';
10+
import * as fs from 'fs';
11+
import * as path from 'path';
12+
13+
function directoryManifest(root, dir = ".", result = { dirs: [], files: [] }) {
14+
const curdir = path.join(root, dir);
15+
const dirObj = fs.opendirSync(curdir);
16+
try {
17+
let d;
18+
while ((d = dirObj.readSync())) {
19+
const entry = path.join(dir, d.name);
20+
if (d.isDirectory()) {
21+
if (d.name === '__pycache__') {
22+
continue;
23+
}
24+
result.dirs.push(entry);
25+
directoryManifest(root, entry, result);
26+
} else if (d.isFile()) {
27+
result.files.push([entry, fs.readFileSync(path.join(root, entry), {encoding: "utf-8"})]);
28+
}
29+
}
30+
return result;
31+
} finally {
32+
dirObj.close();
33+
}
34+
}
1035

1136
const production = !process.env.ROLLUP_WATCH || process.env.NODE_ENV === 'production';
1237

@@ -23,23 +48,28 @@ if (!production) {
2348

2449
export default {
2550
input: 'src/main.ts',
26-
output: [
27-
{
28-
file: 'build/pyscript.js',
29-
format: 'iife',
30-
sourcemap: true,
31-
inlineDynamicImports: true,
32-
name: 'pyscript',
33-
},
34-
{
35-
file: 'build/pyscript.min.js',
36-
format: 'iife',
37-
sourcemap: true,
38-
inlineDynamicImports: true,
39-
name: 'pyscript',
40-
plugins: [terser()],
41-
},
42-
],
51+
output: [{ minify: true }, { minify: false }].map(({ minify }) => ({
52+
file: `build/pyscript${minify ? '.min' : ''}.js`,
53+
format: 'iife',
54+
sourcemap: !production,
55+
inlineDynamicImports: true,
56+
name: 'pyscript',
57+
plugins: [
58+
terser({
59+
compress: {
60+
defaults: minify,
61+
dead_code: true,
62+
global_defs: {
63+
pyscript_package: directoryManifest('./src/python'),
64+
},
65+
},
66+
mangle: minify,
67+
format: {
68+
beautify: !minify,
69+
},
70+
}),
71+
],
72+
})),
4373
plugins: [
4474
css({ output: 'pyscript.css' }),
4575
// Bundle all the Python files into the output file
@@ -57,7 +87,6 @@ export default {
5787
}),
5888
// This will make sure that examples will always get the latest build folder
5989
copy(copy_targets),
60-
// production && terser(),
6190
!production &&
6291
serve({
6392
port: 8080,

pyscriptjs/src/main.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import { StdioDirector as StdioDirector } from './plugins/stdiodirector';
2121
import pyscript from './python/pyscript/__init__.py';
2222
import { robustFetch } from './fetch';
2323

24+
declare const pyscript_package : {dirs: string[], files: [string, string]};
25+
2426
const logger = getLogger('pyscript/main');
2527

2628
/* High-level overview of the lifecycle of a PyScript App:
@@ -207,9 +209,13 @@ export class PyScriptApp {
207209
// compatible with the old behavior.
208210
logger.info('importing pyscript');
209211

210-
// Save and load pyscript.py from FS
211-
interpreter._remote.interface.FS.mkdirTree('/home/pyodide/pyscript');
212-
interpreter._remote.interface.FS.writeFile('pyscript/__init__.py', pyscript);
212+
// Write pyscript package into file system
213+
for(let dir of pyscript_package.dirs) {
214+
interpreter._remote.interface.FS.mkdir('/home/pyodide/' + dir);
215+
}
216+
for(let [path, value] of pyscript_package.files) {
217+
interpreter._remote.interface.FS.writeFile('/home/pyodide/' + path, value);
218+
}
213219
//Refresh the module cache so Python consistently finds pyscript module
214220
interpreter._remote.invalidate_module_path_cache();
215221

0 commit comments

Comments
 (0)
0