-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
253 lines (240 loc) · 7.58 KB
/
Copy pathwebpack.config.js
File metadata and controls
253 lines (240 loc) · 7.58 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
const defaultConfig = require("@wordpress/scripts/config/webpack.config");
const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const RemoveEmptyScriptsPlugin = require("webpack-remove-empty-scripts");
const TerserPlugin = require("terser-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const isProd =
process.env.NODE_ENV === "production" ||
process.argv.includes("--mode=production") ||
process.argv.includes("-p");
const defaultRulesWithoutBabel = defaultConfig.module.rules.filter(
(rule) => !rule.use || !JSON.stringify(rule.use).includes("babel-loader"),
);
/* -------------------------------------------------------------------------
Shared element entries
------------------------------------------------------------------------- */
const ELEMENT_ENTRIES = [
// JS + SCSS pairs
"animated-heading",
"brand-slider",
"button-pro",
"counter",
"draggable-items",
"icon-box",
"image-accordion",
"progressbar",
"testimonial",
"testimonial-2",
"testimonial-3",
"toggle-switch",
"video-box-slider",
"video-box",
"video-mask",
"video-popup",
"video-story",
// CSS-only (JS is a 1-line stub that imports the SCSS)
"floating-elements",
"meta-info",
"posts-slider",
"social-icons",
"team",
"timeline",
];
const elementEntries = ELEMENT_ENTRIES.reduce((acc, name) => {
acc[`elements/${name}`] = `./src/js/elements/${name}.js`;
return acc;
}, {});
/* -------------------------------------------------------------------------
Config 1 — Admin React entries (dashboard, page-import).
Uses type: "javascript/esm" + modules: false so webpack correctly handles
import/export in the React source.
------------------------------------------------------------------------- */
const adminConfig = {
...defaultConfig,
name: "admin",
mode: isProd ? "production" : "development",
devtool: isProd ? false : "source-map",
externals: {
react: "React",
"react-dom": "ReactDOM",
},
entry: {
"admin/dashboard": "./src/admin/dashboard/main.js",
"admin/page-import": "./src/admin/page-import/main.js",
},
output: {
path: path.resolve(__dirname, "public/build"),
filename: "[name].js",
clean: false,
},
module: {
...defaultConfig.module,
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
// Force ESM parsing so webpack accepts the import/export statements
// that Babel leaves intact (package.json has type: "commonjs", which
// would otherwise make webpack parse .js files as scripts).
type: "javascript/esm",
resolve: { fullySpecified: false },
use: {
loader: require.resolve("babel-loader"),
options: {
presets: [
[require.resolve("@babel/preset-env"), { modules: false }],
require.resolve("@babel/preset-react"),
],
},
},
},
...defaultRulesWithoutBabel,
],
},
// Drop CleanWebpackPlugin — it would wipe public/build/ which is shared
// with the bundles config output.
plugins: defaultConfig.plugins.filter(
(p) => p.constructor.name !== "CleanWebpackPlugin",
),
resolve: {
extensions: [".js", ".jsx"],
alias: {
"@": path.resolve(__dirname, "src/admin/dashboard"),
},
},
// Only apply minification in production.
optimization: isProd
? {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: false,
terserOptions: { format: { comments: false } },
}),
],
}
: { minimize: false },
};
/* -------------------------------------------------------------------------
Config 2 — Bundled assets (elements, extensions, sitewide, admin vanilla).
IMPORTANT — SCSS is always extracted to .css files via MiniCssExtractPlugin,
in BOTH production and development. style-loader is intentionally NOT used,
because PHP enqueues these .css files directly from disk. style-loader only
injects styles into the DOM at runtime and never writes a file, which would
cause missing stylesheet errors in development.
------------------------------------------------------------------------- */
const bundlesConfig = {
name: "bundles",
mode: isProd ? "production" : "development",
devtool: isProd ? false : "source-map",
entry: {
...elementEntries,
"extensions/editor-panel": "./src/js/extensions/editor-panel.js",
"extensions/starter-animations-client":
"./src/js/extensions/starter-animations/client.js",
"extensions/starter-animations-builder":
"./src/js/extensions/starter-animations/builder.js",
frontend: "./src/js/frontend.js",
// Admin vanilla (non-React) JS + SCSS bundles
"admin/bricks-animation-addons-admin":
"./src/js/admin/bricks-animation-addons-admin.js",
"admin/aab-template-library": "./src/js/admin/aab-template-library.js",
},
output: {
path: path.resolve(__dirname, "public/build"),
filename: "[name].js",
assetModuleFilename: "images/[name][ext]",
clean: false,
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: require.resolve("babel-loader"),
options: {
presets: [
[require.resolve("@babel/preset-env"), { modules: "commonjs" }],
],
},
},
},
{
test: /\.scss$/,
// Always extract to a physical .css file — PHP enqueues these directly.
// Do NOT switch to style-loader in development; it never writes to disk.
use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"],
},
{
test: /\.(png|jpe?g|gif|svg|woff2?|ttf|eot)$/,
type: "asset/resource",
},
],
},
plugins: [
new RemoveEmptyScriptsPlugin(),
new MiniCssExtractPlugin({ filename: "[name].css" }),
],
optimization: isProd
? {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: false,
terserOptions: { format: { comments: false } },
}),
new CssMinimizerPlugin(),
],
}
: { minimize: false },
};
/* -------------------------------------------------------------------------
Config 3 - Copy source files for WordPress.org review.
Copies readable source files to public/build/src/ for review.
------------------------------------------------------------------------- */
const sourceCopyConfig = {
name: "source-copy",
mode: "development",
devtool: false,
optimization: {
minimize: false,
},
entry: {},
output: {
path: path.resolve(__dirname, "public/build"),
},
plugins: [
new CopyPlugin({
patterns: [
// Copy Elements JS source files
{
context: "src/js/elements",
from: "**/*.js",
to: "elements/src/js/[name][ext]",
},
// Copy Elements SCSS source files as CSS for review
{
context: "src/scss/elements",
from: "**/*.scss",
to: "elements/src/css/[name].css",
},
// Copy Extensions JS source files
{
context: "src/js/extensions",
from: "**/*.js",
to: "extensions/src/js/[path][name][ext]",
},
// Copy Extensions SCSS source files as CSS for review
{
context: "src/scss/extensions",
from: "**/*.scss",
to: "extensions/src/css/[name].css",
},
],
}),
],
};
module.exports = [adminConfig, bundlesConfig, sourceCopyConfig];