production-taskbar-client / webpack / electron.webpack.js
electron.webpack.js
Raw
/* eslint-disable import/no-extraneous-dependencies */
const path = require("path");
const { emptyDirSync } = require("fs-extra");
const NodeExternals = require("webpack-node-externals");
const { EnvironmentPlugin } = require("webpack");
const dotenv = require("dotenv");
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
const ProgressPlugin = require("progress-webpack-plugin");
const log = require("electron-log");

const rootPath = path.resolve(__dirname, "..");
const distPath = path.resolve(rootPath, "dist");
const packagesPath = path.resolve(rootPath, "packages");

const isDev = process.env.NODE_ENV === "development";

module.exports = [
  {
    resolve: {
      extensions: [".js", ".jsx"],
    },
    mode: isDev ? "development" : "production",
    devtool: isDev ? "eval-source-map" : false,
    entry: { main: path.resolve(rootPath, "src/main", "main.js") },
    externals: [new NodeExternals()],
    target: "electron-main",
    module: {
      rules: [
        {
          test: /\.(js|jsx)$/,
          exclude: /node_modules/,
          use: {
            loader: "babel-loader",
            options: {
              presets: [
                [
                  "@babel/preset-react",
                  {
                    modules: "esnext",
                    targets: {
                      electron: "12.0.4",
                    },
                    browserslist: ["Electron"],
                  },
                ],
              ],
            },
          },
        },
      ],
    },
    node: {
      __dirname: false,
    },
    output: {
      path: distPath,
      filename: "[name].js",
    },
    plugins: [
      new ProgressPlugin({
        identifier: "electron-main",
        onStart: () => {
          if (!isDev) {
            // eslint-disable-next-line no-console
            log.info(`[electron.webpack] Cleanup dist and packages folders...`);
            emptyDirSync(distPath);
            emptyDirSync(packagesPath);
          }
        },
      }),
      new BundleAnalyzerPlugin({
        reportFilename: "main_analyze.html",
        openAnalyzer: false,
        analyzerMode: isDev ? "static" : "disabled",
      }),
      new EnvironmentPlugin({
        NODE_ENV: isDev ? "development" : "production",
        ...dotenv.config({
          path: isDev ? ".env.development" : ".env.production",
        }).parsed,
      }),
    ],
  },
  // PRELOAD.JS
  {
    mode: isDev ? "development" : "production",
    devtool: isDev ? "eval-source-map" : false,
    entry: path.resolve(rootPath, "src/main", "preload.js"),
    target: "electron-preload",
    output: {
      path: distPath,
      filename: "preload.js",
    },
    plugins: [
      new ProgressPlugin({ identifier: "electron-preload" }),
      new BundleAnalyzerPlugin({
        reportFilename: "preload_analyze.html",
        openAnalyzer: false,
        analyzerMode: isDev ? "static" : "disabled",
      }),
    ],
  },
  // WINEVENT worker
  {
    mode: isDev ? "development" : "production",
    devtool: isDev ? "eval-source-map" : false,
    entry: path.resolve(rootPath, "src/main/workers", "winEventHook.js"),
    target: "electron-main",
    output: {
      path: path.resolve(rootPath, "dist/workers"),
      filename: "winEventHook.js",
    },
    plugins: [
      new ProgressPlugin({ identifier: "winevent-worker" }),
      new BundleAnalyzerPlugin({
        reportFilename: "winEventHook_analyze.html",
        openAnalyzer: false,
        analyzerMode: isDev ? "static" : "disabled",
      }),
    ],
    externals: {
      "@inigolabs/ref-napi": "require('@inigolabs/ref-napi')",
      "@inigolabs/ffi-napi": "require('@inigolabs/ffi-napi')",
    },
  },
  // Autoswitch worker
  {
    mode: isDev ? "development" : "production",
    devtool: isDev ? "eval-source-map" : false,
    entry: path.resolve(rootPath, "src/main/workers", "autoswitchWorker.js"),
    target: "electron-main",
    output: {
      path: path.resolve(rootPath, "dist/workers"),
      filename: "autoswitchWorker.js",
    },
    plugins: [
      new ProgressPlugin({ identifier: "autoswitch-worker" }),
      new BundleAnalyzerPlugin({
        reportFilename: "autoswitchWorker_analyze.html",
        openAnalyzer: false,
        analyzerMode: isDev ? "static" : "disabled",
      }),
    ],
    externals: {
      "@inigolabs/ref-napi": "require('@inigolabs/ref-napi')",
      "@inigolabs/ffi-napi": "require('@inigolabs/ffi-napi')",
    },
  },
];