1
+ const fs = require ( 'fs' )
1
2
const path = require ( 'path' )
2
- const webpack = require ( 'webpack' )
3
3
const MFS = require ( 'memory-fs' )
4
+ const webpack = require ( 'webpack' )
5
+ const chokidar = require ( 'chokidar' )
4
6
const clientConfig = require ( './webpack.client.config' )
5
7
const serverConfig = require ( './webpack.server.config' )
6
8
@@ -10,15 +12,31 @@ const readFile = (fs, file) => {
10
12
} catch ( e ) { }
11
13
}
12
14
13
- module . exports = function setupDevServer ( app , cb ) {
14
- let bundle , clientManifest
15
- let resolve
16
- const readyPromise = new Promise ( r => { resolve = r } )
17
- const ready = ( ...args ) => {
18
- resolve ( )
19
- cb ( ...args )
15
+ module . exports = function setupDevServer ( app , templatePath , cb ) {
16
+ let bundle
17
+ let template
18
+ let clientManifest
19
+
20
+ let ready
21
+ const readyPromise = new Promise ( r => { ready = r } )
22
+ const update = ( ) => {
23
+ if ( bundle && clientManifest ) {
24
+ ready ( )
25
+ cb ( bundle , {
26
+ template,
27
+ clientManifest
28
+ } )
29
+ }
20
30
}
21
31
32
+ // read template from disk and watch
33
+ template = fs . readFileSync ( templatePath , 'utf-8' )
34
+ chokidar . watch ( templatePath ) . on ( 'change' , ( ) => {
35
+ template = fs . readFileSync ( templatePath , 'utf-8' )
36
+ console . log ( 'index.html template updated.' )
37
+ update ( )
38
+ } )
39
+
22
40
// modify client config to work with hot middleware
23
41
clientConfig . entry . app = [ 'webpack-hot-middleware/client' , clientConfig . entry . app ]
24
42
clientConfig . output . filename = '[name].js'
@@ -39,16 +57,11 @@ module.exports = function setupDevServer (app, cb) {
39
57
stats . errors . forEach ( err => console . error ( err ) )
40
58
stats . warnings . forEach ( err => console . warn ( err ) )
41
59
if ( stats . errors . length ) return
42
-
43
60
clientManifest = JSON . parse ( readFile (
44
61
devMiddleware . fileSystem ,
45
62
'vue-ssr-client-manifest.json'
46
63
) )
47
- if ( bundle ) {
48
- ready ( bundle , {
49
- clientManifest
50
- } )
51
- }
64
+ update ( )
52
65
} )
53
66
54
67
// hot middleware
@@ -65,11 +78,7 @@ module.exports = function setupDevServer (app, cb) {
65
78
66
79
// read bundle generated by vue-ssr-webpack-plugin
67
80
bundle = JSON . parse ( readFile ( mfs , 'vue-ssr-server-bundle.json' ) )
68
- if ( clientManifest ) {
69
- ready ( bundle , {
70
- clientManifest
71
- } )
72
- }
81
+ update ( )
73
82
} )
74
83
75
84
return readyPromise
0 commit comments