-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathwebpack.mix.js
372 lines (350 loc) Β· 10.9 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/**
* ===========================
* Agency Webpack-Mix Config
* A capable website/webapp config built for the modern web agency.
* https://github.com/ben-rogerson/agency-webpack-mix-config
* ===========================
*
* Contents
*
* ποΈ Settings
* π Templates
* π Hashing
* π¨ Styles
* π¨ Styles: CriticalCSS
* π¨ Styles: PurgeCSS
* π¨ Styles: PostCSS
* π¨ Styles: Polyfills
* π¨ Styles: Vendor
* π¨ Styles: Other
* π Scripts
* π Scripts: Polyfills
* π Scripts: Auto import libraries
* π Scripts: Linting
* π Images
* π Icons
* ποΈ Static
* π§ Webpack-dev-server
*/
// ποΈ Base config
const config = {
// Dev domain to proxy
devProxyDomain: process.env.DEFAULT_SITE_URL || "http://site.test",
// Paths to observe for changes then trigger a full page reload
devWatchPaths: ["src/templates"],
// Port to use with webpack-dev-server
devServerPort: 8080,
// Folders where purgeCss can look for used selectors
purgeCssGrabFolders: ["src"],
// Build a static site from the src/template files
buildStaticSite: true,
// Urls for CriticalCss to look for "above the fold" Css
criticalCssUrls: [
// { urlPath: "/", label: "index" },
// { urlPath: "/about", label: "about" },
],
// Folder served to users
publicFolder: "web",
// Foldername for built src assets (publicFolder base)
publicBuildFolder: "dist",
}
// ποΈ Imports
const mix = require("laravel-mix")
const path = require("path")
const globby = require("globby")
// ποΈ Source folders
const source = {
icons: path.resolve("src/icons"),
images: path.resolve("src/images"),
scripts: path.resolve("src/scripts"),
styles: path.resolve("src/styles"),
static: path.resolve("src/static"),
templates: path.resolve("src/templates"),
}
// ποΈ Misc
mix.setPublicPath(config.publicFolder)
mix.disableNotifications()
mix.webpackConfig({ resolve: { alias: source } })
!mix.inProduction() && mix.sourceMaps()
/**
* π Templates (for static sites)
* Convert Twig files to Html
* https://github.com/ben-rogerson/laravel-mix-twig-to-html
*/
if (config.buildStaticSite && source.templates) {
require("laravel-mix-twig-to-html")
mix.twigToHtml({
files: [
{
template: path.resolve(
__dirname,
source.templates,
"**/*.{twig,html}"
),
minify: {
collapseWhitespace: mix.inProduction(),
removeComments: mix.inProduction(),
},
},
],
fileBase: source.templates,
twigOptions: {
data: require(path.join(source.templates, "_data", "data.js")),
},
})
}
/**
* π Hashing (for non-static sites)
* Mix has querystring hashing by default, eg: main.css?id=abcd1234
* This script converts it to filename hashing, eg: main.abcd1234.css
* https://github.com/JeffreyWay/laravel-mix/issues/1022#issuecomment-379168021
*/
if (mix.inProduction() && !config.buildStaticSite) {
// Allow versioning in production
mix.version()
// Get the manifest filepath for filehash conversion
const manifestPath = path.join(config.publicFolder, "mix-manifest.json")
// Run after mix finishes
mix.then(() => {
const convertToFileHash = require("laravel-mix-make-file-hash")
convertToFileHash({
publicPath: config.publicFolder,
manifestFilePath: manifestPath,
})
})
}
/**
* π¨ Styles: Main
* Uses dart-sass which has a replica API to Node-Sass
* https://laravel-mix.com/docs/4.0/css-preprocessors
* https://github.com/sass/node-sass#options
*/
// Get a list of style files within the base styles folder
const styleFiles = globby.sync(`${source.styles}/*.{scss,sass}`)
// Data to send to style files
const styleData = `$isDev: ${!mix.inProduction()};`
// Create an asset for every style file
styleFiles.forEach(styleFile => {
mix.sass(
styleFile,
path.join(config.publicFolder, config.publicBuildFolder),
{ prependData: styleData }
)
})
/**
* π¨ Styles: CriticalCSS
* https://github.com/addyosmani/critical#options
*/
const criticalDomain = config.devProxyDomain
if (criticalDomain && config.criticalCssUrls && config.criticalCssUrls.length) {
require("laravel-mix-critical")
const url = require("url")
mix.critical({
enabled: mix.inProduction(),
urls: config.criticalCssUrls.map(page => ({
src: url.resolve(criticalDomain, page.urlPath),
dest: path.join(
config.publicFolder,
config.publicBuildFolder,
`${page.label}-critical.css`
),
})),
options: {
width: 1200,
height: 1200,
},
})
}
/**
* π¨ Styles: PurgeCSS
* https://github.com/spatie/laravel-mix-purgecss#usage
*/
if (config.purgeCssGrabFolders.length) {
require("laravel-mix-purgecss")
mix.purgeCss({
enabled: mix.inProduction(),
folders: config.purgeCssGrabFolders, // Folders scanned for selectors
whitelist: ["html", "body", "active", "wf-active", "wf-inactive"],
whitelistPatterns: [],
extensions: ["php", "twig", "html", "js", "mjs", "vue"],
})
}
/**
* π¨ Styles: PostCSS
* Extend Css with plugins
* https://laravel-mix.com/docs/4.0/css-preprocessors#postcss-plugins
*/
const postCssPlugins = [
// https://tailwindcss.com/docs/installation/#laravel-mix
// require('tailwindcss')('./tailwind.config.js'),
/**
* π¨ Styles: Polyfills
* Postcss preset env lets you use pre-implemented css features
* See https://cssdb.org/ for supported features
* https://github.com/csstools/postcss-preset-env#readme
*/
require("postcss-preset-env")({ stage: 2 }),
]
mix.options({ postCss: postCssPlugins })
/**
* π¨ Styles: Other
* https://laravel-mix.com/docs/4.0/options
*/
mix.options({
// Disable processing css urls for speed
// https://laravel-mix.com/docs/4.0/css-preprocessors#css-url-rewriting
processCssUrls: false,
})
/**
* π Scripts: Main
* Script files are transpiled to vanilla JavaScript
* https://laravel-mix.com/docs/4.0/mixjs
*/
const scriptFiles = globby.sync(`${source.scripts}/*.{js,mjs}`)
scriptFiles.forEach(scriptFile => {
mix.js(scriptFile, config.publicBuildFolder)
})
/**
* π Scripts: Polyfills
* Automatically add polyfills for target browsers with core-js@3
* See "browserslist" in package.json for your targets
* https://github.com/zloirock/core-js/blob/master/docs/2019-03-19-core-js-3-babel-and-a-look-into-the-future.md
* https://github.com/scottcharlesworth/laravel-mix-polyfill#options
*/
require("laravel-mix-polyfill")
mix.polyfill({
enabled: mix.inProduction(),
useBuiltIns: "usage", // Only add a polyfill when a feature is used
targets: false, // "false" makes the config use browserslist targets in package.json
corejs: 3,
debug: false, // "true" to check which polyfills are being used
})
/**
* π Scripts: Auto import libraries
* Make JavaScript libraries available without an import
* https://laravel-mix.com/docs/4.0/autoloading
*/
mix.autoload({
jquery: ["$", "jQuery", "window.jQuery"],
})
/**
* π Scripts: Vendor
* Separate the JavaScript code imported from node_modules
* https://laravel-mix.com/docs/4.0/extract
* Without mix.extract you'll see an annoying js error after
* launching the dev server - this should be fixed in webpack 5
*/
mix.extract([]) // Empty params = separate all node_modules
// mix.extract(['jquery']) // Specify packages to add to the vendor file
/**
* π Scripts: Linting
*/
if (!mix.inProduction()) {
require("laravel-mix-eslint")
mix.eslint()
}
/**
* π Images
* Images are optimized and copied to the build directory
* https://github.com/CupOfTea696/laravel-mix-imagemin
* https://github.com/Klathmon/imagemin-webpack-plugin#api
*
* Important: laravel-mix-imagemin is incompatible with
* copy-webpack-plugin > 5.1.1, so keep that dependency at that version.
* See: https://github.com/CupOfTea696/laravel-mix-imagemin/issues/9
*/
require("laravel-mix-imagemin")
mix.imagemin(
{
from: path.join(source.images, "**/*"),
to: config.publicBuildFolder,
context: "src/images",
},
{},
{
gifsicle: { interlaced: true },
mozjpeg: { progressive: true, arithmetic: false },
optipng: { optimizationLevel: 3 }, // Lower number = speedier/reduced compression
svgo: {
plugins: [
{ convertPathData: false },
{ convertColors: { currentColor: false } },
{ removeDimensions: true },
{ removeViewBox: false },
{ cleanupIDs: false },
],
},
}
)
/**
* π Icons
* Individual SVG icons are optimised then combined into a single cacheable SVG
* https://github.com/kisenka/svg-sprite-loader#configuration
*/
require("laravel-mix-svg-sprite")
mix.svgSprite(source.icons, path.join(config.publicBuildFolder, "sprite.svg"), {
symbolId: filePath => `icon-${path.parse(filePath).name}`,
extract: true,
})
// Icon options
mix.options({
imgLoaderOptions: {
svgo: {
plugins: [
{ convertColors: { currentColor: true } },
{ removeDimensions: false },
{ removeViewBox: false },
],
},
},
})
/**
* ποΈ Static
* Additional folders with no transform requirements are copied to your build folders
*/
mix.copyDirectory(
source.static,
path.join(config.publicFolder, config.publicBuildFolder)
)
/**
* π§ Webpack-dev-server
* https://webpack.js.org/configuration/dev-server/
*/
mix.webpackConfig({
devServer: {
clientLogLevel: "none", // Hide console feedback so eslint can take over
open: true,
overlay: true,
port: config.devServerPort,
public: `localhost:${config.devServerPort}`,
host: "0.0.0.0", // Allows access from network
https: config.devProxyDomain.includes("https://"),
contentBase: config.devWatchPaths.length
? config.devWatchPaths
: undefined,
watchContentBase: config.devWatchPaths.length > 0,
watchOptions: {
aggregateTimeout: 200,
poll: 200, // Lower for faster reloads (more cpu intensive)
ignored: ["storage", "node_modules", "vendor"],
},
disableHostCheck: true, // Fixes "Invalid Host header error" on assets
headers: {
"Access-Control-Allow-Origin": "*",
},
proxy: {
"**": {
target: config.devProxyDomain,
changeOrigin: true,
secure: false,
},
},
publicPath: "/",
},
})
mix.options({
hmrOptions: {
host: 'localhost',
port: config.devServerPort
},
})