forked from vladislavtkachenko/skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
312 lines (295 loc) · 7.02 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
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
const path = require('path');
const webpack = require('webpack');
/**
* PostCSS plugins
*/
const postCssCssNano = require('cssnano');
const postCssAutoprefixer = require('autoprefixer');
const postCssUrl = require('postcss-url');
const postCssPresetEnv = require('postcss-preset-env');
const postCssFlexBugsFixes = require('postcss-flexbugs-fixes');
/**
* Webpack plugins
*/
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const ExtractCssChunks = require('extract-css-chunks-webpack-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
/**
* Webpack server config
*/
const IS_PRODUCTION = process.env.NODE_ENV === 'production';
const SERVER_HOST = 'localhost';
const SERVER_PORT = 3000;
/**
* Webpack config variables
*/
const PATH_BASE = '/webpack/'
const PATH_ASSET = IS_PRODUCTION ? PATH_BASE : `http://${SERVER_HOST}:${SERVER_PORT}${PATH_BASE}`;
const PATH_SRC = path.resolve(__dirname, 'resources', 'webpack');
const PATH_BUILD = path.resolve(__dirname, 'public','webpack');
const PATH_PUBLIC = path.resolve(__dirname, 'public');
/**
* Функция обработки файлов стилей
*
* @param {boolean} isLoadResources - флаг использования sass-resources-loader
* @param {boolean} isSassSyntax - флаг использования синтаксиса sass
* @returns {any[]}
*/
const styleLoader = (isLoadResources = true, isSassSyntax = true) => {
const loaders = [
ExtractCssChunks.loader,
{
loader: 'css-loader',
options: {
sourceMap: true,
},
}, {
loader: 'postcss-loader',
options: {
sourceMap: true,
plugins: ((() => {
const plugins = [];
plugins.push(
postCssFlexBugsFixes(),
postCssPresetEnv(),
postCssUrl(),
postCssAutoprefixer({
browsers: ['ie >= 9', 'last 4 version', '> 1%', 'safari >= 9', 'ios >= 9'],
}),
);
if (IS_PRODUCTION) {
plugins.push(
postCssCssNano(),
);
}
return plugins;
})()),
},
},
{
loader: 'sass-loader',
options: {
sourceMap: true,
indentedSyntax: isSassSyntax,
includePaths: [
path.resolve(__dirname, 'node_modules'),
path.resolve(PATH_SRC),
],
},
},
];
if (isLoadResources) {
loaders.push({
loader: 'sass-resources-loader',
options: {
sourceMap: true,
resources: path.resolve(PATH_SRC, 'common', 'index.scss'),
},
});
}
return loaders;
};
/**
* Config of Webpack
*/
const config = {
mode: process.env.NODE_ENV,
context: PATH_SRC,
entry: {
app: [path.resolve(PATH_SRC, 'index.js')],
},
output: {
filename: 'js/[name].js?[hash]',
path: PATH_BUILD,
publicPath: PATH_ASSET,
chunkFilename: 'js/[name].js?[hash]',
},
optimization: {
minimizer: [
new UglifyJsPlugin({
sourceMap: true,
uglifyOptions: {
sourceMap: true,
output: {
comments: false,
},
},
}),
],
splitChunks: {
cacheGroups: {
default: false,
vendors: {
test: /[\\/]node_modules[\\/]/,
priority: 1,
name: 'vendors',
chunks: 'initial',
enforce: true,
},
},
},
runtimeChunk: {
name: "manifest",
},
},
module: {
rules: [
{
test: /\.(js|jsx|es6)$/,
loader: 'babel-loader',
options: {
configFile: path.resolve(__dirname, 'babel.config.js'),
},
exclude: file => /node_modules/.test(file) && !/\.vue\.js/.test(file),
},
{
test: /\.vue$/,
use: 'vue-loader',
},
{
test: /\.(jpe?g|png|gif|svg|ico)$/i,
use: [
{
loader: 'file-loader',
options: {
name: 'images/[name]-[hash:8].[ext]',
},
},
],
exclude: [
path.resolve(PATH_SRC, 'fonts'),
path.resolve(PATH_SRC, 'images'),
/inline/i,
],
},
{
test: /\.svg$/,
use: [
{
loader: 'svg-inline-loader?classPrefix',
},
{
loader: 'svgo-loader',
options: {
plugins: [
{ removeViewBox: false },
],
},
},
],
include: [
/inline/i,
],
},
{
test: /\.(woff|woff2|eot|otf|ttf|svg)(\?.*$|$)/,
use: [
{
loader: 'file-loader',
options: {
name: 'fonts/[name]-[hash:8].[ext]',
},
},
],
include: [
path.resolve(__dirname, 'node_modules'),
path.resolve(PATH_SRC, 'fonts'),
],
exclude: [
/inline/i,
],
},
{
test: /\.sass$/,
use: styleLoader(),
},
{
test: /\.scss$/,
use: styleLoader(true, false),
},
{
test: /\.css$/,
use: styleLoader(false, false),
},
{
test: /\.pug$/,
oneOf: [
{
resourceQuery: /^\?vue/,
use: ['pug-plain-loader'],
},
{
use: ['pug-loader']
}
]
},
],
},
resolve: {
modules: [
'node_modules',
path.resolve(__dirname, 'node_modules'),
path.resolve(PATH_SRC),
],
// alias: {
// vue$: 'vue/dist/vue.common.js',
// },
extensions: ['*', '.js', '.es6', '.jsx', '.vue', '.css', '.scss', '.sass'],
},
devtool: IS_PRODUCTION ? 'none' : 'inline-cheap-source-map',
stats: {
// copied from `'minimal'`
all: false,
modules: true,
maxModules: 0,
errors: true,
warnings: true,
// our additional options
moduleTrace: true,
errorDetails: true,
},
devServer: {
stats: {
// copied from `'minimal'`
all: false,
modules: true,
maxModules: 0,
errors: true,
warnings: true,
// our additional options
moduleTrace: true,
errorDetails: true,
},
clientLogLevel: 'error',
host: SERVER_HOST,
port: SERVER_PORT,
headers: { 'Access-Control-Allow-Origin': '*' },
contentBase: path.resolve(PATH_SRC),
},
plugins: [
new ExtractCssChunks({
filename: 'css/[name].css?[hash]',
chunkFilename: 'css/[name].css?[hash]',
hot: IS_PRODUCTION,
}),
new webpack.DefinePlugin({
'process.env': { NODE_ENV: JSON.stringify(process.env.NODE_ENV) },
}),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
}),
new VueLoaderPlugin(),
],
};
config.plugins.push(
new ManifestPlugin({
fileName: path.resolve(PATH_PUBLIC, 'mix-manifest.json'),
publicPath: PATH_ASSET,
basePath: PATH_BASE,
writeToFileEmit: true,
}),
);
module.exports = config;