-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
106 lines (95 loc) · 2.81 KB
/
webpack.config.js
File metadata and controls
106 lines (95 loc) · 2.81 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
// This comes from https://github.com/WordPress/gutenberg/blob/trunk/packages/scripts/config/webpack.config.js
const wpConfig = require('@wordpress/scripts/config/webpack.config');
const fs = require('fs');
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const DependencyExtractionWebpackPlugin = require('@wordpress/dependency-extraction-webpack-plugin');
module.exports = {
...wpConfig,
entry: getEntryPoints(path.resolve(__dirname, 'src')),
resolve: {
alias: {
'@': path.resolve(__dirname, './')
},
extensions: [".ts", ".tsx", ".js", ".jsx"],
},
module: {
rules: [
{
test: /\.tsx?$/,
use: [
{
loader: 'ts-loader',
options: {
configFile: 'tsconfig.json',
transpileOnly: true,
},
},
],
exclude: /node_modules/,
},
{
test: /\.jsx?$/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react']
}
},
exclude: /node_modules/,
},
],
},
plugins: [
new DependencyExtractionWebpackPlugin({
injectPolyfill: true,
getDynamicDependencies(file) {
const dependencies = [];
const content = fs.readFileSync(file, 'utf-8');
const regex = /require\(\s*['"]@wordpress\/([^'"]+)['"]\s*\)/g;
let match;
while ((match = regex.exec(content)) !== null) {
dependencies.push(`wp.${match[1]}`);
}
return dependencies;
},
}),
new CopyWebpackPlugin({
patterns: [
{
from: '**/*.php', // Copy PHP files from src to build
to: '[path][name][ext]', // Maintain folder structure
context: path.resolve(__dirname, 'src')
},
{
from: '**/*.json', // Copy JSON files from src to build
to: '[path][name][ext]', // Maintain folder structure
context: path.resolve(__dirname, 'src')
}
]
})
]
};
function getEntryPoints(directory) {
const entryPoints = {};
scanDirectory(directory);
return entryPoints;
function scanDirectory(dir) {
fs.readdirSync(dir).forEach((file) => {
const fullPath = path.resolve(dir, file);
if (fs.statSync(fullPath).isDirectory()) {
scanDirectory(fullPath);
} else {
const ext = path.extname(file);
if (ext === '.js' || ext === '.jsx' || ext === '.ts' || ext === '.tsx') {
const relativePath = path.relative(directory, fullPath).replace(ext, '');
const entryName = normalizeEntryName(relativePath);
entryPoints[entryName] = fullPath;
}
}
});
}
function normalizeEntryName(filePath) {
return filePath.split(path.sep).join('/');
}
}