-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
webpack.config.js
92 lines (88 loc) · 2.36 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
const path = require('path');
const fs = require('fs');
var nodeExternals = require('webpack-node-externals');
var WebpackObfuscator = require('webpack-obfuscator');
const RemovePlugin = require('remove-files-webpack-plugin');
const filesToKeepInLib = [/src\/scripts/g, /config.js/g, /main.js/g];
const filesToGenerate = [
{
override: true,
path: 'lib/src/main.js',
contents: 'module.exports = { DevicePlugin : require("../bundle.js").default }',
},
{
override: false,
path: 'lib/src/modules/index.js',
contents: 'module.exports = require("../fake-module-loader.js").FakeModuleLoader;',
},
];
//Remove all files that are already bundled from lib folder
const CleanUpLibFolder = new RemovePlugin({
after: {
root: './lib',
test: [
{
folder: './src',
method: (file) => {
return filesToKeepInLib.reduce((flag, f) => flag && !new RegExp(f).test(file), true);
},
recursive: true,
},
],
},
});
class DynamicFileGenerator {
apply(compiler) {
compiler.hooks.compile.tap('CreateFilePlugin', () => {
for (const file of filesToGenerate) {
const fPath = file.path;
const dir = path.dirname(fPath);
if (!file.override && fs.existsSync(fPath)) {
break;
}
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(fPath, file.contents);
}
});
}
}
const getExtendedWebpackConfig = function () {
return fs.existsSync(path.join(__dirname, 'src/modules/webpack.config.js'))
? './src/modules/webpack.config.js'
: undefined;
};
module.exports = {
extends: getExtendedWebpackConfig(),
entry: ['./lib/src/index.js'],
externals: [nodeExternals()],
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'lib'),
library: {
type: 'umd',
},
},
optimization: { minimize: true },
target: 'node',
externalsPresets: {
node: true,
},
mode: 'production',
devtool: 'nosources-source-map',
plugins: [
new DynamicFileGenerator(),
new WebpackObfuscator({
rotateStringArray: true,
splitStrings: true,
target: 'node',
identifierNamesGenerator: 'mangled-shuffled',
sourceMap: true,
transformObjectKeys: true,
unicodeEscapeSequence: true,
stringArray: true,
}),
CleanUpLibFolder,
],
};