const webpack = require('webpack');
const { execSync } = require('child_process');

const gitDescribe = execSync('git describe --tags --always', { encoding: 'utf8' }).trim();

module.exports = {
  "transpileDependencies": [
    "vuetify",
    "leaflet-arrowheads"
  ],
  devServer: {
    host: "0.0.0.0",
    webSocketServer: {
      // Not found in the Webpack docs. Reference:
      // https://github.com/webpack/webpack-dev-server/blob/540c43852ea33f9cb18820e1cef05d5ddb86cc3e/lib/servers/WebsocketServer.js#L21
      options: {
        // Not found in the ws docs. Reference:
        // https://github.com/websockets/ws/blob/d8dd4852b81982fc0a6d633673968dff90985000/lib/websocket-server.js#L68
        path: "/webpack-dev-server"
      }
    },
    client: {
      webSocketURL: "auto://0.0.0.0:0/webpack-dev-server"
    },
    proxy: {
      "^/api(/|$)": {
        target: process.env.DOUGAL_API_URL || "http://127.0.0.1:3000",
      },
      "^/ws(/|$)": {
        target: process.env.DOUGAL_API_URL?.replace(/^http(s?):/i, "ws$1:") || "ws://127.0.0.1:3000",
        ws: true
      }
    }
  },
  chainWebpack: config => {
    config
      .plugin('html')
      .tap(args => {
        args[0].title = 'Dougal Web'
        return args
      })
  },
  configureWebpack: {
    resolve: {
      fallback: {
        path: require.resolve("path-browserify")
      }
    },
    plugins: [
      // Work around for Buffer is undefined:
      // https://github.com/webpack/changelog-v5/issues/10
      new webpack.ProvidePlugin({
        Buffer: ['buffer', 'Buffer'],
      }),

      new webpack.DefinePlugin({
        'process.env.DOUGAL_FRONTEND_VERSION': JSON.stringify(gitDescribe),
      }),
    ],
    module: {
      rules: [
        {
          test: /\.(woff|woff2|eot|ttf|otf)$/i,
          type: 'asset/resource'
        },
      ],
    },
  }
}
