import TerserPlugin from "terser-webpack-plugin" ; import path from "path" ; import webpack , { Configuration , RuleSetRule } from "webpack" ; import { Options as TsLoaderOptions } from "ts-loader" ; import { mapValues } from "remeda" ; import { merge as webpackMerge } from "webpack-merge" ; import { BuildEnvVars } from "./model" ; import { CONSOLE_LOG } from "scripts/lib" ; export const ENVIRONMENT = ( ( ) => { const NODE_ENV = process . env . NODE_ENV as Exclude < BuildEnvVars [ "BUILD_ENVIRONMENT" ] , "production" > | undefined ; return NODE_ENV === "development" || NODE_ENV === "test" || NODE_ENV === "e2e" ? NODE_ENV : "production" ; } ) ( ) ; export const ENVIRONMENT_STATE = { production : ENVIRONMENT === "production" , development : ENVIRONMENT === "development" , test : ENVIRONMENT === "test" , e2e : ENVIRONMENT === "e2e" , } ; export const rootRelativePath = ( ... value ) : string => { return path . join ( process . cwd ( ) , ... value ) ; } ; export const srcRelativePath = ( ... value ) : string => { return rootRelativePath ( "./src" , ... value ) ; } ; export const outputRelativePath = ( ... value ) : string => { return rootRelativePath ( ENVIRONMENT_STATE . development ? "./app-dev" : "./app" , ... value ) ; } ; const definePluginValue = mapValues ( { BUILD_ENVIRONMENT : ENVIRONMENT , ... ( ( ) : StrictOmit < BuildEnvVars , "BUILD_ENVIRONMENT" > => { return { BUILD_DISABLE_CLOSE_TO_TRAY_FEATURE : process . env . ELECTRON_MAIL_BUILD_DISABLE_CLOSE_TO_TRAY_FEATURE ?? false , BUILD_DISABLE_START_HIDDEN_FEATURE : process . env . ELECTRON_MAIL_BUILD_DISABLE_START_HIDDEN_FEATURE ?? false , BUILD_START_MAXIMIZED_BY_DEFAULT : process . env . ELECTRON_MAIL_BUILD_START_MAXIMIZED_BY_DEFAULT ?? false , } ; } ) ( ) , } , ( value ) => JSON . stringify ( value ) ) ; CONSOLE_LOG ( "Injected environment variables:" , definePluginValue ) ; export function buildBaseConfig ( ... [ config ] ) { return webpackMerge ( { watch : Boolean ( Number ( process . env . WEBPACK_ENV_WATCH ) ) , mode : "production" , devtool : false , output : { path : outputRelativePath ( ) , } , plugins : [ new webpack . DefinePlugin ( definePluginValue ) , ] , resolve : { extensions : [ "*" , ".js" , ".ts" ] , alias : { "src" : srcRelativePath ( ) , "package.json" : rootRelativePath ( "package.json" ) , } , } , optimization : { chunkIds : "named" , moduleIds : "named" , minimize : true , minimizer : [ new TerserPlugin ( { terserOptions : { mangle : false , compress : { dead_code : true , defaults : false , unused : true , } , format : { beautify : true , comments : false , indent_level : 2 , } , } , } ) , ] , } , node : { __filename : true , } , } , config ) ; } export function typescriptLoaderRule ( { tsConfigFile } ) { const options = { configFile : tsConfigFile , } ; return { test : /\.ts$/ , use : { loader : "ts-loader" , options , } , } ; }