Przeglądaj źródła

HUE-9425 [ui] Automatically detect and load webpack chunks per app

Johan Ahlen 5 lat temu
rodzic
commit
542f0f6bfb

+ 4 - 2
apps/jobbrowser/src/jobbrowser/templates/job_browser.mako

@@ -18,6 +18,7 @@ from django.utils.translation import ugettext as _
 
 from desktop.conf import CUSTOM, IS_K8S_ONLY
 from desktop.views import commonheader, commonfooter, _ko
+from desktop.webpack_utils import get_hue_bundles
 from metadata.conf import PROMETHEUS
 from notebook.conf import ENABLE_QUERY_SCHEDULING
 
@@ -58,8 +59,9 @@ ${ commonheader("Job Browser", "jobbrowser", user, request) | n,unicode }
 <link rel="stylesheet" href="${ static('desktop/css/bootstrap-spinedit.css') }">
 <link rel="stylesheet" href="${ static('desktop/css/bootstrap-slider.css') }">
 
-${ render_bundle('vendors~tableBrowser~jobBrowser') | n,unicode }
-${ render_bundle('jobBrowser') | n,unicode }
+% for bundle in get_hue_bundles('jobBrowser'):
+  ${ render_bundle(bundle) | n,unicode }
+% endfor
 
 <script src="${ static('desktop/ext/js/bootstrap-datepicker.min.js') }" type="text/javascript" charset="utf-8"></script>
 <script src="${ static('desktop/ext/js/bootstrap-timepicker.min.js') }" type="text/javascript" charset="utf-8"></script>

+ 4 - 2
apps/metastore/src/metastore/templates/metastore.mako

@@ -21,6 +21,7 @@ from desktop import conf
 from desktop.conf import USE_NEW_EDITOR
 from desktop.lib.i18n import smart_unicode
 from desktop.views import commonheader, commonfooter, _ko
+from desktop.webpack_utils import get_hue_bundles
 from metastore.conf import SHOW_TABLE_ERD
 from beeswax.conf import LIST_PARTITIONS_LIMIT
 from webpack_loader.templatetags.webpack_loader import render_bundle
@@ -62,8 +63,9 @@ ${ commonheader(_("Metastore"), app_name, user, request) | n,unicode }
 <link rel="stylesheet" href="${ static('desktop/ext/css/bootstrap-editable.css') }">
 <link rel="stylesheet" href="${ static('notebook/css/notebook.css') }">
 
-${ render_bundle('vendors~tableBrowser~jobBrowser') | n,unicode }
-${ render_bundle('tableBrowser') | n,unicode }
+% for bundle in get_hue_bundles('tableBrowser'):
+  ${ render_bundle(bundle) | n,unicode }
+% endfor
 
 <span class="notebook">
 

+ 4 - 11
desktop/core/src/desktop/templates/common_header.mako

@@ -24,6 +24,7 @@ from desktop.auth.backend import is_admin
 from desktop.conf import USE_NEW_EDITOR
 from desktop.models import hue_version
 from desktop.lib.i18n import smart_unicode
+from desktop.webpack_utils import get_hue_bundles
 
 home_url = url('desktop_views_home')
 if USE_NEW_EDITOR.get():
@@ -145,17 +146,9 @@ if USE_NEW_EDITOR.get():
   <script src="${ static('desktop/js/hue.errorcatcher.js') }"></script>
   % endif
 
-  % if section == "login":
-    ${ render_bundle('login', config='LOGIN') | n,unicode }
-  %else:
-    ${ render_bundle('vendors~hue~notebook~tableBrowser') | n,unicode }
-    ${ render_bundle('vendors~hue~notebook') | n,unicode }
-    ${ render_bundle('vendors~hue') | n,unicode }
-    ${ render_bundle('hue~notebook') | n,unicode }
-    ${ render_bundle('hue~notebook~tableBrowser') | n,unicode }
-    ${ render_bundle('hue~tableBrowser') | n,unicode }
-    ${ render_bundle('hue') | n,unicode }
-  % endif
+  % for bundle in get_hue_bundles('login' if section == 'login' else 'hue', 'LOGIN' if section == 'login' else 'DEFAULT'):
+    ${ render_bundle(bundle) | n,unicode }
+  % endfor
 
   <script src="${ static('desktop/js/bootstrap-tooltip.js') }"></script>
   <script src="${ static('desktop/js/bootstrap-typeahead-touchscreen.js') }"></script>

+ 4 - 1
desktop/core/src/desktop/templates/common_header_m.mako

@@ -16,6 +16,7 @@
 <%!
 from desktop import conf
 from desktop.lib.i18n import smart_unicode
+from desktop.webpack_utils import get_hue_bundles
 from django.utils.translation import ugettext as _
 from metadata.conf import has_optimizer, OPTIMIZER
 
@@ -112,7 +113,9 @@ if USE_NEW_EDITOR.get():
     }
   </script>
 
-  ${ render_bundle('hue') | n,unicode }
+  % for bundle in get_hue_bundles('hue'):
+    ${ render_bundle(bundle) | n,unicode }
+  % endfor
 
   <script src="${ static('desktop/ext/js/jquery/plugins/jquery.touchSwipe.min.js') }"></script>
   <script src="${ static('desktop/js/bootstrap-typeahead-touchscreen.js') }"></script>

+ 4 - 7
desktop/core/src/desktop/templates/hue.mako

