Repository navigation
Expand file tree
/
Copy pathwebpack.config.js
More file actions
300 lines (270 loc) · 8.64 KB
/
Copy pathwebpack.config.js
File metadata and controls
300 lines (270 loc) · 8.64 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* External dependencies
*/
const fs = require( 'fs' );
const path = require( 'path' );
const CssMinimizerPlugin = require( 'css-minimizer-webpack-plugin' );
const RemoveEmptyScriptsPlugin = require( 'webpack-remove-empty-scripts' );
/**
* WordPress dependencies
*/
const { getAsBooleanFromENV } = require( '@wordpress/scripts/utils' );
// BrowserSync runs only in watch mode. wp-scripts injects `watch` into argv for
// `wp-scripts start` (without --no-watch), and --hot implies watching too.
const isWatch =
process.argv.includes( '--watch' ) ||
process.argv.includes( 'watch' ) ||
process.argv.includes( '--hot' );
if ( isWatch ) {
// `quiet: true` suppresses dotenv's per-run "injecting env" banner, which is
// noisy on every rebuild in watch mode.
require( 'dotenv' ).config( { path: '.env.local', quiet: true } );
}
// HMR (BrowserSync) master switch read from .env.local (ENABLE_HMR), defaulting
// on; only an explicit off value disables it. Mirrors is_hmr_enabled() in
// inc/Core/Assets.php so one flag controls both the BrowserSync server (here)
// and its client enqueue (PHP).
const hmrFlag = String( process.env.ENABLE_HMR || '' ).toLowerCase();
const isHmrEnabled = ! [ 'false', '0', 'no', 'off' ].includes( hmrFlag );
const DEFAULT_BS_PORT = 3003;
/**
* Parse a TCP port from an env value, falling back when it is missing or not a
* valid port number (1–65535).
*
* @param {string|undefined} value Raw env value.
* @param {number} fallback Port to use when `value` is invalid.
* @return {number} A valid port.
*/
const toPort = ( value, fallback ) => {
// Digits only: parseInt would read '8888foo' as 8888, and Number would accept hex.
const port = /^\d+$/.test( String( value ?? '' ) ) ? Number( value ) : NaN;
return Number.isInteger( port ) && port >= 1 && port <= 65535
? port
: fallback;
};
const bsPort = toPort( process.env.BS_PORT, DEFAULT_BS_PORT );
const BROWSER_SYNC_FILES = [
'assets/build/css/**/*.css',
'assets/build/js/**/*.js',
'**/*.php',
'!vendor/**',
'!assets/build/**/*.php',
'!assets/build/**/*.map',
'!assets/build/**/*.hot-update.*',
];
/**
* Create BrowserSync only for watch mode.
*
* BrowserSync watches build output and PHP templates. CSS is injected in place;
* everything else triggers a full reload. A custom host and SSL cert paths (for
* HTTPS local sites) are read from .env.local when present.
*
* @return {Array} BrowserSync plugin instances.
*/
const getBrowserSyncPlugins = () => {
if ( ! isWatch || ! isHmrEnabled ) {
return [];
}
const BrowserSyncPlugin = require( 'browser-sync-webpack-plugin' );
return [
new BrowserSyncPlugin(
{
port: bsPort,
...( process.env.WP_HOST ? { host: process.env.WP_HOST } : {} ),
...( process.env.WP_SSL_KEY && process.env.WP_SSL_CERT
? {
https: {
key: process.env.WP_SSL_KEY,
cert: process.env.WP_SSL_CERT,
},
}
: {} ),
files: BROWSER_SYNC_FILES,
notify: false,
open: false,
logSnippet: false,
ghostMode: false,
},
{
injectCss: true,
}
),
];
};
const hasExperimentalModulesFlag = getAsBooleanFromENV(
'WP_EXPERIMENTAL_MODULES'
);
let scriptConfig, moduleConfig;
if ( hasExperimentalModulesFlag ) {
[
scriptConfig,
moduleConfig,
] = require( '@wordpress/scripts/config/webpack.config' );
} else {
scriptConfig = require( '@wordpress/scripts/config/webpack.config' );
}
// The base @wordpress/scripts config adds copy-webpack-plugin (whose class is
// named `CopyPlugin`, not `CopyWebpackPlugin` — that's just wp-scripts' import
// alias) to copy block.json/render.php from the source blocks directory into
// the output. Block compilation is owned by the dedicated `build:blocks`
// script (it scans src/blocks/ and outputs to assets/build/blocks/). Now that
// our source lives in `src/` — wp-scripts' default location — this `build:assets`
// run would otherwise re-discover the blocks and copy them into
// assets/build/js/blocks/. Strip only the copy plugin; the other block plugins
// (PhpFilePaths, manifest, dependency) have interdependencies the script/css
// build still relies on.
const sharedConfig = {
...scriptConfig,
watchOptions: {
...( scriptConfig.watchOptions || {} ),
// Ignore build output (and node_modules) so webpack doesn't watch the
// files it emits into assets/build and rebuild in a loop — which would
// otherwise make BrowserSync reload the browser endlessly.
ignored: [
'**/node_modules/**',
path.resolve( process.cwd(), 'assets', 'build', '**' ),
],
},
output: {
path: path.resolve( process.cwd(), 'assets', 'build', 'js' ),
filename: '[name].js',
chunkFilename: '[name].js',
},
plugins: [
...scriptConfig.plugins
.filter( ( plugin ) => 'CopyPlugin' !== plugin.constructor.name )
.map( ( plugin ) => {
if ( plugin.constructor.name === 'MiniCssExtractPlugin' ) {
plugin.options.filename = '../css/[name].css';
}
return plugin;
} ),
new RemoveEmptyScriptsPlugin(),
],
optimization: {
...scriptConfig.optimization,
splitChunks: {
...scriptConfig.optimization.splitChunks,
},
minimizer: scriptConfig.optimization.minimizer.concat( [
new CssMinimizerPlugin(),
] ),
},
};
/**
* Recursively collect webpack entries from a directory.
*
* Walks `dir` and returns a map of entry name → absolute file path. The entry
* name is the file's path relative to `dir`, without extension and with forward
* slashes, so the build output mirrors the source folder structure (e.g.
* `src/js/admin/widget.js` → entry `admin/widget` → `assets/build/js/admin/widget.js`).
*
* Files and directories whose names start with `_` or `.` are skipped — use the
* `_` prefix for partials/imports that should not become their own entry.
*
* Edge cases to be aware of: two files differing only by extension in the same
* directory (e.g. `foo.js` + `foo.ts`) map to the same entry name and the last
* one walked silently wins — keep a single source file per entry. Also,
* directory entries are read with `withFileTypes`, whose dirents don't resolve
* symlinks, so a symlinked subdirectory is skipped rather than walked.
*
* @param {string} dir Base directory to scan (relative to cwd).
* @param {string[]} extensions File extensions to include (with the dot).
* @param {string[]} [excludeDirs=[]] Directory names to skip while walking.
* @return {Object} Map of entry name to absolute file path.
*/
const collectEntries = ( dir, extensions, excludeDirs = [] ) => {
const entries = {};
const root = path.resolve( process.cwd(), dir );
if ( ! fs.existsSync( root ) ) {
return entries;
}
const walk = ( current ) => {
fs.readdirSync( current, { withFileTypes: true } ).forEach(
( entry ) => {
if (
entry.name.startsWith( '_' ) ||
entry.name.startsWith( '.' )
) {
return;
}
const fullPath = path.join( current, entry.name );
if ( entry.isDirectory() ) {
if ( ! excludeDirs.includes( entry.name ) ) {
walk( fullPath );
}
return;
}
if ( ! extensions.includes( path.extname( entry.name ) ) ) {
return;
}
const name = path
.relative( root, fullPath )
.replace( /\.[^/.]+$/, '' )
.split( path.sep )
.join( '/' );
entries[ name ] = fullPath;
}
);
};
walk( root );
return entries;
};
// Generate a webpack config which includes setup for CSS extraction.
// Recursively scans src/css (including subfolders) and extracts each stylesheet
// into a matching path under build/css.
const styles = {
...sharedConfig,
entry: () => collectEntries( './src/css', [ '.css', '.scss', '.sass' ] ),
module: {
...sharedConfig.module,
},
plugins: [
...sharedConfig.plugins.filter(
( plugin ) =>
plugin.constructor.name !== 'DependencyExtractionWebpackPlugin'
),
],
};
// Recursively scans src/js (including subfolders) for script entry points. The
// `modules` directory is excluded — it is built separately as ES modules below.
const scripts = {
...sharedConfig,
entry: () =>
collectEntries(
'./src/js',
[ '.js', '.jsx', '.ts', '.tsx' ],
[ 'modules' ]
),
plugins: [ ...sharedConfig.plugins, ...getBrowserSyncPlugins() ],
};
let moduleScripts = {};
if ( hasExperimentalModulesFlag ) {
moduleScripts = {
...moduleConfig,
entry: () =>
collectEntries( './src/js/modules', [
'.js',
'.jsx',
'.ts',
'.tsx',
] ),
output: {
...moduleConfig.output,
path: path.resolve(
process.cwd(),
'assets',
'build',
'js',
'modules'
),
filename: '[name].js',
chunkFilename: '[name].js',
},
};
}
const customExports = [ scripts, styles ];
if ( hasExperimentalModulesFlag ) {
customExports.push( moduleScripts );
}
module.exports = customExports;