-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.ost-scripts.js
259 lines (224 loc) · 8.08 KB
/
webpack.ost-scripts.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const SriPlugin = require('webpack-subresource-integrity');
const webpack = require('webpack');
const fs = require('fs');
String.prototype.trimRight = function(charlist) {
if (charlist === undefined)
charlist = "\s";
return this.replace(new RegExp("[" + charlist + "]+$"), "");
};
// Define variables that need to be available in Ost JS files.
const webpackVariables = {};
//region - validations
if ( !process.env.OST_BROWSER_SDK_BASE_URL ) {
throw "||| BUILD FAILED!!! |||\n||| ATTENTION NEEDED|||\n" + "Environemnt Variable OST_BROWSER_SDK_BASE_URL is not set.\n" + "||| BUILD FAILED!!! |||\n";
}
let OST_BROWSER_SDK_PLATFORM_API_ORIGIN = process.env.OST_BROWSER_SDK_PLATFORM_API_ORIGIN;
if ( !OST_BROWSER_SDK_PLATFORM_API_ORIGIN ) {
OST_BROWSER_SDK_PLATFORM_API_ORIGIN = "https://api.ost.com";
console.log("!! OST_BROWSER_SDK_PLATFORM_API_ORIGIN not specified.");
console.log("-- OST_BROWSER_SDK_PLATFORM_API_ORIGIN set to", OST_BROWSER_SDK_PLATFORM_API_ORIGIN);
}
webpackVariables.WP_OST_BROWSER_SDK_PLATFORM_API_ORIGIN = JSON.stringify(OST_BROWSER_SDK_PLATFORM_API_ORIGIN);
if ( !process.env.TOKEN_IDS ) {
throw "||| BUILD FAILED!!! |||\n||| ATTENTION NEEDED|||\n" + "Environemnt Variable TOKEN_ID is not set.\n" + "||| BUILD FAILED!!! |||\n";
}
let OST_BROWSER_SDK_MAIN_DOMAIN = process.env.OST_BROWSER_SDK_MAIN_DOMAIN;
if ( !OST_BROWSER_SDK_MAIN_DOMAIN ) {
OST_BROWSER_SDK_MAIN_DOMAIN = "ostwalletsdk.com";
console.log("!! OST_BROWSER_SDK_MAIN_DOMAIN not specified.");
console.log("-- OST_BROWSER_SDK_MAIN_DOMAIN set to", OST_BROWSER_SDK_MAIN_DOMAIN);
}
webpackVariables.WP_OST_BROWSER_SDK_MAIN_DOMAIN = JSON.stringify(OST_BROWSER_SDK_MAIN_DOMAIN);
let OST_BROWSER_SDK_VERSION = process.env.OST_BROWSER_SDK_VERSION;
if ( typeof OST_BROWSER_SDK_VERSION !== 'string') {
const packageJson = require("./package.json");
OST_BROWSER_SDK_VERSION = `v-${packageJson.version}`;
console.log("!! OST_BROWSER_SDK_VERSION not specified.");
console.log("-- OST_BROWSER_SDK_VERSION set to", OST_BROWSER_SDK_VERSION);
}
webpackVariables.WP_OST_BROWSER_SDK_VERSION = JSON.stringify( OST_BROWSER_SDK_VERSION );
const TOKEN_IDS = process.env.TOKEN_IDS.split(",");
const mappyConfigs = [];
const mappyConfigBuilder = require("./webpack-mappy-config.js");
function buildTokenInfo( tokenIds ) {
if (!( tokenIds instanceof Array )) {
tokenIds = tokenIds.split(",");
}
let len = tokenIds.length;
while( len-- ) {
let thisTokenId = tokenIds[len];
let apiEnvVarName = "DEMO_MAPPY_UI_API_ENDPOINT_" + thisTokenId;
let apiEndPoint = process.env[apiEnvVarName];
if ( !apiEndPoint ) {
throw "||| BUILD FAILED!!! |||\n||| ATTENTION NEEDED|||\n" + "Environemnt Variable " + envVarName + " is not set.\n" + "||| BUILD FAILED!!! |||\n";
}
mappyConfigs.push( {
"token_id": thisTokenId,
"api_end_point": apiEndPoint,
"config": mappyConfigBuilder(
thisTokenId,
apiEndPoint
)
});
}
}
buildTokenInfo( process.env.TOKEN_IDS );
function getMappyConfig( configName, env ) {
// console.log("\n\n\n getMappyConfig: input", configName, env);
let len = mappyConfigs.length;
let output;
while( len-- ) {
let mappyInfo = mappyConfigs[ len ];
let mappyConfig = mappyInfo.config[ env ];
let thisConfig = mappyConfig[ configName ];
if ( !thisConfig ) {
continue;
}
if ( thisConfig instanceof Array ) {
if ( !output ) {
output = [];
}
output.push(...thisConfig);
} else {
//Its an object!
if ( !output ) {
output = {};
}
Object.assign(output, thisConfig);
}
}
// console.log("getMappyConfig: output", output, "\n\n\n");
return output;
}
//endregion
const commonConfig = {
module: {
rules: [
{
test: /\.(js)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: [
"@babel/preset-env"
],
plugins: [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-transform-async-to-generator",
["@babel/plugin-transform-runtime",
{
"regenerator": true
}
]
]
}
}
},
{
test: /\.css$/,
use: ['style-loader','css-loader' ]
}
]
}
};
const sdkVersion = process.env.OST_BROWSER_SDK_VERSION;
let sdkBaseUrl = process.env.OST_BROWSER_SDK_BASE_URL;
sdkBaseUrl = sdkBaseUrl.trimRight("/");
let publicPath = sdkBaseUrl;
let distPath = "dist";
if ( sdkVersion ) {
publicPath = publicPath + "/" + sdkVersion + "/";
distPath = distPath + "/" + sdkVersion + "/ost-sdk-scripts/";
}
publicPath = publicPath.trimRight("/");
const devConfig = {
mode: "development",
devtool: "inline-source-map",
devServer: {
http2: true,
https: {
key: fs.readFileSync('/usr/local/etc/nginx/dev-proxy-https-certificates/dev-proxy-https.key'),
cert: fs.readFileSync('/usr/local/etc/nginx/dev-proxy-https-certificates/dev-proxy-https.crt')
},
contentBase: "./dist",
port: 9090,
liveReload: false,
hot: false,
inline: false,
clientLogLevel: 'silent'
},
plugins: [
new CleanWebpackPlugin(),
new SriPlugin({
hashFuncNames: ['sha256', 'sha384'],
enabled: false,
}),
new webpack.DefinePlugin(webpackVariables)
],
output: {
publicPath: publicPath
}
};
const prodConfig = {
mode: "production",
devtool: "source-map",
plugins: [
new CleanWebpackPlugin(),
new SriPlugin({
hashFuncNames: ['sha256', 'sha384'],
enabled: true
}),
new webpack.DefinePlugin(webpackVariables)
],
output: {
path: path.resolve(__dirname, distPath),
publicPath: publicPath
}
};
const ostConfig = require("./webpack-ost-config.js");
/**
* [description]
* @param {Array} configs - an Array of configs
* @param {Array} pluginsArray - array of plugins to which HtmlWebpackPlugin instances will be pushed into.
* @return {Array[HtmlWebpackPlugin]} Array of HtmlWebpackPlugin instances for the given config
*/
const AddHtmlPlugins = ( configs, pluginsArray ) => {
let cnt = 0;
let len = configs.length;
for(cnt = 0; cnt < len; cnt++ ) {
let conf = configs[cnt];
let pInstance = new HtmlWebpackPlugin( conf );
pluginsArray.push( pInstance );
}
};
module.exports = (env) => {
let envConfig = null;
let sdkConf = null;
let mappyEnv = "prod";
if ( env.NODE_ENV === 'dev' ) {
envConfig = devConfig;
sdkConf = ostConfig.dev;
mappyEnv = "dev";
} else {
if ( !process.env.OST_BROWSER_SDK_VERSION ) {
throw "||| BUILD FAILED!!! |||\n||| ATTENTION NEEDED|||\n" + "Environemnt Variable OST_BROWSER_SDK_VERSION is not set.\n" + "||| BUILD FAILED!!! |||\n";
}
envConfig = prodConfig;
sdkConf = ostConfig.prod;
}
// Add 'entry'
envConfig.entry = Object.assign({}, getMappyConfig("entry", mappyEnv), sdkConf.entry);
// Add HtmlWebpackPlugin(s)
let pluginsArray = envConfig.plugins;
let mappyHtmlPlugins = getMappyConfig("htmlPlugins", mappyEnv);
AddHtmlPlugins(mappyHtmlPlugins, pluginsArray);
AddHtmlPlugins(sdkConf.htmlPlugins, pluginsArray);
return {
...commonConfig,
...envConfig
}
};