5
5
*/
6
6
import { $ } from "basic-devtools" ;
7
7
8
+ import TYPES from "./types.js" ;
8
9
import allPlugins from "./plugins.js" ;
9
10
import { robustFetch as fetch , getText } from "./fetch.js" ;
10
11
import { ErrorCode } from "./exceptions.js" ;
@@ -19,9 +20,10 @@ const badURL = (url, expected = "") => {
19
20
* Given a string, returns its trimmed content as text,
20
21
* fetching it from a file if the content is a URL.
21
22
* @param {string } config either JSON, TOML, or a file to fetch
23
+ * @param {string? } type the optional type to enforce
22
24
* @returns {{json: boolean, toml: boolean, text: string} }
23
25
*/
24
- const configDetails = async ( config ) => {
26
+ const configDetails = async ( config , type ) => {
25
27
let text = config ?. trim ( ) ;
26
28
// we only support an object as root config
27
29
let url = "" ,
@@ -45,74 +47,81 @@ const syntaxError = (type, url, { message }) => {
45
47
return new SyntaxError ( `${ str } \n${ message } ` ) ;
46
48
} ;
47
49
48
- // find the shared config for all py-script elements
49
- let config , type ;
50
+ const configs = new Map ( ) ;
50
51
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 ;
57
55
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 ;
71
58
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
+ }
92
99
}
100
+ } catch ( e ) {
101
+ error = e ;
93
102
}
94
- } catch ( e ) {
95
- error = e ;
96
103
}
97
- }
98
104
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 ( ) ) ;
109
118
}
110
- } else if ( ! parsed ?. plugins ?. includes ( `!${ key } ` ) ) {
111
- toBeAwaited . push ( value ( ) ) ;
112
119
}
113
- }
114
120
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
+ }
117
126
118
- export { parsed as config , plugins , error } ;
127
+ export default configs ;
0 commit comments