-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebpack.mix.js
309 lines (289 loc) Β· 8.27 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
/**
* ===========================
* 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
* π¨ Styles
* π¨ Styles: CriticalCSS
* π¨ Styles: PostCSS
* π¨ Styles: Polyfills
* π¨ Styles: Vendor
* π¨ Styles: Other
* π Scripts
* π Scripts: Polyfills
* π Scripts: Auto import libraries
* π Scripts: Linting
* ποΈ Static
* π§ Webpack-dev-server
*/
require('dotenv').config()
// ποΈ Base config
const config = {
// Dev domain to proxy
devProxyDomain: process.env.BASE_URL || "http://site.test",
// Paths to observe for changes then trigger a full page reload
devWatchPaths: ["templates"],
// Port to use with webpack-dev-server
devServerPort: 8080,
// Build a static site from the src/template files
buildStaticSite: false,
// Urls for CriticalCss to look for "above the fold" Css
criticalCssUrls: [
// { urlPath: "/", label: "homepage"},
// { 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("templates"),
}
// ποΈ Misc
mix.setPublicPath(config.publicFolder)
mix.disableNotifications()
mix.webpackConfig({
resolve: { alias: source },
stats: {
children: true
}
})
!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 PostCSS to preproccess
*/
mix.postCss(
'./src/styles/site.css', path.join(config.publicFolder, config.publicBuildFolder)
)
/**
* π¨ Styles: CriticalCSS
* https://github.com/addyosmani/critical#options
*/
const criticalDomain = config.devProxyDomain
if (criticalDomain && config.criticalCssUrls && config.criticalCssUrls.length) {
require("laravel-mix-criticalcss")
mix.criticalCss({
enabled: true,
paths: {
base: criticalDomain,
templates: '../templates/_critical/', //Where css files need to be written, all these paths are relative to /public
suffix: '_critical.min'
},
urls: config.criticalCssUrls,
//Now using https://github.com/addyosmani/critical v4.0.1
options: {
//It's important to note here you should NOT set inline:true, this will break the whole system.
width: 1200,
height: 1200,
penthouse:{
timeout:1200000
}
},
})
}
/**
* π¨ Styles: PostCSS
* Extend Css with plugins
* https://laravel-mix.com/docs/4.0/css-preprocessors#postcss-plugins
*/
const postCssPlugins = [
/**
* π¨ 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-easy-import')(),
require('tailwindcss/nesting'),
require('tailwindcss'),
// require('postcss-object-fit-images'),
require('postcss-preset-env')({
stage: 1,
autoprefixer: { grid: false },
features: {
'focus-within-pseudo-class': false
}
}),
]
mix.options({ postCss: postCssPlugins })
/**
* π¨ Styles: Other
* https://laravel-mix.com/docs/4.0/options
*/
mix.options({
// postcss-preset-env already processes our css with Autoprefixer, so we don't
// need mix to do it twice.
autoprefixer: false,
// 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,ts,tsx}`)
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 copied to the build directory
*/
// mix.copy('src/static/fonts/icons', './vendor/dolphiq/iconpicker/src/resources-shared/fonts')
/**
* ποΈ 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
},
})