-
Notifications
You must be signed in to change notification settings - Fork 9
/
webpack.config.js
163 lines (141 loc) · 4.01 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
const webpack = require('webpack');
const path = require('path');
const ESLintPlugin = require('eslint-webpack-plugin');
const LodashPlugin = require('lodash-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const pkginfo = require('./package.json');
var createVariants = require('parallel-webpack').createVariants;
var cloneDeep = require('lodash').cloneDeep;
var libraryName = 'opennms';
var variants = {
target: [ 'web', 'node' ],
};
var config = {
entry: {
'opennms': __dirname + '/src/API.ts',
},
devtool: 'source-map',
output: {
libraryTarget: 'umd',
umdNamedDefine: true
},
module: {
rules: [
{
test: /(\.jsx?)$/,
use: [
'cache-loader',
'babel-loader'
]
},
],
},
resolve: {
modules: [
path.resolve(__dirname, 'src'),
'node_modules',
],
extensions: ['.ts', '.js']
},
plugins: [],
node: {
__dirname: true,
global: true,
}
};
function createConfig(options) {
var myconf = cloneDeep(config);
myconf.output.filename = '[name]';
var defs = {
'IS_WEB': options.target === 'web',
'IS_PRODUCTION': options.production,
'global.OPENNMS_JS_VERSION': JSON.stringify(pkginfo.version),
};
myconf.mode = options.production? 'production':'development';
if (options.target === 'web') {
myconf.target = 'web';
} else {
myconf.target = 'node';
}
if (options.target === 'node') {
myconf.output.filename += '.node';
myconf.entry.cli = __dirname + '/src/CLI.ts';
myconf.plugins.push(new webpack.BannerPlugin({
banner: '#!/usr/bin/env node',
raw: true,
entryOnly: true,
include: /cli/i,
}));
}
if (!myconf.optimization) {
myconf.optimization = {};
}
myconf.optimization.chunkIds = 'named';
myconf.optimization.minimize = false;
myconf.optimization.moduleIds = 'named';
myconf.optimization.removeAvailableModules = false;
if (!options.production) {
myconf.module.rules.unshift({
test: /(\.tsx?)$/,
use: [
'cache-loader',
'babel-loader'
],
exclude: [/node_modules/]
});
}
if (options.production) {
myconf.optimization.chunkIds = 'deterministic';
myconf.optimization.concatenateModules = true;
myconf.optimization.flagIncludedChunks = true;
myconf.optimization.mangleExports = 'deterministic';
myconf.optimization.moduleIds = 'deterministic';
myconf.optimization.removeAvailableModules = true;
myconf.optimization.minimize = true;
if (!myconf.optimization.minimizer) {
myconf.optimization.minimizer = [];
} else {
console.log('minimizer exists:',myconf.optimization.minimizer);
}
myconf.optimization.minimizer.push(new TerserPlugin({
extractComments: false,
terserOptions: {
mangle: {
keep_classnames: true,
keep_fnames: true,
reserved: [ '$element', '$super', '$scope', '$uib', '$', 'jQuery', 'exports', 'require', 'angular', 'c3', 'd3' ],
},
compress: true,
}
}));
myconf.module.rules.unshift({
test: /(\.tsx?)$/,
use: [
'cache-loader',
'babel-loader'
],
exclude: [/node_modules/]
});
defs['global.GENTLY'] = false;
myconf.plugins.push(new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false
}));
myconf.plugins.push(new LodashPlugin);
myconf.plugins.push(new ESLintPlugin());
myconf.output.filename += '.min';
}
myconf.plugins.push(new webpack.DefinePlugin(defs));
myconf.plugins.push(new webpack.ProvidePlugin({X2JS: 'x2js'}));
myconf.output.filename += '.js';
console.log('webpack config variant: target=' + options.target + ', production=' + (!!options.production));
return myconf;
}
module.exports = (env, argv) => {
if (argv.mode === 'production') {
variants.production = [ true, false ];
}
const config = createVariants({}, variants, createConfig);
// console.debug('webpack config: ' + JSON.stringify(config, undefined, 2));
return config;
};