Repository navigation
Expand file tree
/
Copy pathwebpack.config.js
More file actions
478 lines (435 loc) · 14.3 KB
/
Copy pathwebpack.config.js
File metadata and controls
478 lines (435 loc) · 14.3 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/**
* External dependencies
*/
const fs = require( 'fs' );
const path = require( 'path' );
const CssMinimizerPlugin = require( 'css-minimizer-webpack-plugin' );
const RemoveEmptyScriptsPlugin = require( 'webpack-remove-empty-scripts' );
/**
* WordPress dependencies
*/
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const isProduction = process.env.NODE_ENV === 'production';
const mode = isProduction ? 'production' : 'development';
// Extend the default config.
const sharedConfig = {
mode,
...defaultConfig,
output: {
path: path.resolve( process.cwd(), 'assets', 'build', 'js' ),
filename: '[name].min.js',
chunkFilename: '[name].min.js',
},
plugins: [
...defaultConfig.plugins
/**
* Remove the RTL CSS plugin from the default configuration.
*
* RTL CSS is not currently in use, and its output was being incorrectly placed in the JS build directory.
* This plugin can be re-enabled in the future if RTL support is needed.
*/
.filter( ( plugin ) => plugin.constructor.name !== 'RtlCssPlugin' )
.map(
( plugin ) => {
if ( plugin.constructor.name === 'MiniCssExtractPlugin' ) {
plugin.options.filename = '../css/[name].css';
}
return plugin;
},
),
new RemoveEmptyScriptsPlugin(),
],
optimization: {
...defaultConfig.optimization,
splitChunks: {
...defaultConfig.optimization.splitChunks,
},
minimizer: defaultConfig.optimization.minimizer.concat( [ new CssMinimizerPlugin() ] ),
},
// Only generate source maps in development mode
devtool: isProduction ? false : 'source-map',
};
// Generate a webpack config which includes setup for CSS extraction.
// Look for css/scss files and extract them into a build/css directory.
const styles = {
mode,
...sharedConfig,
entry: () => {
const entries = {};
const CSS_DIR = path.resolve( process.cwd(), 'assets', 'src', 'css' );
fs.readdirSync( CSS_DIR ).forEach( ( fileName ) => {
const fullPath = `${ CSS_DIR }/${ fileName }`;
// Skip directories and files starting with _
if ( ! fs.lstatSync( fullPath ).isDirectory() && ! fileName.startsWith( '_' ) ) {
entries[ fileName.replace( /\.[^/.]+$/, '' ) ] = fullPath;
}
} );
return entries;
},
module: {
...sharedConfig.module,
},
plugins: [
...sharedConfig.plugins.filter(
( plugin ) => plugin.constructor.name !== 'DependencyExtractionWebpackPlugin',
),
],
// Only generate source maps in development mode
devtool: isProduction ? false : 'source-map',
};
// Example of how to add a new entry point for JS file.
const mainJS = {
...sharedConfig,
entry: {
main: path.resolve( process.cwd(), 'assets', 'src', 'js', 'main.js' ),
},
};
const adminJS = {
...sharedConfig,
entry: {
admin: path.resolve( process.cwd(), 'assets', 'src', 'js', 'admin.js' ),
},
};
const mediaLibrary = {
...sharedConfig,
entry: {
'media-library': path.resolve( process.cwd(), 'assets', 'src', 'js', 'media-library', 'index.js' ),
},
// This entry now code-splits video.js into an async chunk (see videojs-loader). It
// shares the assets/build/js output dir and the default `webpackChunkgodam` global
// with godamPlayerFrontend / godamImageLayersFrontend, which ALSO emit video.js-family
// async chunks. Give this compilation its own runtime global (uniqueName) and prefixed,
// content-hashed chunk filenames so its async chunks can't collide with theirs on disk
// or clobber each other's chunk-loading runtime (the "TypeError: o[t] is not a function"
// class of bug documented on godamImageLayersFrontend below). The content hash also
// avoids a browser serving a stale videojs chunk after a plugin update.
output: {
...sharedConfig.output,
uniqueName: 'godamMediaLibrary',
chunkFilename: 'media-library-[name].[contenthash].min.js',
},
};
const godamPlayerFrontend = {
...sharedConfig,
entry: {
'godam-player-frontend': path.resolve( process.cwd(), 'assets', 'src', 'js', 'godam-player', 'frontend.js' ),
},
};
const godamImageLayersFrontend = {
...sharedConfig,
entry: {
'godam-image-layers-frontend': path.resolve( process.cwd(), 'assets', 'src', 'js', 'godam-image-layers', 'frontend.js' ),
},
// This is a separate webpack compilation that shares the output dir with
// godam-player-frontend, and BOTH code-split FontAwesome into async chunks
// named `fontawesome-core`/`fontawesome-icons` (via loadFontAwesome). With the
// default settings the two builds emit identically-named chunks that clobber
// each other on disk AND share the default `webpackChunkgodam` chunk-loading
// global, so this entry ends up loading the player build's FA chunk under its
// own runtime -> "TypeError: o[t] is not a function" and icons never render.
// Give this entry its own runtime global (uniqueName) and chunk filenames so
// its async chunks stay independent of the player's.
output: {
...sharedConfig.output,
uniqueName: 'godamImageLayers',
chunkFilename: 'godam-image-layers-[name].min.js',
},
};
const godamPlayerAnalytics = {
...sharedConfig,
entry: {
'godam-player-analytics': path.resolve( process.cwd(), 'assets', 'src', 'js', 'godam-player', 'analytics.js' ),
// Standalone layer-analytics runtime for pages without the video player
// (e.g. the GoDAM Image block with product hotspots).
'godam-layer-analytics': path.resolve( process.cwd(), 'assets', 'src', 'js', 'godam-player', 'layer-analytics.js' ),
},
};
const deactivationJS = {
...sharedConfig,
entry: {
'deactivation-feedback': path.resolve( process.cwd(), 'assets', 'src', 'js', 'deactivation-feedback.js' ),
},
};
const dashboardWidgetJS = {
...sharedConfig,
entry: {
'dashboard-widget': path.resolve( process.cwd(), 'assets', 'src', 'js', 'dashboard-widget', 'index.js' ),
},
};
const httpAuthDetector = {
...sharedConfig,
entry: {
'http-auth-detector': path.resolve( process.cwd(), 'assets', 'src', 'js', 'http-auth-detector.js' ),
},
};
const godamGallery = {
...sharedConfig,
entry: {
'godam-gallery': path.resolve( process.cwd(), 'assets', 'src', 'js', 'godam-gallery.js' ),
},
};
const godamVideoEmbed = {
...sharedConfig,
entry: {
'godam-video-embed': path.resolve( process.cwd(), 'assets', 'src', 'js', 'godam-video-embed.js' ),
},
};
const godamVideoPreview = {
...sharedConfig,
entry: {
'godam-video-preview': path.resolve( process.cwd(), 'assets', 'src', 'js', 'godam-video-preview.js' ),
},
};
const gfGodamRecorderEditorJS = {
...sharedConfig,
entry: {
'gf-godam-recorder-editor': path.resolve( process.cwd(), 'assets', 'src', 'js', 'gravity-form', 'gf-godam-recorder-editor.js' ),
},
};
const wpFormsGodamRecorderEditorJS = {
...sharedConfig,
entry: {
'wpforms-godam-recorder-editor': path.resolve( process.cwd(), 'assets', 'src', 'js', 'wpforms-godam-recorder-editor.js' ),
},
};
const jetpackFormJS = {
...sharedConfig,
entry: {
'jetpack-form': path.resolve( process.cwd(), 'assets', 'src', 'js', 'jetpack-form.js' ),
},
};
const elementorWidgetJS = {
...sharedConfig,
entry: {
'godam-elementor-frontend': path.resolve( process.cwd(), 'assets', 'src', 'js', 'elementor', 'frontend.js' ),
},
};
const elementorEditorJS = {
...sharedConfig,
entry: {
'godam-elementor-editor': path.resolve( process.cwd(), 'assets', 'src', 'js', 'elementor', 'editor.js' ),
},
};
const godamRecorder = {
...sharedConfig,
entry: {
'godam-recorder': path.resolve( process.cwd(), 'assets', 'src', 'js', 'godam-recorder', 'index.js' ),
},
};
const fluentForms = {
...sharedConfig,
entry: {
fluentforms: path.resolve( process.cwd(), 'assets', 'src', 'js', 'fluentforms', 'index.js' ),
'godam-fluentforms-editor': path.resolve( process.cwd(), 'assets', 'src', 'js', 'fluentforms', 'editor.js' ),
},
};
const everestForms = {
...sharedConfig,
entry: {
everestforms: path.resolve( process.cwd(), 'assets', 'src', 'js', 'everestforms', 'index.js' ),
},
};
const godamPlayerSDK = {
...sharedConfig,
entry: {
godamPlayerSDK: path.resolve( process.cwd(), 'assets', 'src', 'js', 'godam-player', 'godam-player-sdk.js' ),
'godam-player-sdk': path.resolve( process.cwd(), 'assets', 'src', 'js', 'godam-player', 'godam-player-sdk.js' ),
},
};
const lifterLMSBlock = {
...sharedConfig,
entry: {
lifterLMSBlock: path.resolve( process.cwd(), 'assets', 'src', 'js', 'lifterlms', 'block.js' ),
'godam-lifterlms-block': path.resolve( process.cwd(), 'assets', 'src', 'js', 'lifterlms', 'block.js' ),
},
};
const lifterLMSEmbed = {
...sharedConfig,
entry: {
lifterLMSEmbed: path.resolve( process.cwd(), 'assets', 'src', 'js', 'lifterlms', 'embed.js' ),
'godam-lifterlms-embed': path.resolve( process.cwd(), 'assets', 'src', 'js', 'lifterlms', 'embed.js' ),
},
};
const ninjaForms = {
...sharedConfig,
entry: {
'ninja-forms': path.resolve( process.cwd(), 'assets', 'src', 'js', 'ninja-forms', 'index.js' ),
},
};
const wpBakeryVideoSelectorParam = {
...sharedConfig,
entry: {
'wpbakery-video-selector-param': path.resolve( process.cwd(), 'assets', 'src', 'js', 'wpbakery', 'wpbakery-video-selector-param.js' ),
},
};
const wpBakeryAudioSelectorParam = {
...sharedConfig,
entry: {
'wpbakery-audio-selector-param': path.resolve( process.cwd(), 'assets', 'src', 'js', 'wpbakery', 'wpbakery-audio-selector-param.js' ),
},
};
const wpBakeryImageSrcSelectorParam = {
...sharedConfig,
entry: {
'wpbakery-image-src-selector-param': path.resolve( process.cwd(), 'assets', 'src', 'js', 'wpbakery', 'wpbakery-image-src-selector-param.js' ),
},
};
const wpBakeryImageSelectorParam = {
...sharedConfig,
entry: {
'wpbakery-image-selector-param': path.resolve( process.cwd(), 'assets', 'src', 'js', 'wpbakery', 'wpbakery-image-selector-param.js' ),
},
};
const wpBakeryDocumentSelectorParam = {
...sharedConfig,
entry: {
'wpbakery-document-selector-param': path.resolve( process.cwd(), 'assets', 'src', 'js', 'wpbakery', 'wpbakery-document-selector-param.js' ),
},
};
const wpBakeryDocumentCoverSelectorParam = {
...sharedConfig,
entry: {
'wpbakery-document-cover-selector-param': path.resolve( process.cwd(), 'assets', 'src', 'js', 'wpbakery', 'wpbakery-document-cover-selector-param.js' ),
},
};
const ninjaFormsSubmissionsList = {
...sharedConfig,
entry: {
'ninja-forms-submissions-list': path.resolve( process.cwd(), 'assets', 'src', 'js', 'ninja-forms', 'ninja-forms-submissions-list.js' ),
},
};
const blockExtensions = {
...sharedConfig,
entry: {
'block-extensions': path.resolve( process.cwd(), 'assets', 'src', 'block-extensions', 'index.js' ),
},
};
// Define the `pages` directory
const pagesDir = path.resolve( __dirname, './pages' );
// Create an entry object, mapping each subdirectory to its `index.js`
const entryPoints = {};
fs.readdirSync( pagesDir ).forEach( ( folder ) => {
const folderPath = path.join( pagesDir, folder );
const entryFile = path.join( folderPath, 'index.js' );
if ( fs.statSync( folderPath ).isDirectory() && fs.existsSync( entryFile ) ) {
entryPoints[ folder ] = entryFile; // Use the folder name as the key
}
} );
entryPoints[ 'page-css' ] = path.resolve( process.cwd(), 'pages', 'index.js' );
const pages = {
mode,
entry: entryPoints, // Dynamic entry points for each page
output: {
path: path.resolve( __dirname, './assets/build/pages' ), // Output directory
filename: '[name].min.js', // Each entry gets its own output file
chunkFilename: '[name].min.js',
},
module: {
rules: [
{
test: /\.(js|jsx)$/, // Handle JS/JSX files
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [ '@babel/preset-env', '@babel/preset-react' ],
},
},
},
{
test: /\.(css|scss)$/, // Handle CSS files
// `sideEffects: true` keeps bare CSS imports (e.g.
// `@wordpress/dataviews/build-style/style.css`) from being
// tree-shaken out of the bundle in production mode.
sideEffects: true,
use: [ 'style-loader', 'css-loader', 'postcss-loader', 'sass-loader' ],
},
{
test: /\.(png|jpg|jpeg|gif|svg|webp)$/, // Handle image files
use: [
{
loader: 'file-loader',
options: {
name: '[name].[hash].[ext]',
outputPath: 'images',
},
},
],
},
{
// Emit font files referenced from imported stylesheets (e.g. the
// Titan One sticker face in `godam-player.scss`) as their own
// files instead of inlining them, so they are fetched only when a
// rule actually uses the font. The `styles` config handles this
// via the @wordpress/scripts defaults; the `pages` config needs
// its own rule.
test: /\.woff2$/,
type: 'asset/resource',
generator: {
filename: 'fonts/[name].[hash][ext]',
},
},
],
},
externals: {
react: 'React',
'react-dom': 'ReactDOM',
'@wordpress/element': [ 'wp', 'element' ], // For WordPress compatibility
'@wordpress/i18n': [ 'wp', 'i18n' ],
'@wordpress/components': [ 'wp', 'components' ],
'@wordpress/api-fetch': [ 'wp', 'apiFetch' ],
'@wordpress/primitives': [ 'wp', 'primitives' ],
'@wordpress/data': [ 'wp', 'data' ],
'@wordpress/notices': [ 'wp', 'notices' ],
// `@wordpress/dataviews` reaches into `@wordpress/components` private
// APIs through `@wordpress/private-apis`. The lock store is keyed on a
// per-module-instance WeakMap, so a locally bundled copy of
// private-apis would fail to unlock the WP-provided components build
// ("Cannot unlock an object that was not locked before"). Externalize
// it to wp.privateApis so the bundled DataViews shares the same
// instance core registered. DataViews itself is still bundled because
// core ships no `wp-dataviews` script handle.
'@wordpress/private-apis': [ 'wp', 'privateApis' ],
},
resolve: {
extensions: [ '.js', '.jsx' ], // Automatically resolve these extensions
},
// Only generate source maps in development mode
devtool: isProduction ? false : 'source-map',
};
module.exports = [
mainJS,
adminJS,
mediaLibrary,
godamPlayerFrontend,
godamImageLayersFrontend,
godamPlayerAnalytics,
deactivationJS,
dashboardWidgetJS,
httpAuthDetector,
godamGallery,
godamVideoEmbed,
godamVideoPreview,
gfGodamRecorderEditorJS,
wpFormsGodamRecorderEditorJS,
jetpackFormJS,
styles, // Do not remove this.
pages,
elementorWidgetJS,
elementorEditorJS,
godamRecorder,
fluentForms,
everestForms,
godamPlayerSDK,
lifterLMSBlock,
lifterLMSEmbed,
ninjaForms,
wpBakeryVideoSelectorParam,
wpBakeryAudioSelectorParam,
wpBakeryImageSrcSelectorParam,
wpBakeryImageSelectorParam,
wpBakeryDocumentSelectorParam,
wpBakeryDocumentCoverSelectorParam,
ninjaFormsSubmissionsList,
blockExtensions,
];