-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebpack.config.js
More file actions
182 lines (166 loc) · 4.36 KB
/
webpack.config.js
File metadata and controls
182 lines (166 loc) · 4.36 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* External dependencies
*/
const fs = require('fs');
const path = require('path');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts');
const RtlCssPlugin = require('rtlcss-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
/**
* WordPress dependencies
*/
const [scriptConfig] = require('@wordpress/scripts/config/webpack.config');
/**
* Read all file entries in a directory.
* @param {string} dir Directory to read.
* @return {Object} Object with file entries.
*/
const readAllFileEntries = (dir) => {
const entries = {};
if (!fs.existsSync(dir)) {
return entries;
}
if (fs.readdirSync(dir).length === 0) {
return entries;
}
fs.readdirSync(dir).forEach((fileName) => {
// Skip hidden files (starting with '.') and directories
if (fileName.startsWith('.')) {
return; // Skip files like .DS_Store
}
const fullPath = path.resolve(dir, fileName);
if (!fs.lstatSync(fullPath).isDirectory() && !fileName.startsWith('_')) {
entries[fileName.replace(/\.[^/.]+$/, '')] = fullPath;
}
});
return entries;
};
// Shared configuration function
const getSharedConfig = (env, argv) => {
const isProduction = argv.mode === 'production';
const plugins = [
...scriptConfig.plugins.filter((plugin) => !(plugin instanceof RtlCssPlugin)),
new CleanWebpackPlugin({
cleanStaleWebpackAssets: isProduction
}),
new MiniCssExtractPlugin({
filename: '[name].css',
}),
new RemoveEmptyScriptsPlugin({
stage: RemoveEmptyScriptsPlugin.STAGE_AFTER_PROCESS_PLUGINS,
}),
new RtlCssPlugin({
filename: '[name]-rtl.css',
}),
];
const rules = [
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader'
},
{
test: /\.(sc|sa|c)ss$/,
exclude: /node_modules/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader',
]
}
];
const optimization = {
...scriptConfig.optimization,
splitChunks: {
...scriptConfig.optimization.splitChunks,
},
minimizer: scriptConfig.optimization.minimizer.concat([
new CssMinimizerPlugin({
minimizerOptions: {
preset: [
'default',
{
discardComments: { removeAll: true },
normalizeWhitespace: isProduction,
},
],
},
}),
]),
};
return {
...scriptConfig,
devtool: false,
module: {
rules: rules,
noParse: /\.(DS_Store)$/,
},
optimization: optimization,
plugins: plugins,
resolve: {
modules: [
path.resolve(process.cwd(), 'node_modules'),
'node_modules',
],
},
externals: {
jquery: 'jQuery'
}
};
};
// Styles configuration
const stylesConfig = (env, argv) => {
const config = getSharedConfig(env, argv);
return {
...config,
entry: () => readAllFileEntries('./assets/src/css'),
output: {
...config.output,
path: path.resolve(process.cwd(), 'assets', 'build', 'css'),
filename: '[name].js', // Needed even though we're extracting CSS
}
};
};
// Scripts configuration
const scriptsConfig = (env, argv) => {
const config = getSharedConfig(env, argv);
return {
...config,
entry: () => readAllFileEntries('./assets/src/js'),
output: {
...config.output,
path: path.resolve(process.cwd(), 'assets', 'build', 'js'),
}
};
};
// Assets configuration
const assetsConfig = (env, argv) => {
const config = getSharedConfig(env, argv);
return {
...config,
plugins: [
...config.plugins,
new CopyPlugin({
patterns: [
{
from: './assets/src/images',
to: path.resolve(process.cwd(), 'assets', 'build', 'images'),
},
// {
// from: './assets/src/library',
// to: path.resolve(process.cwd(), 'assets', 'build', 'library'),
// },
],
}),
],
entry: {}, // No entry points needed for copy plugin
output: {
path: path.resolve(process.cwd(), 'assets', 'build'),
}
};
};
module.exports = [stylesConfig, scriptsConfig, assetsConfig];