-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
175 lines (168 loc) · 4.64 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
/**
* WEBPACK CONFIG
*
* This file extends the default wp-scripts webpack config file found here
*
* @see https://webpack.js.org/concepts/ Webpack Docs
* @see https://github.com/WordPress/gutenberg/blob/trunk/packages/scripts/config/webpack.config.js WordPress Webpack Config
* @see https://developer.bu.edu/gutenberg/gutenberg-handbook/webpack-config-js/ BU Documentation
*
* Run `npm list webpack` to see current version.
*/
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const { mergeWithRules } = require( 'webpack-merge' );
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
const RemoveEmptyScriptsPlugin = require( 'webpack-remove-empty-scripts' );
// Import our theme paths from a separate file that can be modified in the theme.
const {
blockEntryPoints,
pluginEntryPoints,
sassCompiler,
statsConfig,
customSassOptions,
} = require( './webpack.customizations' );
/**
* Block Config for @wordpress/scripts & webpack
*
* Do not modify the entry points of this config as it uses the `getWebpackEntryPoints` function from wp-scripts that finds all blocks and block.json files and builds a list of entrypoints for webpack from that automagically.
*/
const blocksConfig = {
module: {
rules: [
{
test: /\.(sc|sa)ss$/,
use: [
{
loader: require.resolve( 'sass-loader' ),
options: {
sassOptions: customSassOptions,
implementation: require( sassCompiler ),
},
},
],
},
],
},
stats: statsConfig,
};
/**
* Block Styles Config for @wordpress/scripts & webpack
*
* Used to compile additional stylesheets for blocks such as Decorative Styles and Shared/Common styles.
* This config entirely replaces the default entry points from the @wordpress/scripts defaultConfig. This ensures
* that the blocks are not processed a second time.
*/
const blockStylesConfig = {
entry: {
...blockEntryPoints,
},
module: {
rules: [
{
test: /\.(sc|sa)ss$/,
use: [
{
loader: require.resolve( 'sass-loader' ),
options: {
sassOptions: customSassOptions,
implementation: require( sassCompiler ),
},
},
],
},
],
},
stats: statsConfig,
};
/**
* Plugin General Styles/JS Config for @wordpress/scripts & webpack
*
* Used to compile additional stylesheets or JS for this plugin as a whole. Used for anything that IS NOT a block.
* This config entirely replaces the default entry points from the @wordpress/scripts defaultConfig. This ensures
* that the blocks are not processed a second time.
*/
const pluginConfig = {
// Set devtool mode to sourcemap so we generate sourcemap files even for production builds.
devtool: 'source-map',
entry: {
...pluginEntryPoints,
},
module: {
rules: [
{
test: /\.(sc|sa)ss$/,
use: [
{
loader: require.resolve( 'css-loader' ),
options: {
sourceMap: true, // Set sourceMap to true so we generate a map even for prod builds.
},
},
{
loader: require.resolve( 'sass-loader' ),
options: {
sassOptions: customSassOptions,
sourceMap: true, // Set sourceMap to true so we generate a map even for prod builds.
implementation: require( sassCompiler ),
},
},
],
},
],
},
stats: statsConfig,
plugins: [
// Grab the defaultConfig's plugins array and filter it to remove what we don't need.
...defaultConfig.plugins.filter(
// Remove CopyWebpackPlugin from the pluginConfig so we don't copy block.json & php files into our output folder for the theme's files.
( plugin ) => ! ( plugin instanceof CopyWebpackPlugin )
),
new RemoveEmptyScriptsPlugin(), // Add new plugin that removes empty script files for CSS only entries
],
};
/**
* Now we use `webpack-merge` to combine our custom rules defined here with the base WordPress rules.
* Export the new modified configuration for webpack and use the webpack-merge functions to merge our modified configuration in.
* @see https://github.com/survivejs/webpack-merge?tab=readme-ov-file#mergewithrules
*/
module.exports = [
mergeWithRules( {
module: {
rules: {
test: 'match',
use: {
loader: 'match',
options: 'merge',
},
},
},
stats: 'replace',
} )( defaultConfig, blocksConfig ),
mergeWithRules( {
entry: 'replace',
module: {
rules: {
test: 'match',
use: {
loader: 'match',
options: 'merge',
},
},
},
stats: 'replace',
} )( defaultConfig, blockStylesConfig ),
mergeWithRules( {
entry: 'replace',
module: {
rules: {
test: 'match',
use: {
loader: 'match',
options: 'merge',
},
},
},
stats: 'replace',
plugins: 'replace',
} )( defaultConfig, pluginConfig ),
];