From 3052a60d57e1455a159b7ed87400fdcdd7f4757d Mon Sep 17 00:00:00 2001
From: David Beniamine <david.beniamine@tetras-libre.fr>
Date: Thu, 1 Dec 2022 00:39:39 +0100
Subject: [PATCH] WIP cleaner build using webpack hooks

---
 Dockerfile.devserver |  2 +-
 build_deps.sh        | 18 ------------------
 package-lock.json    | 15 +--------------
 package.json         |  4 +---
 webpack.config.js    | 42 +++++++++++++++++++++++++++++++-----------
 5 files changed, 34 insertions(+), 47 deletions(-)
 delete mode 100755 build_deps.sh

diff --git a/Dockerfile.devserver b/Dockerfile.devserver
index 4f71e92..a17d738 100644
--- a/Dockerfile.devserver
+++ b/Dockerfile.devserver
@@ -5,4 +5,4 @@ RUN chown node:node /opt
 EXPOSE 9000
 WORKDIR /opt
 USER node
-CMD npm install && npm run serve
+CMD npm run serve
diff --git a/build_deps.sh b/build_deps.sh
deleted file mode 100755
index 51a21ad..0000000
--- a/build_deps.sh
+++ /dev/null
@@ -1,18 +0,0 @@
-#!/bin/bash
-declare -A SUBMODULES=(
-    ['mirador-video-annotation']="build:es"
-    ['annotations-plugin']="build"
-)
-
-run_on_modules(){
-    for mod in "${!SUBMODULES[@]}"; do
-        echo -e "\t$mod"
-        cd $mod
-        npm run ${SUBMODULES[${mod}]}
-        cd ..
-    done
-}
-
-npm install
-echo "Compiling submodules"
-run_on_modules build
diff --git a/package-lock.json b/package-lock.json
index d36b3cd..d841e2a 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -22,8 +22,7 @@
       "devDependencies": {
         "webpack": "^4.43.0",
         "webpack-cli": "^4.3.12",
-        "webpack-dev-server": "^4.11.1",
-        "webpack-shell-plugin": "^0.5.0"
+        "webpack-dev-server": "^4.11.1"
       }
     },
     "annotations-plugin": {
@@ -24917,12 +24916,6 @@
         "node": ">=10.0.0"
       }
     },
-    "node_modules/webpack-shell-plugin": {
-      "version": "0.5.0",
-      "resolved": "https://registry.npmjs.org/webpack-shell-plugin/-/webpack-shell-plugin-0.5.0.tgz",
-      "integrity": "sha512-BJMcVgXzA7Yc/Dq9JFj/wvquYG5qP3lgmCpMYzhP1a+LxZ/z3TbqKJ3W2xTkzQqBeYsgTdOcSdZ/+qYoFBhMhA==",
-      "dev": true
-    },
     "node_modules/webpack-sources": {
       "version": "1.4.3",
       "license": "MIT",
@@ -42380,12 +42373,6 @@
         "wildcard": "^2.0.0"
       }
     },
-    "webpack-shell-plugin": {
-      "version": "0.5.0",
-      "resolved": "https://registry.npmjs.org/webpack-shell-plugin/-/webpack-shell-plugin-0.5.0.tgz",
-      "integrity": "sha512-BJMcVgXzA7Yc/Dq9JFj/wvquYG5qP3lgmCpMYzhP1a+LxZ/z3TbqKJ3W2xTkzQqBeYsgTdOcSdZ/+qYoFBhMhA==",
-      "dev": true
-    },
     "webpack-sources": {
       "version": "1.4.3",
       "requires": {
diff --git a/package.json b/package.json
index 95e52a4..1542dd7 100644
--- a/package.json
+++ b/package.json
@@ -5,7 +5,6 @@
   "private": true,
   "scripts": {
     "build": "webpack --config webpack.config.js",
-    "preserve": "npm run build",
     "serve": "webpack serve --config webpack.config.js"
   },
   "author": "",
@@ -24,7 +23,6 @@
   "devDependencies": {
     "webpack": "^4.43.0",
     "webpack-cli": "^4.3.12",
-    "webpack-dev-server": "^4.11.1",
-    "webpack-shell-plugin": "^0.5.0"
+    "webpack-dev-server": "^4.11.1"
   }
 }
diff --git a/webpack.config.js b/webpack.config.js
index c0519ef..9314ee7 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -1,7 +1,7 @@
 const path = require('path');
 const webpack = require('webpack');
 const Dotenv = require('dotenv-webpack');
-const WebpackShellPlugin = require('webpack-shell-plugin');
+const child_process = require('child_process');
 
 module.exports = {
     mode: process.env.WEBPACK_MODE,
@@ -13,8 +13,7 @@ module.exports = {
     },
     devServer: {
       hot: true,
-      liveReload: true,
-      watchFiles: ['src/**/*', '*/src/**/*'],
+      watchFiles: ['src/**/*.js'],
       client: {
           logging: 'verbose',
           overlay: true,
@@ -35,13 +34,34 @@ module.exports = {
       port: 9000
     },
     plugins: [
-      new webpack.IgnorePlugin({
-        /* cf https://gitlab.tetras-libre.fr/iiif/mirador-video-annotation/-/blob/annotation-on-video/webpack.config.js#L42 */
-        resourceRegExp: /@blueprintjs\/(core|icons)/, // ignore optional UI framework dependencies
-      }),
-    new WebpackShellPlugin({
-      onBuildStart:['./build_deps.sh']
-    }),
-      new Dotenv()
+        new webpack.IgnorePlugin({
+            /* cf https://gitlab.tetras-libre.fr/iiif/mirador-video-annotation/-/blob/annotation-on-video/webpack.config.js#L42 */
+            resourceRegExp: /@blueprintjs\/(core|icons)/, // ignore optional UI framework dependencies
+        }),
+        {
+            apply: (compiler) => {
+                function spawn(cmd, args, cwd) {
+                    let ret = child_process.spawnSync(cmd, args, {cwd: path.resolve(cwd)});
+                    if (ret.output) {
+                        console.log(ret.output.toString());
+                    }
+                    if (ret.status !== 0) {
+                        throw Error('Error while running ""'+cmd+' '+args);
+                    }
+                }
+                // TODO this hook runs again and again in watch mode, use something more restrictive
+                compiler.hooks.compile.tap("compile workspaces", () => {
+                    console.log('Compiling workspaces');
+                    console.log('annotations plugin');
+                    // TODO Use values from package.json
+                    spawn("rm", ["-f", ".babelrc"], 'annotations-plugin');
+                    spawn("npm", ["run", "build"], 'annotations-plugin');
+                    console.log('mirador');
+                    spawn("npm", ["run", "build:es"], 'mirador-video-annotation');
+                    console.log('Workspaces compiled successfully');
+                });
+            }
+        },
+        new Dotenv()
     ]
 };
-- 
GitLab