* @typedef {Object} ScriptInfo * @property {string} fqURL The original fully qualified URL * @property {ScriptInfo[]} deps Array of other ScriptInfos this is script dependant on * @property {boolean} isWorker True if this script came from `new Worker('someurl')` vs `import` or `importScripts` *
( text, baseUrl, scriptInfos = {} )
| 249 | */ |
| 250 | |
| 251 | async function getWorkerScripts( text, baseUrl, scriptInfos = {} ) { |
| 252 | |
| 253 | const parentScriptInfo = scriptInfos[ baseUrl ]; |
| 254 | const workerRE = /(new\s+Worker\s*\(\s*)('|")(.*?)('|")/g; |
| 255 | const importScriptsRE = /(importScripts\s*\(\s*)('|")(.*?)('|")/g; |
| 256 | const importRE = /(import.*?)(?!'three')('|")(.*?)('|")/g; |
| 257 | |
| 258 | const newScripts = []; |
| 259 | const slashRE = /\/manual\/examples\/[^/]+$/; |
| 260 | |
| 261 | function replaceWithUUID( match, prefix, quote, url ) { |
| 262 | |
| 263 | const fqURL = getFQUrl( url, baseUrl ); |
| 264 | if ( ! slashRE.test( fqURL ) ) { |
| 265 | |
| 266 | return match.toString(); |
| 267 | |
| 268 | } |
| 269 | |
| 270 | if ( ! scriptInfos[ url ] ) { |
| 271 | |
| 272 | scriptInfos[ fqURL ] = { |
| 273 | fqURL, |
| 274 | deps: [], |
| 275 | isWorker: prefix.indexOf( 'Worker' ) >= 0, |
| 276 | }; |
| 277 | newScripts.push( fqURL ); |
| 278 | |
| 279 | } |
| 280 | |
| 281 | parentScriptInfo.deps.push( scriptInfos[ fqURL ] ); |
| 282 | |
| 283 | return `${prefix}${quote}${fqURL}${quote}`; |
| 284 | |
| 285 | } |
| 286 | |
| 287 | function replaceWithUUIDModule( match, prefix, quote, url ) { |
| 288 | |
| 289 | // modules are either relative, fully qualified, or a module name |
| 290 | // Skip it if it's a module name |
| 291 | return ( url.startsWith( '.' ) || url.includes( '://' ) ) |
| 292 | ? replaceWithUUID( match, prefix, quote, url ) |
| 293 | : match.toString(); |
| 294 | |
| 295 | } |
| 296 | |
| 297 | text = text.replace( workerRE, replaceWithUUID ); |
| 298 | text = text.replace( importScriptsRE, replaceWithUUID ); |
| 299 | text = text.replace( importRE, replaceWithUUIDModule ); |
| 300 | |
| 301 | await Promise.all( newScripts.map( ( url ) => { |
| 302 | |
| 303 | return getScript( url, scriptInfos ); |
| 304 | |
| 305 | } ) ); |
| 306 | |
| 307 | return { text, scriptInfos }; |
| 308 |