@@ -8,11 +8,18 @@ export const main = async (
88 pkg : { name : string ; version : string } ,
99 options ?: Parameters < typeof DefaultRubyVM > [ 1 ] ,
1010) => {
11+ const scriptEnv = deriveEnv ( document . currentScript ) ;
1112 const response = fetch (
1213 `https://cdn.jsdelivr.net/npm/${ pkg . name } @${ pkg . version } /dist/ruby+stdlib.wasm` ,
1314 ) ;
1415 const module = await compileWebAssemblyModule ( response ) ;
15- const { vm } = await DefaultRubyVM ( module , options ) ;
16+ const { vm } = await DefaultRubyVM ( module , {
17+ ...options ,
18+ env : {
19+ ...scriptEnv ,
20+ ...options ?. env ,
21+ } ,
22+ } ) ;
1623 await mainWithRubyVM ( vm ) ;
1724} ;
1825
@@ -90,6 +97,34 @@ const deriveEvalStyle = (tag: Element): "async" | "sync" => {
9097 return rawEvalStyle ;
9198} ;
9299
100+ const deriveEnv = ( tag : Element | null ) : Record < string , string > => {
101+ const rawEnv = tag ?. getAttribute ( "data-env" ) ;
102+ if ( ! rawEnv ) {
103+ return { } ;
104+ }
105+
106+ const trimmedEnv = rawEnv . trim ( ) ;
107+ if ( ! trimmedEnv ) {
108+ return { } ;
109+ }
110+
111+ return trimmedEnv
112+ . split ( / \s + / )
113+ . reduce < Record < string , string > > ( ( env , entry ) => {
114+ const delimiterIndex = entry . indexOf ( "=" ) ;
115+ if ( delimiterIndex <= 0 ) {
116+ console . warn (
117+ `data-env entry must be in the KEY=value format. ${ entry } is ignored.` ,
118+ ) ;
119+ return env ;
120+ }
121+
122+ // Only the first "=" separates key and value so values can contain "=".
123+ env [ entry . slice ( 0 , delimiterIndex ) ] = entry . slice ( delimiterIndex + 1 ) ;
124+ return env ;
125+ } , { } ) ;
126+ } ;
127+
93128const loadScriptAsync = async (
94129 tag : Element ,
95130) : Promise < { scriptContent : string ; evalStyle : "async" | "sync" } | null > => {
0 commit comments