-
Notifications
You must be signed in to change notification settings - Fork 60
/
webpack.common.js
198 lines (184 loc) · 5.83 KB
/
webpack.common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const webpack = require("webpack");
const path = require("path");
const dotenv = require("dotenv").config();
const tailwindcss = require("tailwindcss");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
const { sentryWebpackPlugin } = require("@sentry/webpack-plugin");
const AddReactDisplayNamePlugin = require("babel-plugin-add-react-displayname");
const getEnvVariable = (variable, defaultValue) =>
process.env[variable] ?? defaultValue;
const sdkEnv = getEnvVariable("sdkEnv", "local");
const enableLogging = getEnvVariable("enableLogging", "false") === "true";
const envSdkUrl = getEnvVariable("ENV_SDK_URL", "");
const envBackendUrl = getEnvVariable("ENV_BACKEND_URL", "");
const envLoggingUrl = getEnvVariable("ENV_LOGGING_URL", "");
const repoVersion = require("./package.json").version;
const majorVersion = "v" + repoVersion.split(".")[0];
const repoName = require("./package.json").name;
const repoPublicPath =
sdkEnv === "local" ? "" : `/web/${repoVersion}/${majorVersion}`;
const getSdkUrl = (env, customUrl) => {
if (customUrl) return customUrl;
const urls = {
prod: "https://checkout.hyperswitch.io",
sandbox: "https://beta.hyperswitch.io",
integ: "https://dev.hyperswitch.io",
local: "http://localhost:9050",
};
return urls[env] || urls.local;
};
const sdkUrl = getSdkUrl(sdkEnv, envSdkUrl);
const getEnvironmentDomain = (prodDomain, integDomain, defaultDomain) => {
switch (sdkEnv) {
case "prod":
return prodDomain;
case "integ":
return integDomain;
default:
return defaultDomain;
}
};
const backendDomain = getEnvironmentDomain("checkout", "dev", "beta");
const confirmDomain = getEnvironmentDomain("api", "integ-api", "sandbox");
const backendEndPoint =
envBackendUrl || `https://${backendDomain}.hyperswitch.io/api`;
const confirmEndPoint =
envBackendUrl || `https://${confirmDomain}.hyperswitch.io`;
const logEndpoint = envLoggingUrl;
const loggingLevel = "DEBUG";
const maxLogsPushedPerEventName = 100;
module.exports = (publicPath = "auto") => {
const entries = {
app: "./index.js",
HyperLoader: "./src/hyper-loader/HyperLoader.bs.js",
};
const plugins = [
new MiniCssExtractPlugin(),
new CopyPlugin({
patterns: [{ from: "public" }],
}),
new webpack.DefinePlugin({
repoName: JSON.stringify(repoName),
repoVersion: JSON.stringify(repoVersion),
publicPath: JSON.stringify(repoPublicPath),
sdkUrl: JSON.stringify(sdkUrl),
backendEndPoint: JSON.stringify(backendEndPoint),
confirmEndPoint: JSON.stringify(confirmEndPoint),
logEndpoint: JSON.stringify(logEndpoint),
sentryDSN: JSON.stringify(process.env.SENTRY_DSN),
sentryScriptUrl: JSON.stringify(process.env.SENTRY_SCRIPT_URL),
enableLogging: enableLogging,
loggingLevel: JSON.stringify(loggingLevel),
maxLogsPushedPerEventName: JSON.stringify(maxLogsPushedPerEventName),
}),
new HtmlWebpackPlugin({
inject: false,
template: "./public/build.html",
}),
new HtmlWebpackPlugin({
// Also generate a test.html
inject: false,
filename: "fullscreenIndex.html",
template: "./public/fullscreenIndexTemplate.html",
}),
];
if (process.env.NODE_ENV === "production") {
plugins.push(
new BundleAnalyzerPlugin({
analyzerMode: "static",
reportFilename: "bundle-report.html",
openAnalyzer: false,
})
);
}
if (
process.env.SENTRY_AUTH_TOKEN &&
process.env.IS_SENTRY_ENABLED === "true"
) {
plugins.push(
sentryWebpackPlugin({
org: "sentry",
project: "hyperswitch-react-sdk",
authToken: process.env.SENTRY_AUTH_TOKEN,
url: process.env.SENTRY_URL,
release: {
name: "0.2",
uploadLegacySourcemaps: {
paths: ["dist"],
},
},
})
);
}
return {
mode: sdkEnv === "local" ? "development" : "production",
devtool: sdkEnv === "local" ? "eval-source-map" : "source-map",
output: {
path:
sdkEnv && sdkEnv !== "local"
? path.resolve(__dirname, "dist", sdkEnv)
: path.resolve(__dirname, "dist"),
clean: true,
publicPath: `${repoPublicPath}/`,
},
optimization:
sdkEnv === "local"
? {}
: {
sideEffects: true,
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
drop_console: false,
},
mangle: {
keep_fnames: true, // Prevent function names from being mangled
keep_classnames: true, // Prevent class names from being mangled
},
},
}),
],
},
plugins,
module: {
rules: [
{
test: /\.css$/i,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [[tailwindcss("./tailwind.config.js")]],
},
},
},
],
},
{
test: /\.jsx?$/, // Matches both .js and .jsx files
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env", "@babel/preset-react"],
plugins: [AddReactDisplayNamePlugin],
},
},
},
],
},
entry: entries,
resolve: {
extensions: [".js", ".jsx"],
},
};
};