From fc21ef5c62d845576a916414468b3a57370a57b2 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 4 Jun 2021 14:39:47 +0200 Subject: Speed up plugins loading --- client/src/root-helpers/plugins.ts | 5 +++-- client/src/root-helpers/utils.ts | 38 -------------------------------------- 2 files changed, 3 insertions(+), 40 deletions(-) (limited to 'client/src/root-helpers') 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 { RegisterClientVideoFieldOptions, ServerConfigPlugin } from '../../../shared/models' +import { environment } from '../environments/environment' import { ClientScript as ClientScriptModule } from '../types/client-script.model' -import { importModule } from './utils' interface HookStructValue extends RegisterClientHookOptions { plugin: ServerConfigPlugin @@ -101,7 +101,8 @@ function loadPlugin (options: { console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name) - return importModule(clientScript.script) + const absURL = (environment.apiUrl || window.location.origin) + clientScript.script + return import(/* webpackIgnore: true */ absURL) .then((script: ClientScriptModule) => script.register({ registerHook, registerVideoField, registerSettingsScript, peertubeHelpers })) .then(() => sortHooksByPriority(hooks)) .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 @@ -import { environment } from '../environments/environment' - function objectToUrlEncoded (obj: any) { const str: string[] = [] for (const key of Object.keys(obj)) { @@ -21,41 +19,6 @@ function copyToClipboard (text: string) { document.body.removeChild(el) } -// 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) - }) -} - function wait (ms: number) { return new Promise(res => { setTimeout(() => res(), ms) @@ -64,7 +27,6 @@ function wait (ms: number) { export { copyToClipboard, - importModule, objectToUrlEncoded, wait } -- cgit v1.2.3