-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
116 lines (108 loc) · 2.82 KB
/
webpack.config.js
File metadata and controls
116 lines (108 loc) · 2.82 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
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const { resolve, basename } = require( 'path' );
const { spawn } = require( 'child_process' );
const chalk = require( 'chalk' );
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
let rsyncError = false;
module.exports = {
...defaultConfig,
plugins: [
...defaultConfig.plugins,
/**
* Copy PHP files to the build directory.
*/
new CopyWebpackPlugin( {
patterns: [
{
from: 'src/*/*.php',
filter: ( absoluteFilename ) => {
// Only copy PHP files that belong to the current block being built
const blockName = absoluteFilename
.split( '/src/' )[ 1 ]
.split( '/' )[ 0 ];
const currentBlock = basename(
defaultConfig.output?.path || ''
);
return blockName === currentBlock;
},
to: '[name][ext]',
},
],
} ),
/**
* During watch mode we want to sync the build directory to the server.
* This relies on the user setting the RSYNC_DESTINATION environment variable.
* If it doesn't exist then we don't do anything.
*/
process.env.RSYNC_DESTINATION && rsyncError === false
? {
apply: ( compiler ) => {
compiler.hooks.done.tap(
'CustomWatchClosePlugin',
( stats ) => {
if (
! stats.hasErrors() &&
! stats.hasWarnings() &&
stats.compilation.compiler.watchMode
) {
process.stdout.write(
'!!! --- UPLOADING TO SERVER --- !!!\n'
);
const rsync = spawn( 'rsync', [
'-avz',
resolve( '.', 'build' ),
process.env.RSYNC_DESTINATION,
] );
/**
* Handle rsync output
*
* strip everything except the sent bytes
*/
rsync.stdout.on( 'data', ( data ) => {
if (
data.toString().includes( 'sent' )
) {
process.stdout.write(
data.toString()
);
}
} );
/**
* Handle rsync errors
*
* If we have any errors we set the rsyncError flag to true
*/
rsync.stderr.on( 'data', ( data ) => {
process.stderr.write(
chalk.red(
`rsync stderr: ${ data }`
)
);
rsyncError = true;
} );
/**
* Handle rsync completion
*/
rsync.on( 'close', ( code ) => {
if ( code === 0 ) {
process.stdout.write(
chalk.green(
'!!! --- UPLOAD SUCCESS --- !!!\n'
)
);
} else {
process.stdout.write(
chalk.red(
`rsync process exited with code ${ code }`
)
);
}
} );
}
}
);
},
}
: undefined,
],
};