-
Notifications
You must be signed in to change notification settings - Fork 15
/
webpack.config.js
268 lines (255 loc) · 9.33 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
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
259
260
261
262
263
264
265
266
267
268
/* eslint-disable no-nested-ternary */
/* eslint-disable camelcase */
/* eslint-disable quote-props */
const webpack = require('webpack');
const path = require('path');
const fs = require('fs');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ExtensionReloader = require('webpack-ext-reloader');
const TerserPlugin = require('terser-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const { VueLoaderPlugin } = require('vue-loader');
let version = require('./package.json').version;
module.exports = (env, argv) => {
const supportedBrowsers = ['chrome', 'firefox'];
const browser = env.browser || 'chrome';
let launchDarklyId = '';
let extensionId = 'fgddmllnllkalaagkghckoinaemmogpe';
// Default Rudderstack staging setup
let rudderstackWriteKey = '';
let rudderstackDataplaneUrl = '';
if ((env.alpha || env.beta) && (!process.env.XVCHROME_ALPHA_KEY || !process.env.XVCHROME_BETA_KEY) && browser === 'chrome') {
console.error('Alpha or Beta keys are missing!');
process.exit(-1);
}
if (env.beta) {
let launchDarklyId = '';
extensionId = 'olhkmmhcabcohlcodnbkfoekafimgkmb';
} else if (env.alpha) {
let launchDarklyId = '';
extensionId = 'gicmddjpmepnbabhkhihlgdfccbppflm';
} else if (process.env.NODE_ENV === 'production') {
let launchDarklyId = '';
let rudderstackWriteKey = '';
let rudderstackDataplaneUrl = '';
}
if (process.env.CIRCLE_BUILD_NUM) {
version += '.' + process.env.CIRCLE_BUILD_NUM;
} else if (fs.existsSync('VERSION')) {
version = fs.readFileSync('VERSION', 'utf8').trim();
}
const config = {
mode: process.env.NODE_ENV,
context: path.join(__dirname, 'source'),
entry: {
'scripts/background': './scripts/background.js',
'scripts/popup': './scripts/popup.js',
'scripts/networkLock': './scripts/networkLock.js',
},
output: {
path: path.join(__dirname, 'build', env.beta || env.alpha ? (env.beta ? 'beta' : 'alpha') : process.env.NODE_ENV, browser),
filename: '[name].js',
},
stats: {
entrypoints: false,
children: false,
},
resolve: {
extensions: ['.js', '.vue', '.scss'],
alias: {
'@': path.resolve(__dirname, 'source'),
},
},
devtool: 'cheap-source-map',
module: {
rules: [
{
test: /\.vue$/,
use: {
loader: 'vue-loader',
},
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: [/node_modules/],
},
{
test: /\.(sa|sc|c)ss$/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
},
{
loader: 'sass-loader',
options: {
additionalData: '@import "@/styles/shared/vendor/assets/stylesheets/eds/core/_variables.scss";',
},
},
],
},
],
},
performance: {
hints: false,
},
ignoreWarnings: [/only default export is available soon/],
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
mangle: false,
compress: {
drop_console: true,
},
output: {
comments: false,
},
},
}),
],
},
plugins: [
new CleanWebpackPlugin({
cleanStaleWebpackAssets: false,
}),
new webpack.DefinePlugin({
global: 'window',
__IS_BETA__: !!env.beta,
__IS_ALPHA__: !!env.alpha,
__IS_RELEASE__: process.env.NODE_ENV === 'production',
__BROWSER__: JSON.stringify(browser),
__LD_ID__: `'${launchDarklyId}'`,
__RS_WRITE_KEY__: `'${rudderstackWriteKey}'`,
__RS_DATAPLANE_URL__: `'${rudderstackDataplaneUrl}'`,
}),
new VueLoaderPlugin(),
new CopyWebpackPlugin({
patterns: [
{ from: 'images', to: 'images' },
{
from: '_locales',
to: '_locales',
globOptions: {
ignore: ['**/_locations/**/*'],
},
transform: (_content, filePath) => {
const content = _content.toString();
const rx = new RegExp(`_@${browser}@`, 'gi');
const originalMessage = content.toString().replace(rx, '');
let locations = {};
let prependedLocations = {};
try {
// eslint-disable-next-line import/no-dynamic-require
locations = require(filePath.replace('_locales', '_locales/_locations'));
} catch (ex) {
// Throw again so we know which file is invalid
throw new Error('Invalid JSON location file:' + filePath);
}
for (let key in locations) {
prependedLocations['_locationName_' + key.replace(/[ \-()]/g, '_')] = locations[key];
}
return JSON.stringify({
...JSON.parse(originalMessage),
...prependedLocations,
});
},
},
{ from: 'html/**/*', to: 'html/[name][ext]' },
{
from: 'styles/shared/vendor/assets/fonts/eds/**/*',
to: 'fonts/[name][ext]',
},
{
from: '**/*',
to: 'images/[path][name][ext]',
context: 'styles/shared/vendor/assets/images/eds/',
},
{ from: 'fonts/**/*', to: 'fonts/[name][ext]' },
{
from: 'scripts/content/**/*',
to: 'scripts/content/[name][ext]',
transform: (_content, filePath) => {
const content = _content.toString();
let transformedContent = content.toString().replace('__EXTENSION_ID__', `'${extensionId}'`);
if (browser === 'chrome') {
transformedContent = transformedContent.replace('inject_chrome() {', 'inject() {');
} else {
transformedContent = transformedContent.replace('inject_firefox() {', 'inject() {');
transformedContent = transformedContent.replace('__GPSJS_CONTENT__', fs.readFileSync('source/scripts/content/gps.js', 'utf-8').replace(/([`$\\{}])/g, '\\$1'));
}
return transformedContent;
},
},
{
from: 'manifest.json',
to: 'manifest.json',
transform: (_content) => {
const content = _content.toString();
const manifestObj = JSON.parse(content);
manifestObj.version = version;
Object.keys(manifestObj).forEach(function (key) {
if (new RegExp(supportedBrowsers.join('|')).test(key)) {
let rxCurrentBrowser = new RegExp(`__${browser}__`);
if (rxCurrentBrowser.test(key)) {
manifestObj[key.replace(rxCurrentBrowser, '')] = manifestObj[key];
}
delete manifestObj[key];
}
});
if (env.alpha || env.beta) {
let currentBuildType = env.alpha ? 'Alpha' : 'Beta';
switch (browser) {
case 'chrome':
const chromeKeys = {
Alpha: process.env.XVCHROME_ALPHA_KEY,
Beta: process.env.XVCHROME_BETA_KEY,
};
manifestObj.name = currentBuildType === 'Beta' ? '__MSG_app_name_title_beta__' : '(Alpha) ExpressVPN Extension';
manifestObj.description =
currentBuildType === 'Beta' ? '__MSG_app_description_text_beta__' : '(Alpha) ExpressVPN Extension';
manifestObj.key = chromeKeys[currentBuildType];
break;
case 'firefox':
manifestObj.name = `(${currentBuildType}) ExpressVPN Extension`;
manifestObj.description = `(${currentBuildType}) ExpressVPN Extension`;
manifestObj.applications.gecko.id = '[email protected]';
break;
default:
break;
}
} else if (browser === 'chrome') {
manifestObj.key =
'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs24qmLQZjYn5vnBdvJ77lURW/Ig8V4PPR+RWzenikcFUnvU3CaI92jlb7PBM1YtR4q4DyhNlGMiSccYxcwH5Z2wogWW/BUxV0idK8e24Xtl3HQvDvEHl684x2QJWbebXdV3d1XPu4FfwBJBEbVDwxqItSojdc+eam1GZtXfiibz4hgum2uNhnydIG/vxTJ/nWAJ8uBWOgHaiU47t49ma4ptvNQZ3QTaDsFbKtl+kfV9jNRChVW7skadvixTOIVZBLcnnNxVhgefsERz97IN6ngTQN0W2x2q3cKfl6EU0K7HStw1zet3/lorsqyoZbpHE3HWWsj+/RBCrAFjxqPO7hwIDAQAB';
}
if (config.mode === 'development') {
manifestObj.web_accessible_resources[0].resources.push('html/*');
}
return JSON.stringify(manifestObj, null, 2);
},
},
],
}),
],
};
if (config.mode === 'production') {
config.plugins = (config.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"',
},
}),
]);
}
if (process.env.HMR === 'true') {
config.plugins = (config.plugins || []).concat([
new ExtensionReloader({
manifest: path.join(__dirname, 'source', 'manifest.json'),
}),
]);
}
return config;
};