-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
59 lines (52 loc) · 1.99 KB
/
Copy pathwebpack.config.js
File metadata and controls
59 lines (52 loc) · 1.99 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
const { glob } = require('glob');
const path = require('path');
const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts');
const defaultConfig = require('@wordpress/scripts/config/webpack.config');
/**
* Auto-discover entries from `resources/assets/`:
*
* - apps/<name>/index.{ts,tsx,js,jsx} → public/apps/<name>.js (+ .css via MiniCssExtract)
* - apps/<name>.{ts,tsx,js,jsx} → public/apps/<name>.js
* - css/<name>.{scss,less,css} → public/css/<name>.css
* - js/<name>.{ts,js} → public/js/<name>.js
*
* New app/style/script? Just drop a file in the right folder, no package.json edits needed.
*/
function autoEntries() {
const entries = {};
// React/TS apps — folder-based (for apps with multiple files)
glob.sync('resources/assets/apps/*/index.{ts,tsx,js,jsx}').forEach((file) => {
const name = path.basename(path.dirname(file));
entries[`apps/${name}`] = `./${file}`;
});
// React/TS apps — single-file (for lightweight apps)
glob.sync('resources/assets/apps/*.{ts,tsx,js,jsx}').forEach((file) => {
const name = path.basename(file).replace(/\.(ts|tsx|js|jsx)$/, '');
entries[`apps/${name}`] = `./${file}`;
});
// Standalone styles (CSS, SCSS, LESS)
glob.sync('resources/assets/css/*.{scss,less,css}').forEach((file) => {
const name = path.basename(file).replace(/\.(scss|less|css)$/, '');
entries[`css/${name}`] = `./${file}`;
});
// Standalone scripts (JS, TS)
glob.sync('resources/assets/js/*.{ts,js}').forEach((file) => {
const name = path.basename(file).replace(/\.(ts|js)$/, '');
entries[`js/${name}`] = `./${file}`;
});
return entries;
}
module.exports = {
...defaultConfig,
entry: autoEntries(),
output: {
...defaultConfig.output,
path: path.resolve(__dirname, 'public'),
filename: '[name].js',
},
plugins: [
...defaultConfig.plugins,
// Strips the empty `.js` that webpack would generate for pure-CSS entries.
new RemoveEmptyScriptsPlugin(),
],
};