-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
96 lines (89 loc) · 2.48 KB
/
Copy pathwebpack.config.js
File metadata and controls
96 lines (89 loc) · 2.48 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
// Load the default @wordpress/scripts config object
const path = require( 'path' );
const fs = require( 'fs' );
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
/**
* Webpack plugin to prepend ABSPATH guard to generated .asset.php files.
* WordPress.org requires all PHP files to prevent direct access.
*/
class AbsPathGuardPlugin {
apply( compiler ) {
compiler.hooks.afterEmit.tap( 'AbsPathGuardPlugin', ( compilation ) => {
const outputPath = compilation.outputOptions.path;
const assetFiles = fs.readdirSync( outputPath ).filter( ( f ) => f.endsWith( '.asset.php' ) );
assetFiles.forEach( ( file ) => {
const filePath = path.join( outputPath, file );
const content = fs.readFileSync( filePath, 'utf8' );
if ( ! content.includes( 'ABSPATH' ) ) {
fs.writeFileSync( filePath, content.replace( '<?php', '<?php if ( ! defined( \'ABSPATH\' ) ) { exit; } ' ) );
}
} );
} );
}
}
// Use the defaultConfig but add the common aliases, modules and plugins.
const commonConfig = {
...defaultConfig,
resolve: {
alias: {
...defaultConfig.resolve.alias,
'@Scripts': path.resolve( __dirname, 'scripts/' ),
'@DashboardApp': path.resolve( __dirname, 'admin/dashboard-app/src/' ),
'@Sidebar': path.resolve( __dirname, 'sidebar/src/' ),
},
},
module: {
rules: [
...defaultConfig.module.rules,
{
test: /\.(png|jpg|jpeg|gif|ico|svg)$/,
use: [
'file-loader',
],
},
],
},
plugins: [
...defaultConfig.plugins.filter( function ( plugin ) {
if ( plugin.constructor.name === 'LiveReloadPlugin' ) {
return false;
}
return true;
} ),
new AbsPathGuardPlugin(),
],
};
// Now using the commonConfig that inherits the defaultConfig, replace the entry and output properties for each app.
// Config for the Zip AI Dashboard App.
const dashboardConfig = Object.assign( {}, commonConfig, {
name: 'zip-dashboard-app',
entry: {
'dashboard-app': path.resolve(
__dirname,
'admin/dashboard-app/src/DashboardApp.js'
),
},
output: {
filename: '[name].js',
path: path.resolve( __dirname, 'admin/dashboard-app/build' ),
},
} );
// Config for the Zip AI Sidebar App.
const sidebarConfig = Object.assign( {}, commonConfig, {
name: 'zip-sidebar-app',
entry: {
'sidebar-app': path.resolve(
__dirname,
'sidebar/src/index.js'
),
},
output: {
filename: '[name].js',
path: path.resolve( __dirname, 'sidebar/build' ),
},
} );
// Export all the configs.
module.exports = [
dashboardConfig,
sidebarConfig,
];