-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
155 lines (144 loc) · 4.8 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
const path = require("path");
const rimraf = require("rimraf");
const webpack = require("webpack");
const shell = require("shell-env");
const git = require("git-rev-sync");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const EventHooksPlugin = require("event-hooks-webpack-plugin");
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
const manifest = require("./package.json");
const shellEnv = Object(shell.sync());
const webpackPath = path.resolve(__dirname, "dist/webpack");
const releasePath = path.resolve(__dirname, "dist/release");
/**
* @readonly
* @desc Webpack/Custom Command Line Interface
*/
class CustomDefaultConfig {
static get argv() {
return {
mode: "production",
devtool: false,
};
}
static get env() {
return {
// Custom envionment variables
};
}
constructor(envProxy, argvProxy) {
this.envProxy = envProxy;
this.argvProxy = argvProxy;
this.lastCompiled = new Date().toISOString();
}
get production() {
return this.argvProxy.mode === "production";
}
get outputPath() {
return this.production ? releasePath : webpackPath;
}
}
/**
* @desc Webpack Config
*/
module.exports = function(_env = {}, _argv = {}) {
const env = new Proxy(CustomDefaultConfig.env, {
get(target, key, receiver) {
if (_env[key] != null) return _env[key];
return Reflect.get(target, key, receiver);
},
});
const argv = new Proxy(CustomDefaultConfig.argv, {
get(target, key, receiver) {
if (_argv[key] != null) return _argv[key];
return Reflect.get(target, key, receiver);
},
});
const config = new CustomDefaultConfig(env, argv);
const preamble = `/*! @preserve ${manifest.name}: ${manifest.version}-${git.short()} (${config.lastCompiled}) */`;
console.log(`webpack mode: ${argv.mode}, git revision: ${git.short()}, current branch: ${git.branch()}`);
return {
mode: argv.mode,
entry: {
"browser-storage": "./src/index.ts",
},
devtool: argv.devtool,
module: {
rules: [
{
test: /\.tsx?$/,
use: [
{
loader: "ts-loader",
options: {
compilerOptions: {
module: "esnext",
},
},
},
],
},
],
},
output: {
path: config.outputPath,
filename: "[name].js",
libraryTarget: "umd",
},
resolve: {
extensions: [".tsx", ".ts", ".jsx", ".js"],
},
plugins: [
new webpack.ProgressPlugin(shellEnv["CI"] ? new Function() : null),
new webpack.DefinePlugin({
__X_METADATA__: JSON.stringify({
name: manifest.name,
version: manifest.version,
revision: git.short(),
production: config.production,
lastCompiled: config.lastCompiled,
}),
}),
new EventHooksPlugin({
environment: function() {
if (config.production) {
rimraf.sync(releasePath);
} else {
rimraf.sync(webpackPath);
}
},
}),
new BundleAnalyzerPlugin({
analyzerMode: config.production ? "static" : "disabled",
openAnalyzer: false,
}),
],
optimization: {
minimizer: [
new UglifyJsPlugin({
sourceMap: Boolean(argv.devtool),
extractComments: false,
uglifyOptions: {
compress: {
drop_console: false,
drop_debugger: true,
},
output: {
/**
* @desc escape Unicode characters in strings and regexps
* (affects directives with non-ascii characters becoming invalid)
*/
ascii_only: false,
/**
* A real coup for debugging!
*/
max_line_len: 4096,
preamble: preamble,
},
},
}),
],
},
node: false,
};
};