Add import polyfill
This commit is contained in:
parent
893f0f2f0f
commit
2b36e477bf
|
@ -1,3 +1,4 @@
|
||||||
|
/* eslint-disable @typescript-eslint/no-implied-eval */
|
||||||
import * as debug from 'debug'
|
import * as debug from 'debug'
|
||||||
import { firstValueFrom, ReplaySubject } from 'rxjs'
|
import { firstValueFrom, ReplaySubject } from 'rxjs'
|
||||||
import { first, shareReplay } from 'rxjs/operators'
|
import { first, shareReplay } from 'rxjs/operators'
|
||||||
|
@ -227,7 +228,7 @@ class PluginsManager {
|
||||||
console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
|
console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
|
||||||
|
|
||||||
const absURL = (environment.apiUrl || window.location.origin) + clientScript.script
|
const absURL = (environment.apiUrl || window.location.origin) + clientScript.script
|
||||||
return import(/* webpackIgnore: true */ absURL)
|
return dynamicImport(absURL)
|
||||||
.then((script: ClientScriptModule) => script.register({ registerHook, registerVideoField, registerSettingsScript, peertubeHelpers }))
|
.then((script: ClientScriptModule) => script.register({ registerHook, registerVideoField, registerSettingsScript, peertubeHelpers }))
|
||||||
.then(() => this.sortHooksByPriority())
|
.then(() => this.sortHooksByPriority())
|
||||||
.catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err))
|
.catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err))
|
||||||
|
@ -250,3 +251,45 @@ export {
|
||||||
OnFormFields,
|
OnFormFields,
|
||||||
OnSettingsScripts
|
OnSettingsScripts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
async function dynamicImport (url: string) {
|
||||||
|
try {
|
||||||
|
// eslint-disable-next-line no-new-func
|
||||||
|
return new Function(`return import('${url}')`)()
|
||||||
|
} catch {
|
||||||
|
console.log('Fallback to import polyfill')
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const vector = '$importModule$' + Math.random().toString(32).slice(2)
|
||||||
|
const script = document.createElement('script')
|
||||||
|
|
||||||
|
const destructor = () => {
|
||||||
|
delete window[vector]
|
||||||
|
script.onerror = null
|
||||||
|
script.onload = null
|
||||||
|
script.remove()
|
||||||
|
URL.revokeObjectURL(script.src)
|
||||||
|
script.src = ''
|
||||||
|
}
|
||||||
|
|
||||||
|
script.defer = true
|
||||||
|
script.type = 'module'
|
||||||
|
|
||||||
|
script.onerror = () => {
|
||||||
|
reject(new Error(`Failed to import: ${url}`))
|
||||||
|
destructor()
|
||||||
|
}
|
||||||
|
script.onload = () => {
|
||||||
|
resolve(window[vector])
|
||||||
|
destructor()
|
||||||
|
}
|
||||||
|
const loader = `import * as m from "${url}"; window.${vector} = m;` // export Module
|
||||||
|
const blob = new Blob([ loader ], { type: 'text/javascript' })
|
||||||
|
script.src = URL.createObjectURL(blob)
|
||||||
|
|
||||||
|
document.head.appendChild(script)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue