import { Configuration , ConfigurationFactory , ContextReplacementPlugin , IgnorePlugin } from 'webpack' ; import GitRevisionPlugin from 'git-revision-webpack-plugin' ; import WrapperWebpackPlugin from 'wrapper-webpack-plugin' ; import WebpackMerge from 'webpack-merge' ; import { parse , resolve } from 'path' ; const gitRevisionPlugin = new GitRevisionPlugin ( { commithashCommand : 'rev-parse --short HEAD' } ) ; type CustomConfiguration = Omit < Configuration , 'entry' > ; const MergeConfigurations = ( name , path , config = { } ) : ConfigurationFactory => ( env , argv ) => { const dirname = process . cwd ( ) ; const entry = { [ name ] : path || `${ dirname }/${ name }.ts` } ; const { npm_package_name = '' , npm_package_version = '' } = typeof env === 'string' ? { } : env ; console . log ( 'BUNDLE:' , resolve ( entry [ name ] ) ) ; return WebpackMerge ( { entry , output : { path : `${ dirname }/dist` , filename : `${ name }.js` , libraryTarget : 'commonjs' } , module : { rules : [ { test : /\.ts$/ , loader : 'ts-loader' , options : { onlyCompileBundledFiles : true } } ] } , plugins : [ ... [ 'app-root-path' , 'express' , 'google-spreadsheet' , 'optional' , 'typeorm' ] . map ( module => new ContextReplacementPlugin ( new RegExp ( `node_modules/${ module }` ) , ( data ) => { data . dependencies = data . dependencies . map ( dependency => { delete dependency . critical ; return dependency ; } ) ; } ) ) , ... [ 'pg-native' , 'hiredis' , ... [ 'bufferutil' , 'utf-8-validate' ] , ... [ '@sap/hdbext' , 'mongodb' , 'mssql' , 'mysql' , 'mysql2' , 'oracledb' , 'pg-query-stream' , 'react-native-sqlite-storage' , 'sql.js' , 'sqlite3' , 'typeorm-aurora-data-api-driver' ] ] . map ( dependency => new IgnorePlugin ( new RegExp ( `^${ dependency }$` ) ) ) , new WrapperWebpackPlugin ( { header : ( ) => `Object.assign(process.env, {
              npm_package_name: '${ npm_package_name }',
              npm_package_version: '${ npm_package_version }',
              npm_package_git_branch: '${ gitRevisionPlugin . branch ( ) }',
              npm_package_git_commit: '${ gitRevisionPlugin . commithash ( ) }',
              npm_package_build_date: '${ new Date ( ) . toISOString ( ) }'
            });` } ) ] , resolve : { extensions : [ '.ts' , '.js' ] } , externals : { sharp : 'commonjs sharp' } , target : 'node' , stats : { all : false , assets : true , errors : true , warnings : true } } , config ) ; } ; type WebpackConfigurationBuilderOptions = Partial < { name : string ; } > ; class WebpackConfigurationBuilder { private name : string ; constructor ( private path , { name } = { } ) { this . name = name || parse ( path ) . name ; } public UsingDefaultConfig ( ) { return MergeConfigurations ( this . name , this . path ) ; } public UsingCustomConfig ( config ) { return MergeConfigurations ( this . name , this . path , config ) ; } } export default { Bundle : ( path , options ? ) => new WebpackConfigurationBuilder ( path , options ) } ;