8000 Improve offline dist content (#1836) · shivashankarv/pyscript@c8ec29a · GitHub
[go: up one dir, main page]

Skip to content

Commit c8ec29a

Browse files
Improve offline dist content (pyscript#1836)
1 parent e81830a commit c8ec29a

18 files changed

+385
-98
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,5 @@ pyscript.core/dist
147147
pyscript.core/dist.zip
148148
pyscript.core/src/plugins.js
149149
pyscript.core/src/stdlib/pyscript.js
150+
pyscript.core/src/3rd-party/*
151+
!pyscript.core/src/3rd-party/READMEmd

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ repos:
4242
rev: "v3.0.0-alpha.6"
4343
hooks:
4444
- id: prettier
45-
exclude: pyscript\.core/test|pyscript\.core/dist|pyscript\.core/types|pyscript.core/src/stdlib/pyscript.js|pyscript\.sw/|pyscript.core/src/toml\.js
45+
exclude: pyscript\.core/test|pyscript\.core/dist|pyscript\.core/types|pyscript.core/src/stdlib/pyscript.js|pyscript\.sw/|pyscript.core/src/3rd-party
4646
args: [--tab-width, "4"]
4747

4848
- repo: https://github.com/pycqa/isort

pyscript.core/.eslintrc.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = {
1919
ecmaVersion: "latest",
2020
sourceType: "module",
2121
},
22-
ignorePatterns: ["toml.js"],
22+
ignorePatterns: ["3rd-party"],
2323
rules: {
2424
"no-implicit-globals": ["error"],
2525
},

pyscript.core/package-lock.json

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

pyscript.core/package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
},
2121
"scripts": {
2222
"server": "npx static-handler --coi .",
23-
"build": "npm run build:toml && npm run build:stdlib && npm run build:plugins && npm run build:core && eslint src/ && npm run ts && npm run test:mpy",
24-
"build:core": "rm -rf dist && rollup --config rollup/core.config.js",
23+
"build": "npm run build:3rd-party && npm run build:stdlib && npm run build:plugins && npm run build:core && eslint src/ && npm run ts && npm run test:mpy",
24+
"build:core": "rm -rf dist && rollup --config rollup/core.config.js && cp src/3rd-party/*.css dist/",
2525
"build:plugins": "node rollup/plugins.cjs",
2626
"build:stdlib": "node rollup/stdlib.cjs",
27-
"build:toml": "node rollup/toml.cjs",
27+
"bui 67E6 ld:3rd-party": "node rollup/3rd-party.cjs",
2828
"test:mpy": "static-handler --coi . 2>/dev/null & SH_PID=$!; EXIT_CODE=0; playwright test --fully-parallel test/ || EXIT_CODE=$?; kill $SH_PID 2>/dev/null; exit $EXIT_CODE",
2929
"dev": "node dev.cjs",
3030
"release": "npm run build && npm run zip",
@@ -48,6 +48,7 @@
4848
},
4949
"devDependencies": {
5050
"@playwright/test": "^1.39.0",
51+
"@rollup/plugin-commonjs": "^25.0.7",
5152
"@rollup/plugin-node-resolve": "^15.2.3",
5253
"@rollup/plugin-terser": "^0.4.4",
5354
"@webreflection/toml-j0.4": "^1.1.3",
@@ -57,7 +58,9 @@
5758
"rollup-plugin-postcss": "^4.0.2",
5859
"rollup-plugin-string": "^3.0.0",
5960
"static-handler": "^0.4.3",
60-
"typescript": "^5.2.2"
61+
"typescript": "^5.2.2",
62+
"xterm": "^5.3.0",
63+
"xterm-readline": "^1.1.1"
6164
},
6265
"repository": {
6366
"type": "git",

pyscript.core/rollup/3rd-party.cjs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const { copyFileSync, writeFileSync } = require("node:fs");
2+
const { join } = require("node:path");
3+
4+
const CDN = "https://cdn.jsdelivr.net/npm";
5+
6+
const targets = join(__dirname, "..", "src", "3rd-party");
7+
const node_modules = join(__dirname, "..", "node_modules");
8+
9+
const { devDependencies } = require(join(__dirname, "..", "package.json"));
10+
11+
const v = (name) => devDependencies[name].replace(/[^\d.]/g, "");
12+
13+
// Fetch a module via jsdelivr CDN `/+esm` orchestration
14+
// then sanitize the resulting outcome to avoid importing
15+
// anything via `/npm/...` through Rollup
16+
const resolve = (name) => {
17+
const cdn = `${CDN}/${name}@${v(name)}/+esm`;
18+
console.debug("fetching", cdn);
19+
return fetch(cdn)
20+
.then((b) => b.text())
21+
.then((text) =>
22+
text.replace(
23+
/("|')\/npm\/(.+)?\+esm\1/g,
24+
// normalize `/npm/module@version/+esm` as
25+
// just `module` so that rollup can do the rest
26+
(_, quote, module) => {
27+
const i = module.lastIndexOf("@");
28+
return `${quote}${module.slice(0, i)}${quote}`;
29+
},
30+
),
31+
);
32+
};
33+
34+
// key/value pairs as:
35+
// "3rd-party/file-name.js"
36+
// string as content or
37+
// Promise<string> as resolved content
38+
const modules = {
39+
"toml.js": join(node_modules, "@webreflection", "toml-j0.4", "toml.js"),
40+
"xterm.js": resolve("xterm"),
41+
"xterm.css": fetch(`${CDN}/xterm@${v("xterm")}/css/xterm.min.css`).then(
42+
(b) => b.text(),
43+
),
44+
"xterm-readline.js": resolve("xterm-readline"),
45+
};
46+
47+
for (const [target, source] of Object.entries(modules)) {
48+
if (typeof source === "string") copyFileSync(source, join(targets, target));
49+
else {
50+
source.then((text) => writeFileSync(join(targets, target), text));
51+
}
52+
}

pyscript.core/rollup/core.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// the default exported as npm entry.
< 10000 /td>
33

44
import { nodeResolve } from "@rollup/plugin-node-resolve";
5+
import commonjs from "@rollup/plugin-commonjs";
56
import terser from "@rollup/plugin-terser";
67
import postcss from "rollup-plugin-postcss";
78

@@ -11,7 +12,9 @@ export default [
1112
{
1213
input: "./src/core.js",
1314
plugins: plugins.concat(
14-
process.env.NO_MIN ? [nodeResolve()] : [nodeResolve(), terser()],
15+
process.env.NO_MIN
16+
? [nodeResolve(), commonjs()]
17+
: [nodeResolve(), commonjs(), terser()],
1518
),
1619
output: {
1720
esModule: true,

pyscript.core/rollup/toml.cjs

Lines changed: 0 additions & 14 deletions
This file was deleted.

pyscript.core/src/3rd-party/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# PyScript 3rd Party
2+
3+
This folder contains artifacts created via [3rd-party.cjs](../../rollup/3rd-party.cjs).
4+
5+
As we would like to offer a way to run PyScript offline, and we already offer a `dist` folder with all the necessary scripts, we have created a foreign dependencies resolver that allow to lazy-load CDN dependencies out of the box.
6+
7+
Please **note** these dependencies are **not interpreters**, because interpreters have their own mechanism, folders structure, WASM files, and whatnot, to work locally, but at least XTerm or the TOML parser, among other lazy dependencies, should be available within the dist folder.

pyscript.core/src/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ for (const [TYPE] of TYPES) {
8989
} else if (toml || type === "toml") {
9090
try {
9191
const { parse } = await import(
92-
/* webpackIgnore: true */ "./toml.js"
92+
/* webpackIgnore: true */ "./3rd-party/toml.js"
9393
);
9494
parsed = parse(text);
9595
} catch (e) {

0 commit comments

Comments
 (0)
0