From eb8bad1f96cf4a27a0c0ab54af1da4b339457f34 Mon Sep 17 00:00:00 2001
From: Maurizio Vitale <maurizio.vitale@alfresco.com>
Date: Tue, 17 Jan 2023 14:47:50 +0000
Subject: [PATCH] [AAE-12124] Use a function to remove js files (#8157)

* Use a function to remove js files

* try to check the asset
---
 angular.json                |  2 +-
 lib/config/index.js         | 38 +++++++++++++++++++++++++++++++++++++
 lib/config/webpack.style.js | 13 ++++++++++---
 3 files changed, 49 insertions(+), 4 deletions(-)
 create mode 100644 lib/config/index.js

diff --git a/angular.json b/angular.json
index dfc954612c..ea586cd101 100644
--- a/angular.json
+++ b/angular.json
@@ -422,7 +422,7 @@
               "options": {
               "commands": [
                   {
-                      "command": "$(npm bin)/webpack -- --config ./lib/config/webpack.style.js --progress --profile --bail && rm ./dist/libs/core/lib/prebuilt-themes/*.js"
+                      "command": "$(npm bin)/webpack -- --config ./lib/config/webpack.style.js --progress --profile --bail"
                   }
               ]
           }
diff --git a/lib/config/index.js b/lib/config/index.js
new file mode 100644
index 0000000000..5cfedfdee2
--- /dev/null
+++ b/lib/config/index.js
@@ -0,0 +1,38 @@
+function DisableOutputWebpackPlugin(options) {
+	if (options && options.test && !Array.isArray(options.test))
+		options.test = [options.test]
+
+	this.options = options
+}
+
+DisableOutputWebpackPlugin.prototype.apply = function(compiler) {
+	compiler.hooks.emit.tapAsync('DisableOutputWebpackPlugin', (compilation, callback) => {
+
+		if (this.options && this.options.test) {
+			if (Object.keys(compilation.assets).length === 0 ) {
+				throw Error ('Error: The asset pre-theme is not there!')
+			}
+			Object.keys(compilation.assets).forEach((asset) => {
+				let output = true
+				this.options.test.some((regex) => {
+					if (asset.match(regex) != null) {
+						output = false
+						return true
+					}
+					return false
+				})
+
+				if (!output)
+					delete compilation.assets[asset]
+			});
+		} else {
+			Object.keys(compilation.assets).forEach((asset) => {
+				delete compilation.assets[asset]
+			})
+		}
+
+		callback();
+	});
+};
+
+module.exports = DisableOutputWebpackPlugin;
\ No newline at end of file
diff --git a/lib/config/webpack.style.js b/lib/config/webpack.style.js
index b6928ef51d..7cc117eb06 100644
--- a/lib/config/webpack.style.js
+++ b/lib/config/webpack.style.js
@@ -1,4 +1,5 @@
 const MiniCssExtractPlugin = require('mini-css-extract-plugin');
+const DisableOutputWebpackPlugin = require('./index');
 const path = require("path");
 const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
 
@@ -24,8 +25,6 @@ module.exports = {
 
     output: {
         path: path.resolve(__dirname, '../../dist/libs/core/lib/prebuilt-themes/'),
-        filename: '[name].js',
-        publicPath: '/dist'
     },
 
     module: {
@@ -34,5 +33,13 @@ module.exports = {
             use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"]
         }]
     },
-    plugins: [new MiniCssExtractPlugin()]
+    plugins: [
+        new MiniCssExtractPlugin({
+            filename: "[name].css",
+            chunkFilename: "[id].css"
+        }),
+        new DisableOutputWebpackPlugin({
+            test: /\.js$/,
+        })
+    ]
 };