-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
111 lines (104 loc) · 3.51 KB
/
webpack.config.js
File metadata and controls
111 lines (104 loc) · 3.51 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
/* eslint-disable no-console */
// Set environment variable for free build
if ( typeof process.env.BUILD_TYPE === 'undefined' ) {
process.env.BUILD_TYPE = 'free'
}
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' )
const { externals } = require( './.config/externals' )
const rules = require( './.config/rules' )
const path = require( 'path' )
const { exec } = require( 'child_process' )
const fs = require( 'fs' )
const webpack = require( 'webpack' )
// Count premium action types
function getPremiumActionTypesCount() {
const actionTypesDir = path.resolve( __dirname, './pro__premium_only/src/action-types' )
if ( fs.existsSync( actionTypesDir ) ) {
const files = fs.readdirSync( actionTypesDir )
return files.filter( file => file.startsWith( 'class-action-type-' ) && file.endsWith( '.php' ) ).length
}
return 0
}
// Count premium interaction types
function getPremiumInteractionTypesCount() {
const interactionTypesDir = path.resolve( __dirname, './pro__premium_only/src/interaction-types' )
if ( fs.existsSync( interactionTypesDir ) ) {
const files = fs.readdirSync( interactionTypesDir )
return files.filter( file => file.startsWith( 'class-interaction-type-' ) && file.endsWith( '.php' ) ).length
}
return 0
}
// Compile all the frontend scripts to dist/frontend-*.js files.
function getFrontendActionEntries() {
const entries = {}
const scriptsDir = path.resolve( __dirname, './src/action-types/frontend/scripts' )
if ( fs.existsSync( scriptsDir ) ) {
const files = fs.readdirSync( scriptsDir )
for ( const file of files ) {
if ( file.endsWith( '.js' ) ) {
const name = `frontend-${ file.replace( /.js$/, '' ) }`
entries[ name ] = path.resolve( scriptsDir, file )
}
}
}
return entries
}
module.exports = {
...defaultConfig,
entry: {
editor: path.resolve( __dirname, './src/editor/editor.js' ),
admin: path.resolve( __dirname, './src/admin/admin.js' ),
frontend: path.resolve( __dirname, './src/frontend/scripts/frontend.js' ),
'admin-manage-interactions': path.resolve( __dirname, './src/admin/manage-interactions.js' ),
...getFrontendActionEntries(),
},
output: {
...defaultConfig.output,
filename: '[name].js',
path: path.resolve( __dirname, 'dist' ),
clean: process.env.NODE_ENV === 'production' && ! process.env.PRESERVE_PREMIUM_FILES, // Don't clean if premium files should be preserved
},
resolve: {
...defaultConfig.resolve,
alias: {
...defaultConfig.resolve?.alias,
'~interact': path.resolve( __dirname, 'src' ),
'~premium': path.resolve( __dirname, 'pro__premium_only/src' ),
},
},
externals,
module: {
...defaultConfig.module,
strictExportPresence: true,
rules,
},
plugins: [
...defaultConfig.plugins,
new webpack.DefinePlugin( {
BUILD_TYPE: JSON.stringify( process.env.BUILD_TYPE || 'free' ),
PREMIUM_ACTIONS_NUM: getPremiumActionTypesCount(),
PREMIUM_INTERACTIONS_NUM: getPremiumInteractionTypesCount(),
} ),
{
apply: compiler => {
compiler.hooks.watchRun.tap( 'FrontendPHPGenerator', () => {
// Use the appropriate script based on BUILD_TYPE
const buildType = process.env.BUILD_TYPE || 'free'
const script = buildType === 'premium' ? 'npm run build:frontend-php:premium' : 'npm run build:frontend-php'
exec( script, ( error, stdout, stderr ) => {
if ( error ) {
console.error( 'Error generating frontend PHP:', error )
return
}
if ( stdout ) {
console.log( stdout )
}
if ( stderr ) {
console.error( stderr )
}
} )
} )
},
},
],
}