-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Allow pyscript package to contain multiple files #1262
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
Changes from all commits
1e34b4b
b8d0835
564e1e1
19bfa96
04ae065
00f2d64
6ae4d7f
21ea205
ea45633
144efac
09be677
ad00491
2e158d4
ed61b1e
81b4c35
81878c8
dcdfc93
6154b5c
3c6fe2d
54ab4b3
8bb9ec4
40324ab
d68c6e7
bcb9d92
336782a
054ada0
859f127
281328b
06ea704
d0c6815
7957814
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,32 @@ import { terser } from 'rollup-plugin-terser'; | |
import typescript from '@rollup/plugin-typescript'; | ||
import css from 'rollup-plugin-css-only'; | ||
import serve from 'rollup-plugin-serve'; | ||
import { string } from 'rollup-plugin-string'; | ||
import copy from 'rollup-plugin-copy'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
function directoryManifest(root, dir = '.', result = { dirs: [], files: [] }) { | ||
const curdir = path.join(root, dir); | ||
const dirObj = fs.opendirSync(curdir); | ||
try { | ||
let d; | ||
while ((d = dirObj.readSync())) { | ||
hoodmane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const entry = path.join(dir, d.name); | ||
if (d.isDirectory()) { | ||
if (d.name === '__pycache__') { | ||
continue; | ||
} | ||
result.dirs.push(entry); | ||
directoryManifest(root, entry, result); | ||
} else if (d.isFile()) { | ||
result.files.push([entry, fs.readFileSync(path.join(root, entry), { encoding: 'utf-8' })]); | ||
} | ||
} | ||
return result; | ||
} finally { | ||
dirObj.close(); | ||
} | ||
} | ||
|
||
const production = !process.env.ROLLUP_WATCH || process.env.NODE_ENV === 'production'; | ||
|
||
|
@@ -22,29 +46,30 @@ if (!production) { | |
|
||
export default { | ||
input: 'src/main.ts', | ||
output: [ | ||
{ | ||
file: 'build/pyscript.js', | ||
format: 'iife', | ||
sourcemap: true, | ||
inlineDynamicImports: true, | ||
name: 'pyscript', | ||
}, | ||
{ | ||
file: 'build/pyscript.min.js', | ||
format: 'iife', | ||
sourcemap: true, | ||
inlineDynamicImports: true, | ||
name: 'pyscript', | ||
plugins: [terser()], | ||
}, | ||
], | ||
output: [{ minify: true }, { minify: false }].map(({ minify }) => ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where do I pass the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to change because of #1298. |
||
file: `build/pyscript${minify ? '.min' : ''}.js`, | ||
format: 'iife', | ||
sourcemap: !production, | ||
inlineDynamicImports: true, | ||
name: 'pyscript', | ||
plugins: [ | ||
terser({ | ||
compress: { | ||
defaults: minify, | ||
dead_code: true, | ||
global_defs: { | ||
pyscript_package: directoryManifest('./src/python'), | ||
}, | ||
}, | ||
mangle: minify, | ||
format: { | ||
beautify: !minify, | ||
}, | ||
}), | ||
], | ||
})), | ||
plugins: [ | ||
css({ output: 'pyscript.css' }), | ||
// Bundle all the Python files into the output file | ||
string({ | ||
include: './src/**/*.py', | ||
}), | ||
resolve({ | ||
browser: true, | ||
}), | ||
|
@@ -55,7 +80,6 @@ export default { | |
}), | ||
// This will make sure that examples will always get the latest build folder | ||
copy(copy_targets), | ||
// production && terser(), | ||
!production && | ||
serve({ | ||
port: 8080, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ export function make_PyScript(interpreter: InterpreterClient, app: PyScriptApp) | |
* | ||
* Concurrent access to the multiple py-script tags is thus avoided. | ||
*/ | ||
let releaseLock: any; | ||
let releaseLock: () => void; | ||
try { | ||
releaseLock = await app.tagExecutionLock(); | ||
ensureUniqueId(this); | ||
|
@@ -35,13 +35,15 @@ export function make_PyScript(interpreter: InterpreterClient, app: PyScriptApp) | |
this.innerHTML = ''; | ||
|
||
app.plugins.beforePyScriptExec({ interpreter: interpreter, src: pySrc, pyScriptTag: this }); | ||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ | ||
const result = (await pyExec(interpreter, pySrc, this)).result; | ||
app.plugins.afterPyScriptExec({ | ||
interpreter: interpreter, | ||
src: pySrc, | ||
pyScriptTag: this, | ||
result: result, | ||
}); | ||
/* eslint-enable @typescript-eslint/no-unsafe-assignment */ | ||
} finally { | ||
releaseLock(); | ||
} | ||
|
@@ -53,7 +55,8 @@ export function make_PyScript(interpreter: InterpreterClient, app: PyScriptApp) | |
try { | ||
const response = await robustFetch(url); | ||
return await response.text(); | ||
} catch (e) { | ||
} catch (err) { | ||
const e = err as Error; | ||
_createAlertBanner(e.message); | ||
this.innerHTML = ''; | ||
throw e; | ||
|
@@ -163,15 +166,15 @@ const pyAttributeToEvent: Map<string, string> = new Map<string, string>([ | |
]); | ||
|
||
/** Initialize all elements with py-* handlers attributes */ | ||
export function initHandlers(interpreter: InterpreterClient) { | ||
export async function initHandlers(interpreter: InterpreterClient) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are these now async? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry these changes are unrelated... |
||
logger.debug('Initializing py-* event handlers...'); | ||
for (const pyAttribute of pyAttributeToEvent.keys()) { | ||
createElementsWithEventListeners(interpreter, pyAttribute); | ||
await createElementsWithEventListeners(interpreter, pyAttribute); | ||
} | ||
} | ||
|
||
/** Initializes an element with the given py-on* attribute and its handler */ | ||
function createElementsWithEventListeners(interpreter: InterpreterClient, pyAttribute: string) { | ||
async function createElementsWithEventListeners(interpreter: InterpreterClient, pyAttribute: string) { | ||
const matches: NodeListOf<HTMLElement> = document.querySelectorAll(`[${pyAttribute}]`); | ||
for (const el of matches) { | ||
// If the element doesn't have an id, let's add one automatically! | ||
|
@@ -195,7 +198,7 @@ function createElementsWithEventListeners(interpreter: InterpreterClient, pyAttr | |
// the source code may contain a syntax error, which will cause | ||
// the splashscreen to not be removed. | ||
try { | ||
interpreter.run(source); | ||
await interpreter.run(source); | ||
} catch (e) { | ||
logger.error((e as Error).message); | ||
} | ||
|
@@ -204,7 +207,8 @@ function createElementsWithEventListeners(interpreter: InterpreterClient, pyAttr | |
void (async () => { | ||
try { | ||
await interpreter.run(handlerCode); | ||
} catch (err) { | ||
} catch (e) { | ||
const err = e as Error; | ||
displayPyException(err, el.parentElement); | ||
} | ||
})(); | ||
|
Uh oh!
There was an error while loading. Please reload this page.