-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
93 lines (86 loc) · 2.24 KB
/
webpack.config.js
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
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const { resolve } = require( 'path' );
const { spawn } = require( 'child_process' );
const chalk = require( 'chalk' );
let rsyncError = false;
module.exports = {
...defaultConfig,
plugins: [
...defaultConfig.plugins,
/**
* 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,
],
};