Skip to content
Snippets Groups Projects
Commit 41c71505 authored by Calen Pennington's avatar Calen Pennington
Browse files

Load all webpack chunks into fragments as correctly-typed resources

parent e14f7003
No related merge requests found
......@@ -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)
......
......@@ -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'])
......@@ -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
......
......@@ -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):
......
......@@ -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))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment