From 911869f99e6d02a16cc67de45ea1f14488ba2626 Mon Sep 17 00:00:00 2001
From: Clement Ho <clemmakesapps@gmail.com>
Date: Thu, 22 Feb 2018 13:14:48 +0000
Subject: [PATCH] Webpack auto include for EE imports

---
 app/assets/javascripts/dispatcher.js           |  5 -----
 config/webpack.config.js                       | 18 ++++++++++++------
 .../pages/projects/blame/show/index.js         |  1 +
 .../pages/projects/blob/show/index.js          |  3 ++-
 .../javascripts/pages/projects/edit/index.js   |  4 ++--
 .../merge_requests/creations/new/index.js      |  1 +
 .../projects/merge_requests/edit/index.js      |  1 +
 .../pages/projects/milestones/show/index.js    |  1 +
 .../projects/settings/repository/show/index.js |  1 +
 .../javascripts/pages/projects/show/index.js   |  1 +
 .../javascripts/pages/search/show/index.js     |  1 +
 11 files changed, 23 insertions(+), 14 deletions(-)

diff --git a/app/assets/javascripts/dispatcher.js b/app/assets/javascripts/dispatcher.js
index b2ce0b73131..184ccb47552 100644
--- a/app/assets/javascripts/dispatcher.js
+++ b/app/assets/javascripts/dispatcher.js
@@ -62,11 +62,6 @@ var Dispatcher;
         case 'projects:blame:show':
           shortcut_handler = true;
           break;
-        case 'projects:edit':
-          import(/* webpackChunkName: "ee_projects_edit" */ 'ee/pages/projects/edit')
-            .then(callDefault)
-            .catch(fail);
-          break;
         case 'projects:tree:show':
           import(/* webpackChunkName: "ee_projects_edit" */ 'ee/pages/projects/tree/show')
             .then(callDefault)
diff --git a/config/webpack.config.js b/config/webpack.config.js
index 9489e32f614..9ae7d0d77b5 100644
--- a/config/webpack.config.js
+++ b/config/webpack.config.js
@@ -29,13 +29,19 @@ var pageEntries = glob.sync('pages/**/index.js', { cwd: path.join(ROOT_PATH, 'ap
 var dispatcher = fs.readFileSync(path.join(ROOT_PATH, 'app/assets/javascripts/dispatcher.js')).toString();
 var dispatcherChunks = dispatcher.match(/(?!import\(')\.\/pages\/[^']+/g);
 
-pageEntries.forEach(( path ) => {
-  let chunkPath = path.replace(/\/index\.js$/, '');
-  if (!dispatcherChunks.includes('./' + chunkPath)) {
-    let chunkName = chunkPath.replace(/\//g, '.');
-    autoEntries[chunkName] = './' + path;
+function generateAutoEntries(path, prefix = '.') {
+  const chunkPath = path.replace(/\/index\.js$/, '');
+  if (!dispatcherChunks.includes(`${prefix}/${chunkPath}`)) {
+    const chunkName = chunkPath.replace(/\//g, '.');
+    autoEntries[chunkName] = `${prefix}/${path}`;
   }
-});
+}
+
+pageEntries.forEach(( path ) => generateAutoEntries(path));
+
+// add and replace any ce entries with ee entries
+const eePageEntries = glob.sync('pages/**/index.js', { cwd: path.join(ROOT_PATH, 'ee/app/assets/javascripts') });
+eePageEntries.forEach(( path ) => generateAutoEntries(path, 'ee'));
 
 // report our auto-generated bundle count
 var autoEntriesCount = Object.keys(autoEntries).length;
diff --git a/ee/app/assets/javascripts/pages/projects/blame/show/index.js b/ee/app/assets/javascripts/pages/projects/blame/show/index.js
index 47d64024181..38a115bb485 100644
--- a/ee/app/assets/javascripts/pages/projects/blame/show/index.js
+++ b/ee/app/assets/javascripts/pages/projects/blame/show/index.js
@@ -1,3 +1,4 @@
+import '~/pages/projects/blame/show/index';
 import initBlob from '../../shared/init_blob';
 
 export default () => initBlob();
diff --git a/ee/app/assets/javascripts/pages/projects/blob/show/index.js b/ee/app/assets/javascripts/pages/projects/blob/show/index.js
index 47d64024181..264f3320275 100644
--- a/ee/app/assets/javascripts/pages/projects/blob/show/index.js
+++ b/ee/app/assets/javascripts/pages/projects/blob/show/index.js
@@ -1,3 +1,4 @@
+import '~/pages/projects/blob/show/index';
 import initBlob from '../../shared/init_blob';
 
-export default () => initBlob();
+export default initBlob;
diff --git a/ee/app/assets/javascripts/pages/projects/edit/index.js b/ee/app/assets/javascripts/pages/projects/edit/index.js
index b9d552b42f3..a7563309528 100644
--- a/ee/app/assets/javascripts/pages/projects/edit/index.js
+++ b/ee/app/assets/javascripts/pages/projects/edit/index.js
@@ -5,11 +5,11 @@ import UserCallout from '~/user_callout';
 import groupsSelect from '~/groups_select';
 import ApproversSelect from 'ee/approvers_select';
 
-export default () => {
+document.addEventListener('DOMContentLoaded', () => {
   new UsersSelect();
   groupsSelect();
 
   new UserCallout({ className: 'js-service-desk-callout' });
   new UserCallout({ className: 'js-mr-approval-callout' });
   new ApproversSelect();
-};
+});
diff --git a/ee/app/assets/javascripts/pages/projects/merge_requests/creations/new/index.js b/ee/app/assets/javascripts/pages/projects/merge_requests/creations/new/index.js
index af8b117d784..dc29a916542 100644
--- a/ee/app/assets/javascripts/pages/projects/merge_requests/creations/new/index.js
+++ b/ee/app/assets/javascripts/pages/projects/merge_requests/creations/new/index.js
@@ -1,3 +1,4 @@
+import '~/pages/projects/merge_requests/creations/new/index';
 import UserCallout from '~/user_callout';
 import initForm from '../../shared/init_form';
 
diff --git a/ee/app/assets/javascripts/pages/projects/merge_requests/edit/index.js b/ee/app/assets/javascripts/pages/projects/merge_requests/edit/index.js
index b48d8f9f26c..b163b828b88 100644
--- a/ee/app/assets/javascripts/pages/projects/merge_requests/edit/index.js
+++ b/ee/app/assets/javascripts/pages/projects/merge_requests/edit/index.js
@@ -1,3 +1,4 @@
+import '~/pages/projects/merge_requests/edit/index';
 import initForm from '../shared/init_form';
 
 export default () => initForm();
diff --git a/ee/app/assets/javascripts/pages/projects/milestones/show/index.js b/ee/app/assets/javascripts/pages/projects/milestones/show/index.js
index f18f98b4e9a..689d910fdcb 100644
--- a/ee/app/assets/javascripts/pages/projects/milestones/show/index.js
+++ b/ee/app/assets/javascripts/pages/projects/milestones/show/index.js
@@ -1,3 +1,4 @@
+import '~/pages/projects/milestones/show/index';
 import UserCallout from '~/user_callout';
 
 export default () => new UserCallout();
diff --git a/ee/app/assets/javascripts/pages/projects/settings/repository/show/index.js b/ee/app/assets/javascripts/pages/projects/settings/repository/show/index.js
index 22ec84296cc..6f68ab7ca80 100644
--- a/ee/app/assets/javascripts/pages/projects/settings/repository/show/index.js
+++ b/ee/app/assets/javascripts/pages/projects/settings/repository/show/index.js
@@ -1,3 +1,4 @@
+import '~/pages/projects/settings/repository/show/index';
 import UsersSelect from '~/users_select';
 import UserCallout from '~/user_callout';
 
diff --git a/ee/app/assets/javascripts/pages/projects/show/index.js b/ee/app/assets/javascripts/pages/projects/show/index.js
index d78df14edf5..74c183da1a4 100644
--- a/ee/app/assets/javascripts/pages/projects/show/index.js
+++ b/ee/app/assets/javascripts/pages/projects/show/index.js
@@ -1,3 +1,4 @@
+import '~/pages/projects/show/index';
 import initGeoInfoModal from '../../../init_geo_info_modal';
 
 export default () => initGeoInfoModal();
diff --git a/ee/app/assets/javascripts/pages/search/show/index.js b/ee/app/assets/javascripts/pages/search/show/index.js
index f18f98b4e9a..acb552a2672 100644
--- a/ee/app/assets/javascripts/pages/search/show/index.js
+++ b/ee/app/assets/javascripts/pages/search/show/index.js
@@ -1,3 +1,4 @@
+import '~/pages/search/show/index';
 import UserCallout from '~/user_callout';
 
 export default () => new UserCallout();
-- 
2.30.9