Jelajahi Sumber

HUE-8856 [frontend] Add python endpoint for dynamic imports

For now we do a 302 redirect to the static resources.
Johan Ahlen 6 tahun lalu
induk
melakukan
e2f01be2fd

+ 1 - 0
desktop/core/src/desktop/js/publicPath.js

@@ -0,0 +1 @@
+__webpack_public_path__ = (window.HUE_BASE_URL || '') + '/dynamic_bundle/';

+ 2 - 0
desktop/core/src/desktop/urls.py

@@ -121,6 +121,8 @@ dynamic_patterns += [
   url(r'^desktop/workers/aceSqlLocationWorker.js', desktop_views.ace_sql_location_worker),
   url(r'^desktop/workers/aceSqlSyntaxWorker.js', desktop_views.ace_sql_syntax_worker),
 
+  url(r'^dynamic_bundle/(?P<bundle_path>.+)', desktop_views.dynamic_bundle),
+
   # Unsupported browsers
   url(r'^boohoo$', desktop_views.unsupported, name='desktop_views_unsupported'),
 

+ 9 - 0
desktop/core/src/desktop/views.py

@@ -28,6 +28,7 @@ import traceback
 import zipfile
 import validate
 
+from django.http import HttpResponseRedirect
 from django.conf import settings
 from django.contrib.staticfiles.storage import staticfiles_storage
 from django.shortcuts import render_to_response
@@ -64,6 +65,7 @@ from desktop.log.access import access_log_level, access_warn, AccessInfo
 from desktop.log import set_all_debug as _set_all_debug, reset_all_debug as _reset_all_debug, get_all_debug as _get_all_debug
 from desktop.models import Settings, hue_version, _get_apps, UserPreferences
 
+from webpack_loader.utils import get_files
 
 LOG = logging.getLogger(__name__)
 
@@ -375,6 +377,13 @@ def ace_sql_location_worker(request):
 def ace_sql_syntax_worker(request):
   return HttpResponse(render('ace_sql_syntax_worker.mako', request, None), content_type="application/javascript")
 
+def dynamic_bundle(request, bundle_path):
+  bundle_name = re.sub(r'-(bundle|chunk).*', '', bundle_path)
+  files = get_files(bundle_name)
+  if len(files) == 1:
+    return HttpResponseRedirect(files[0]['url'])
+  return render("404.mako", request, dict(uri=request.build_absolute_uri()), status=404)
+
 def assist_m(request):
   return render('assist_m.mako', request, None)
 

+ 3 - 1
tools/jison/generateParsers.js

@@ -201,7 +201,9 @@ const generateParser = parserName =>
               deleteFile(targetPath); // Remove concatenated file
             }
             console.log('Adjusting JS...');
-            const generatedJsFileName = parserConfig.target.replace('.jison', '.js').replace(/^.*\/([^/]+)$/, '$1');
+            const generatedJsFileName = parserConfig.target
+              .replace('.jison', '.js')
+              .replace(/^.*\/([^/]+)$/, '$1');
             readFile(generatedJsFileName)
               .then(contents => {
                 parserConfig

+ 19 - 20
webpack.config.js

@@ -4,27 +4,27 @@ const CleanWebpackPlugin = require('clean-webpack-plugin');
 const CleanObsoleteChunks = require('webpack-clean-obsolete-chunks');
 const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
 
-
-const path = require('path')
-const each = require('lodash/fp/each')
+const path = require('path');
+const each = require('lodash/fp/each');
 
 // https://github.com/ezhome/webpack-bundle-tracker/issues/25
 class RelativeBundleTracker extends BundleTracker {
-  convertPathChunks(chunks){
-    each(each(chunk => {
-      chunk.path = path.relative(this.options.path, chunk.path)
-    }))(chunks)
+  convertPathChunks(chunks) {
+    each(
+      each(chunk => {
+        chunk.path = path.relative(this.options.path, chunk.path);
+      })
+    )(chunks);
   }
   writeOutput(compiler, contents) {
-    if (contents.status === 'done')  {
-      this.convertPathChunks(contents.chunks)
+    if (contents.status === 'done') {
+      this.convertPathChunks(contents.chunks);
     }
 
-    super.writeOutput(compiler, contents)
+    super.writeOutput(compiler, contents);
   }
 }
 
-
 module.exports = {
   devtool: 'source-map',
   mode: 'development',
@@ -34,12 +34,9 @@ module.exports = {
   },
   resolve: {
     extensions: ['.json', '.jsx', '.js'],
-    modules: [
-      'node_modules',
-      'js'
-    ],
+    modules: ['node_modules', 'js'],
     alias: {
-      'bootstrap': __dirname + '/node_modules/bootstrap-2.3.2/js'
+      bootstrap: __dirname + '/node_modules/bootstrap-2.3.2/js'
     }
   },
   entry: {
@@ -56,9 +53,9 @@ module.exports = {
     }
   },
   output: {
-    path:  __dirname + '/desktop/core/src/desktop/static/desktop/js/bundles/hue',
+    path: __dirname + '/desktop/core/src/desktop/static/desktop/js/bundles/hue',
     filename: '[name]-bundle-[hash].js',
-    chunkFilename: '[name]-bundle-[hash].js',
+    chunkFilename: '[name]-chunk-[hash].js'
   },
   module: {
     rules: [
@@ -78,7 +75,9 @@ module.exports = {
     // new BundleAnalyzerPlugin({ analyzerPort: 9000 }),
     new CleanObsoleteChunks(),
     new CleanWebpackPlugin([__dirname + '/desktop/core/src/desktop/static/desktop/js/bundles/hue']),
-    new RelativeBundleTracker({path: '.', filename: "webpack-stats.json"}),
-    new webpack.BannerPlugin('\nLicensed to Cloudera, Inc. under one\nor more contributor license agreements.  See the NOTICE file\ndistributed with this work for additional information\nregarding copyright ownership.  Cloudera, Inc. licenses this file\nto you under the Apache License, Version 2.0 (the\n"License"); you may not use this file except in compliance\nwith the License.  You may obtain a copy of the License at\n\nhttp://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an "AS IS" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n')
+    new RelativeBundleTracker({ path: '.', filename: 'webpack-stats.json' }),
+    new webpack.BannerPlugin(
+      '\nLicensed to Cloudera, Inc. under one\nor more contributor license agreements.  See the NOTICE file\ndistributed with this work for additional information\nregarding copyright ownership.  Cloudera, Inc. licenses this file\nto you under the Apache License, Version 2.0 (the\n"License"); you may not use this file except in compliance\nwith the License.  You may obtain a copy of the License at\n\nhttp://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an "AS IS" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n'
+    )
   ]
 };