|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/* eslint-disable node/no-missing-require */ |
| 4 | +const webpackDevMiddleware = require('webpack-dev-middleware'); |
| 5 | +const webpackHotMiddleware = require('webpack-hot-middleware'); |
| 6 | +const browserSync = require('browser-sync'); |
| 7 | +/* eslint-enable node/no-missing-require */ |
| 8 | +const merge = require('deepmerge'); |
| 9 | + |
| 10 | +class BrowserSyncDevHotWebpackPlugin { |
| 11 | + constructor(options) { |
| 12 | + this.watcher = null; |
| 13 | + this.compiler = null; |
| 14 | + this.options = merge.all([ |
| 15 | + { |
| 16 | + browserSyncOptions: {}, |
| 17 | + callback() {}, // eslint-disable-line no-empty-function |
| 18 | + devMiddlewareOptions: { |
| 19 | + noInfo: true, |
| 20 | + stats: false |
| 21 | + }, |
| 22 | + hotMiddlewareOptions: {} |
| 23 | + }, |
| 24 | + options |
| 25 | + ]); |
| 26 | + } |
| 27 | + |
| 28 | + apply(compiler) { |
| 29 | + if (this.options.disable) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + this.compiler = compiler; |
| 34 | + |
| 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 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + start() { |
| 45 | + let browserSyncURLLocal = 'initialization'; |
| 46 | + let browserSyncURLExternal = 'initialization'; |
| 47 | + let browserSyncURLUI = 'initialization'; |
| 48 | + let browserSyncURLUIExternal = 'initialization'; |
| 49 | + const watcherConfig = merge.all([ |
| 50 | + { |
| 51 | + proxy: { |
| 52 | + middleware: this.middleware(), |
| 53 | + proxyReq: [ |
| 54 | + (proxyReq) => { |
| 55 | + proxyReq.setHeader('X-Browser-Sync-URL-Local', browserSyncURLLocal); |
| 56 | + proxyReq.setHeader('X-Browser-Sync-URL-External', browserSyncURLExternal); |
| 57 | + proxyReq.setHeader('X-Browser-Sync-URL-UI', browserSyncURLUI); |
| 58 | + proxyReq.setHeader('X-Browser-Sync-URL-UI-External', browserSyncURLUIExternal); |
| 59 | + proxyReq.setHeader('X-Dev-Middleware', 'On'); |
| 60 | + proxyReq.setHeader('X-Hot-Middleware', 'On'); |
| 61 | + } |
| 62 | + ] |
| 63 | + } |
| 64 | + }, |
| 65 | + this.options.browserSyncOptions |
| 66 | + ]); |
| 67 | + |
| 68 | + this.watcher.init(watcherConfig, (error, bs) => { |
| 69 | + if (error) { |
| 70 | + throw error; |
| 71 | + } |
| 72 | + |
| 73 | + const URLs = bs.getOption('urls'); |
| 74 | + |
| 75 | + browserSyncURLLocal = URLs.get('local'); |
| 76 | + browserSyncURLExternal = URLs.get('external'); |
| 77 | + browserSyncURLUI = URLs.get('ui'); |
| 78 | + browserSyncURLUIExternal = URLs.get('ui-external'); |
| 79 | + |
| 80 | + this.options.callback.bind(this); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + middleware() { |
| 85 | + const hotMiddlewareOptions = merge.all([ |
| 86 | + { |
| 87 | + log: this.watcher.notify.bind(this.watcher) |
| 88 | + }, |
| 89 | + this.options.hotMiddlewareOptions |
| 90 | + ]); |
| 91 | + |
| 92 | + this.webpackDevMiddleware = webpackDevMiddleware(this.compiler, this.options.devMiddlewareOptions); |
| 93 | + this.webpackHotMiddleware = webpackHotMiddleware(this.compiler, hotMiddlewareOptions); |
| 94 | + |
| 95 | + return [this.webpackDevMiddleware, this.webpackHotMiddleware]; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +module.exports = BrowserSyncDevHotWebpackPlugin; |
0 commit comments