From f8ddfb59454db9a8d7ebf0423dd6d9db7cf5b722 Mon Sep 17 00:00:00 2001 From: Toby Lawrence Date: Tue, 22 Mar 2016 11:09:18 -0400 Subject: [PATCH] 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. --- common/djangoapps/pipeline_mako/__init__.py | 6 +++--- .../pipeline_mako/tests/test_render.py | 16 ++++++++-------- lms/envs/common.py | 16 ++++++++-------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/common/djangoapps/pipeline_mako/__init__.py b/common/djangoapps/pipeline_mako/__init__.py index dd1c9d26aa..d8987550a4 100644 --- a/common/djangoapps/pipeline_mako/__init__.py +++ b/common/djangoapps/pipeline_mako/__init__.py @@ -128,17 +128,17 @@ def render_require_js_path_overrides(path_overrides): # pylint: disable=invalid ''' 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. # This will also include the base static URL (for example, "/static/") and the # ".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 # 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. 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)) diff --git a/common/djangoapps/pipeline_mako/tests/test_render.py b/common/djangoapps/pipeline_mako/tests/test_render.py index c4ab694f51..a7c50d8e19 100644 --- a/common/djangoapps/pipeline_mako/tests/test_render.py +++ b/common/djangoapps/pipeline_mako/tests/test_render.py @@ -7,20 +7,20 @@ from pipeline_mako import render_require_js_path_overrides class RequireJSPathOverridesTest(TestCase): """Test RequireJS path overrides. """ - OVERRIDES = [ - 'js/vendor/jquery.min.js', - 'js/vendor/backbone-min.js', - 'js/vendor/text.js' - ] + OVERRIDES = { + 'jquery': 'js/vendor/jquery.min.js', + 'backbone': 'js/vendor/backbone-min.js', + 'text': 'js/vendor/text.js' + } OVERRIDES_JS = [ "