forked from fanout/reconnecting-eventsource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
130 lines (114 loc) · 2.76 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
const path = require('path');
const webpack = require('webpack');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const globalName = "ReconnectingEventSource";
const srcDir = path.join(__dirname, 'src');
const { BUILD_TARGET = "" } = process.env;
function buildEntry() {
let entry;
switch(BUILD_TARGET) {
case "umd":
case "umd-min":
entry = "./index-umd.js";
break;
default:
entry = "./index.js";
}
return entry;
}
function buildOutput() {
let outputDir;
switch(BUILD_TARGET) {
case "commonjs":
outputDir = "lib";
break;
default:
outputDir = "dist";
}
const outputPath = path.join(__dirname, outputDir);
let outputFilename;
switch(BUILD_TARGET) {
case "umd":
outputFilename = globalName + ".js";
break;
case "umd-min":
outputFilename = globalName + ".min.js";
break;
default:
outputFilename = "index.js";
}
const output = {
path: outputPath,
filename: outputFilename,
};
switch(BUILD_TARGET) {
case "commonjs":
Object.assign(output, {
libraryTarget: 'commonjs2'
});
break;
case "umd":
case "umd-min":
Object.assign(output, {
libraryTarget: 'var',
library: globalName
});
}
return output;
}
function addBabelRule(rules) {
let moduleType;
switch(BUILD_TARGET) {
case "commonjs":
moduleType = "commonjs";
break;
default:
moduleType = "umd";
}
const babelRule = {
test: /\.js$/,
use: [
{
// Use Babel to transpile ES2015 + ES2017 async syntax
loader: 'babel-loader',
options: JSON.stringify({
presets: [
['env', {modules: moduleType}]
]
})
}
],
include: srcDir
};
return [...rules, babelRule];
}
function addMinifyPlugin(minimizer) {
if (BUILD_TARGET === "umd-min") {
const minifyPlugin = new UglifyJsPlugin({
uglifyOptions: {
compress: {}
}
});
return [...minimizer, minifyPlugin];
}
return minimizer;
}
const entry = buildEntry();
const output = buildOutput();
let rules = [];
rules = addBabelRule(rules);
let minimizer = [];
minimizer = addMinifyPlugin(minimizer);
const config = {
mode: 'production',
context: srcDir,
entry,
output,
module: {
rules
},
optimization: {
minimizer
}
};
module.exports = config;