-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwebpack.config.js
More file actions
161 lines (154 loc) · 4.54 KB
/
webpack.config.js
File metadata and controls
161 lines (154 loc) · 4.54 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
const path = require( 'path' );
const fs = require( 'fs-extra' );
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );
const I18nLoaderWebpackPlugin = require( '@automattic/i18n-loader-webpack-plugin' );
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
// Remove old build files
fs.removeSync( path.resolve( __dirname, './assets/build/' ) );
fs.removeSync( path.resolve( __dirname, './assets/blocks/' ) );
const chunkUniqueKey = Date.now().toString();
const resolve = {
alias: {
'@directorist-gutenberg': path.resolve( __dirname, 'resources/js' ),
'@blocks': path.resolve( __dirname, 'resources/blocks' ),
'@utils': path.resolve( __dirname, 'resources/js/utils' ),
'@icon': path.resolve( __dirname, 'resources/svg/icons' ),
'@block-icon': path.resolve( __dirname, 'resources/blocks-icon' ),
'@assets': path.resolve( __dirname, 'assets' ),
'@image': path.resolve( __dirname, 'resources/images' ),
},
};
const plugins = [
...defaultConfig[ 0 ].plugins.reduce( ( acc, plugin ) => {
if (
plugin.constructor.name !== 'DependencyExtractionWebpackPlugin' &&
plugin.constructor.name !== 'RtlCssPlugin'
) {
acc.push( plugin );
}
return acc;
}, [] ),
new I18nLoaderWebpackPlugin( {
textdomain: 'directorist-gutenberg',
} ),
new DependencyExtractionWebpackPlugin(),
];
const moduleConfig = {
...defaultConfig[ 0 ].module,
rules: [
// Filter out default SVG rules from WordPress scripts - we handle SVGs explicitly
// This prevents WordPress scripts from processing SVGs into React components
...defaultConfig[ 0 ].module.rules.filter( ( rule ) => {
// Exclude any SVG rules that WordPress scripts might have
// (WordPress scripts may use @svgr/webpack which transforms SVGs)
if ( rule.test && rule.test.toString().includes( 'svg' ) ) {
return false;
}
return true;
} ),
{
test: /\.(bmp|png|jpe?g|gif|webp)$/i,
type: 'asset/resource',
generator: {
filename: 'images/[name][ext]',
},
},
{
// Special rule for SVG icons directory (font-awesome and line-awesome)
// Only used with require.context to get file names, not to bundle
test: /\.svg$/i,
include: [
path.resolve( __dirname, 'resources/svg/icons/icon-library' ),
],
// Use a simple loader that just returns the module path for require.context
// This prevents webpack from trying to emit the files as assets
use: {
loader: path.resolve( __dirname, 'webpack-svg-name-loader.js' ),
},
},
{
// All other SVGs (like block icons, times.svg, etc.) - treat as assets
test: /\.svg$/i,
exclude: [
path.resolve( __dirname, 'resources/svg/icons/icon-library' ),
],
type: 'asset/resource',
generator: {
filename: 'icons/[name][ext]',
},
},
],
};
module.exports = [
{
...defaultConfig[ 0 ],
output: {
...defaultConfig[ 0 ].output,
path: path.resolve( __dirname, './assets/blocks/' ),
},
module: moduleConfig,
plugins,
resolve,
},
{
...defaultConfig[ 0 ],
module: moduleConfig,
entry: {
/**
* Admin scripts
*/
'js/admin': './resources/js/admin/index.js',
'css/admin': './resources/sass/admin.scss',
/**
* Block scripts
*/
'js/blocks-editor': './resources/js/block-sripts/editor.js',
'css/blocks-frontend': './resources/sass/blocks/frontend.scss',
'css/blocks-editor': './resources/sass/blocks/editor.scss',
},
output: {
...defaultConfig[ 0 ].output,
path: path.resolve( __dirname, './assets/build/' ),
filename: '[name].js',
chunkFilename: 'chunk/[name].js?ver=' + chunkUniqueKey,
},
plugins: [
...plugins.reduce( ( acc, plugin ) => {
if ( plugin.constructor.name !== 'CopyPlugin' ) {
acc.push( plugin );
}
return acc;
}, [] ),
// Copy icon-library folder to build output
new CopyWebpackPlugin( {
patterns: [
{
from: path.resolve( __dirname, 'resources/svg/icons/icon-library' ),
to: path.resolve( __dirname, 'assets/build/icons/icon-library' ),
},
],
} ),
],
resolve,
},
{
...defaultConfig[ 1 ],
entry: {
'js/blocks-frontend': './resources/js/block-sripts/frontend.js',
},
output: {
...defaultConfig[ 1 ].output,
path: path.resolve( __dirname, './assets/build/' ),
filename: '[name].js',
chunkFilename: '[name].js?ver=' + chunkUniqueKey,
},
plugins: defaultConfig[ 1 ].plugins.reduce( ( acc, plugin ) => {
if ( plugin.constructor.name !== 'RtlCssPlugin' ) {
acc.push( plugin );
}
return acc;
}, [] ),
resolve,
},
];