-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
148 lines (135 loc) · 3.71 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
const CopyPlugin = require("copy-webpack-plugin");
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const path = require("path");
const webpack = require("webpack");
const APP_PATH = path.resolve(__dirname, "src");
const PUBLIC_PATH = path.resolve(__dirname, "public");
const DEST_PATH = path.resolve(__dirname, "dist");
const isProduction = process.env.NODE_ENV === "production";
const devConfig = {
srcmap: true,
asyncTypeChecking: true,
};
const prodConfig = {
autoprefixer: true,
minimize: true,
extractCss: true,
useHashInFilename: true,
failOnError: true,
};
const config = isProduction ? prodConfig : devConfig;
const styleLoaders = {
test: /\.s?css$/,
loader: [
"style-loader",
isProduction && {
loader: MiniCssExtractPlugin.loader,
},
"css-loader",
"sass-loader",
{
loader: require.resolve("postcss-loader"),
options: {
plugins: () => [
require("postcss-preset-env")({
autoprefixer: config.autoprefixer
? {
flexbox: "no-2009",
}
: false,
stage: 3,
}),
require("postcss-normalize")(),
],
},
},
].filter(Boolean),
};
module.exports = {
entry: APP_PATH,
mode: isProduction ? "production" : "development",
bail: isProduction,
devtool: config.srcmap ? "source-map" : false,
stats: "minimal",
output: {
path: DEST_PATH,
filename: config.useHashInFilename ? "[name].[hash].js" : "bundle.js",
chunkFilename: config.useHashInFilename
? "[name].[hash].chunk.js"
: "[name].chunk.js",
publicPath: "/",
},
resolve: {
extensions: [".ts", ".tsx", ".js", ".jsx", ".json"],
},
devServer: {
contentBase: PUBLIC_PATH,
},
optimization: {
minimize: config.minimize,
minimizer: [new TerserPlugin(), new OptimizeCSSAssetsPlugin()],
splitChunks: {
chunks: "all",
name: false,
},
},
module: {
rules: [
{
enforce: "pre",
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
loader: "eslint-loader",
options: {
failOnError: config.failOnError,
},
},
{
test: /\.(ts|js)x?$/,
use: ["babel-loader", "source-map-loader"],
exclude: /node_modules/,
},
styleLoaders,
{
test: /\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$/,
loader: "file-loader",
},
],
},
plugins: [
new webpack.EnvironmentPlugin(["NODE_ENV"]),
new HtmlWebpackPlugin({
inject: true,
template: path.join(PUBLIC_PATH, "index.html"),
...(config.minimize
? {
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
}
: {}),
}),
isProduction && new CopyPlugin([{ from: PUBLIC_PATH, to: DEST_PATH }]),
isProduction &&
new MiniCssExtractPlugin({
filename: "[name].[contenthash:8].css",
}),
new ForkTsCheckerWebpackPlugin({
async: config.asyncTypeChecking,
useTypescriptIncrementalApi: true,
}),
].filter(Boolean),
};