From 279d9042d249802309d77f764eb6264bfbe87ab0 Mon Sep 17 00:00:00 2001 From: Khanjan Dalwadi <80506682+KD1712@users.noreply.github.com> Date: Fri, 6 Sep 2024 18:53:08 +0530 Subject: [PATCH] Refactor webpack configuration to improve library exports (#1565) * Refactor webpack configuration to improve library exports * Revert default export of UpgradeClient and removed library name to get direct access to other class and types export * Made default export for UpgradeClient * Refactored webpack configs into a single function for all * Updated file import --------- Co-authored-by: khanjan Co-authored-by: khanjan Co-authored-by: Pratik Prajapati <33730817+ppratikcr7@users.noreply.github.com> Co-authored-by: danoswaltCL <97542869+danoswaltCL@users.noreply.github.com> --- clientlibs/js/src/index.ts | 12 ++--- clientlibs/js/webpack.config.ts | 88 +++++++++------------------------ 2 files changed, 26 insertions(+), 74 deletions(-) diff --git a/clientlibs/js/src/index.ts b/clientlibs/js/src/index.ts index a4fc70c2ce..513dc6bc08 100644 --- a/clientlibs/js/src/index.ts +++ b/clientlibs/js/src/index.ts @@ -2,11 +2,7 @@ import UpgradeClient from './UpGradeClient/UpgradeClient'; import Assignment from './Assignment/Assignment'; import { UpGradeClientEnums, UpGradeClientInterfaces, UpGradeClientRequests } from './types'; import { MARKED_DECISION_POINT_STATUS } from 'upgrade_types'; -export { - UpgradeClient, - Assignment, - UpGradeClientEnums, - UpGradeClientInterfaces, - UpGradeClientRequests, - MARKED_DECISION_POINT_STATUS, -}; + +export default UpgradeClient; + +export { Assignment, UpGradeClientEnums, UpGradeClientInterfaces, UpGradeClientRequests, MARKED_DECISION_POINT_STATUS }; diff --git a/clientlibs/js/webpack.config.ts b/clientlibs/js/webpack.config.ts index 685609eec1..242588310d 100644 --- a/clientlibs/js/webpack.config.ts +++ b/clientlibs/js/webpack.config.ts @@ -29,80 +29,36 @@ const generalConfiguration = { ], }; -const browser = { +const createConfig = ( + target: string, + outputPath: string, + useCustomHttpClient: boolean, + isBrowser: boolean, + externals = {} +) => ({ ...generalConfiguration, + target, output: { - filename: 'index.js', - path: path.resolve(__dirname, 'dist/browser'), + library: 'UpgradeClient', + globalObject: 'this', libraryTarget: 'umd', - library: 'upgrade-client-lib', - }, - plugins: [ - new webpack.DefinePlugin({ - API_VERSION: version, - USE_CUSTOM_HTTP_CLIENT: JSON.stringify(false), - IS_BROWSER: JSON.stringify(true), - }), - ], -}; - -const node = { - ...generalConfiguration, - target: 'node', - output: { + libraryExport: 'default', filename: 'index.js', - path: path.resolve(__dirname, 'dist/node'), - libraryTarget: 'umd', - library: 'upgrade-client-lib', + path: path.resolve(__dirname, outputPath), }, + externals, plugins: [ new webpack.DefinePlugin({ API_VERSION: version, - USE_CUSTOM_HTTP_CLIENT: JSON.stringify(false), - IS_BROWSER: JSON.stringify(false), + USE_CUSTOM_HTTP_CLIENT: JSON.stringify(useCustomHttpClient), + IS_BROWSER: JSON.stringify(isBrowser), }), ], -}; - -const browserLite = { - ...generalConfiguration, - output: { - filename: 'index.js', - path: path.resolve(__dirname, 'dist/browser-lite'), - libraryTarget: 'umd', - library: 'upgrade-client-lib', - }, - externals: { - axios: 'axios', - }, - plugins: [ - new webpack.DefinePlugin({ - API_VERSION: version, - USE_CUSTOM_HTTP_CLIENT: JSON.stringify(true), - IS_BROWSER: JSON.stringify(true), - }), - ], -}; - -const nodeLite = { - ...generalConfiguration, - target: 'node', - output: { - filename: 'index.js', - path: path.resolve(__dirname, 'dist/node-lite'), - libraryTarget: 'umd', - library: 'upgrade-client-lib', - }, - externals: { - axios: 'axios', - }, - plugins: [ - new webpack.DefinePlugin({ - API_VERSION: version, - USE_CUSTOM_HTTP_CLIENT: JSON.stringify(true), - IS_BROWSER: JSON.stringify(false), - }), - ], -}; +}); -module.exports = [browser, node, browserLite, nodeLite]; +module.exports = [ + createConfig(undefined, 'dist/browser', false, true), + createConfig('node', 'dist/node', false, false), + createConfig(undefined, 'dist/browser-lite', true, true, { axios: 'axios' }), + createConfig('node', 'dist/node-lite', true, false, { axios: 'axios' }), +];