forked from BitGo/BitGoJS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
165 lines (139 loc) · 4.96 KB
/
webpack.config.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
const path = require('path');
const webpack = require('webpack');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const HTMLWebpackPlugin = require('html-webpack-plugin');
const glob = require('glob');
// Loaders handle certain extensions and apply transforms
function setupRules(env) {
const rules = [];
if (env.prod) {
// TODO: If we want to add babel, uncomment this. Maybe add an IE flag to CME
// Babelify all .js files for prod builds (except external modules)
// rules.push({
// test: /\.js$/,
// exclude: /(node_modules|bower_components)/,
// use: [
// {
// loader: 'babel-loader',
// options: {
// presets: ['env']
// }
// }
// ]
// });
}
return rules;
}
// Tell Webpack not to bundle these at all when required
// We have to add some core node libraries (fs, net, tls) because ripple-lib
// does not have browser-friendly builds
function setupExternals() {
const externals = ['morgan', 'superagent-proxy'];
// TODO: Bug ripple to make a browser-friendly build of ripple-lib?
const nodeLibraries = ['fs', 'net', 'tls'];
externals.push(...nodeLibraries);
return externals;
}
/**
* This feature is mostly for browsers where we don't want to have a build with coins that people don't need
* In order to specify the coins you want, you must pass the --env.coins="csv coins" flag
* If nothing is passed, all coins are going to be available.
* In webpack, we have to define via plugin what we want.to exclude but also we want to include the coins if the
* user didn't specify anything or in node environments
* @param env
* @returns [webpack.DefinePlugin]
*/
function getCoinsToExclude(env) {
if (!env.coins) {
return [];
}
const allCoins = ['btc', 'bch', 'btg', 'ltc', 'eth', 'rmg', 'xrp', 'xlm', 'dash', 'zec'];
const compileCoins = env.coins.split(',').map(coin => coin.trim().toLowerCase());
const invalidCoins = compileCoins.filter(allCoin => {
return !allCoins.includes(allCoin);
});
if (invalidCoins.length) {
throw new Error(`Invalid coins: ${invalidCoins.join(',')} \n Valid options are: ${allCoins.join(',')}`);
}
return allCoins.filter(allCoin => {
return !compileCoins.includes(allCoin);
})
.map(coin => {
return new webpack.DefinePlugin({
'process.env': {
[`BITGO_EXCLUDE_${coin.toUpperCase()}`]: JSON.stringify('exclude')
}
});
});
}
// Used for extra processing that does not involve transpilation (e.g. minification)
function setupPlugins(env) {
const excludeCoins = getCoinsToExclude(env);
const plugins = [
// This is for handling dynamic requires in third-party libraries
// By default, webpack will bundle _everything_ that could possibly match the expression
// inside a dynamic 'require'. This changes Webpack so that it bundles nothing.
new webpack.ContextReplacementPlugin(/.*$/, /$NEVER_MATCH^/),
...excludeCoins
];
if (!env.test) {
// Create a browser.html which automatically includes BitGoJS
plugins.push(new HTMLWebpackPlugin({ filename: 'browser.html', title: 'BitGo SDK Sandbox' }));
}
if (env.prod) {
// Minimize output files in production
plugins.push(new UglifyJSPlugin({
uglifyOptions: {
mangle: false
}
}));
}
return plugins;
}
// Test configuration - does not bundle main code, but merges all files in test/ to single output file
function getTestConfig(env) {
return {
// Take everything in the test directory
entry: glob.sync(path.join(__dirname, 'test', '**', '*.js')),
// Output everything into browser/tests.js
output: {
path: path.join(__dirname, 'browser'),
filename: 'tests.js'
},
externals: setupExternals(env),
plugins: setupPlugins(env)
};
}
// Webpack configuration takes environment as argument, returns a config object
module.exports = function setupWebpack(env) {
// If no env is provided, default to dev
if (!env) {
console.log('No environment provided - defaulting to dev. Use the npm `compile` tasks for a better build experience.');
env = { dev: true };
}
if (env.test) {
// Compile tests
return getTestConfig(env);
}
// Compile source code
return {
// Main project entry point
entry: path.join(__dirname, 'src', 'index.js'),
// Output directory and filename
// Library acts like 'standalone' for browserify, defines it globally if module system not found
output: {
path: path.join(__dirname, 'browser'),
filename: env.prod ? 'BitGoJS.min.js' : 'BitGoJS.js',
library: 'BitGoJS',
libraryTarget: 'umd'
},
// All of our transpilation settings. Should really only need 'loaders' for now
module: { rules: setupRules(env) },
// Do not bundle these when encountered
externals: setupExternals(env),
// Any extra processing
plugins: setupPlugins(env),
// Create a source map for the bundled code (dev and test only)
devtool: !env.prod && 'cheap-eval-source-map'
};
};