-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
164 lines (159 loc) · 4.3 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
const path = require('path')
const webpack = require('webpack')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractCssChunks = require('extract-css-chunks-webpack-plugin')
const { GenerateSW } = require('workbox-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const CompressionPlugin = require('compression-webpack-plugin')
const isProduction = process.argv.indexOf('-p') !== -1 // Check if we are in production mode
const apiURL = process.env.API_URL || "/api/tranvia"
const cleanOptions = {
root: path.resolve(__dirname),
exclude: ['.gitkeep'],
verbose: true,
dry: false
}
const BUILD_DIR = path.resolve(__dirname, 'dist')
const APP_DIR = path.resolve(__dirname, 'src')
module.exports = {
mode: isProduction ? 'production' : 'development',
entry: {
main: APP_DIR + '/index.js'
},
target: 'web',
optimization: {
splitChunks: {
cacheGroups: {
vendor: {
test: /preact|preact-compat|react-router-dom|decko|react-ink|unistore|webpack|workbox-webpack-plugin/,
chunks: 'initial',
name: 'vendor',
}
}
}
},
output: {
path: BUILD_DIR,
publicPath: '/',
filename: '[name]-[hash:6].js',
chunkFilename: '[name]-[hash:6].js'
},
module: {
rules: [
{
test: /\.js?$/,
include: APP_DIR,
use: ['babel-loader']
},
{
test: /\.scss$/,
use: [
ExtractCssChunks.loader,
{
loader: 'css-loader',
options: {
modules: true,
localIdentName: 'tf-tr[hash:6]'
}
},
'postcss-loader',
'sass-loader'
]
},
{
test: /\.(png|jpg|jpeg|gif|svg)$/,
loader: 'file-loader',
options: {
name: 'assets/img/[name].[ext]?[hash:6]'
}
},
{
test: /\.(woff2|woff)$/,
loader: 'file-loader',
options: {
name: 'assets/fonts/[name].[ext]?[hash:6]'
}
}
]
},
devServer: {
contentBase: APP_DIR,
compress: true,
historyApiFallback: true,
stats: 'minimal',
hot: true,
inline: true,
port: 8081,
host: '0.0.0.0',
disableHostCheck: true,
proxy: {
"/api/tranvia": {
"target": 'https://tranviaonline.metrotenerife.com/api/infoStops/infoPanel',
"pathRewrite": { '^/api/tranvia': '' },
"changeOrigin": true,
"secure": false
}
}
},
resolve: {
alias: {
react: 'preact-compat',
'react-dom': 'preact-compat'
},
modules: [APP_DIR + '/components', APP_DIR + '/store', APP_DIR + '/scripts', 'node_modules']
},
plugins: [
new CleanWebpackPlugin(BUILD_DIR, cleanOptions),
isProduction ? new CompressionPlugin({
algorithm: 'gzip',
test: /\.(html|js|css|json|svg|png|jpeg)$/,
minRatio: 0.8
}) : new webpack.HotModuleReplacementPlugin(),
new webpack.DefinePlugin({
"__API__": `"${apiURL}"`
}),
new ExtractCssChunks({
filename: '[name]-[hash:6].css',
chunkFilename: '[name]-[hash:6].css',
hot: true,
cssModules: true
}),
new HtmlWebpackPlugin({
minify: {
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true
},
template: APP_DIR + '/index.html',
inject: false
}),
new CopyWebpackPlugin([
{ from: APP_DIR + '/assets/favicons', to: BUILD_DIR + '/assets/favicons'},
{ from: APP_DIR + '/assets/robots.txt', to: BUILD_DIR + '/robots.txt'},
{ from: APP_DIR + '/assets/.well-known', to: BUILD_DIR + '/.well-known' },
{ from: APP_DIR + '/privacy-policy.html', to: BUILD_DIR + '/privacy-policy.html' }
]),
new GenerateSW({
runtimeCaching: [
{
urlPattern: new RegExp(apiURL.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')),
handler: 'networkOnly'
},
{
urlPattern: new RegExp('^https://fonts.(?:googleapis|gstatic).com/(.*)'),
handler: 'cacheFirst'
},
{
urlPattern: /\/assets\/.*/,
handler: 'cacheFirst'
}
],
navigateFallback: '/',
swDest: "sw.js",
directoryIndex: 'index.html',
clientsClaim: true,
skipWaiting: true
})
]
}