-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwebpack.mix.js
More file actions
205 lines (189 loc) · 5.4 KB
/
webpack.mix.js
File metadata and controls
205 lines (189 loc) · 5.4 KB
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
/**
* Connect dependencies
*/
const mix = require('laravel-mix');
const glob = require('glob');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
/**
* Setup options
* https://laravel-mix.com/docs/6.0/api#optionsoptions
*/
mix.options({
processCssUrls: false,
});
mix.disableNotifications();
function applyFontRule(fontPath) {
mix.js(`webfonts-loader/${fontPath}.font.js`, `assets/build/fonts/${fontPath}`)
.webpackConfig({
devtool: false,
module: {
rules: [
{
test: /webfonts-loader(\/|\\).*\.font\.js$/,
use: [
{loader: MiniCssExtractPlugin.loader},
{
loader: 'css-loader',
options: {url: false, sourceMap: false},
},
{loader: 'webfonts-loader'},
],
},
],
},
});
}
applyFontRule('block-icons');
applyFontRule('icons');
/**
* Setup options for dev mode
* In main ESLint config we can use 'overrides' for special files. For example:
* 'overrides': [
* {
* 'env': {
* 'node': true
* },
* 'files': [
* 'some-file.{js,jsx}'
* ],
* 'parserOptions': {
* 'sourceType': 'script'
* },
* 'rules': {
* 'indent': [
* 'error',
* 4
* ]
* }
* }
* ]
*/
if (!mix.inProduction()) {
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const ESLintWebpackPlugin = require('eslint-webpack-plugin');
const StylelintWebpackPlugin = require('stylelint-webpack-plugin');
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
mix.sourceMaps();
mix.webpackConfig({
devtool: 'inline-source-map', // or 'source-map'
plugins: [
/**
*Remove assets files(css, js) from build folders
*/
new CleanWebpackPlugin({
verbose: true,// Write Logs to Console (Always enabled when dry is true)
dry: false,// Simulate the removal of files
cleanStaleWebpackAssets: false,// Automatically remove all unused webpack assets on rebuild
protectWebpackAssets: false,// Do not allow removal of current webpack assets
//Removes files once prior to Webpack compilation Not included in rebuilds (watch mode)
cleanOnceBeforeBuildPatterns: [
'**/build/**/*.{css,js,map,txt}',
'!vendor/**',
'!vendor-custom/**',
'!node_modules/**',
],
}),
/**
* BrowserSync runs on dev mode only
*/
new BrowserSyncPlugin({
/**
* Proxying to nginx container with alias APP_DOMAIN
* Proxy should be the same as WP_SITEURL in wp-config.php
*/
proxy: getAppUrl(),
/**
* Set external host network IP.
* If hostIp is undefined, just find your local network IP in your system
* and use it in your other devices browser to sync with BrowserSync.
*/
host: getHostIp(),
port: 3000,
open: false,
/**
* No files will be tracked, browser reloads after assets was build.
*/
files: [],
/**
* Debug mode
*/
//logLevel: 'debug',
//logPrefix: 'Browsersync',
}),
/**
* Code QA
*/
new ESLintWebpackPlugin({
fix: false,
extensions: ['js', 'jsx'],
overrideConfigFile: '.eslintrc.json',
failOnError: false,
cache: true,
}),
new StylelintWebpackPlugin({
fix: false,
extensions: ['scss'],
configFile: '.stylelintrc.json',
failOnError: false,
files: [
'assets/src/**/*.scss',
'blocks/!(_)**/src/*.scss',
],
cache: true,
}),
],
});
function getAppUrl() {
const appProtocol = process.env.APP_PROTOCOL;
const appDomain = process.env.APP_DOMAIN;
let appPort = '';
if (appProtocol === 'https') {
appPort = process.env.APP_HTTPS_PORT;
} else {
appPort = process.env.APP_HTTP_PORT;
}
let appUrl = appProtocol + '://' + appDomain;
if (appPort !== '80' && appPort !== '443') {
appUrl += ':' + appPort;
}
return appUrl;
}
function getHostIp() {
return process.env.HOST_IP || 'your.local.network.ip';
}
}
/**
* Read the folders and look for assets files.
*
* Files with names start with '_' will be ignored
* For example, 'partials/_body.scss' just need to include to main file
*
* Block folders that names start with '_' will be ignored too.
* Example, '_StarterBlock' - should not be registered
*/
const allAssets = glob.sync(
'{assets/src/styles/!(_)*.scss,assets/src/js/*.{js,jsx},assets/src/js/bootstrap/*.{js,jsx}}')
.concat(
glob.sync('{blocks/!(_)**/src/!(_)*.scss,blocks/!(_)**/src/*.{js,jsx}}'));
/**
* Run Preprocessing
*/
allAssets.forEach(assetPath => {
if (assetPath.endsWith('.scss')) {
mix.sass(
assetPath,
assetPath
.replace(/\/src\//, '/build/')
.replace(/\\src\\/, '\\build\\')
.replace(/\.(scss)$/, '.css'),
);
} else if (assetPath.endsWith('.js') || assetPath.endsWith('.jsx')) {
mix.js(
assetPath,
assetPath
.replace(/\/src\//, '/build/')
.replace(/\\src\\/, '\\build\\')
.replace(/\.(jsx)$/, '.js'),
);
}
});