-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathwebpack.mix.js
executable file
·194 lines (156 loc) · 3.99 KB
/
webpack.mix.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const { join, resolve } = require('path')
const { copySync, removeSync } = require('fs-extra')
const mix = require('laravel-mix')
const findRemoveSync = require('find-remove')
require('laravel-mix-versionhash')
require('laravel-mix-workbox')
// const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const serviceWorkerConfig = {
additionalManifestEntries: [{ url: '/', revision: Date.now().toString() }],
navigateFallback: '/',
modifyURLPrefix: {
'//': '/'
},
// Do not precache images
exclude: [/\.(?:png|jpg|jpeg|txt)$/],
// Define runtime caching rules.
runtimeCaching: [
{
// Match any request from inside icon
urlPattern: /icon/,
// Apply a cache-first strategy.
handler: 'CacheFirst',
options: {
// Use a custom cache name.
cacheName: 'icons',
cacheableResponse: {
statuses: [0, 200]
}
}
},
{
// Match any request from api
urlPattern: /api/,
// Apply a network-first strategy.
handler: 'NetworkFirst',
options: {
// Use a custom cache name.
cacheName: 'apis',
cacheableResponse: {
statuses: [0, 200]
}
}
},
{
urlPattern: /\.(?:svg|ico)$/,
// Apply a cache-first strategy.
handler: 'CacheFirst',
options: {
// Use a custom cache name.
cacheName: 'images'
}
},
{
// Match any request that ends with .png, .jpg, .jpeg
urlPattern: /\.(?:png|jpg|jpeg)$/,
// Apply a cache-first strategy.
handler: 'CacheFirst',
options: {
// Use a custom cache name.
cacheName: 'images',
// Only cache 30 images.
expiration: {
maxEntries: 30,
maxAgeSeconds: 30 * 24 * 60 * 60 // 30 days
}
}
},
{
urlPattern: /^https:\/\/fonts\.googleapis\.com/,
// Apply a stale-while-revalidate strategy.
handler: 'StaleWhileRevalidate',
options: {
// Use a custom cache name.
cacheName: 'google-fonts-stylesheets'
}
},
{
urlPattern: /^https:\/\/fonts\.gstatic\.com/,
// Apply a cache-first strategy.
handler: 'CacheFirst',
options: {
// Use a custom cache name.
cacheName: 'google-fonts-webfonts',
cacheableResponse: {
statuses: [0, 200]
},
expiration: {
maxAgeSeconds: 60 * 60 * 24 * 365,
maxEntries: 30
}
}
}
],
skipWaiting: true
}
mix
.js('resources/js/app.js', 'public/dist/js')
.vue({
extractStyles: true
})
.sass('resources/sass/app.scss', 'public/dist/css')
.options({ processCssUrls: false })
.disableNotifications()
if (mix.inProduction()) {
mix
// .extract() // Disabled until resolved: https://github.com/JeffreyWay/laravel-mix/issues/1889
// .version() // Use `laravel-mix-versionhash` for the generating correct Laravel Mix manifest file.
.versionHash()
.generateSW(serviceWorkerConfig)
} else {
mix.sourceMaps()
}
let outputConfig = {
chunkFilename: 'dist/js/[chunkhash].js',
path: resolve(__dirname, mix.inProduction() ? './public/build' : './public')
}
if (mix.inProduction()) {
outputConfig = {
...outputConfig,
publicPath: '/'
}
}
mix.webpackConfig({
plugins: [
// new BundleAnalyzerPlugin()
],
resolve: {
extensions: ['.js', '.json', '.vue'],
alias: {
'~': join(__dirname, './resources/js')
}
},
output: outputConfig
})
mix.then(() => {
process.nextTick(() => {
cleanOldBuild()
if (mix.inProduction()) {
publishAssets()
}
})
})
function cleanOldBuild () {
const publicDir = resolve(__dirname, './public')
findRemoveSync(publicDir, {
maxLevel: 1,
files: 'workbox|worker',
regex: true
})
}
function publishAssets () {
const publicDir = resolve(__dirname, './public')
removeSync(join(publicDir, 'dist'))
copySync(join(publicDir, 'build'), publicDir)
removeSync(join(publicDir, 'build'))
}