generated from alleyinteractive/create-wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
142 lines (128 loc) · 3.98 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
const path = require('path');
const fs = require('fs');
const defaultConfig = require('@wordpress/scripts/config/webpack.config');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = (env, { mode }) => ({
...defaultConfig,
// Dynamically produce entries from the slotfills index file and all blocks.
entry: () => {
const blocks = defaultConfig.entry();
/**
* Get the entry points from a directory.
*
* @returns {Object} An object of entries.
*/
function getEntries(entryDirName) {
const directoryPath = path.join(__dirname, entryDirName);
const directoryExists = fs.existsSync(directoryPath);
if (directoryExists) {
return fs
.readdirSync(directoryPath)
.reduce((acc, dirPath) => {
// Ignore .gitkeep files.
if (dirPath?.includes('.gitkeep')) {
return acc;
}
acc[
`${entryDirName}-${dirPath}`
] = path.join(__dirname, entryDirName, dirPath);
return acc;
}, {});
}
// eslint-disable-next-line no-console
console.log(`Directory "${entryDirName}" does not exist.`);
return {};
}
return {
...blocks,
...getEntries('entries'),
...{
// All other custom entry points can be included here.
},
};
},
// Use different filenames for production and development builds for clarity.
output: {
clean: mode === 'production',
filename: (pathData) => {
const dirname = pathData.chunk.name;
// Process all non-entries entries.
if (!pathData.chunk.name.includes('entries-')) {
return '[name].js';
}
const srcDirname = dirname.replace('entries-', '');
return `${srcDirname}/index.js`;
},
path: path.join(__dirname, 'build'),
},
// Configure plugins.
plugins: [
...defaultConfig.plugins,
new CopyWebpackPlugin({
patterns: [
{
from: '**/{index.php,*.css}',
context: 'entries',
noErrorOnMissing: true,
},
{
from: '**/*.svg',
context: path.join(__dirname, 'blocks'),
noErrorOnMissing: true,
},
],
}),
new MiniCssExtractPlugin({
filename: (pathData) => {
const dirname = pathData.chunk.name;
// Process all blocks.
if (!pathData.chunk.name.includes('entries-')) {
return '[name].css';
}
const srcDirname = dirname.replace('entries-', '');
return `${srcDirname}/index.css`;
},
}),
new CleanWebpackPlugin({
cleanAfterEveryBuildPatterns: [
/**
* Remove duplicate entry CSS files generated from default
* MiniCssExtractPlugin plugin in wpScripts.
*
* The default MiniCssExtractPlugin filename is [name].css
* resulting in the generation of the entries-*.css files.
* The configuration in this file for MiniCssExtractPlugin outputs
* the entry CSS into the entry src directory name.
*/
'entries-*.css',
// Maps are built when running the start mode with wpScripts.
'entries-*.css.map',
],
protectWebpackAssets: false,
}),
],
// This webpack alias rule is needed at the root to ensure that the paths are resolved
// using the custom alias defined below.
resolve: {
alias: {
...defaultConfig.resolve.alias,
'@': path.resolve(__dirname),
},
extensions: ['.js', '.jsx', '.ts', '.tsx', '...'],
},
// Cache the generated webpack modules and chunks to improve build speed.
// @see https://webpack.js.org/configuration/cache/
cache: {
...defaultConfig.cache,
type: 'filesystem',
},
devServer: mode === 'production' ? {} : {
...defaultConfig.devServer,
allowedHosts: 'all',
static: {
directory: '/build',
},
},
});