-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
246 lines (244 loc) · 7.43 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
const path = require('path');
const PnpWebpackPlugin = require('pnp-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const eslintCodeframeFormatter = require('eslint-codeframe-formatter');
const LiveReloadPlugin = require('webpack-livereload-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const StylelintWebpackPlugin = require('stylelint-webpack-plugin');
const stylelintFormatterPretty = require('stylelint-formatter-pretty');
const imageminGifsicle = require('imagemin-gifsicle');
const imageminMozjpeg = require('imagemin-mozjpeg');
const imageminPngquant = require('imagemin-pngquant');
const imageminSvgo = require('imagemin-svgo');
const WebpackManifestPlugin = require('webpack-manifest-plugin');
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
const notifier = require('node-notifier');
const isDevelopment = (process.env.NODE_ENV === 'development');
const isProduction = (process.env.NODE_ENV === 'production');
module.exports = {
entry: {
styles: './assets/styles.scss',
app: './assets/app.jsx',
},
output: {
path: path.resolve(__dirname, 'build'), // where to put compiled files to
filename: isDevelopment ? '[name].dev.js' : '[name].[contenthash].js',
chunkFilename: isDevelopment ? '[name].dev.js' : '[name].[contenthash].js',
jsonpFunction: 'wpreactjsonp', // pick the unique jsonp function name, so the react apps doesn't collide
pathinfo: false,
},
devtool: isDevelopment ? 'cheap-module-eval-source-map' : 'source-map',
stats: {
children: false,
chunks: false,
colors: true,
depth: false,
env: true,
modules: false,
reasons: false,
},
resolve: {
symlinks: false,
plugins: [PnpWebpackPlugin],
extensions: ['.js', '.json', '.jsx'],
},
resolveLoader: {
plugins: [PnpWebpackPlugin.moduleLoader(module)],
},
externals: { // We don't need to bundle some libraries that are already included in WordPress
'react': 'React',
'react-dom': 'ReactDOM',
'jquery': 'jQuery',
'@wordpress/i18n': 'wp.i18n',
},
optimization: {
splitChunks: {
chunks: 'all', // This allows us to split the code into small chunks that loads faster
},
},
module: {
strictExportPresence: true,
rules: [
{
parser: {
requireEnsure: false,
},
},
{ // check the code with eslint during compilation
test: /\.jsx?$/,
exclude: /node_modules/,
enforce: 'pre',
use: [
{
loader: require.resolve('eslint-loader'),
options: {
formatter: eslintCodeframeFormatter,
},
},
],
},
{ // compile js and jsx files with babel
test: /\.jsx?$/,
exclude: /node_modules/,
use: [
{
loader: require.resolve('babel-loader'),
},
],
},
{ // We want to handle (s)css files, but CSS modules will be handled differentlys
test: /\.s?css$/,
exclude: /\.module\.s?css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
sourceMap: true,
},
},
{
loader: require.resolve('css-loader'),
options: {
sourceMap: true,
importLoaders: 2,
},
},
{
loader: require.resolve('postcss-loader'),
options: {
sourceMap: true,
},
},
{
loader: require.resolve('sass-loader'),
options: {
sourceMap: true,
implementation: require('sass'),
},
},
],
},
{ // CSS modules can be used in React application
test: /\.s?css$/,
include: /\.module\.s?css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
sourceMap: true,
},
},
{
loader: require.resolve('css-loader'),
options: {
sourceMap: true,
importLoaders: 2,
modules: true,
},
},
{
loader: require.resolve('postcss-loader'),
options: {
sourceMap: true,
},
},
{
loader: require.resolve('sass-loader'),
options: {
sourceMap: true,
implementation: require('sass'),
},
},
],
},
{ // load the fonts
test: /.(eot|woff|woff2|ttf|otf)$/,
loader: require.resolve('file-loader'),
options: {
name: isDevelopment ? '[name].dev.[ext]' : '[name].[contenthash].[ext]',
outputPath: 'fonts',
},
},
{ // load and optimise images
test: /\.(gif|jpg|png|svg)$/,
use: [
{
loader: require.resolve('url-loader'),
options: {
limit: 8192,
name: isDevelopment ? '[name].dev.[ext]' : '[name].[contenthash].[ext]',
outputPath: 'images',
},
},
{
loader: require.resolve('img-loader'),
options: {
plugins: isProduction && [
imageminGifsicle({
interlaced: false,
}),
imageminMozjpeg({
progressive: true,
arithmetic: false,
}),
imageminPngquant({
floyd: 0.5,
speed: 2,
}),
imageminSvgo({
plugins: [
{ removeTitle: true },
{ cleanupAttrs: true },
{ removeXMLProcInst: true },
{ removeComments: true },
{ removeMetadata: true },
{ removeXMLNS: false },
{ removeEditorsNSData: true },
{ removeEmptyAttrs: true },
{ convertPathData: true },
{ convertTransform: true },
{ removeUnusedNS: true },
{ mergePaths: true },
{ convertShapeToPath: true },
],
}),
],
},
},
],
},
],
},
plugins: [
new CleanWebpackPlugin({ // Clean build folder when compilation starts
cleanStaleWebpackAssets: false,
}),
new LiveReloadPlugin({ // Auto reload the page during development
appendScriptTag: true,
protocol: 'http',
hostname: 'localhost',
}),
new MiniCssExtractPlugin({ // Extract CSS files
filename: isDevelopment ? '[name].dev.css' : '[name].[contenthash].css',
}),
new StylelintWebpackPlugin({ // Check the CSS with stylelint
formatter: stylelintFormatterPretty,
}),
new WebpackManifestPlugin({
fileName: 'assets-manifest.json', // This file helps us with loading the assets in WordPress
}),
new FriendlyErrorsWebpackPlugin({ // This will make the errors nicer in console
onErrors: (severity, errors) => {
if (severity !== 'error') {
return;
}
const error = errors[0];
notifier.notify({
title: 'Webpack error',
message: severity + ': ' + error.name,
subtitle: error.file || '',
});
}
}),
],
};