Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tutormfe/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
MFE_APPS: Filter[dict[str, MFE_ATTRS_TYPE], []] = Filter()

PLUGIN_SLOTS: Filter[list[tuple[str, str, str]], []] = Filter()

INJECTED_SCRIPTS: Filter[list[tuple[str, str, str]], []] = Filter()
19 changes: 18 additions & 1 deletion tutormfe/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from tutor.types import Config, get_typed

from .__about__ import __version__
from .hooks import MFE_APPS, MFE_ATTRS_TYPE, PLUGIN_SLOTS
from .hooks import MFE_APPS, MFE_ATTRS_TYPE, PLUGIN_SLOTS, INJECTED_SCRIPTS

# Handle version suffix in main mode, just like tutor core
if __version_suffix__:
Expand Down Expand Up @@ -138,6 +138,22 @@ def iter_plugin_slots(mfe_name: str) -> t.Iterable[tuple[str, str]]:
"""
yield from get_plugin_slots(mfe_name)

@tutor_hooks.lru_cache
def get_injected_scripts(mfe_name: str) -> list[tuple[str, str]]:
"""
This function is cached for performance.
"""
return [i[-2:] for i in INJECTED_SCRIPTS.iterate() if i[0] == mfe_name]


def iter_injected_scripts(mfe_name: str) -> t.Iterable[tuple[str, str]]:
"""
Yield:

(script_name, script_content)
"""
yield from get_injected_scripts(mfe_name)


def is_mfe_enabled(mfe_name: str) -> bool:
return mfe_name in get_mfes()
Expand All @@ -155,6 +171,7 @@ def get_mfe(mfe_name: str) -> t.Union[MFE_ATTRS_TYPE, t.Any]:
("iter_plugin_slots", iter_plugin_slots),
("is_mfe_enabled", is_mfe_enabled),
("MFEMountData", MFEMountData),
("iter_injected_scripts", iter_injected_scripts),
]
)

Expand Down
16 changes: 15 additions & 1 deletion tutormfe/templates/mfe/build/mfe/env.config.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,23 @@ function addPlugins(config, slot_name, plugins) {

async function setConfig () {
let config = {
pluginSlots: {}
pluginSlots: {},
injectedScripts: []
};

// Inject scripts here since FPF is not guaranteed to be present in all MFEs
{%- for script_name, script_content in iter_injected_scripts("all") %}
config.injectedScripts.push({{ script_content }});
{%- endfor %}

{%- for app_name, _ in iter_mfes() %}
if (process.env.APP_ID == '{{ app_name }}') {
{%- for script_name, script_content in iter_injected_scripts(app_name) %}
config.injectedScripts.push({{ script_content }});
{%- endfor %}
}
{%- endfor %}

try {
/* We can't assume FPF exists, as it's not declared as a dependency in all
* MFEs, so we import it dynamically. In addition, for dynamic imports to
Expand Down
Loading