forked from signum-network/signum-xt-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.public.config.js
62 lines (53 loc) · 1.7 KB
/
webpack.public.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
const CopyWebpackPlugin = require('copy-webpack-plugin');
const path = require('path');
const pkg = require('./package.json');
module.exports = (publicPath, outputPath, manifestFile, targetBrowser) => {
return new CopyWebpackPlugin({
patterns: [
{
from: publicPath,
to: outputPath,
filter: file => !file.endsWith('.html') && !file.endsWith('manifest.v2.json')
},
{
from: path.join(publicPath, manifestFile),
to: path.join(outputPath, 'manifest.json'),
toType: 'file',
transform: content => {
const manifest = transformManifestKeys(JSON.parse(content), targetBrowser);
return JSON.stringify(manifest, null, 2);
}
}
]
});
};
/**
* Fork of `wext-manifest`
*/
const browserVendors = ['chrome', 'firefox', 'opera', 'edge', 'safari'];
const vendorRegExp = new RegExp(`^__((?:(?:${browserVendors.join('|')})\\|?)+)__(.*)`);
const transformManifestKeys = (manifest, vendor) => {
if (Array.isArray(manifest)) {
return manifest.map(newManifest => {
return transformManifestKeys(newManifest, vendor);
});
}
if (typeof manifest === 'object') {
return Object.entries(manifest).reduce((newManifest, [key, value]) => {
const match = key.match(vendorRegExp);
if (match) {
const vendors = match[1].split('|');
// Swap key with non prefixed name
if (vendors.indexOf(vendor) > -1) {
newManifest[match[2]] = value;
}
} else if (key === 'version') {
newManifest[key] = pkg.version;
} else {
newManifest[key] = transformManifestKeys(value, vendor);
}
return newManifest;
}, {});
}
return manifest;
};