|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -/* eslint-disable node/no-missing-require */ |
4 | | -const webpackDevMiddleware = require('webpack-dev-middleware'); |
5 | | -const webpackHotMiddleware = require('webpack-hot-middleware'); |
| 3 | +/* eslint-disable no-sync */ |
| 4 | +const EventEmitter = require('events'); |
6 | 5 | const browserSync = require('browser-sync'); |
7 | | -/* eslint-enable node/no-missing-require */ |
8 | 6 | const merge = require('deepmerge'); |
| 7 | +const { desire } = require('./utils'); |
9 | 8 |
|
10 | | -class BrowserSyncDevHotWebpackPlugin { |
| 9 | +const webpackDevMiddleware = desire('webpack-dev-middleware'); |
| 10 | +const webpackHotMiddleware = desire('webpack-hot-middleware'); |
| 11 | + |
| 12 | +class BrowserSyncDevHotWebpackPlugin extends EventEmitter { |
11 | 13 | constructor(options) { |
12 | | - this.watcher = null; |
| 14 | + super(); |
| 15 | + |
13 | 16 | this.compiler = null; |
| 17 | + this.middleware = []; |
| 18 | + this.watcher = browserSync.create(); |
14 | 19 | this.options = merge.all([ |
15 | 20 | { |
16 | | - browserSyncOptions: {}, |
| 21 | + browserSync: {}, |
17 | 22 | callback() {}, // eslint-disable-line no-empty-function |
18 | | - devMiddlewareOptions: { |
| 23 | + devMiddleware: { |
19 | 24 | noInfo: true, |
20 | 25 | stats: false |
21 | 26 | }, |
22 | | - hotMiddlewareOptions: {} |
| 27 | + hotMiddleware: {} |
23 | 28 | }, |
24 | 29 | options |
25 | 30 | ]); |
26 | 31 | } |
27 | 32 |
|
| 33 | + registerEvents() { |
| 34 | + this.on('webpack.compilation', () => this.watcher.notify('Rebuilding...')); |
| 35 | + this.once('webpack.done', this.start.bind(this)); |
| 36 | + } |
| 37 | + |
28 | 38 | apply(compiler) { |
29 | 39 | if (this.options.disable) { |
30 | 40 | return; |
31 | 41 | } |
32 | 42 |
|
| 43 | + this.registerEvents(); |
33 | 44 | this.compiler = compiler; |
34 | 45 |
|
35 | | - compiler.plugin('done', () => { |
36 | | - if (!this.watcher) { |
37 | | - this.watcher = browserSync.create(); |
38 | | - compiler.plugin('compilation', () => this.watcher.notify('Rebuilding...')); |
39 | | - this.start(); |
40 | | - } |
41 | | - }); |
| 46 | + compiler.plugin('compilation', this.emit.bind(this, 'webpack.compilation')); |
| 47 | + compiler.plugin('done', this.emit.bind(this, 'webpack.done')); |
42 | 48 | } |
43 | 49 |
|
44 | | - start() { |
45 | | - let browserSyncURLLocal = 'initialization'; |
46 | | - let browserSyncURLExternal = 'initialization'; |
47 | | - let browserSyncURLUI = 'initialization'; |
48 | | - let browserSyncURLUIExternal = 'initialization'; |
49 | | - const watcherConfig = merge.all([ |
| 50 | + setupWebpackDevMiddleware() { |
| 51 | + this.webpackDevMiddleware = webpackDevMiddleware(this.compiler, merge.all([ |
| 52 | + { |
| 53 | + publicPath: this.options.publicPath || this.compiler.options.output.publicPath |
| 54 | + }, |
| 55 | + this.compiler.options.devServer || {}, |
| 56 | + this.options.devMiddleware |
| 57 | + ])); |
| 58 | + |
| 59 | + this.middleware.push(this.webpackDevMiddleware); |
| 60 | + } |
| 61 | + |
| 62 | + setupWebpackHotMiddleware() { |
| 63 | + this.webpackHotMiddleware = webpackHotMiddleware(this.compiler, merge.all([ |
| 64 | + { |
| 65 | + log: this.watcher.notify.bind(this.watcher) |
| 66 | + }, |
| 67 | + this.options.hotMiddleware |
| 68 | + ])); |
| 69 | + |
| 70 | + this.middleware.push(this.webpackHotMiddleware); |
| 71 | + } |
| 72 | + |
| 73 | + config() { |
| 74 | + this.browserSyncURLLocal = null; |
| 75 | + this.browserSyncURLExternal = null; |
| 76 | + this.browserSyncURLUI = null; |
| 77 | + this.browserSyncURLUIExternal = null; |
| 78 | + |
| 79 | + this.options.browserSync = merge.all([ |
50 | 80 | { |
51 | 81 | proxy: { |
52 | | - middleware: this.middleware(), |
| 82 | + middleware: this.middleware, |
53 | 83 | proxyReq: [ |
54 | 84 | (proxyReq) => { |
55 | | - if (browserSyncURLLocal) { |
56 | | - proxyReq.setHeader('X-Browser-Sync-URL-Local', browserSyncURLLocal); |
| 85 | + if (this.browserSyncURLLocal) { |
| 86 | + proxyReq.setHeader('X-Browser-Sync-URL-Local', this.browserSyncURLLocal); |
57 | 87 | } |
58 | 88 |
|
59 | | - if (browserSyncURLExternal) { |
60 | | - proxyReq.setHeader('X-Browser-Sync-URL-External', browserSyncURLExternal); |
| 89 | + if (this.browserSyncURLExternal) { |
| 90 | + proxyReq.setHeader('X-Browser-Sync-URL-External', this.browserSyncURLExternal); |
61 | 91 | } |
62 | 92 |
|
63 | | - if (browserSyncURLUI) { |
64 | | - proxyReq.setHeader('X-Browser-Sync-URL-UI', browserSyncURLUI); |
| 93 | + if (this.browserSyncURLUI) { |
| 94 | + proxyReq.setHeader('X-Browser-Sync-URL-UI', this.browserSyncURLUI); |
65 | 95 | } |
66 | 96 |
|
67 | | - if (browserSyncURLUIExternal) { |
68 | | - proxyReq.setHeader('X-Browser-Sync-URL-UI-External', browserSyncURLUIExternal); |
| 97 | + if (this.browserSyncURLUIExternal) { |
| 98 | + proxyReq.setHeader('X-Browser-Sync-URL-UI-External', this.browserSyncURLUIExternal); |
69 | 99 | } |
70 | 100 |
|
71 | | - proxyReq.setHeader('X-Dev-Middleware', 'On'); |
72 | | - proxyReq.setHeader('X-Hot-Middleware', 'On'); |
| 101 | + if (webpackDevMiddleware) { |
| 102 | + proxyReq.setHeader('X-Dev-Middleware', 'On'); |
| 103 | + } |
| 104 | + |
| 105 | + if (webpackHotMiddleware) { |
| 106 | + proxyReq.setHeader('X-Hot-Middleware', 'On'); |
| 107 | + } |
73 | 108 | } |
74 | 109 | ] |
| 110 | + }, |
| 111 | + watchOptions: { |
| 112 | + ignoreInitial: true |
75 | 113 | } |
76 | 114 | }, |
77 | | - this.options.browserSyncOptions |
| 115 | + this.options.browserSync |
78 | 116 | ]); |
| 117 | + } |
79 | 118 |
|
80 | | - this.watcher.init(watcherConfig, (error, bs) => { |
81 | | - if (error) { |
82 | | - throw error; |
83 | | - } |
84 | | - |
85 | | - const URLs = bs.getOption('urls'); |
| 119 | + setup() { |
| 120 | + if (webpackDevMiddleware) { |
| 121 | + this.setupWebpackDevMiddleware(); |
| 122 | + } |
86 | 123 |
|
87 | | - browserSyncURLLocal = URLs.get('local'); |
88 | | - browserSyncURLExternal = URLs.get('external'); |
89 | | - browserSyncURLUI = URLs.get('ui'); |
90 | | - browserSyncURLUIExternal = URLs.get('ui-external'); |
| 124 | + if (webpackHotMiddleware) { |
| 125 | + this.setupWebpackHotMiddleware(); |
| 126 | + } |
91 | 127 |
|
92 | | - this.options.callback.bind(this); |
93 | | - }); |
| 128 | + this.config(); |
94 | 129 | } |
95 | 130 |
|
96 | | - middleware() { |
97 | | - const hotMiddlewareOptions = merge.all([ |
98 | | - { |
99 | | - log: this.watcher.notify.bind(this.watcher) |
100 | | - }, |
101 | | - this.options.hotMiddlewareOptions |
102 | | - ]); |
| 131 | + start() { |
| 132 | + this.setup(); |
103 | 133 |
|
104 | | - this.webpackDevMiddleware = webpackDevMiddleware(this.compiler, this.options.devMiddlewareOptions); |
105 | | - this.webpackHotMiddleware = webpackHotMiddleware(this.compiler, hotMiddlewareOptions); |
| 134 | + process.nextTick(() => { |
| 135 | + this.watcher.init(this.options.browserSync, (error, bs) => { |
| 136 | + if (error) { |
| 137 | + throw error; |
| 138 | + } |
| 139 | + |
| 140 | + const URLs = bs.getOption('urls'); |
106 | 141 |
|
107 | | - return [this.webpackDevMiddleware, this.webpackHotMiddleware]; |
| 142 | + this.browserSyncURLLocal = URLs.get('local'); |
| 143 | + this.browserSyncURLExternal = URLs.get('external'); |
| 144 | + this.browserSyncURLUI = URLs.get('ui'); |
| 145 | + this.browserSyncURLUIExternal = URLs.get('ui-external'); |
| 146 | + |
| 147 | + this.options.callback.bind(this); |
| 148 | + }); |
| 149 | + }); |
108 | 150 | } |
109 | 151 | } |
110 | 152 |
|
|
0 commit comments