-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathwebpack.config.js
More file actions
83 lines (78 loc) · 2.6 KB
/
webpack.config.js
File metadata and controls
83 lines (78 loc) · 2.6 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
/**
* 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,
module: {
...config.module,
rules: [
...( config.module?.rules || [] ),
// Mark `@wordpress/views` side-effect-free so webpack can tree-shake
// its barrel export. Views' `useViewConfig` pulls in `lock-unlock.mjs`,
// which opts into `@wordpress/private-apis` under the name
// `@wordpress/views` — a name WordPress core's allowlist rejects,
// crashing the app at module-eval time. We only use `useView`, so
// letting webpack drop the unused chain avoids the crash.
{
test: /node_modules[\\/]@wordpress[\\/]views[\\/]/,
sideEffects: false,
},
],
},
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 );