-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathwebpack.config.js
More file actions
58 lines (46 loc) · 1.65 KB
/
webpack.config.js
File metadata and controls
58 lines (46 loc) · 1.65 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
const fs = require('fs');
const path = require('path');
// eslint-disable-next-line import/extensions
const config = require('10up-toolkit/config/webpack.config.js');
const webpack = require('webpack');
const WORDPRESS_NAMESPACE = '@wordpress/';
const BUNDLED_PACKAGES = ['@wordpress/icons', '@wordpress/interface', '@wordpress/style-engine'];
function externalizeWpDeps({ request }, callback) {
if (!BUNDLED_PACKAGES.includes(request) && request.startsWith(WORDPRESS_NAMESPACE)) {
return callback(null, request, 'commonjs2');
}
return callback();
}
function getEntriesFromSubFolders(parentDirectory) {
const entries = {};
fs.readdirSync(parentDirectory, { withFileTypes: true }).forEach((childDirectory) => {
if (!childDirectory.isDirectory()) return;
const potentialEntryPaths = ['index.js', 'index.jsx', 'index.ts', 'index.tsx'].map(
(fileName) => path.join(parentDirectory, childDirectory.name, fileName),
);
for (const potentialEntryPath of potentialEntryPaths) {
if (fs.existsSync(potentialEntryPath)) {
const entryName = path.join(
path.basename(parentDirectory),
childDirectory.name,
'index',
);
entries[entryName] = potentialEntryPath;
break;
}
}
});
return entries;
}
module.exports = {
...config,
entry: {
...config.entry(),
...getEntriesFromSubFolders(path.resolve(__dirname, 'api')),
...getEntriesFromSubFolders(path.resolve(__dirname, 'components')),
...getEntriesFromSubFolders(path.resolve(__dirname, 'hooks')),
...getEntriesFromSubFolders(path.resolve(__dirname, 'stores')),
},
devtool: 'source-map',
plugins: [...config.plugins, new webpack.ExternalsPlugin(null, externalizeWpDeps)],
};