-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
112 lines (106 loc) · 2.56 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
const HtmlWebPackPlugin = require("html-webpack-plugin");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
.BundleAnalyzerPlugin;
const path = require("path");
const webpack = require("webpack");
const CompressionPlugin = require("compression-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");
let config = {
output: {
path: path.resolve(__dirname, "build"),
filename: "bundle.js",
},
resolve: {
modules: [path.join(__dirname, "src"), "node_modules"],
alias: {
react: path.join(__dirname, "node_modules", "react"),
},
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
},
},
{
test: /\.css$/,
use: [
{
loader: "style-loader",
},
{
loader: "css-loader",
},
],
},
],
},
plugins: [
new CopyPlugin({
patterns: [{ from: "public" }],
}),
new HtmlWebPackPlugin({
hash: true,
template: "./public/index.html",
filename: "./index.html", //relative to root of the application
}),
],
};
module.exports = (env, argv) => {
config.mode = argv.mode;
if (argv.mode === "development") {
config.devServer = {
compress: true,
hot: true,
contentBase: "./build",
historyApiFallback: true, //For react router
open: true,
};
config.output.publicPath = "/";
}
if (argv.mode === "production") {
config.entry = ["./src"];
config.devtool = "source-map";
config.output.filename = "[name].[chunkhash].bundle.js";
config.output.publicPath = "/";
config.output.chunkFilename = "[name].[chunkhash].bundle.js";
config.optimization = {
moduleIds: "hashed",
runtimeChunk: {
name: "manifest",
},
splitChunks: {
cacheGroups: {
vendors: {
test: /node_modules/,
name: "vendors",
chunks: "all",
},
},
},
};
config.plugins.push(
new BundleAnalyzerPlugin({
analyzerMode: "static",
}),
new CompressionPlugin({
test: /\.js(\?.*)?$/i,
})
);
config.performance = {
hints: "warning",
// Calculates sizes of gziped bundles.
assetFilter: function (assetFilename) {
return assetFilename.endsWith(".js.gz");
},
};
}
if (env === "production-gh") {
config.output.publicPath = "/music-search-web";
}
console.log("Webpack config\n");
return config;
};