From 41c71505e397f4937d8e999c23801137b1cbb004 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Thu, 6 Sep 2018 15:47:13 -0400 Subject: [PATCH 01/10] Load all webpack chunks into fragments as correctly-typed resources --- cms/djangoapps/contentstore/views/preview.py | 7 +++---- common/lib/xmodule/xmodule/util/xmodule_django.py | 11 +++++++++++ common/lib/xmodule/xmodule/vertical_block.py | 4 ++-- common/lib/xmodule/xmodule/x_module.py | 4 ++-- openedx/core/lib/xblock_utils/__init__.py | 4 ++-- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/cms/djangoapps/contentstore/views/preview.py b/cms/djangoapps/contentstore/views/preview.py index 7848c3d0d1..17b8dcf398 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 0e40c497da..ed4c8bd76b 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 f47fcfbf41..1cc44d69de 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 7c4d7dc29b..88848c17a5 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 3c6f968c25..85955e139e 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)) From e6f11072b0b7e0ea5c02b0e249d7e1a3fd81e307 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Fri, 7 Sep 2018 09:24:35 -0400 Subject: [PATCH 02/10] fixup! Load all webpack chunks into fragments as correctly-typed resources --- common/lib/xmodule/xmodule/util/xmodule_django.py | 1 + 1 file changed, 1 insertion(+) diff --git a/common/lib/xmodule/xmodule/util/xmodule_django.py b/common/lib/xmodule/xmodule/util/xmodule_django.py index ed4c8bd76b..44c9b449df 100644 --- a/common/lib/xmodule/xmodule/util/xmodule_django.py +++ b/common/lib/xmodule/xmodule/util/xmodule_django.py @@ -6,6 +6,7 @@ runtime environment with the djangoapps in common configured to load # NOTE: we are importing this method so that any module that imports us has access to get_current_request from crum import get_current_request +import webpack_loader def get_current_request_hostname(): From 15b762aaaa26c0457c0f0dc7477b59ebdc01556c Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Fri, 7 Sep 2018 10:40:09 -0400 Subject: [PATCH 03/10] fixup! Load all webpack chunks into fragments as correctly-typed resources --- cms/conftest.py | 2 +- common/lib/conftest.py | 2 +- conftest.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cms/conftest.py b/cms/conftest.py index fbce4a8e12..3a25a994f0 100644 --- a/cms/conftest.py +++ b/cms/conftest.py @@ -60,6 +60,6 @@ def no_webpack_loader(monkeypatch): lambda entry, extension=None, config='DEFAULT', attrs='': '' ) monkeypatch.setattr( - "webpack_loader.utils.get_as_tags", + "webpack_loader.utils.get_files", lambda entry, extension=None, config='DEFAULT', attrs='': [] ) diff --git a/common/lib/conftest.py b/common/lib/conftest.py index 2ccd86acf3..b9db58ddf5 100644 --- a/common/lib/conftest.py +++ b/common/lib/conftest.py @@ -14,6 +14,6 @@ def no_webpack_loader(monkeypatch): lambda entry, extension=None, config='DEFAULT', attrs='': '' ) monkeypatch.setattr( - "webpack_loader.utils.get_as_tags", + "webpack_loader.utils.get_files", lambda entry, extension=None, config='DEFAULT', attrs='': [] ) diff --git a/conftest.py b/conftest.py index 7621d7906e..4ced0047c0 100644 --- a/conftest.py +++ b/conftest.py @@ -17,6 +17,6 @@ def no_webpack_loader(monkeypatch): lambda entry, extension=None, config='DEFAULT', attrs='': '' ) monkeypatch.setattr( - "webpack_loader.utils.get_as_tags", + "webpack_loader.utils.get_files", lambda entry, extension=None, config='DEFAULT', attrs='': [] ) From 16f05ecce4adb01af18c245a465f99199998cc43 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Fri, 7 Sep 2018 11:02:52 -0400 Subject: [PATCH 04/10] fixup! Load all webpack chunks into fragments as correctly-typed resources --- cms/conftest.py | 4 ++++ common/lib/conftest.py | 4 ++++ conftest.py | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/cms/conftest.py b/cms/conftest.py index 3a25a994f0..9e1e46fc93 100644 --- a/cms/conftest.py +++ b/cms/conftest.py @@ -59,6 +59,10 @@ def no_webpack_loader(monkeypatch): "webpack_loader.templatetags.webpack_loader.render_bundle", lambda entry, extension=None, config='DEFAULT', attrs='': '' ) + monkeypatch.setattr( + "webpack_loader.utils.get_as_tags", + lambda entry, extension=None, config='DEFAULT', attrs='': [] + ) monkeypatch.setattr( "webpack_loader.utils.get_files", lambda entry, extension=None, config='DEFAULT', attrs='': [] diff --git a/common/lib/conftest.py b/common/lib/conftest.py index b9db58ddf5..180348f6f1 100644 --- a/common/lib/conftest.py +++ b/common/lib/conftest.py @@ -13,6 +13,10 @@ def no_webpack_loader(monkeypatch): "webpack_loader.templatetags.webpack_loader.render_bundle", lambda entry, extension=None, config='DEFAULT', attrs='': '' ) + monkeypatch.setattr( + "webpack_loader.utils.get_as_tags", + lambda entry, extension=None, config='DEFAULT', attrs='': [] + ) monkeypatch.setattr( "webpack_loader.utils.get_files", lambda entry, extension=None, config='DEFAULT', attrs='': [] diff --git a/conftest.py b/conftest.py index 4ced0047c0..ec683b154d 100644 --- a/conftest.py +++ b/conftest.py @@ -16,6 +16,10 @@ def no_webpack_loader(monkeypatch): "webpack_loader.templatetags.webpack_loader.render_bundle", lambda entry, extension=None, config='DEFAULT', attrs='': '' ) + monkeypatch.setattr( + "webpack_loader.utils.get_as_tags", + lambda entry, extension=None, config='DEFAULT', attrs='': [] + ) monkeypatch.setattr( "webpack_loader.utils.get_files", lambda entry, extension=None, config='DEFAULT', attrs='': [] From 493bbe93104480476b2ae5db2dfc9e5f976dbdc8 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Mon, 10 Sep 2018 12:43:04 -0400 Subject: [PATCH 05/10] DO NOT MERGE: debug logging to figure out why libraries won't load --- cms/static/js/views/paged_container.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cms/static/js/views/paged_container.js b/cms/static/js/views/paged_container.js index b162256e2b..f2312754e6 100644 --- a/cms/static/js/views/paged_container.js +++ b/cms/static/js/views/paged_container.js @@ -96,10 +96,15 @@ define(['jquery', 'underscore', 'common/js/components/utils/view_utils', 'js/vie data: this.getRenderParameters(options.page_number, options.force_render), headers: {Accept: 'application/json'}, success: function(fragment) { + console.log("Paged Container: Got Fragments"); self.handleXBlockFragment(fragment, options); - self.processPaging({requested_page: options.page_number}); + console.log("Paged Container: Handled Fragments"); + self.processPaging({ requested_page: options.page_number}); + console.log("Paged Container: Processed Paging"); self.page.updatePreviewButton(self.collection.showChildrenPreviews); + console.log("Paged Container: Preview button updated"); self.page.renderAddXBlockComponents(); + console.log("Paged Container: Add XBlock rendered"); if (options.force_render) { var $target = $('.studio-xblock-wrapper[data-locator="' + options.force_render + '"]'); // Scroll us to the element with a little buffer at the top for context. From b36aa292ddfa7b6d5fb4b5063f78fdce52b2018b Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Mon, 10 Sep 2018 14:10:29 -0400 Subject: [PATCH 06/10] fixup! Load all webpack chunks into fragments as correctly-typed resources --- cms/static/js/views/paged_container.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/cms/static/js/views/paged_container.js b/cms/static/js/views/paged_container.js index f2312754e6..716f0b53b9 100644 --- a/cms/static/js/views/paged_container.js +++ b/cms/static/js/views/paged_container.js @@ -96,20 +96,19 @@ define(['jquery', 'underscore', 'common/js/components/utils/view_utils', 'js/vie data: this.getRenderParameters(options.page_number, options.force_render), headers: {Accept: 'application/json'}, success: function(fragment) { - console.log("Paged Container: Got Fragments"); - self.handleXBlockFragment(fragment, options); - console.log("Paged Container: Handled Fragments"); - self.processPaging({ requested_page: options.page_number}); - console.log("Paged Container: Processed Paging"); - self.page.updatePreviewButton(self.collection.showChildrenPreviews); - console.log("Paged Container: Preview button updated"); - self.page.renderAddXBlockComponents(); - console.log("Paged Container: Add XBlock rendered"); - if (options.force_render) { - var $target = $('.studio-xblock-wrapper[data-locator="' + options.force_render + '"]'); - // Scroll us to the element with a little buffer at the top for context. - ViewUtils.setScrollOffset($target, ($(window).height() * 0.10)); + var originalDone = options.done; + options.done = function() { + self.processPaging({ requested_page: options.page_number }); + self.page.updatePreviewButton(self.collection.showChildrenPreviews); + self.page.renderAddXBlockComponents(); + if (options.force_render) { + var $target = $('.studio-xblock-wrapper[data-locator="' + options.force_render + '"]'); + // Scroll us to the element with a little buffer at the top for context. + ViewUtils.setScrollOffset($target, ($(window).height() * 0.10)); + } + originalDone(); } + self.handleXBlockFragment(fragment, options); } }); }, From cb075a32409aaa450271ae7729fae5b398f470a1 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Mon, 10 Sep 2018 15:09:34 -0400 Subject: [PATCH 07/10] fixup! Load all webpack chunks into fragments as correctly-typed resources --- cms/static/js/views/paged_container.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cms/static/js/views/paged_container.js b/cms/static/js/views/paged_container.js index 716f0b53b9..3310bd5a3d 100644 --- a/cms/static/js/views/paged_container.js +++ b/cms/static/js/views/paged_container.js @@ -106,7 +106,9 @@ define(['jquery', 'underscore', 'common/js/components/utils/view_utils', 'js/vie // Scroll us to the element with a little buffer at the top for context. ViewUtils.setScrollOffset($target, ($(window).height() * 0.10)); } - originalDone(); + if (originalDone) { + originalDone(); + } } self.handleXBlockFragment(fragment, options); } From 9c32073e9baa73164f1dd006b8ef3784e3292a44 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Tue, 11 Sep 2018 13:56:05 -0400 Subject: [PATCH 08/10] fixup! Load all webpack chunks into fragments as correctly-typed resources --- common/lib/xmodule/xmodule/js/src/conditional/display.js | 5 ++--- webpack.common.config.js | 5 ++++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/common/lib/xmodule/xmodule/js/src/conditional/display.js b/common/lib/xmodule/xmodule/js/src/conditional/display.js index 09da8b32f4..8bfda99007 100644 --- a/common/lib/xmodule/xmodule/js/src/conditional/display.js +++ b/common/lib/xmodule/xmodule/js/src/conditional/display.js @@ -152,9 +152,8 @@ eval.call(window, data); console.log("JavaScript text resource eval'd", resource); } else if (kind === 'url') { - // This is a dependency loaded from the LMS (not ideal) - return ViewUtils.loadJavaScript(data).done(function() { - console.log('JavaScript url resource loaded', resource); + $script(data, data, function () { + console.log('JavaScript url resource loaded', resource); }); } } else if (mimetype === 'text/html') { diff --git a/webpack.common.config.js b/webpack.common.config.js index 5ad625f5eb..e4602d30e5 100644 --- a/webpack.common.config.js +++ b/webpack.common.config.js @@ -106,7 +106,10 @@ module.exports = Merge.smart({ Popper: 'popper.js', // used by bootstrap CodeMirror: 'codemirror', 'edx.HtmlUtils': 'edx-ui-toolkit/js/utils/html-utils', - AjaxPrefix: 'ajax_prefix' + AjaxPrefix: 'ajax_prefix', + // This is used by some XModules/XBlocks, which don't have + // any other way to declare that dependency. + $script: 'scriptjs' }), // Note: Until karma-webpack releases v3, it doesn't play well with From db2078ed2f30a63e685d2283c74d166cbdecc464 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Wed, 12 Sep 2018 14:27:54 -0400 Subject: [PATCH 09/10] fixup! Load all webpack chunks into fragments as correctly-typed resources --- cms/static/js/views/xblock.js | 7 ++++++- common/lib/xmodule/xmodule/js/src/conditional/display.js | 7 ++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/cms/static/js/views/xblock.js b/cms/static/js/views/xblock.js index cc8bb7e95e..8436ec8c3f 100644 --- a/cms/static/js/views/xblock.js +++ b/cms/static/js/views/xblock.js @@ -137,7 +137,12 @@ define(['jquery', 'underscore', 'common/js/components/utils/view_utils', 'js/vie // that at least the rendered HTML will be in place. try { return this.addXBlockFragmentResources(resources).done(function() { - blockView.updateHtml(element, html); + console.log('Updating HTML'); + try { + blockView.updateHtml(element, html); + } catch (e) { + console.error(e, e.stack); + } }); } catch (e) { console.error(e, e.stack); diff --git a/common/lib/xmodule/xmodule/js/src/conditional/display.js b/common/lib/xmodule/xmodule/js/src/conditional/display.js index 8bfda99007..3aa94908bd 100644 --- a/common/lib/xmodule/xmodule/js/src/conditional/display.js +++ b/common/lib/xmodule/xmodule/js/src/conditional/display.js @@ -149,12 +149,9 @@ } } else if (mimetype === 'application/javascript') { if (kind === 'text') { - eval.call(window, data); - console.log("JavaScript text resource eval'd", resource); + $head.append(''); } else if (kind === 'url') { - $script(data, data, function () { - console.log('JavaScript url resource loaded', resource); - }); + $script(data, data); } } else if (mimetype === 'text/html') { if (placement === 'head') { From 461f35255cdfad6ee17f39f73a8ab91fd99365ac Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Wed, 12 Sep 2018 21:54:19 -0400 Subject: [PATCH 10/10] fixup! Load all webpack chunks into fragments as correctly-typed resources --- common/lib/xmodule/xmodule/js/src/conditional/display.js | 1 + 1 file changed, 1 insertion(+) diff --git a/common/lib/xmodule/xmodule/js/src/conditional/display.js b/common/lib/xmodule/xmodule/js/src/conditional/display.js index 3aa94908bd..fe0d7284ac 100644 --- a/common/lib/xmodule/xmodule/js/src/conditional/display.js +++ b/common/lib/xmodule/xmodule/js/src/conditional/display.js @@ -149,6 +149,7 @@ } } else if (mimetype === 'application/javascript') { if (kind === 'text') { + // xss-lint: disable=javascript-jquery-append,javascript-concat-html $head.append(''); } else if (kind === 'url') { $script(data, data);