-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
173 lines (166 loc) · 4.22 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* Configure HTMLWebpack plugin */
// Import all app configs
const appConfig = require("./build/app.js");
const appConfigDev = require("./build/dev.js");
const appConfigProduction = require("./build/prod.js");
const argv = require("yargs").argv;
const _ = require("lodash");
const ENV = argv.env || "dev";
const settings = composeConfig(ENV);
settings.env = ENV;
console.log("WEBPACK started with this settings:", settings);
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const HTMLWebpackPluginConfig = new HtmlWebpackPlugin({
template: __dirname + "/src/index.html",
filename: "index.html",
inject: "body"
});
const VueLoaderPlugin = require("vue-loader/lib/plugin");
/* Configure BrowserSync */
const BrowserSyncPlugin = require("browser-sync-webpack-plugin");
const BrowserSyncPluginConfig = new BrowserSyncPlugin(
{
host: "localhost",
port: 3000,
proxy: "http://localhost:8080/"
},
(config = {
reload: false
})
);
/* Configure ProgressBar */
const ProgressBarPlugin = require("progress-bar-webpack-plugin");
const ProgressBarPluginConfig = new ProgressBarPlugin();
const plugins = [
HTMLWebpackPluginConfig,
BrowserSyncPluginConfig,
ProgressBarPluginConfig,
new CopyPlugin({
patterns:[
{ from: "assets", to: "assets" },
{ from: "fonts", to: "fonts" },
{ from: "static", to: "" }
]
}),
new VueLoaderPlugin()
/*
new WorkboxPlugin.GenerateSW({
clientsClaim: true,
skipWaiting: true
})
*/
];
if (ENV === "prod") {
//PWA
const WorkboxPlugin = require("workbox-webpack-plugin");
plugins.push(
new WorkboxPlugin.GenerateSW({
clientsClaim: true,
skipWaiting: true
})
);
}
/* Export configuration */
module.exports = {
mode: settings.mode,
devtool: settings.devtool,
entry: {
main: "./src/index.ts"
},
output: {
path: __dirname + "/dist",
filename: "[name]_[contenthash].bundle.js"
// chunkFilename: "[name].chunk.js"
},
// got from here https://medium.com/hackernoon/the-100-correct-way-to-split-your-chunks-with-webpack-f8a9df5b7758
optimization: {
runtimeChunk: "single",
minimize: true,
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("@", "")}`;
}
}
}
}
},
module: {
rules: [
{
test: /\.(woff|woff2|ttf|eot)$/,
use: "file-loader?name=fonts/[name].[ext]!static"
},
{
test: /\.tsx?$/,
loader: "ts-loader",
exclude: /node_modules/,
options: {
appendTsSuffixTo: [/\.vue$/]
}
},
{
test: /\.css$/,
exclude: /[\/\\]src[\/\\]/,
use: [
{
loader: "style-loader",
options: { injectType: "singletonStyleTag" }
},
{
loader: "css-loader"
}
]
},
{
test: /\.css$/,
exclude: /[\/\\](node_modules|bower_components|public)[\/\\]/,
use: [
{
loader: "style-loader",
options: { injectType: "singletonStyleTag" }
},
{
loader: "css-loader",
options: {}
}
]
},
{
test: /\.vue$/,
loader: "vue-loader"
}
]
},
resolve: {
extensions: [".web.ts", ".web.js", ".ts", ".js", ".vue"],
alias: {
vue$: "vue/dist/vue.esm.js"
},
fallback: {
"stream": require.resolve("stream-browserify"),
"buffer": require.resolve("buffer/")
}
},
plugins: plugins
};
function composeConfig(env) {
if (env === "dev") {
return _.merge({}, appConfig, appConfigDev);
}
if (env === "prod") {
return _.merge({}, appConfig, appConfigProduction);
}
}