webpack-livereload-plugin
Advanced tools
Comparing version 3.0.1 to 3.0.2
@@ -5,2 +5,4 @@ # Changelog | ||
### [3.0.2](https://github.com/statianzo/webpack-livereload-plugin/compare/v3.0.1...v3.0.2) (2021-08-04) | ||
### [3.0.1](https://github.com/statianzo/webpack-livereload-plugin/compare/v3.0.0...v3.0.1) (2021-03-09) | ||
@@ -7,0 +9,0 @@ |
61
index.js
@@ -28,2 +28,3 @@ /* jshint node:true */ | ||
useSourceHash: false, | ||
useSourceSize: false, | ||
appendScriptTag: false, | ||
@@ -39,2 +40,3 @@ delay: 0, | ||
this.sourceHashs = {}; | ||
this.sourceSizes = {}; | ||
this.webpack = null; | ||
@@ -52,3 +54,4 @@ this.infrastructureLogger = null; | ||
compiler.hooks.watchRun.tapAsync(PLUGIN_NAME, this._start.bind(this)); | ||
compiler.hooks.afterEmit.tap(PLUGIN_NAME, this._afterEmit.bind(this)) | ||
compiler.hooks.afterEmit.tap(PLUGIN_NAME, this._afterEmit.bind(this)); | ||
compiler.hooks.emit.tap(PLUGIN_NAME, this._emit.bind(this)); | ||
compiler.hooks.failed.tap(PLUGIN_NAME, this._failed.bind(this)); | ||
@@ -151,2 +154,3 @@ } | ||
.filter(this._fileIgnoredOrNotEmitted.bind(this)) | ||
.filter(this._fileSizeDoesntMatch.bind(this)) | ||
.filter(this._fileHashDoesntMatch.bind(this)) | ||
@@ -171,3 +175,11 @@ .map((data) => data[0]) | ||
* @private | ||
* @param compilation | ||
*/ | ||
_emit(compilation) { | ||
Object.entries(compilation.assets).forEach(this._calculateSourceHash.bind(this)); | ||
} | ||
/** | ||
* @private | ||
*/ | ||
_failed() { | ||
@@ -177,2 +189,3 @@ this.lastHash = null; | ||
this.sourceHashs = {}; | ||
this.sourceSizes = {}; | ||
} | ||
@@ -235,2 +248,21 @@ | ||
/** | ||
* Check compiled source size | ||
* | ||
* @param data | ||
* @returns {boolean} | ||
* @private | ||
*/ | ||
_fileSizeDoesntMatch(data) { | ||
if (!this.options.useSourceSize) | ||
return true; | ||
if (this.sourceSizes[data[0]] === data[1].size()) { | ||
return false; | ||
} | ||
this.sourceSizes[data[0]] = data[1].size(); | ||
return true; | ||
} | ||
/** | ||
* Check compiled source hash | ||
@@ -246,9 +278,11 @@ * | ||
const sourceHash = LiveReloadPlugin.generateHashCode(data[1].source()); | ||
if (this.sourceHashs[data[0]] === sourceHash) { | ||
if ( | ||
this.sourceHashs[data[0]] !== undefined | ||
&& this.sourceHashs[data[0]].hash === this.sourceHashs[data[0]].calculated | ||
) { | ||
return false; | ||
} | ||
this.sourceHashs[data[0]] = sourceHash; | ||
// Update source hash | ||
this.sourceHashs[data[0]].hash = this.sourceHashs[data[0]].calculated; | ||
return true; | ||
@@ -258,4 +292,21 @@ } | ||
/** | ||
* Calculate compiled source hash | ||
* | ||
* @param data | ||
* @returns {void} | ||
* @private | ||
*/ | ||
_calculateSourceHash(data) { | ||
if (!this.options.useSourceHash) return; | ||
// Calculate source hash | ||
this.sourceHashs[data[0]] = { | ||
hash: this.sourceHashs[data[0]] ? this.sourceHashs[data[0]].hash : null, | ||
calculated: LiveReloadPlugin.generateHashCode(data[1].source()) | ||
}; | ||
} | ||
/** | ||
* @private | ||
*/ | ||
get logger() { | ||
@@ -262,0 +313,0 @@ if (this.infrastructureLogger) { |
{ | ||
"name": "webpack-livereload-plugin", | ||
"version": "3.0.1", | ||
"version": "3.0.2", | ||
"description": "Livereload for webpack", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -49,2 +49,3 @@ # webpack-livereload-plugin | ||
- `useSourceHash` - (Default: `false`) create hash for each file source and only notify livereload if hash has changed | ||
- `useSourceSize` - (Default: `false`) check size for each file source and only notify livereload if size has changed (Faster than `useSourceHash` but it has a downside. If file size hasn't changed no reload is triggered. For example if color has changed from `#000000` to `#ffffff` no reload will be triggered!) | ||
@@ -51,0 +52,0 @@ ## Why? |
49
test.js
@@ -13,2 +13,3 @@ const test = require('tape'); | ||
t.equal(plugin.options.useSourceHash, false); | ||
t.equal(plugin.options.useSourceSize, false); | ||
t.equal(plugin.options.appendScriptTag, false); | ||
@@ -182,2 +183,7 @@ t.equal(plugin.options.delay, 0); | ||
assets: { | ||
'c.js': { | ||
emitted: true, | ||
size: () => 1, | ||
source: () => "asdf", | ||
}, | ||
'b.js': { | ||
@@ -197,14 +203,49 @@ emitted: true, | ||
plugin.sourceHashs = { | ||
'b.js': 'Wrong hash', | ||
'a.js': hashCode('asdf'), | ||
'b.js': {hash: 'Wrong hash'}, | ||
'a.js': {hash: hashCode('asdf')}, | ||
}; | ||
plugin.server = { | ||
notifyClients: function(files) { | ||
t.deepEqual(files.sort(), ['b.js']); | ||
t.deepEqual(files.sort(), ['b.js', 'c.js']); | ||
t.end(); | ||
} | ||
}; | ||
plugin._emit(compilation); | ||
plugin._afterEmit(compilation); | ||
}); | ||
test('filters out resized files', function(t) { | ||
const plugin = new LiveReloadPlugin({ | ||
useSourceSize: true, | ||
}); | ||
const compilation = { | ||
assets: { | ||
'c.js': { | ||
emitted: true, | ||
size: () => 10, | ||
}, | ||
'b.js': { | ||
emitted: true, | ||
size: () => 10, | ||
}, | ||
'a.js': { | ||
emitted: true, | ||
size: () => 20, | ||
}, | ||
}, | ||
children: [] | ||
}; | ||
plugin.sourceSizes = { | ||
'b.js': 20, | ||
'a.js': 20, | ||
}; | ||
plugin.server = { | ||
notifyClients: function(files) { | ||
t.deepEqual(files.sort(), ['b.js', 'c.js']); | ||
t.end(); | ||
} | ||
}; | ||
plugin._afterEmit(compilation); | ||
}); | ||
test('children trigger notification', function(t) { | ||
@@ -281,2 +322,2 @@ const plugin = new LiveReloadPlugin(); | ||
t.end(); | ||
}); | ||
}); |
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
22338
619
80