From 569066edf51dbf24e46c02845002780569136d25 Mon Sep 17 00:00:00 2001
From: Calen Pennington <cale@edx.org>
Date: Fri, 22 Dec 2017 11:09:15 -0500
Subject: [PATCH] Convert cms/static/js/factories/asset_index.js to be bundled
 using webpack

---
 cms/static/cms/js/build.js         |  1 -
 cms/static/js/pages/asset_index.js |  7 +++++++
 cms/templates/asset_index.html     | 18 ++++++++--------
 webpack.common.config.js           | 33 ++++++++++++++++++++++++++++--
 4 files changed, 47 insertions(+), 12 deletions(-)
 create mode 100644 cms/static/js/pages/asset_index.js

diff --git a/cms/static/cms/js/build.js b/cms/static/cms/js/build.js
index 3f86a8c891e..a92365aaece 100644
--- a/cms/static/cms/js/build.js
+++ b/cms/static/cms/js/build.js
@@ -17,7 +17,6 @@
          * done.
          */
         modules: getModulesList([
-            'js/factories/asset_index',
             'js/factories/base',
             'js/factories/container',
             'js/factories/course_create_rerun',
diff --git a/cms/static/js/pages/asset_index.js b/cms/static/js/pages/asset_index.js
new file mode 100644
index 00000000000..f1daafff4d3
--- /dev/null
+++ b/cms/static/js/pages/asset_index.js
@@ -0,0 +1,7 @@
+define(
+    ['js/factories/asset_index', 'common/js/utils/page_factory'],
+    function(AssetIndexFactory, invokePageFactory) {
+        'use strict';
+        invokePageFactory('AssetIndexFactory', AssetIndexFactory);
+    }
+);
diff --git a/cms/templates/asset_index.html b/cms/templates/asset_index.html
index c00ee465793..d4ada8d55c2 100644
--- a/cms/templates/asset_index.html
+++ b/cms/templates/asset_index.html
@@ -27,16 +27,16 @@
     % endif
 </%block>
 
-<%block name="requirejs">
+<%block name="page_bundle">
     % if not waffle_flag_enabled:
-            require(["js/factories/asset_index"], function (AssetIndexFactory) {
-                AssetIndexFactory({
-                  assetCallbackUrl: "${asset_callback_url|n, js_escaped_string}",
-                  uploadChunkSizeInMBs: ${chunk_size_in_mbs|n, dump_js_escaped_json},
-                  maxFileSizeInMBs: ${max_file_size_in_mbs|n, dump_js_escaped_json},
-                  maxFileSizeRedirectUrl: "${max_file_size_redirect_url|n, js_escaped_string}"
-                });
-            });
+        <%static:invoke_page_bundle page_name="js/pages/asset_index" class_name="AssetIndexFactory">
+            {
+                assetCallbackUrl: "${asset_callback_url|n, js_escaped_string}",
+                uploadChunkSizeInMBs: ${ chunk_size_in_mbs| n, dump_js_escaped_json },
+                maxFileSizeInMBs: ${ max_file_size_in_mbs| n, dump_js_escaped_json },
+                maxFileSizeRedirectUrl: "${max_file_size_redirect_url|n, js_escaped_string}"
+            }
+        </%static:invoke_page_bundle>
     % endif
 </%block>
 
diff --git a/webpack.common.config.js b/webpack.common.config.js
index bc5693410b8..9655245d04d 100644
--- a/webpack.common.config.js
+++ b/webpack.common.config.js
@@ -11,9 +11,21 @@ var namespacedRequireFiles = [
     path.resolve(__dirname, 'common/static/common/js/components/views/feedback_notification.js'),
     path.resolve(__dirname, 'common/static/common/js/components/views/feedback_prompt.js'),
     path.resolve(__dirname, 'common/static/common/js/components/views/feedback.js'),
+    path.resolve(__dirname, 'common/static/common/js/components/views/paging_footer.js'),
+    path.resolve(__dirname, 'cms/static/js/views/paging.js'),
     path.resolve(__dirname, 'common/static/common/js/components/utils/view_utils.js')
 ];
 
+// These files are used by RequireJS as well, so we can't remove
+// the instances of "text!some/file.underscore" (which webpack currently
+// processes twice). So instead we have webpack dynamically remove the `text!` prefix
+// until we can remove RequireJS from the system.
+var filesWithTextBangUnderscore = [
+    path.resolve(__dirname, 'cms/static/js/views/assets.js'),
+    path.resolve(__dirname, 'cms/static/js/views/paging_header.js'),
+    path.resolve(__dirname, 'common/static/common/js/components/views/paging_footer.js')
+]
+
 var defineHeader = /\(function ?\(define(, require)?\) ?\{/;
 var defineFooter = /\}\)\.call\(this, define \|\| RequireJS\.define(, require \|\| RequireJS\.require)?\);/;
 
@@ -25,6 +37,7 @@ module.exports = {
         Import: './cms/static/js/features/import/factories/import.js',
         CourseOrLibraryListing: './cms/static/js/features_jsx/studio/CourseOrLibraryListing.jsx',
         'js/pages/login': './cms/static/js/pages/login.js',
+        'js/pages/asset_index': './cms/static/js/pages/asset_index.js',
 
         // LMS
         SingleSupportForm: './lms/static/support/jsx/single_support_form.jsx',
@@ -114,11 +127,26 @@ module.exports = {
                     }
                 )
             },
+            {
+                test: filesWithTextBangUnderscore,
+                loader: StringReplace.replace(
+                    ['babel-loader'],
+                    {
+                        replacements: [
+                            {
+                                pattern: /text!(.*\.underscore)/,
+                                replacement: function(match, p1) { return p1; }
+                            }
+                        ]
+                    }
+                )
+            },
             {
                 test: /\.(js|jsx)$/,
                 exclude: [
                     /node_modules/,
-                    namespacedRequireFiles
+                    namespacedRequireFiles,
+                    filesWithTextBangUnderscore
                 ],
                 use: 'babel-loader'
             },
@@ -174,7 +202,8 @@ module.exports = {
             'node_modules',
             'common/static/js/vendor/',
             'cms/static',
-            'common/static/js/src'
+            'common/static/js/src',
+            'common/static/js/vendor/jQuery-File-Upload/js/'
         ]
     },
 
-- 
GitLab