1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-24 23:26:50 +02:00

Generator: Resolves #4229: Add support for compiling content script

This commit is contained in:
Laurent Cozic
2021-01-03 13:21:48 +00:00
parent 8492a7ee26
commit 3e948146cd
76 changed files with 2893 additions and 259 deletions

View File

@ -2,6 +2,9 @@ import { PluginStates } from '../reducer';
import { ContentScriptType } from '../api/types';
import { dirname } from '@joplin/renderer/pathUtils';
import shim from '../../../shim';
import Logger from '../../../Logger';
const logger = Logger.create('loadContentScripts');
export interface ExtraContentScript {
id: string;
@ -28,17 +31,24 @@ function loadContentScripts(plugins: PluginStates, scriptType: ContentScriptType
if (!contentScripts) continue;
for (const contentScript of contentScripts) {
const module = shim.requireDynamic(contentScript.path);
if (!module.default || typeof module.default !== 'function') throw new Error(`Content script must export a function under the "default" key: Plugin: ${pluginId}: Script: ${contentScript.id}`);
try {
const module = shim.requireDynamic(contentScript.path);
if (!module.default || typeof module.default !== 'function') throw new Error(`Content script must export a function under the "default" key: Plugin: ${pluginId}: Script: ${contentScript.id}`);
const loadedModule = module.default({});
if (!loadedModule.plugin && !loadedModule.codeMirrorResources && !loadedModule.codeMirrorOptions) throw new Error(`Content script must export a "plugin" key or a list of CodeMirror assets or define a CodeMirror option: Plugin: ${pluginId}: Script: ${contentScript.id}`);
const loadedModule = module.default({});
if (!loadedModule.plugin && !loadedModule.codeMirrorResources && !loadedModule.codeMirrorOptions) throw new Error(`Content script must export a "plugin" key or a list of CodeMirror assets or define a CodeMirror option: Plugin: ${pluginId}: Script: ${contentScript.id}`);
output.push({
id: contentScript.id,
module: loadedModule,
assetPath: dirname(contentScript.path),
});
output.push({
id: contentScript.id,
module: loadedModule,
assetPath: dirname(contentScript.path),
});
} catch (error) {
// This function must not throw as doing so would crash the
// application, which we want to avoid for plugins. Instead log
// the error, and continue loading the other content scripts.
logger.error(error.message);
}
}
}