aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/root-helpers
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-04 14:39:47 +0200
committerChocobozzz <me@florianbigard.com>2021-06-04 15:45:44 +0200
commitfc21ef5c62d845576a916414468b3a57370a57b2 (patch)
tree818102bc909210870f9a28692361f7b5cbe219de /client/src/root-helpers
parent2989628b7913383b39ac34c7db8666a21f8e5037 (diff)
downloadPeerTube-fc21ef5c62d845576a916414468b3a57370a57b2.tar.gz
PeerTube-fc21ef5c62d845576a916414468b3a57370a57b2.tar.zst
PeerTube-fc21ef5c62d845576a916414468b3a57370a57b2.zip
Speed up plugins loading
Diffstat (limited to 'client/src/root-helpers')
-rw-r--r--client/src/root-helpers/plugins.ts5
-rw-r--r--client/src/root-helpers/utils.ts38
2 files changed, 3 insertions, 40 deletions
diff --git a/client/src/root-helpers/plugins.ts b/client/src/root-helpers/plugins.ts
index 8c1c858b7..10c111a8c 100644
--- a/client/src/root-helpers/plugins.ts
+++ b/client/src/root-helpers/plugins.ts
@@ -11,8 +11,8 @@ import {
11 RegisterClientVideoFieldOptions, 11 RegisterClientVideoFieldOptions,
12 ServerConfigPlugin 12 ServerConfigPlugin
13} from '../../../shared/models' 13} from '../../../shared/models'
14import { environment } from '../environments/environment'
14import { ClientScript as ClientScriptModule } from '../types/client-script.model' 15import { ClientScript as ClientScriptModule } from '../types/client-script.model'
15import { importModule } from './utils'
16 16
17interface HookStructValue extends RegisterClientHookOptions { 17interface HookStructValue extends RegisterClientHookOptions {
18 plugin: ServerConfigPlugin 18 plugin: ServerConfigPlugin
@@ -101,7 +101,8 @@ function loadPlugin (options: {
101 101
102 console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name) 102 console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
103 103
104 return importModule(clientScript.script) 104 const absURL = (environment.apiUrl || window.location.origin) + clientScript.script
105 return import(/* webpackIgnore: true */ absURL)
105 .then((script: ClientScriptModule) => script.register({ registerHook, registerVideoField, registerSettingsScript, peertubeHelpers })) 106 .then((script: ClientScriptModule) => script.register({ registerHook, registerVideoField, registerSettingsScript, peertubeHelpers }))
106 .then(() => sortHooksByPriority(hooks)) 107 .then(() => sortHooksByPriority(hooks))
107 .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err)) 108 .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err))
diff --git a/client/src/root-helpers/utils.ts b/client/src/root-helpers/utils.ts
index 06591b95e..00bd92411 100644
--- a/client/src/root-helpers/utils.ts
+++ b/client/src/root-helpers/utils.ts
@@ -1,5 +1,3 @@
1import { environment } from '../environments/environment'
2
3function objectToUrlEncoded (obj: any) { 1function objectToUrlEncoded (obj: any) {
4 const str: string[] = [] 2 const str: string[] = []
5 for (const key of Object.keys(obj)) { 3 for (const key of Object.keys(obj)) {
@@ -21,41 +19,6 @@ function copyToClipboard (text: string) {
21 document.body.removeChild(el) 19 document.body.removeChild(el)
22} 20}
23 21
24// Thanks: https://github.com/uupaa/dynamic-import-polyfill
25function importModule (path: string) {
26 return new Promise((resolve, reject) => {
27 const vector = '$importModule$' + Math.random().toString(32).slice(2)
28 const script = document.createElement('script')
29
30 const destructor = () => {
31 delete window[ vector ]
32 script.onerror = null
33 script.onload = null
34 script.remove()
35 URL.revokeObjectURL(script.src)
36 script.src = ''
37 }
38
39 script.defer = true
40 script.type = 'module'
41
42 script.onerror = () => {
43 reject(new Error(`Failed to import: ${path}`))
44 destructor()
45 }
46 script.onload = () => {
47 resolve(window[ vector ])
48 destructor()
49 }
50 const absURL = (environment.apiUrl || window.location.origin) + path
51 const loader = `import * as m from "${absURL}"; window.${vector} = m;` // export Module
52 const blob = new Blob([ loader ], { type: 'text/javascript' })
53 script.src = URL.createObjectURL(blob)
54
55 document.head.appendChild(script)
56 })
57}
58
59function wait (ms: number) { 22function wait (ms: number) {
60 return new Promise<void>(res => { 23 return new Promise<void>(res => {
61 setTimeout(() => res(), ms) 24 setTimeout(() => res(), ms)
@@ -64,7 +27,6 @@ function wait (ms: number) {
64 27
65export { 28export {
66 copyToClipboard, 29 copyToClipboard,
67 importModule,
68 objectToUrlEncoded, 30 objectToUrlEncoded,
69 wait 31 wait
70} 32}