From e065666ccdba30e4377edaf4c5f72c3907ee07ca Mon Sep 17 00:00:00 2001
From: pkupczyk <piotr.kupczyk@id.ethz.ch>
Date: Sat, 14 Aug 2021 10:13:51 +0200
Subject: [PATCH] SSDM-11506 : Improve initial loading time of NG UI - offer
 both a bundle with and without a minification

---
 .../public/resources/api/v3/.gitignore        |  4 +--
 .../resource/js/v3/build.sh                   |  6 ++--
 .../js/v3/config.bundle.min.template.js       | 31 +++++++++++++++++++
 .../resource/js/v3/r.config.template.js       |  4 +--
 4 files changed, 38 insertions(+), 7 deletions(-)
 create mode 100644 openbis_standard_technologies/resource/js/v3/config.bundle.min.template.js

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/api/v3/.gitignore b/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/api/v3/.gitignore
index de4bae125ed..6f336601a44 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/api/v3/.gitignore
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/api/v3/.gitignore
@@ -1,2 +1,2 @@
-config.bundle.js
-openbis.bundle.js
\ No newline at end of file
+config.bundle.*
+openbis.bundle.*
\ No newline at end of file
diff --git a/openbis_standard_technologies/resource/js/v3/build.sh b/openbis_standard_technologies/resource/js/v3/build.sh
index c4e0122053b..86ba22cd4e7 100755
--- a/openbis_standard_technologies/resource/js/v3/build.sh
+++ b/openbis_standard_technologies/resource/js/v3/build.sh
@@ -22,11 +22,13 @@ find ./as ./dss ./util -name "*.js" | sed 's/^.\//"/g' | sed 's/.js$/",/g' > $TE
 cd $CURRENT_DIR
 cat r.config.template.js | sed -e '\|__FILES__|{' -e "r $TEMP_DIR/files.js" -e 'd' -e '}' > $TEMP_DIR/r.config.js
 cat config.bundle.template.js | sed -e '\|__FILES__|{' -e "r $TEMP_DIR/files.js" -e 'd' -e '}' > $TEMP_DIR/config.bundle.js
+cat config.bundle.min.template.js | sed -e '\|__FILES__|{' -e "r $TEMP_DIR/files.js" -e 'd' -e '}' > $TEMP_DIR/config.bundle.min.js
 
 # create a JS bundle using NodeJS binary installed by NodeJS gradle plugin
-$OPENBIS_STANDARD_TECHNOLOGIES_DIR/node/nodejs/node-*/bin/node r.js -o $TEMP_DIR/r.config.js baseUrl=$V3_DIR
+$OPENBIS_STANDARD_TECHNOLOGIES_DIR/node/nodejs/node-*/bin/node r.js -o $TEMP_DIR/r.config.js baseUrl=$V3_DIR optimize=none out=$TEMP_DIR/openbis.bundle.js
+$OPENBIS_STANDARD_TECHNOLOGIES_DIR/node/nodejs/node-*/bin/node r.js -o $TEMP_DIR/r.config.js baseUrl=$V3_DIR optimize=uglify out=$TEMP_DIR/openbis.bundle.min.js
 
 # copy relevant files to the V3 public folder
-cp $TEMP_DIR/config.bundle.js $TEMP_DIR/openbis.bundle.js $V3_DIR
+cp $TEMP_DIR/config.bundle.js $TEMP_DIR/config.bundle.min.js $TEMP_DIR/openbis.bundle.js $TEMP_DIR/openbis.bundle.min.js $V3_DIR
 
 popd > /dev/null
\ No newline at end of file
diff --git a/openbis_standard_technologies/resource/js/v3/config.bundle.min.template.js b/openbis_standard_technologies/resource/js/v3/config.bundle.min.template.js
new file mode 100644
index 00000000000..e731f42ffe2
--- /dev/null
+++ b/openbis_standard_technologies/resource/js/v3/config.bundle.min.template.js
@@ -0,0 +1,31 @@
+var require = (function () {
+  var getBaseUrl = function () {
+    // To find where the V3 API has been loaded from we scan the script tags until finding this file
+    // This way is possible to load the V3 API from a separate domain
+    var scripts = document.getElementsByTagName('script')
+    var baseUrl = null
+    for (var sIdx = 0; sIdx < scripts.length; sIdx++) {
+      var src = scripts[sIdx].src
+      var substringEnd = -1
+      if ((substringEnd = src.indexOf('/api/v3/config.bundle.min.js')) > -1) {
+        baseUrl = src.substring(0, substringEnd + '/api/v3'.length)
+      }
+    }
+    return baseUrl
+  }
+
+  return {
+    baseUrl: getBaseUrl(),
+    bundles: {
+      'openbis.bundle.min': [
+        'jquery',
+        'stjs',
+        'underscore',
+        'moment',
+        //
+        __FILES__
+        //
+      ]
+    }
+  }
+})()
diff --git a/openbis_standard_technologies/resource/js/v3/r.config.template.js b/openbis_standard_technologies/resource/js/v3/r.config.template.js
index 8e46ef030a6..fa748f0093f 100644
--- a/openbis_standard_technologies/resource/js/v3/r.config.template.js
+++ b/openbis_standard_technologies/resource/js/v3/r.config.template.js
@@ -18,7 +18,5 @@
     underscore: {
       exports: '_'
     }
-  },
-  out: 'openbis.bundle.js',
-  optimize: 'none'
+  }
 })
-- 
GitLab