Commit f8ddfb59 by Toby Lawrence

Use a module/path mapping for RequireJS overrides instead of just paths.

Instead of attempting to derive the module portion of a RequireJS
override strictly from the path to the JS file, we now use a dictionary
where the module name must be explicitly specified.  This allows us to
compensate for files which do not follow a naming scheme that is
compatible with RequireJS without having to normalize all files.  This
is extremely important when using third-party dependencies.
parent 88aa4a90
...@@ -128,17 +128,17 @@ def render_require_js_path_overrides(path_overrides): # pylint: disable=invalid ...@@ -128,17 +128,17 @@ def render_require_js_path_overrides(path_overrides): # pylint: disable=invalid
</script>''' </script>'''
new_paths = [] new_paths = []
for url_path in path_overrides: for module in path_overrides:
# Calculate the full URL, including any hashes added to the filename by the pipeline. # Calculate the full URL, including any hashes added to the filename by the pipeline.
# This will also include the base static URL (for example, "/static/") and the # This will also include the base static URL (for example, "/static/") and the
# ".js" extension. # ".js" extension.
actual_url = staticfiles_storage.url(url_path) actual_url = staticfiles_storage.url(path_overrides[module])
# RequireJS assumes that every file it tries to load has a ".js" extension, so # RequireJS assumes that every file it tries to load has a ".js" extension, so
# we need to remove ".js" from the module path. # we need to remove ".js" from the module path.
# RequireJS also already has a base URL set to the base static URL, so we can remove that. # RequireJS also already has a base URL set to the base static URL, so we can remove that.
path = actual_url.replace('.js', '').replace(django_settings.STATIC_URL, '') path = actual_url.replace('.js', '').replace(django_settings.STATIC_URL, '')
new_paths.append("'{module}': '{path}'".format(module=url_path.replace('.js', ''), path=path)) new_paths.append("'{module}': '{path}'".format(module=module, path=path))
return html.format(overrides=',\n'.join(new_paths)) return html.format(overrides=',\n'.join(new_paths))
...@@ -7,20 +7,20 @@ from pipeline_mako import render_require_js_path_overrides ...@@ -7,20 +7,20 @@ from pipeline_mako import render_require_js_path_overrides
class RequireJSPathOverridesTest(TestCase): class RequireJSPathOverridesTest(TestCase):
"""Test RequireJS path overrides. """ """Test RequireJS path overrides. """
OVERRIDES = [ OVERRIDES = {
'js/vendor/jquery.min.js', 'jquery': 'js/vendor/jquery.min.js',
'js/vendor/backbone-min.js', 'backbone': 'js/vendor/backbone-min.js',
'js/vendor/text.js' 'text': 'js/vendor/text.js'
] }
OVERRIDES_JS = [ OVERRIDES_JS = [
"<script type=\"text/javascript\">", "<script type=\"text/javascript\">",
"(function (require) {", "(function (require) {",
"require.config({", "require.config({",
"paths: {", "paths: {",
"'js/vendor/jquery.min': 'js/vendor/jquery.min',", "'jquery': 'js/vendor/jquery.min',",
"'js/vendor/backbone-min': 'js/vendor/backbone-min',", "'text': 'js/vendor/text',",
"'js/vendor/text': 'js/vendor/text'", "'backbone': 'js/vendor/backbone-min'",
"}", "}",
"});", "});",
"}).call(this, require || RequireJS.require);", "}).call(this, require || RequireJS.require);",
......
...@@ -1633,14 +1633,14 @@ REQUIRE_ENVIRONMENT = "node" ...@@ -1633,14 +1633,14 @@ REQUIRE_ENVIRONMENT = "node"
# If you want to load JavaScript dependencies using RequireJS # If you want to load JavaScript dependencies using RequireJS
# but you don't want to include those dependencies in the JS bundle for the page, # but you don't want to include those dependencies in the JS bundle for the page,
# then you need to add the js urls in this list. # then you need to add the js urls in this list.
REQUIRE_JS_PATH_OVERRIDES = [ REQUIRE_JS_PATH_OVERRIDES = {
'js/bookmarks/views/bookmark_button.js', 'bookmark_button': 'js/bookmarks/views/bookmark_button.js',
'js/views/message_banner.js', 'message_banner': 'js/views/message_banner.js',
'js/vendor/moment.min.js', 'moment': 'js/vendor/moment.min.js',
'js/vendor/url.min.js', 'url': 'js/vendor/url.min.js',
'js/courseware/course_home_events.js', 'course_home_events': 'js/courseware/course_home_events.js',
'js/courseware/toggle_element_visibility.js' 'toggle_element_visibility': 'js/courseware/toggle_element_visibility.js'
] }
################################# CELERY ###################################### ################################# CELERY ######################################
# Celery's task autodiscovery won't find tasks nested in a tasks package. # Celery's task autodiscovery won't find tasks nested in a tasks package.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment