-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
157 lines (151 loc) · 3.92 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
156
157
const path = require("path");
const webpack = require("webpack");
const sass = require("sass");
const HTMLWebpackPlugin = require("html-webpack-plugin");
const { VueLoaderPlugin } = require("vue-loader");
const TerserPlugin = require("terser-webpack-plugin");
const dotenv = require("dotenv").config({ path: "./.env" });
const dotenvExample = require("dotenv").config({ path: "./.env.example" });
const sourcePath = path.resolve(__dirname, "src");
const indexFile = path.join(sourcePath, "index.html");
const javascriptEntry = path.join(sourcePath, "main.ts");
const publicPath = path.resolve(__dirname, "public");
const isProd = process.env.NODE_ENV === "production";
const variables = dotenv.parsed || dotenvExample.parsed;
// Merge inn all process.env values that match dotenv keys
Object.keys(process.env).forEach(key => {
if (key in variables) {
variables[key] = process.env[key];
}
});
module.exports = {
mode: process.env.NODE_ENV,
context: publicPath,
entry: javascriptEntry,
output: {
path: `${publicPath}/dist/`,
publicPath: "/dist/",
filename: "[name].[contenthash].js",
clean: true
},
module: {
rules: [
{
test: /\.js$/,
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"]
},
exclude: /node_modules/
},
{
test: /\.vue$/,
use: ["vue-loader"]
},
{
test: /\.ts$/,
loader: "ts-loader",
exclude: /node_modules/,
options: {
appendTsSuffixTo: [/\.vue$/]
}
},
{
test: /\.scss$/,
use: [
"vue-style-loader",
// isProd ? MiniCssExtractPlugin.loader : "vue-style-loader",
"css-loader",
"sass-loader"
]
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: "file-loader",
options: {
name: "[name].[ext]?[hash]"
}
}
]
},
plugins: [
new VueLoaderPlugin(),
new HTMLWebpackPlugin({
template: indexFile,
filename: "index.html",
minify: isProd
}),
new webpack.DefinePlugin({
"process.env": JSON.stringify(variables)
})
],
resolve: {
extensions: [".js", ".ts", ".vue", ".json", ".scss"],
alias: {
vue: "@vue/runtime-dom",
"@": path.resolve(__dirname, "src"),
src: path.resolve(__dirname, "src"),
assets: `${publicPath}/assets`,
components: path.resolve(__dirname, "src/components")
}
},
devtool: "source-map",
performance: {
hints: false
},
optimization: {
splitChunks: {
chunks: "all",
maxInitialRequests: Infinity,
minSize: 0,
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name(module) {
// get the name. E.g. node_modules/packageName/not/this/part.js
// or node_modules/packageName
const packageName = module.context.match(
/[\\/]node_modules[\\/](.*?)([\\/]|$)/
)[1];
// npm package names are URL-safe, but some servers don't like @ symbols
return `npm.${packageName.replace("@", "")}`;
}
}
}
},
minimizer: [
new TerserPlugin({
parallel: true,
terserOptions: {
sourceMap: true
// https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
}
})
]
},
devServer: {
static: publicPath,
historyApiFallback: {
index: "/dist/index.html"
},
compress: true,
hot: true,
port: 8080
}
};
if (isProd) {
module.exports.mode = "production";
module.exports.devtool = false;
module.exports.performance.hints = "warning";
}
// enable proxy for anything that hits /Api
// View README or update src/config.ts:SEASONED_API_URL
const { SEASONED_API } = process.env;
if (SEASONED_API) {
module.exports.devServer.proxy = {
"/api": {
target: SEASONED_API,
changeOrigin: true
}
};
}