This repository was archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
186 lines (174 loc) · 4.77 KB
/
webpack.config.js
File metadata and controls
186 lines (174 loc) · 4.77 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
// npm imports
const CleanWebpackPlugin = require('clean-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
.BundleAnalyzerPlugin;
const IgnoreEmitPlugin = require('ignore-emit-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const StyleLintPlugin = require('stylelint-webpack-plugin');
const WildcardsEntryWebpackPlugin = require('wildcards-entry-webpack-plugin-forked');
const path = require('path');
// custom imports
const CollectIgnoredWebpackHelper = require('./webpack-custom-helpers/collect-ignored-webpack-helper');
const getVendorName = require('./webpack-custom-helpers/vendor-name-generate-helper');
const pkg = require('./package.json');
/**
* Next object contains default entry points represents by key which is the name
* of our entry point and the value which is path. Do not forget to resolve the path
* if you are going to add some more entries as a defaults (path.resolve).
* @type Object
*/
const defaultEntries = { app: path.resolve(__dirname, './src/app.scss') };
/**
* grab entries via wild card and `WildcardsEntryWebpackPlugin`
*/
const entry = WildcardsEntryWebpackPlugin.entry(
'./src/components/**/*.+(scss|js)',
defaultEntries,
'components',
);
/**
* If our entry point is a scss file, put to next array corresponding js file
* to prevent creating dummy js file for each scss entry point.
* You have to do this only for default entry points from defined erlier
* `defaultEntries` Object
* @type Array
*/
const ignoredFiles = ['app.js'];
const ignoredEmitFiles = CollectIgnoredWebpackHelper.ignored(
'./src/components/**/*.+(js|scss)',
'./src/components/**/_*.scss',
ignoredFiles,
'.scss',
);
/**
* Next array defines list of the used plugins.
* @type Array
*/
const plugins = [
new MiniCssExtractPlugin({
filename: '[name].css',
}),
new CleanWebpackPlugin(path.resolve(__dirname, 'build'), {}),
new StyleLintPlugin({
configFile: path.resolve(__dirname, '.stylelintrc'),
context: path.resolve(__dirname, './src/'),
files: '**/*.scss',
failOnError: false,
quiet: false,
}),
new WildcardsEntryWebpackPlugin(),
];
/**
* set default stats
* @type Object
*/
let stats = {
children: false,
builtAt: true,
assets: false,
hash: false,
colors: true,
env: true,
modules: false,
entrypoints: false,
};
/**
* If we have `explore` script running push to the list of plugins `BundleAnalyzerPlugin`
*/
if (process.env.IS_EXPLORE) {
// fill free to check for available options https://github.com/webpack-contrib/webpack-bundle-analyzer
plugins.push(new BundleAnalyzerPlugin({}));
// reset stats to see everything
stats = {};
}
/**
* if we have `build` script running push to the list of plugins `IgnoreEmitPlugin`
* with array of ignored files
*/
if (process.env.NODE_ENV === 'production') {
plugins.push(new IgnoreEmitPlugin(ignoredEmitFiles));
}
module.exports = {
entry,
output: {
path: path.resolve(__dirname, 'build'),
publicPath: `${pkg.basePath}${pkg.name}/build/`,
chunkFilename: '[name].js',
filename: '[name].js',
},
stats,
performance: {
hints: 'warning',
},
devtool: false,
externals: {
jquery: 'jQuery',
},
module: {
rules: [
{
enforce: 'pre',
test: /\.js$/,
exclude: [/node_modules/],
loader: 'eslint-loader',
},
{
test: /\.js$/,
exclude: [/node_modules/],
use: {
loader: 'babel-loader',
},
},
{
test: /\.scss$/,
use: [
'style-loader',
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader',
],
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
loader: 'file-loader?name=assets/fonts/[name].[ext]',
},
{
test: /\.(jpe?g|png|gif)$/i,
loaders: 'file-loader?name=assets/images/[name].[ext]',
},
],
},
plugins,
optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
shared: {
test: /[\\/]src[\\/]shared[\\/]/,
name(module, chunks, cacheGroupKey) {
// generate a chunk name...
return getVendorName(chunks, cacheGroupKey);
},
minSize: 0,
},
externals: {
test: /[\\/]src[\\/]externals[\\/]/,
name(module, chunks, cacheGroupKey) {
// generate a chunk name...
return getVendorName(chunks, cacheGroupKey);
},
minSize: 0,
},
commons: {
test: /[\\/]node_modules[\\/]/,
name(module, chunks, cacheGroupKey) {
// generate a chunk name...
return getVendorName(chunks, cacheGroupKey);
},
minSize: 0,
},
},
},
},
};