browser-sync-webpack-plugin
Advanced tools
Comparing version 2.0.1 to 2.2.0
@@ -1,56 +0,58 @@ | ||
const { extend, isFunction } = require('lodash') | ||
const { extend } = require('lodash') | ||
const browserSync = require('browser-sync') | ||
const isCssOnlyEmission = require('./isCssOnlyEmission') | ||
const getCssOnlyEmittedAssetsNames = require('./getCssOnlyEmittedAssetsNames') | ||
function BrowserSyncPlugin(browserSyncOptions, pluginOptions) { | ||
const defaultPluginOptions = { | ||
reload: true, | ||
name: 'bs-webpack-plugin', | ||
callback: undefined, | ||
injectCss: false | ||
} | ||
const defaultPluginOptions = { | ||
reload: true, | ||
name: 'bs-webpack-plugin', | ||
callback: undefined, | ||
injectCss: false | ||
} | ||
this.browserSyncOptions = extend({}, browserSyncOptions) | ||
this.options = extend({}, defaultPluginOptions, pluginOptions) | ||
class BrowserSyncPlugin { | ||
constructor (browserSyncOptions, pluginOptions) { | ||
this.browserSyncOptions = extend({}, browserSyncOptions) | ||
this.options = extend({}, defaultPluginOptions, pluginOptions) | ||
this.browserSync = browserSync.create(this.options.name) | ||
this.isWebpackWatching = false | ||
this.isBrowserSyncRunning = false | ||
} | ||
this.browserSync = browserSync.create(this.options.name) | ||
this.isWebpackWatching = false | ||
this.isBrowserSyncRunning = false | ||
} | ||
BrowserSyncPlugin.prototype.apply = function (compiler) { | ||
compiler.plugin('watch-run', (watching, callback) => { | ||
this.isWebpackWatching = true | ||
callback(null, null) | ||
}) | ||
compiler.plugin('compilation', () => { | ||
if (this.isBrowserSyncRunning && this.browserSyncOptions.notify) { | ||
this.browserSync.notify('Rebuilding...') | ||
apply (compiler) { | ||
const watchRunCallback = () => { | ||
this.isWebpackWatching = true | ||
} | ||
}) | ||
const compilationCallback = () => { | ||
if (this.isBrowserSyncRunning && this.browserSyncOptions.notify) { | ||
this.browserSync.notify('Rebuilding...') | ||
} | ||
} | ||
const doneCallback = stats => { | ||
if (!this.isWebpackWatching) { | ||
return | ||
} | ||
compiler.plugin('done', stats => { | ||
if (this.isWebpackWatching) { | ||
if (this.isBrowserSyncRunning) { | ||
if (this.options.reload) { | ||
if (this.options.injectCss && isCssOnlyEmission(stats)) { | ||
this.browserSync.reload('*.css') | ||
} else { | ||
this.browserSync.reload() | ||
} | ||
} | ||
} else { | ||
if (isFunction(this.options.callback)) { | ||
this.browserSync.init(this.browserSyncOptions, this.options.callback) | ||
} else { | ||
this.browserSync.init(this.browserSyncOptions) | ||
} | ||
if (!this.isBrowserSyncRunning) { | ||
this.browserSync.init(this.browserSyncOptions, this.options.callback) | ||
this.isBrowserSyncRunning = true | ||
} | ||
if (this.options.reload) { | ||
this.browserSync.reload(this.options.injectCss && getCssOnlyEmittedAssetsNames(stats)) | ||
} | ||
} | ||
}) | ||
if (typeof compiler.hooks !== 'undefined') { | ||
compiler.hooks.watchRun.tap(BrowserSyncPlugin.name, watchRunCallback) | ||
compiler.hooks.compilation.tap(BrowserSyncPlugin.name, compilationCallback) | ||
compiler.hooks.done.tap(BrowserSyncPlugin.name, doneCallback) | ||
} else { | ||
compiler.plugin('watch-run', watchRunCallback) | ||
compiler.plugin('compilation', compilationCallback) | ||
compiler.plugin('done', doneCallback) | ||
} | ||
} | ||
} | ||
module.exports = BrowserSyncPlugin |
{ | ||
"name": "browser-sync-webpack-plugin", | ||
"version": "2.0.1", | ||
"version": "2.2.0", | ||
"description": "BrowserSync and Webpack integration", | ||
@@ -17,6 +17,10 @@ "keywords": [ | ||
}, | ||
"devDependencies": {}, | ||
"devDependencies": { | ||
"husky": "^0.15.0-rc.9", | ||
"lint-staged": "^7.0.0", | ||
"standard": "^11.0.1" | ||
}, | ||
"peerDependencies": { | ||
"browser-sync": "^2", | ||
"webpack": "^1 || ^2 || ^3" | ||
"webpack": "^1 || ^2 || ^3 || ^4" | ||
}, | ||
@@ -30,3 +34,14 @@ "repository": { | ||
"license": "MIT", | ||
"private": false | ||
"private": false, | ||
"lint-staged": { | ||
"lib/**/*.js": [ | ||
"standard --fix", | ||
"git add" | ||
] | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# [BrowserSync](http://www.browsersync.io/) plugin for [Webpack](http://webpack.github.io/) | ||
# [BrowserSync](https://browsersync.io/) plugin for [Webpack](https://webpack.js.org/) | ||
@@ -10,5 +10,9 @@ Easily use BrowserSync in your Webpack project. | ||
``` | ||
or | ||
```bash | ||
$ yarn add --dev browser-sync-webpack-plugin | ||
``` | ||
With release of 2.0.0 the plugin is expected to be used in Node v4+ environment. | ||
Support for Node v3 and lower was dropped, but you can install and use the plugin version of 1.2.0 in older environments. | ||
Support for Node v3 and lower was dropped, but you can install and use the plugin version of 1.2.0 in older environments. | ||
@@ -31,3 +35,3 @@ ## Usage: | ||
```javascript | ||
const BrowserSyncPlugin = require('browser-sync-webpack-plugin'); | ||
const BrowserSyncPlugin = require('browser-sync-webpack-plugin') | ||
@@ -58,3 +62,3 @@ module.exports = { | ||
```javascript | ||
const BrowserSyncPlugin = require('browser-sync-webpack-plugin'); | ||
const BrowserSyncPlugin = require('browser-sync-webpack-plugin') | ||
@@ -86,8 +90,10 @@ module.exports = { | ||
Another plugin options supported are: | ||
--- | ||
* `name` - BrowserSync [instance name](http://www.browsersync.io/docs/api/#api-name) | ||
* `callback` - BrowserSync [instance init callback](http://www.browsersync.io/docs/api/#api-cb). | ||
* `injectCss` - allows BrowserSync to inject changes inplace instead of reloading the page when changed chunks are all CSS files | ||
Other supported plugin options are: | ||
* `name` - default: `bs-webpack-plugin`, BrowserSync [instance name](http://www.browsersync.io/docs/api/#api-name) | ||
* `callback` - default: `undefined`, BrowserSync [instance init callback](http://www.browsersync.io/docs/api/#api-cb). | ||
* `injectCss` - default: `false`, allows BrowserSync to inject changes inplace instead of reloading the page when changed chunks are all CSS files | ||
## Contributing: | ||
@@ -94,0 +100,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7650
102
3
61