8000 [RC] Work on the reverted changes (#1753) · pyscript/pyscript@e4eedd8 · GitHub
[go: up one dir, main page]

Skip to content

Commit e4eedd8

Browse files
[RC] Work on the reverted changes (#1753)
1 parent c9e7fe1 commit e4eedd8

File tree

14 files changed

+182
-118
lines changed

14 files changed

+182
-118
lines changed

pyscript.core/.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.pytest_cache/
12
node_modules/
23
rollup/
34
test/

pyscript.core/package-lock.json

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

pyscript.core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pyscript/core",
3-
"version": "0.2.0",
3+
"version": "0.2.2",
44
"type": "module",
55
"description": "PyScript",
66
"module": "./index.js",
@@ -38,7 +38,7 @@
3838
"devDependencies": {
3939
"@rollup/plugin-node-resolve": "^15.2.1",
4040
"@rollup/plugin-terser": "^0.4.3",
41-
"rollup": "^3.29.2",
41+
"rollup": "^3.29.3",
4242
"rollup-plugin-postcss": "^4.0.2",
4343
"rollup-plugin-string": "^3.0.0",
4444
"static-handler": "^0.4.2",

pyscript.core/src/config.js

Lines changed: 70 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
import { $ } from "basic-devtools";
77

8+
import TYPES from "./types.js";
89
import allPlugins from "./plugins.js";
910
import { robustFetch as fetch, getText } from "./fetch.js";
1011
import { ErrorCode } from "./exceptions.js";
@@ -19,9 +20,10 @@ const badURL = (url, expected = "") => {
1920
* Given a string, returns its trimmed content as text,
2021
* fetching it from a file if the content is a URL.
2122
* @param {string} config either JSON, TOML, or a file to fetch
23+
* @param {string?} type the optional type to enforce
2224
* @returns {{json: boolean, toml: boolean, text: string}}
2325
*/
24-
const configDetails = async (config) => {
26+
const configDetails = async (config, type) => {
2527
let text = config?.trim();
2628
// we only support an object as root config
2729
let url = "",
@@ -45,74 +47,81 @@ const syntaxError = (type, url, { message }) => {
4547
return new SyntaxError(`${str}\n${message}`);
4648
};
4749

48-
// find the shared config for all py-script elements
49-
let config, type;
50+
const configs = new Map();
5051

51-
/** @type {Promise<any> | undefined} A Promise wrapping any plugins which should be loaded. */
52-
let plugins;
53-
/** @type {any} The PyScript configuration parsed from the JSON or TOML object*. May be any of the return types of JSON.parse() or toml-j0.4's parse() ( {number | string | boolean | null | object | Array} ) */
54-
let parsed;
55-
/** @type {SyntaxError | undefined} The error thrown when parsing the PyScript config, if any.*/
56-
let error;
52+
for (const [TYPE] of TYPES) {
53+
/** @type {Promise<any> | undefined} A Promise wrapping any plugins which should be loaded. */
54+
let plugins;
5755

58-
let pyConfig = $("py-config");
59-
if (pyConfig) {
60-
config = pyConfig.getAttribute("src") || pyConfig.textContent;
61-
type = pyConfig.getAttribute("type");
62-
} else {
63-
pyConfig = $(
64-
[
65-
'script[type="py"][config]:not([worker])',
66-
"py-script[config]:not([worker])",
67-
].join(","),
68-
);
69-
if (pyConfig) config = pyConfig.getAttribute("config");
70-
}
56+
/** @type {any} The PyScript configuration parsed from the JSON or TOML object*. May be any of the return types of JSON.parse() or toml-j0.4's parse() ( {number | string | boolean | null | object | Array} ) */
57+
let parsed;
7158

72-
// catch possible fetch errors
73-
if (config) {
74-
try {
75-
const { json, toml, text, url } = await configDetails(config);
76-
config = text;
77-
if (json || type === "json") {
78-
try {
79-
parsed = JSON.parse(text);
80-
} catch (e) {
81-
error = syntaxError("JSON", url, e);
82-
}
83-
} else if (toml || type === "toml") {
84-
try {
85-
const { parse } = await import(
86-
/* webpackIgnore: true */
87-
"https://cdn.jsdelivr.net/npm/@webreflection/toml-j0.4/toml.js"
88-
);
89-
parsed = parse(text);
90-
} catch (e) {
91-
error = syntaxError("TOML", url, e);
59+
/** @type {SyntaxError | undefined} The error thrown when parsing the PyScript config, if any.*/
60+
let error;
61+
62+
let config,
63+
type,
64+
pyConfig = $(`${TYPE}-config`);
65+
if (pyConfig) {
66+
config = pyConfig.getAttribute("src") || pyConfig.textContent;
67+
type = pyConfig.getAttribute("type");
68+
} else {
69+
pyConfig = $(
70+
[
71+
`script[type="${TYPE}"][config]:not([worker])`,
72+
`${TYPE}-script[config]:not([worker])`,
73+
].join(","),
74+
);
75+
if (pyConfig) config = pyConfig.getAttribute("config");
76+
}
77+
78+
// catch possible fetch errors
79+
if (config) {
80+
try {
81+
const { json, toml, text, url } = await configDetails(config, type);
82+
config = text;
83+
if (json || type === "json") {
84+
try {
85+
parsed = JSON.parse(text);
86+
} catch (e) {
87+
error = syntaxError("JSON", url, e);
88+
}
89+
} else if (toml || type === "toml") {
90+
try {
91+
const { parse } = await import(
92+
/* webpackIgnore: true */
93+
"https://cdn.jsdelivr.net/npm/@webreflection/toml-j0.4/toml.js"
94+
);
95+
parsed = parse(text);
96+
} catch (e) {
97+
error = syntaxError("TOML", url, e);
98+
}
9299
}
100+
} catch (e) {
101+
error = e;
93102
}
94-
} catch (e) {
95-
error = e;
96103
}
97-
}
98104

99-
// parse all plugins and optionally ignore only
100-
// those flagged as "undesired" via `!` prefix
101-
const toBeAwaited = [];
102-
for (const [key, value] of Object.entries(allPlugins)) {
103-
if (error) {
104-
if (key === "error") {
105-
// show on page the config is broken, meaning that
106-
// it was not possible to disable error plugin neither
107-
// as that part wasn't correctly parsed anyway
108-
value().then(({ notify }) => notify(error.message));
105+
// parse all plugins and optionally ignore only
106+
// those flagged as "undesired" via `!` prefix
107+
const toBeAwaited = [];
108+
for (const [key, value] of Object.entries(allPlugins)) {
109+
if (error) {
110+
if (key === "error") {
111+
// show on page the config is broken, meaning that
112+
// it was not possible to disable error plugin neither
113+
// as that part wasn't correctly parsed anyway
114+
value().then(({ notify }) => notify(error.message));
115+
}
116+
} else if (!parsed?.plugins?.includes(`!${key}`)) {
117+
toBeAwaited.push(value());
109118
}
110-
} else if (!parsed?.plugins?.includes(`!${key}`)) {
111-
toBeAwaited.push(value());
112119
}
113-
}
114120

115-
// assign plugins as Promise.all only if needed
116-
if (toBeAwaited.length) plugins = Promise.all(toBeAwaited);
121+
// assign plugins as Promise.all only if needed
122+
if (toBeAwaited.length) plugins = Promise.all(toBeAwaited);
123+
124+
configs.set(TYPE, { config: parsed, plugins, error });
125+
}
117126

118-
export { parsed as config, plugins, error };
127+
export default configs;

0 commit comments

Comments
 (0)
0