-
Notifications
You must be signed in to change notification settings - Fork 9
/
webpack.config.js
106 lines (95 loc) · 2.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
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();
return {
...blocks,
...fs
.readdirSync('./entries')
.reduce((acc, dirPath) => {
acc[
`entries-${dirPath}`
] = `./entries/${dirPath}`;
return acc;
}, {
// All other custom entry points can be included here.
'src-app': './src/js/app.ts',
}),
};
},
// 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,
},
],
}),
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',
],
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',
},
});