Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor webpack configuration to improve library exports #1565

12 changes: 4 additions & 8 deletions clientlibs/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
94 changes: 25 additions & 69 deletions clientlibs/js/webpack.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path = require('path');
import webpack = require('webpack');
import packageJson = require('./package.json');
const path = require('path');
const webpack = require('webpack');
const packageJson = require('./package.json');

KD1712 marked this conversation as resolved.
Show resolved Hide resolved
const version = packageJson.version.split('.')[0];

Expand Down Expand Up @@ -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' }),
];
Loading