]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Fix bad import on FF ESR
authorChocobozzz <me@florianbigard.com>
Tue, 20 Aug 2019 07:01:19 +0000 (09:01 +0200)
committerChocobozzz <me@florianbigard.com>
Tue, 20 Aug 2019 07:01:19 +0000 (09:01 +0200)
client/src/app/core/plugins/plugin.service.ts
client/src/app/shared/misc/utils.ts

index 3bb82e8a987f8b32d008a8465d7cc37aad6718d4..3af36765af795ed8de83fbac93b4ccd25add3242 100644 (file)
@@ -18,6 +18,7 @@ import { PublicServerSetting } from '@shared/models/plugins/public-server.settin
 import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
 import { RegisterClientHelpers } from '../../../types/register-client-option.model'
 import { PluginTranslation } from '@shared/models/plugins/plugin-translation.model'
+import { importModule } from '@app/shared/misc/utils'
 
 interface HookStructValue extends RegisterClientHookOptions {
   plugin: ServerConfigPlugin
@@ -222,7 +223,7 @@ export class PluginService implements ClientHook {
     console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
 
     return this.zone.runOutsideAngular(() => {
-      return import(/* webpackIgnore: true */ clientScript.script)
+      return importModule(clientScript.script)
         .then((script: ClientScriptModule) => script.register({ registerHook, peertubeHelpers }))
         .then(() => this.sortHooksByPriority())
         .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err))
index 85fc1c3a0987af8f03af863be655be1480790791..f26240d216eee3461aab8d85eb20f12326d386b0 100644 (file)
@@ -134,6 +134,41 @@ function scrollToTop () {
   window.scroll(0, 0)
 }
 
+// Thanks: https://github.com/uupaa/dynamic-import-polyfill
+function importModule (path: string) {
+  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: ${path}`))
+      destructor()
+    }
+    script.onload = () => {
+      resolve(window[ vector ])
+      destructor()
+    }
+    const absURL = (environment.apiUrl || window.location.origin) + path
+    const loader = `import * as m from "${absURL}"; window.${vector} = m;` // export Module
+    const blob = new Blob([ loader ], { type: 'text/javascript' })
+    script.src = URL.createObjectURL(blob)
+
+    document.head.appendChild(script)
+  })
+}
+
 export {
   sortBy,
   durationToString,
@@ -147,5 +182,6 @@ export {
   objectToFormData,
   objectLineFeedToHtml,
   removeElementFromArray,
+  importModule,
   scrollToTop
 }