-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.build.config.js
202 lines (179 loc) · 4 KB
/
webpack.build.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"use strict";
const NODE_ENV = process.env.NODE_ENV || "development";
const MODE = NODE_ENV.split(":")[0];
const MODE_2 = NODE_ENV.split(":")[1];
const StringReplacePlugin = require("string-replace-webpack-plugin");
const WebpackNotifierPlugin = require("webpack-notifier");
const BrowserSyncPlugin = require("browser-sync-webpack-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const path = require("path");
const webpack = require("webpack");
const fs = require("fs");
const crypto = require("crypto");
const compress = require("compression");
const packagenpm = require("./package.json");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const BundleAnalyzerPlugin = require(
"webpack-bundle-analyzer"
).BundleAnalyzerPlugin;
let extractHTML = new ExtractTextPlugin("[name].html");
let objBuildList = {};
/**
* Templates
*/
objBuildList = Object.assign(
objBuildList,
{
"./lib/UserID": ["./lib/UserID.ts"]
}
);
if (MODE_2 !== "stat") {
objBuildList = Object.assign(
objBuildList,
{
"./dist/simple-typescript-example/index": ["./src/simple-typescript-example/index.ts"],
"./dist/simple-javascript-example/index": ["./src/simple-javascript-example/index.ts"],
"./dist/test-scope/index": ["./src/test-scope/index.ts"]
}
);
}
/**
* Plugins list
*/
let arrPlugins = [
new WebpackNotifierPlugin(),
new StringReplacePlugin(),
extractHTML
];
/**
* Add BrowserSync for development mode
*/
if (MODE_2 === "stat") {
arrPlugins.push(
new BundleAnalyzerPlugin()
);
}
/**
* Add BrowserSync for development mode
*/
if (MODE_2 === "watch") {
arrPlugins.push(
new BrowserSyncPlugin({
host: "localhost",
port: 8080,
server: {
baseDir: ["./"],
middleware: function (req, res, next) {
var gzip = compress();
gzip(req, res, next);
}
}
})
);
}
/**
* Add uglifyer for production mode
*/
if (MODE === "production" || MODE === "testing") {
arrPlugins.push(
new webpack.optimize.UglifyJsPlugin({
minimize: true,
sourceMap: false,
output: {
comments: false
},
compressor: {
warnings: false
}
})
);
}
/**
* Add additional plugins
*/
arrPlugins.push(
new webpack.DefinePlugin({
"process.env.NODE_ENV": JSON.stringify(MODE)
})
);
arrPlugins.push(
new CleanWebpackPlugin([
"./dist"
])
);
let replacements = StringReplacePlugin.replace({
replacements: [
{
pattern: /#HASH#/gi,
replacement: () => {
return crypto.createHash("md5").update(
(new Date()).getTime().toString()).digest("hex");
}
},
{
pattern: /#PACKAGE_NAME#/gi,
replacement: () => {
return packagenpm.name;
}
},
{
pattern: /#PACKAGE_VERSION#/gi,
replacement: () => {
return packagenpm.version;
}
}
]
});
module.exports = {
entry: objBuildList,
output: {
filename: MODE === "production" ? "[name].js" : "[name].js",
library: "UserID",
libraryTarget: "umd",
umdNamedDefine: true
},
externals: {
"UserID": "UserID"
},
devtool: (
MODE === "development" ? "inline-source-map" : (
MODE === "testing" ? "inline-source-map" : ""
)
),
plugins: arrPlugins,
resolve: {
extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
},
resolveLoader: {
extensions: [".js", ".ts", ".jsx", ".tsx"]
},
module: {
loaders: [
{
test: /\.ts(x?)$/,
use: [
{
loader: replacements
},
{
loader: "babel-loader",
options: {
presets: ["es2015"]
}
},
{
loader: "ts-loader"
}
]
},
{
test: /\.html/i,
use: extractHTML.extract(["html-loader"])
},
{
test: /\.json$/,
use: "json-loader"
}
]
}
};