diff --git a/cms/djangoapps/contentstore/views/preview.py b/cms/djangoapps/contentstore/views/preview.py index 7848c3d0d10e95cb6ae9d262661004e2491bd3cd..17b8dcf3987ed0c547b18c0dae72e5cdd9455e61 100644 --- a/cms/djangoapps/contentstore/views/preview.py +++ b/cms/djangoapps/contentstore/views/preview.py @@ -40,6 +40,7 @@ from xmodule.modulestore.django import ModuleI18nService, modulestore from xmodule.partitions.partitions_service import PartitionService from xmodule.services import SettingsService from xmodule.studio_editable import has_author_view +from xmodule.util.xmodule_django import add_webpack_to_fragment from xmodule.x_module import AUTHOR_VIEW, PREVIEW_VIEWS, STUDENT_VIEW, ModuleSystem, XModule, XModuleDescriptor import webpack_loader.utils @@ -304,11 +305,9 @@ def _studio_wrap_xblock(xblock, view, frag, context, display_name_only=False): if isinstance(xblock, (XModule, XModuleDescriptor)): # Add the webpackified asset tags class_name = getattr(xblock.__class__, 'unmixed_class', xblock.__class__).__name__ - for tag in webpack_loader.utils.get_as_tags(class_name): - frag.add_resource(tag, mimetype='text/html', placement='head') + add_webpack_to_fragment(frag, class_name) - for tag in webpack_loader.utils.get_as_tags("js/factories/xblock_validation"): - frag.add_resource(tag, mimetype='text/html', placement='head') + add_webpack_to_fragment(frag, "js/factories/xblock_validation") html = render_to_string('studio_xblock_wrapper.html', template_context) frag = wrap_fragment(frag, html) diff --git a/common/lib/xmodule/xmodule/util/xmodule_django.py b/common/lib/xmodule/xmodule/util/xmodule_django.py index 0e40c497da38b4d54340225fc979773359c9a0fe..ed4c8bd76bb89a94dbf63a7f77f17c0184347144 100644 --- a/common/lib/xmodule/xmodule/util/xmodule_django.py +++ b/common/lib/xmodule/xmodule/util/xmodule_django.py @@ -18,3 +18,14 @@ def get_current_request_hostname(): hostname = request.META.get('HTTP_HOST') return hostname + + +def add_webpack_to_fragment(fragment, bundle_name, extension=None, config='DEFAULT'): + """ + Add all webpack chunks to the supplied fragment as the appropriate resource type. + """ + for chunk in webpack_loader.utils.get_files(bundle_name, extension, config): + if chunk['name'].endswith(('.js', '.js.gz')): + fragment.add_javascript_url(chunk['url']) + elif chunk['name'].endswith(('.css', '.css.gz')): + fragment.add_css_url(chunk['url']) diff --git a/common/lib/xmodule/xmodule/vertical_block.py b/common/lib/xmodule/xmodule/vertical_block.py index f47fcfbf4198d55cc8ac2915edc25926736357d6..1cc44d69de4aeb9c3ec406ae801a327d91114c3f 100644 --- a/common/lib/xmodule/xmodule/vertical_block.py +++ b/common/lib/xmodule/xmodule/vertical_block.py @@ -16,6 +16,7 @@ from xmodule.mako_module import MakoTemplateBlockBase from xmodule.progress import Progress from xmodule.seq_module import SequenceFields from xmodule.studio_editable import StudioEditableBlock +from xmodule.util.xmodule_django import add_webpack_to_fragment from xmodule.x_module import STUDENT_VIEW, XModuleFields from xmodule.xml_module import XmlParserMixin @@ -98,8 +99,7 @@ class VerticalBlock(SequenceFields, XModuleFields, StudioEditableBlock, XmlParse 'bookmark_id': u"{},{}".format(child_context['username'], unicode(self.location)), # pylint: disable=no-member })) - for tag in webpack_loader.utils.get_as_tags('VerticalStudentView'): - fragment.add_resource(tag, mimetype='text/html', placement='head') + add_webpack_to_fragment(fragment, 'VerticalStudentView') fragment.initialize_js('VerticalStudentView') return fragment diff --git a/common/lib/xmodule/xmodule/x_module.py b/common/lib/xmodule/xmodule/x_module.py index 7c4d7dc29b86bb2695d06048b10257d12a653a97..88848c17a535333d489748fb6408ce62eb6bed0f 100644 --- a/common/lib/xmodule/xmodule/x_module.py +++ b/common/lib/xmodule/xmodule/x_module.py @@ -32,6 +32,7 @@ from xmodule import block_metadata_utils from xmodule.fields import RelativeTime from xmodule.errortracker import exc_info_to_str from xmodule.modulestore.exceptions import ItemNotFoundError +from xmodule.util.xmodule_django import add_webpack_to_fragment from opaque_keys.edx.keys import UsageKey from opaque_keys.edx.asides import AsideUsageKeyV2, AsideDefinitionKeyV2 @@ -260,8 +261,7 @@ def shim_xmodule_js(block, fragment): fragment.initialize_js('XBlockToXModuleShim') fragment.json_init_args = {'xmodule-type': block.js_module_name} - for tag in webpack_loader.utils.get_as_tags('XModuleShim'): - fragment.add_resource(tag, mimetype='text/html', placement='head') + add_webpack_to_fragment(fragment, 'XModuleShim') class XModuleFields(object): diff --git a/openedx/core/lib/xblock_utils/__init__.py b/openedx/core/lib/xblock_utils/__init__.py index 3c6f968c25c3c852444822c2b826755cf9f8e756..85955e139e7621d43ba42f71858efef9e201d2f4 100644 --- a/openedx/core/lib/xblock_utils/__init__.py +++ b/openedx/core/lib/xblock_utils/__init__.py @@ -26,6 +26,7 @@ from xblock.exceptions import InvalidScopeError from xblock.scorable import ScorableXBlockMixin from xmodule.seq_module import SequenceModule +from xmodule.util.xmodule_django import add_webpack_to_fragment from xmodule.vertical_block import VerticalBlock from xmodule.x_module import shim_xmodule_js, XModuleDescriptor, XModule, PREVIEW_VIEWS, STUDIO_VIEW @@ -149,8 +150,7 @@ def wrap_xblock( if isinstance(block, (XModule, XModuleDescriptor)): # Add the webpackified asset tags - for tag in webpack_loader.utils.get_as_tags(class_name): - frag.add_resource(tag, mimetype='text/html', placement='head') + add_webpack_to_fragment(frag, class_name) return wrap_fragment(frag, render_to_string('xblock_wrapper.html', template_context))