@@ -22,6 +22,7 @@
   from desktop.views import _ko, commonshare, login_modal
   from desktop.lib.i18n import smart_unicode
   from desktop.models import PREFERENCE_IS_WELCOME_TOUR_SEEN, hue_version, get_cluster_config
+  from desktop.webpack_utils import get_hue_bundles
 
   from dashboard.conf import IS_ENABLED as IS_DASHBOARD_ENABLED
   from filebrowser.conf import SHOW_UPLOAD_BUTTON
@@ -277,13 +278,9 @@ ${ hueIcons.symbols() }
 </div>
 ${ commonshare() | n,unicode }
 
-${ render_bundle('vendors~hue~notebook~tableBrowser') | n,unicode }
-${ render_bundle('vendors~hue~notebook') | n,unicode }
-${ render_bundle('vendors~hue') | n,unicode }
-${ render_bundle('hue~notebook~tableBrowser') | n,unicode }
-${ render_bundle('hue~notebook') | n,unicode }
-${ render_bundle('hue~tableBrowser') | n,unicode }
-${ render_bundle('hue') | n,unicode }
+% for bundle in get_hue_bundles('hue'):
+  ${ render_bundle(bundle) | n,unicode }
+% endfor
 
 <script src="${ static('desktop/js/polyfills.js') }"></script>
 <script src="${ static('desktop/ext/js/tether.js') }"></script>

+ 54 - 0
desktop/core/src/desktop/webpack_utils.py

@@ -0,0 +1,54 @@
+# Licensed to Cloudera, Inc. under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  Cloudera, Inc. licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import time
+
+from django.conf import settings
+from webpack_loader.exceptions import WebpackError, WebpackLoaderTimeoutError
+from webpack_loader.utils import get_loader
+
+def get_hue_bundles(app_name, config='DEFAULT'):
+    '''
+    Util function to get all bundles related to app_name including vendor bundles
+    similar to get_bundle in https://github.com/owais/django-webpack-loader/blob/master/webpack_loader/loader.py
+    '''
+    loader = get_loader(config)
+    assets = loader.get_assets()
+
+    if settings.DEBUG and assets.get('status') == 'compiling':
+        timeout = loader.config['TIMEOUT'] or 0
+        timed_out = False
+        start = time.time()
+
+        while assets.get('status') == 'compiling' and not timed_out:
+            time.sleep(loader.config['POLL_INTERVAL'])
+            if timeout and (time.time() - timeout > start):
+                timed_out = True
+            assets = loader.get_assets()
+
+        if timed_out:
+            raise WebpackLoaderTimeoutError(
+                "Timed Out. Bundles for `{0}` took more than {1} seconds "
+                "to compile.".format(app_name, timeout)
+            )
+
+    if assets.get('status') == 'done':
+        return [chunk for chunk in assets['chunks'] if
+                chunk.startswith(app_name) or
+                chunk.startswith('vendors~' + app_name) or
+                (not chunk.startswith('hue') and not chunk.startswith('vendors~hue') and app_name in chunk)]
+
+    raise WebpackError("Failed to find bundles for `{0}` in config `{1}`".format(app_name, config))

+ 5 - 2
desktop/libs/notebook/src/notebook/templates/editor_components.mako

@@ -23,6 +23,8 @@ from desktop import conf
 from desktop.auth.backend import is_admin
 from desktop.lib.i18n import smart_unicode
 from desktop.views import _ko, antixss
+from desktop.webpack_utils import get_hue_bundles
+
 from metadata.conf import has_optimizer, OPTIMIZER
 
 from notebook.conf import ENABLE_QUERY_BUILDER, ENABLE_QUERY_SCHEDULING, ENABLE_BATCH_EXECUTE, ENABLE_EXTERNAL_STATEMENT, ENABLE_PRESENTATION
@@ -87,8 +89,9 @@ from notebook.conf import ENABLE_QUERY_BUILDER, ENABLE_QUERY_SCHEDULING, ENABLE_
 <!-- End query builder imports -->
 % endif
 
-${ render_bundle('vendors~notebook') | n,unicode }
-${ render_bundle('notebook') | n,unicode }
+% for bundle in get_hue_bundles('notebook'):
+  ${ render_bundle(bundle) | n,unicode }
+% endfor
 
 <!--[if IE 9]>
   <script src="${ static('desktop/ext/js/classList.min.js') }" type="text/javascript" charset="utf-8"></script>

+ 5 - 2
desktop/libs/notebook/src/notebook/templates/editor_components2.mako

@@ -23,6 +23,8 @@
   from desktop.auth.backend import is_admin
   from desktop.lib.i18n import smart_unicode
   from desktop.views import _ko, antixss
+  from desktop.webpack_utils import get_hue_bundles
+
   from metadata.conf import has_optimizer, OPTIMIZER
 
   from notebook.conf import ENABLE_QUERY_BUILDER, ENABLE_QUERY_SCHEDULING, ENABLE_BATCH_EXECUTE, ENABLE_EXTERNAL_STATEMENT, ENABLE_PRESENTATION
@@ -87,8 +89,9 @@
     <!-- End query builder imports -->
   % endif
 
-  ${ render_bundle('vendors~notebook') | n,unicode }
-  ${ render_bundle('notebook') | n,unicode }
+  % for bundle in get_hue_bundles('notebook'):
+    ${ render_bundle(bundle) | n,unicode }
+  % endfor
 
   <!--[if IE 9]>
   <script src="${ static('desktop/ext/js/classList.min.js') }" type="text/javascript" charset="utf-8"></script>