-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
83 lines (70 loc) · 2.18 KB
/
webpack.config.js
File metadata and controls
83 lines (70 loc) · 2.18 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
/**
* WordPress Webpack Config Extension
*
* Extends the default @wordpress/scripts webpack configuration
* to support the monorepo structure under /sources/.
*/
const path = require('path');
const fs = require('fs');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
/** @type {import('webpack').Configuration} */
const defaultConfig = require('@wordpress/scripts/config/webpack.config');
function stylesEntries() {
const entries = {};
const stylesPath = path.resolve(__dirname, 'sources/client/styles');
function scanDirectory(dir, prefix = '') {
const items = fs.readdirSync(dir, { withFileTypes: true });
items.forEach((item) => {
const fullPath = path.join(dir, item.name);
// Add root-level style files (non-partials) as entries
if (prefix === '' && item.name.endsWith('.scss') && !item.name.startsWith('_')) {
const entryKey = path.basename(item.name, '.scss');
entries[entryKey] = fullPath;
return;
}
if (!item.isDirectory() || item.name.includes('mixins')) {
return;
}
const indexScss = path.join(fullPath, 'index.scss');
if (fs.existsSync(indexScss)) {
const entryKey = prefix ? `${prefix}/${item.name}` : item.name;
entries[entryKey] = indexScss;
return; // Do not descend when this directory already provides an entry
}
const newPrefix = prefix ? `${prefix}/${item.name}` : `@${item.name}`;
scanDirectory(fullPath, newPrefix);
});
}
scanDirectory(stylesPath);
return entries;
}
// Export config with custom entries and output path
/** @type {import('webpack').Configuration} */
const styles = {
...defaultConfig,
entry: stylesEntries(),
resolve: {
...(defaultConfig.resolve || {}),
alias: {
'@burokku/mixins': path.resolve(__dirname, 'sources/client/styles/mixins'),
},
},
output: {
...(defaultConfig.output || {}),
path: path.resolve(__dirname, 'dist/styles'),
clean: true
},
plugins: [
...(defaultConfig.plugins || []).filter(plugin => plugin.constructor.name !== 'RtlCssPlugin'),
new CleanWebpackPlugin({
cleanAfterEveryBuildPatterns: [
'**/*.js',
'**/*.js.map',
'**/*.css.map',
'**/*.php'
],
protectWebpackAssets: false
})
]
};
module.exports = styles;