/
webpack.config.js
178 lines (155 loc) · 4.22 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
var webpack = require('webpack');
var path = require('path');
var TypedocWebpackPlugin = require('typedoc-webpack-plugin');
var UglifyJsPlugin = require('uglifyjs-webpack-plugin');
var pkginfo = require('./package.json');
var createVariants = require('parallel-webpack').createVariants;
var clonedeep = require('lodash.clonedeep');
var argv = require('yargs').argv;
var isProduction = argv.env === 'production';
var justDocs = argv.env === 'docs';
var libraryName = 'opennms';
var variants = {
target: [ 'web', 'node' ],
};
if (isProduction) {
variants.production = [ true, false ];
}
if (justDocs) {
variants = {
target: ['node'],
docs: [true],
};
}
var config = {
entry: {
'opennms': __dirname + '/src/API.ts',
},
devtool: 'source-map',
output: {
path: __dirname + '/dist',
libraryTarget: 'umd',
umdNamedDefine: true
},
module: {
rules: [
{
test: /(\.jsx?)$/,
use: [
'cache-loader',
'babel-loader'
]
},
{
test: /(\.tsx?)$/,
use: [
'cache-loader',
'babel-loader',
'ts-loader'
],
exclude: [/node_modules/]
}
],
},
resolve: {
modules: [
path.resolve('./src'),
path.resolve('./node_modules')
],
extensions: ['.webpack.js', '.web.js', '.ts', '.js']
},
plugins: [],
node: {
fs: 'empty',
__dirname: true,
child_process: false,
global: true,
process: false
}
};
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';
myconf.node = { process: false };
}
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 = {};
}
if (options.production) {
myconf.optimization.minimize = true;
if (!myconf.optimization.minimizer) {
myconf.optimization.minimizer = [];
} else {
console.log('minimizer exists:',myconf.optimization.minimizer);
}
myconf.optimization.minimizer.push(new UglifyJsPlugin({
cache: true,
parallel: true,
sourceMap: true,
uglifyOptions: {
mangle: {
reserved: [ '$element', '$super', '$scope', '$uib', '$', 'jQuery', 'exports', 'require', 'angular', 'c3', 'd3' ]
},
compress: true
}
}));
myconf.module.rules.unshift({
// run tslint on typescript files before rendering
enforce: 'pre',
test: /\.tsx?$/,
use: [
{
loader: 'tslint-loader',
options: {
typeCheck: true
}
}
],
exclude: [/node_modules/]
});
defs['global.GENTLY'] = false;
myconf.plugins.push(new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false
}));
myconf.output.filename += '.min';
}
myconf.plugins.push(new webpack.DefinePlugin(defs));
myconf.plugins.push(new webpack.ProvidePlugin({X2JS: 'x2js'}));
// build docs either on a dedicated doc build, or during production node.js build
var buildDocs = !!(justDocs || (options.production && options.target === 'node'));
if (buildDocs) {
// generate documentation
var tsconfig = require('./tsconfig.json');
tsconfig.name = 'OpenNMS.js';
tsconfig.mode = 'file';
tsconfig.ignoreCompilerErrors = true;
tsconfig.exclude = "/**/+(node_modules|test)/**/*";
tsconfig.excludeExternals = false;
myconf.plugins.push(new TypedocWebpackPlugin(tsconfig));
}
myconf.output.filename += '.js';
console.log('Building variant: target=' + options.target + ', production=' + (!!options.production) + ', docs=' + buildDocs);
return myconf;
}
module.exports = createVariants({}, variants, createConfig);