-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
118 lines (113 loc) · 2.9 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
const path = require("path");
const glob = require("glob");
const { BannerPlugin } = require("webpack");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCssAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const PurgeCssPlugin = require("purgecss-webpack-plugin");
const UglifyJSPlugin = require("uglifyjs-webpack-plugin");
const packageInfo = require("./package.json");
const PORT = process.env.PORT || 3000;
const NODE_ENV = {
DEVELOPMENT: "development",
PRODUCTION: "production",
};
const PATH = {
NODE_MODULES: path.join(__dirname, "node_modules"),
PUBLIC: path.join(__dirname, "public"),
DIST: path.join(__dirname, "dist"),
SRC: path.join(__dirname, "src"),
};
const commonConfig = {
mode: process.env.NODE_ENV,
entry: {
bundle: "./src/app.tsx",
},
output: {
path: PATH.DIST,
filename: "[name].js",
library: "bundle",
libraryTarget: "umd",
},
module: {
rules: [
{
test: /\.tsx?$/,
use: "ts-loader",
exclude: /node_modules/,
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
{
test: /\.js$/,
enforce: "pre",
use: ["source-map-loader"],
},
],
},
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
plugins: [
new BannerPlugin({
banner: `Repository: ${packageInfo.name} | Version: ${packageInfo.version} | Author: ${packageInfo.author} | License: ${packageInfo.license}`,
}),
new MiniCssExtractPlugin({
filename: "[name].css",
}),
new CopyWebpackPlugin({
patterns: [
{
from: path.join(PATH.PUBLIC, "**", "*"),
to: path.join(PATH.DIST),
transformPath(targetPath, absolutePath) {
return path.relative(PATH.PUBLIC, absolutePath);
},
},
],
}),
new OptimizeCssAssetsPlugin({
cssProcessorPluginOptions: {
preset: ["default", { discardComments: { removeAll: true } }],
},
}),
new PurgeCssPlugin({
paths: glob.sync(path.join(PATH.SRC, "**", "*"), { nodir: true }),
}),
],
devServer: {
contentBase: PATH.DIST,
historyApiFallback: true,
compress: true,
open: true,
port: PORT,
},
};
const prodConfig = {
optimization: {
minimizer: [
new UglifyJSPlugin({
uglifyOptions: {
compress: {
drop_console: true,
},
},
}),
],
},
};
const runBeforeWebpack = () => {
switch (process.env.NODE_ENV) {
case NODE_ENV.DEVELOPMENT:
return commonConfig;
case NODE_ENV.PRODUCTION:
return Object.assign({}, commonConfig, prodConfig);
default:
throw new Error(
`process.env.NODE_ENV does NOT match with "${NODE_ENV.DEVELOPMENT}" or "${NODE_ENV.PRODUCTION}".`
);
}
};
module.exports = runBeforeWebpack;