-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathwebpack.config.js
More file actions
73 lines (70 loc) · 2.62 KB
/
webpack.config.js
File metadata and controls
73 lines (70 loc) · 2.62 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
/**
* Custom Webpack Configuration for WP Multi Network
*
* This configuration extends @wordpress/scripts default webpack config
* to customize the output location and naming of built assets.
*
* NOTE: Due to how webpack handles CSS extraction from JavaScript imports,
* the CSS files are generated with auto-generated chunk names that include
* a 'style-' prefix (e.g., 'style-wp-multi-network.css'). To maintain
* backward compatibility with the existing plugin structure, we use a
* post-build rename script (see package.json 'rename-assets') to rename
* these files to match the expected names ('wp-multi-network.css').
*
* @see https://webpack.js.org/configuration/
*/
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const MiniCssExtractPlugin = require( 'mini-css-extract-plugin' );
const RtlCssPlugin = require( 'rtlcss-webpack-plugin' );
const path = require( 'path' );
/**
* Helper function to generate CSS filename based on chunk name
*
* @param {Object} pathData - Webpack path data object
* @param {string} suffix - Optional suffix for the filename (e.g., '-rtl')
* @return {string} The CSS filename
*/
function getCssFilename( pathData, suffix = '' ) {
if ( pathData.chunk && pathData.chunk.name ) {
const chunkName = pathData.chunk.name; // e.g., 'wp-multi-network.min'
const parts = chunkName.split('.');
if (parts.length > 1) {
// Reconstruct as: name + suffix + .extension
const baseName = parts.slice(0, -1).join('.');
const ext = parts[parts.length - 1];
return 'css/' + baseName + suffix + '.' + ext + '.css';
}
return 'css/' + chunkName + suffix + '.css';
}
return 'css/[name]' + suffix + '.css';
}
module.exports = {
...defaultConfig,
entry: {
'wp-multi-network.min': [
path.resolve( __dirname, 'wp-multi-network/assets/js/wp-multi-network.js' ),
path.resolve( __dirname, 'wp-multi-network/assets/css/wp-multi-network.css' )
]
},
output: {
path: path.resolve( process.cwd(), 'wp-multi-network/assets' ),
filename: 'js/[name].js',
},
plugins: [
// Remove default CSS plugins and replace with custom configuration
// to place CSS files in the css/ subdirectory with proper naming.
// @wordpress/scripts generates CSS chunks with auto-generated names
// (e.g., 'style-wp-multi-network') which we need to customize.
...defaultConfig.plugins.filter(
( plugin ) =>
! ( plugin instanceof MiniCssExtractPlugin ) &&
! ( plugin instanceof RtlCssPlugin )
),
new MiniCssExtractPlugin( {
filename: ( pathData ) => getCssFilename( pathData ),
} ),
new RtlCssPlugin( {
filename: ( pathData ) => getCssFilename( pathData, '-rtl' ),
} ),
],
};