-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathwebpack.config.js
More file actions
67 lines (62 loc) · 2 KB
/
webpack.config.js
File metadata and controls
67 lines (62 loc) · 2 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
/**
* Custom webpack configuration extending WordPress Scripts
*
* This configuration:
* - Places JS chunks in their source directory structure (src/foo/ → build/foo/)
* - Handles app vendor chunks separately
*/
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const processConfig = ( config ) => {
return {
...config,
output: {
...config.output,
// Place JS chunks in their source directory with content hash for cache busting
chunkFilename: ( pathData ) => {
const chunk = pathData.chunk;
const chunkGraph = pathData.webpack?.chunkGraph;
if ( chunk && chunkGraph ) {
for ( const module of chunkGraph.getChunkModules( chunk ) ) {
const modulePath = module.resource || module.context || '';
const srcMatch = modulePath.match( /\/src\/([^/]+)\// );
if ( srcMatch ) {
return `${ srcMatch[ 1 ] }/[name].[contenthash:8].js`;
}
}
}
return '[name].[contenthash:8].js';
},
},
optimization: {
...config.optimization,
splitChunks: {
...( config.optimization?.splitChunks || {} ),
cacheGroups: {
...( config.optimization?.splitChunks?.cacheGroups || {} ),
// TanStack Router - loaded async since it's large (~250KB)
tanstackRouter: {
test: /[\\/]node_modules[\\/]@tanstack[\\/]/,
name: 'app/tanstack-router',
chunks: ( chunk ) => chunk.name && chunk.name.startsWith( 'app/' ),
priority: 30,
reuseExistingChunk: true,
enforce: true,
},
// App vendor chunk (other dependencies)
appVendors: {
test: /[\\/]node_modules[\\/]/,
name: 'app/vendors',
chunks: ( chunk ) => chunk.name && chunk.name.startsWith( 'app/' ),
priority: 20,
reuseExistingChunk: true,
enforce: true,
},
// Disable default vendors to avoid unused files
default: false,
defaultVendors: false,
},
},
},
};
};
module.exports = Array.isArray( defaultConfig ) ? defaultConfig.map( processConfig ) : processConfig( defaultConfig );