-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
261 lines (245 loc) · 6.37 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
const path = require('path');
const defaultConfig = require('@wordpress/scripts/config/webpack.config');
const RemovePlugin = require('remove-files-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const SVGSpritemapPlugin = require('svg-spritemap-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');
const StylelintPlugin = require('stylelint-webpack-plugin');
const glob = require('glob');
const postcssRTL = require('postcss-rtl');
// Function to check for the existence of files matching a pattern
function hasFiles(pattern) {
return glob.sync(pattern, { posix: true, dotRelative: true }).length > 0;
}
// Dynamically generate entry points for each file inside 'assets/scss/blocks'
const coreBlockEntryPaths = glob
.sync('./assets/scss/blocks/core/*.scss', {
posix: true,
dotRelative: true,
})
.reduce((acc, filePath) => {
const entryKey = filePath.split(/[\\/]/).pop().replace('.scss', '');
acc[`css/blocks/${entryKey}`] = filePath;
return acc;
}, {});
// Dynamically generate entry points for each block
const blockEntryPaths = glob
.sync('./assets/blocks/**/index.js', { posix: true, dotRelative: true })
.reduce((acc, filePath) => {
const entryKey = filePath
.replace('./assets/blocks/', '')
.replace('/index.js', '');
acc[`../blocks/${entryKey}/index`] = filePath;
return acc;
}, {});
const blockScssPaths = glob
.sync('./assets/blocks/**/style.scss', { posix: true, dotRelative: true })
.reduce((acc, filePath) => {
const entryKey = filePath
.replace('./assets/blocks/', '')
.replace('/style.scss', '');
acc[`../blocks/${entryKey}/style`] = filePath;
return acc;
}, {});
const styleScssPaths = glob
.sync('./assets/scss/_index.scss', { posix: true, dotRelative: true })
.reduce((acc, filePath) => {
const entryKey = 'style';
acc[`css/${entryKey}`] = filePath;
return acc;
}, {});
// CopyPlugin patterns to include PHP and JSON files
const copyPluginPatterns = [];
// Only add PHP and JSON patterns if these files exist
if (hasFiles('./assets/blocks/**/*.php')) {
copyPluginPatterns.push({
from: './assets/blocks/**/*.php',
to: ({ context, absoluteFilename }) => {
return absoluteFilename.replace(
`${context}/assets/blocks/`,
'../blocks/'
);
},
});
}
if (hasFiles('./assets/blocks/**/*.json')) {
copyPluginPatterns.push({
from: './assets/blocks/core/*.json',
to: ({ context, absoluteFilename }) => {
return absoluteFilename.replace(
`${context}/assets/blocks/`,
'../blocks/'
);
},
});
}
module.exports = {
...defaultConfig,
entry: {
...defaultConfig.entry,
admin: './assets/scss/editor.scss',
index: './assets/js/index.js',
variations: './assets/js/block-variations/index.js',
filters: './assets/js/block-filters/index.js',
...styleScssPaths,
...blockEntryPaths,
...blockScssPaths,
...coreBlockEntryPaths,
},
output: {
filename: (pathData) => {
const entryName = pathData.chunk.name;
if (
entryName.includes('css/blocks') ||
blockEntryPaths[entryName] ||
blockScssPaths[entryName]
) {
return '[name].js';
}
return 'js/[name].js';
},
path: path.resolve(__dirname, 'build'),
},
module: {
rules: [
{
test: /\.(webp|png|jpe?g|gif)$/,
type: 'asset/resource',
generator: {
filename: 'images/[name][ext]',
},
},
{
test: /\.(sa|sc|c)ss$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'postcss-loader',
options: {
postcssOptions: (loader) => {
const options = {
plugins: [require('autoprefixer')],
};
if (loader.filename) {
const isRTL =
loader.filename.includes('-rtl.css');
if (isRTL) {
options.plugins.push(postcssRTL());
}
}
return options;
},
},
},
'sass-loader',
],
},
{
test: /\.svg$/,
type: 'asset/inline',
use: 'svg-transform-loader',
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
type: 'asset/resource',
generator: {
filename: 'fonts/[name][ext]',
},
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react'],
},
},
},
],
},
plugins: [
...defaultConfig.plugins,
new MiniCssExtractPlugin({
filename: (pathData) => {
const entryName = pathData.chunk.name;
if (
entryName.includes('css/blocks') ||
blockEntryPaths[entryName] ||
blockScssPaths[entryName]
) {
return '[name].css';
}
if (entryName === 'css/style') {
return 'css/style.css';
}
return '[name].css';
},
}),
new CopyPlugin({
patterns: [
{
from: '**/*.{jpg,jpeg,png,gif,svg}',
to: 'images/[path][name][ext]',
context: path.resolve(process.cwd(), 'assets/images'),
noErrorOnMissing: true,
},
{
from: '*.svg',
to: 'images/icons/[name][ext]',
context: path.resolve(process.cwd(), 'assets/images/icons'),
noErrorOnMissing: true,
},
{
from: '**/*.{woff,woff2,eot,ttf,otf}',
to: 'fonts/[path][name][ext]',
context: path.resolve(process.cwd(), 'assets/fonts'),
noErrorOnMissing: true,
},
...copyPluginPatterns, // Include patterns for PHP and JSON files
],
}),
new SVGSpritemapPlugin('assets/images/icons/*.svg', {
output: {
filename: 'images/icons/sprite.svg',
},
sprite: {
prefix: false,
},
}),
new RemovePlugin({
after: {
log: false,
test: [
{
folder: path.resolve(__dirname, 'build/css/blocks'),
method: (absoluteItemPath) => {
return new RegExp(/\.js$/, 'm').test(
absoluteItemPath
);
},
},
{
folder: path.resolve(__dirname, 'build/css/blocks'),
method: (absoluteItemPath) => {
return new RegExp(/\.php$/, 'm').test(
absoluteItemPath
);
},
},
],
},
}),
new CleanWebpackPlugin(),
new ESLintPlugin(),
new StylelintPlugin(),
],
performance: {
maxAssetSize: 550000, // Increase the asset size limit to 550 KB
maxEntrypointSize: 550000, // Increase the entry point size limit to 550 KB
hints: 'warning', // You can set this to 'error' to make the build fail on these warnings or 'false' to disable them
},
};