Skip to content

Commit a5d2c96

Browse files
author
evilebottnawi
committed
Chore: prepare 0.1.0 release.
1 parent bbac06b commit a5d2c96

File tree

5 files changed

+129
-64
lines changed

5 files changed

+129
-64
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
44

55
This project adheres to [Semantic Versioning](http://semver.org/).
66

7+
# 0.1.0 - 2017-04-13
8+
9+
- Added: allow use plugin if `webpack-dev-middleware` or `webpack-hot-middleware` not installed.
10+
- Changed: rename `browserSyncOptions` to `browserSync`.
11+
- Changed: rename `devMiddlewareOptions` to `devMiddleware`.
12+
- Changed: rename `hotMiddlewareOptions` to `hotMiddleware`.
13+
- Changed: by default `ignoreInitial` is now `true`.
14+
- Fixed: problem with resolve port when `browser-sync` and `webpack-dev-middleware` works together.
15+
716
# 0.0.3 - 2017-03-13
817

918
- Fixed: don't crush if `urls.local`, or `urls.external`, or `urls.ui`, or `urls.ui-external`

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![dependencies Status](https://david-dm.org/itgalaxy/browser-sync-dev-hot-webpack-plugin/status.svg)](https://david-dm.org/itgalaxy/browser-sync-dev-hot-webpack-plugin)
66
[![devDependencies Status](https://david-dm.org/itgalaxy/browser-sync-dev-hot-webpack-plugin/dev-status.svg)](https://david-dm.org/itgalaxy/browser-sync-dev-hot-webpack-plugin?type=dev)
77

8-
Combines BrowserSync, webpack-dev-middleware, and webpack-hot-middleware into one plugin.
8+
Combines `browser-sync`, `webpack-dev-middleware`, and `webpack-hot-middleware` into one plugin.
99

1010
## Install
1111

@@ -27,9 +27,9 @@ const webpackConfig = {
2727
plugins: [
2828
// Other plugins...
2929
new BrowserSyncHotPlugin({
30-
browserSyncOptions: BROWSER_SYNC_OPTIONS,
31-
devMiddlewareOptions: DEV_MIDDLEWARE_OPTIONS,
32-
hotMiddlewareOptions: HOT_MIDDLEWARE_OPTIONS,
30+
browserSync: BROWSER_SYNC_OPTIONS,
31+
devMiddleware: DEV_MIDDLEWARE_OPTIONS,
32+
hotMiddleware: HOT_MIDDLEWARE_OPTIONS,
3333
callback() {
3434
console.log('Callback')
3535
}

package.json

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "browser-sync-dev-hot-webpack-plugin",
3-
"version": "0.0.3",
3+
"version": "0.1.0",
44
"description": "Combines BrowserSync, webpack-dev-middleware, and webpack-hot-middleware into one plugin",
55
"license": "MIT",
66
"author": "itgalaxy <[email protected]>",
@@ -29,15 +29,15 @@
2929
"url": "https://github.com/itgalaxy/browser-sync-dev-hot-webpack-plugin/issues"
3030
},
3131
"files": [
32-
"src",
33-
"!**/__tests__"
32+
"src"
3433
],
3534
"main": "src/BrowserSyncDevHotWebpackPlugin.js",
3635
"dependencies": {
3736
"deepmerge": "^1.3.2"
3837
},
3938
"devDependencies": {
4039
"ajv-cli": "^1.1.0",
40+
"browser-sync":"^2.18.8",
4141
"eslint": "^3.15.0",
4242
"eslint-plugin-ava": "^4.0.0",
4343
"eslint-plugin-import": "^2.2.0",
@@ -52,7 +52,10 @@
5252
"npm-run-all": "^4.0.0",
5353
"package-schema": "^1.0.0",
5454
"remark-cli": "^3.0.0",
55-
"remark-preset-lint-itgalaxy": "^6.0.0"
55+
"remark-preset-lint-itgalaxy": "^6.0.0",
56+
"webpack": "^1 || ^2 || ^2.0.0-beta || ^2.1.0-beta || ^2.2.0-rc.0",
57+
"webpack-dev-middleware": "^1.0.0",
58+
"webpack-hot-middleware": "^2.0.0"
5659
},
5760
"peerDependencies": {
5861
"browser-sync": "^2.0.0",

src/BrowserSyncDevHotWebpackPlugin.js

+98-56
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,152 @@
11
'use strict';
22

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');
65
const browserSync = require('browser-sync');
7-
/* eslint-enable node/no-missing-require */
86
const merge = require('deepmerge');
7+
const { desire } = require('./utils');
98

10-
class BrowserSyncDevHotWebpackPlugin {
9+
const webpackDevMiddleware = desire('webpack-dev-middleware');
10+
const webpackHotMiddleware = desire('webpack-hot-middleware');
11+
12+
class BrowserSyncDevHotWebpackPlugin extends EventEmitter {
1113
constructor(options) {
12-
this.watcher = null;
14+
super();
15+
1316
this.compiler = null;
17+
this.middleware = [];
18+
this.watcher = browserSync.create();
1419
this.options = merge.all([
1520
{
16-
browserSyncOptions: {},
21+
browserSync: {},
1722
callback() {}, // eslint-disable-line no-empty-function
18-
devMiddlewareOptions: {
23+
devMiddleware: {
1924
noInfo: true,
2025
stats: false
2126
},
22-
hotMiddlewareOptions: {}
27+
hotMiddleware: {}
2328
},
2429
options
2530
]);
2631
}
2732

33+
registerEvents() {
34+
this.on('webpack.compilation', () => this.watcher.notify('Rebuilding...'));
35+
this.once('webpack.done', this.start.bind(this));
36+
}
37+
2838
apply(compiler) {
2939
if (this.options.disable) {
3040
return;
3141
}
3242

43+
this.registerEvents();
3344
this.compiler = compiler;
3445

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'));
4248
}
4349

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([
5080
{
5181
proxy: {
52-
middleware: this.middleware(),
82+
middleware: this.middleware,
5383
proxyReq: [
5484
(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);
5787
}
5888

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);
6191
}
6292

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);
6595
}
6696

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);
6999
}
70100

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+
}
73108
}
74109
]
110+
},
111+
watchOptions: {
112+
ignoreInitial: true
75113
}
76114
},
77-
this.options.browserSyncOptions
115+
this.options.browserSync
78116
]);
117+
}
79118

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+
}
86123

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+
}
91127

92-
this.options.callback.bind(this);
93-
});
128+
this.config();
94129
}
95130

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();
103133

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');
106141

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+
});
108150
}
109151
}
110152

src/utils.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
module.exports.desire = (dependency, fallback) => {
4+
try {
5+
require.resolve(dependency);
6+
} catch (error) { // eslint-disable-line no-unused-vars
7+
return fallback;
8+
}
9+
10+
return require(dependency); // eslint-disable-line global-require, import/no-dynamic-require
11+
};

0 commit comments

Comments
 (0)