-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
143 lines (135 loc) · 3.21 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
const pkg = require("./package.json");
const path = require("path");
const postcssPresetEnv = require("postcss-preset-env");
const cssnano = require("cssnano");
const CleanWebpackPlugin = require("clean-webpack-plugin").CleanWebpackPlugin;
const CopyWebpackPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const AssetsManifestPlugin = require("webpack-assets-manifest");
const SWPrecacheWebpackPlugin = require("sw-precache-webpack-plugin");
const TerserWebpackPlugin = require("terser-webpack-plugin");
const isProduction = process.env.NODE_ENV === "production";
const prod = {
mode: "production"
};
const dev = {
mode: "development",
devtool: "inline-source-map",
devServer: {
contentBase: "dist"
}
};
const htmlMinifyOptions = {
removeComments: true,
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true
};
function postcssPlugins() {
const plugins = [
postcssPresetEnv({
autoprefixer: {
grid: "autoplace",
flexbox: "no-2009"
}
})
];
if (isProduction) {
plugins.push(
cssnano({
preset: "default"
})
);
}
return plugins;
}
module.exports = Object.assign({}, isProduction ? prod : dev, {
entry: "./src/hnpwa-app.ts",
output: {
path: path.resolve(__dirname, "dist"),
filename: "[name].[contenthash:8].js",
chunkFilename: "[name].[contenthash:8].chunk.js"
},
module: {
rules: [
{
enforce: "pre",
test: /\.(ts|js)$/,
exclude: /node_modules/,
use: [{ loader: "eslint-loader", options: { formatter: "codeframe" } }]
},
{
test: /\.ts$/,
exclude: /node_modules/,
use: ["babel-loader", "ts-loader"]
},
{
test: /\.css$/,
use: [
"to-string-loader",
"css-loader",
{ loader: "postcss-loader", options: { plugins: postcssPlugins } }
]
},
{
test: /\.(png|jpe?g|gif)$/,
use: ["file-loader"]
}
]
},
plugins: [
new CleanWebpackPlugin(),
new CopyWebpackPlugin([
{
from: "public",
to: "",
ignore: ["index.html"]
}
]),
new HtmlWebpackPlugin({
template: "public/index.html",
inject: true,
minify: isProduction ? htmlMinifyOptions : false,
isProduction
}),
new AssetsManifestPlugin({
output: "assets-manifest.json",
publicPath: "dist"
}),
isProduction &&
new SWPrecacheWebpackPlugin({
cacheId: `${pkg.name}-v${pkg.version}-${Date.now()}`,
filename: "service-worker.js",
minify: true,
staticFileGlobsIgnorePatterns: [/\.map$/, /assets-manifest\.json$/]
})
].filter(Boolean),
optimization: {
minimize: isProduction,
minimizer: [
new TerserWebpackPlugin({
terserOptions: {
warning: false,
mangle: true,
output: {
ecma: 5,
comments: false,
ascii_only: true
}
}
})
],
splitChunks: {
chunks: "all",
cacheGroups: {
vendors: {
test: /node_modules/,
priority: -10
}
}
}
},
resolve: {
extensions: [".ts", ".js"]
}
});