css-hot-loader
Advanced tools
Comparing version 1.0.4 to 1.1.0-beta
@@ -11,25 +11,45 @@ var getCurrentScriptUrl = function() { | ||
} | ||
return function() { | ||
return src.replace('.js', '.css'); | ||
return function(fileMap) { | ||
var splitResult = src.exec(/([^\\/])+\.js$/); | ||
var filename = splitResult && splitResult[1]; | ||
if (!filename) { | ||
return [src.replace('.js', '.css')]; | ||
} | ||
return fileMap.split(',').map(function(mapRule) { | ||
var reg = new RegExp(fileName + '\\.js$', 'g') | ||
return src.replace(reg, mapRule.replace(/{fileName}/g, filename) + '.css'); | ||
}); | ||
}; | ||
} | ||
var getScriptSrc = getCurrentScriptUrl(); | ||
var getScriptSrc = getCurrentScriptUrl(fileMap); | ||
function getLinkElement() { | ||
var element; | ||
var src = getScriptSrc(); | ||
function reloadStyle(src) { | ||
var elements = document.querySelectorAll('link'); | ||
var loaded = false; | ||
for (var i = 0, el = null; el = elements[i]; i++) { | ||
if (el.href.indexOf(src) > -1) { | ||
element = el; | ||
var getReloadUrl(el.href, src); | ||
if (url) { | ||
el.href = url + '?' + Date.now(); | ||
loaded = true; | ||
} | ||
} | ||
return element; | ||
return loaded; | ||
} | ||
function getReloadUrl(href, src) { | ||
var ret; | ||
src.some(function(url) { | ||
if (href.indexOf(src) > -1) { | ||
ret = url; | ||
} | ||
}); | ||
return ret; | ||
} | ||
function reloadAll() { | ||
var elements = document.querySelectorAll('link'); | ||
for (var i = 0, el = null; el = elements[i]; i++) { | ||
const src = el.href.split('?')[0]; | ||
var src = el.href.split('?')[0]; | ||
el.href = src + '?' + Date.now(); | ||
@@ -39,15 +59,17 @@ } | ||
module.exports = function() { | ||
if (typeof document === 'undefined') { | ||
return; | ||
} | ||
module.exports = function(options) { | ||
return function() { | ||
if (typeof document === 'undefined') { | ||
return; | ||
} | ||
var src = getScriptSrc(); | ||
var el = getLinkElement(); | ||
if (el) { | ||
el.href = src + '?' + Date.now(); | ||
console.log('[HMR] css reload %s', src); | ||
} else { | ||
reloadAll(); | ||
var src = getScriptSrc(options.fileMap); | ||
var reloaded = reloadStyle(src); | ||
if (reloaded) { | ||
console.log('[HMR] css reload %s', src); | ||
} else { | ||
console.log('[HMR] css reload all css'); | ||
reloadAll(); | ||
} | ||
} | ||
}; |
@@ -0,7 +1,17 @@ | ||
const loaderUtils = require('loader-utils'); | ||
const defaultOptions = { | ||
fileMap: '{fileName}', | ||
}; | ||
module.exports = function(content) { | ||
this.cacheable(); | ||
const options = Object.assign( | ||
{}, | ||
loaderUtils.getOptions(this), // it is safe to pass null to Object.assign() | ||
defaultOptions | ||
); | ||
return content + ` | ||
if(module.hot) { | ||
// ${Date.now()} | ||
const cssReload = require('${require.resolve('./hotModuleReplacement')}'); | ||
const cssReload = require('${require.resolve('./hotModuleReplacement')}')(options); | ||
module.hot.dispose(cssReload); | ||
@@ -8,0 +18,0 @@ module.hot.accept(undefined, cssReload); |
{ | ||
"name": "css-hot-loader", | ||
"version": "1.0.4", | ||
"version": "1.1.0-beta", | ||
"description": "css hot reload work with extract-text-webpack-plugin", | ||
@@ -16,3 +16,6 @@ "main": "index.js", | ||
"author": "shepherdwind", | ||
"license": "ISC" | ||
"license": "ISC", | ||
"dependencies": { | ||
"loader-utils": "^1.1.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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
6945
87
1
3
+ Addedloader-utils@^1.1.0
+ Addedbig.js@5.2.2(transitive)
+ Addedemojis-list@3.0.0(transitive)
+ Addedjson5@1.0.2(transitive)
+ Addedloader-utils@1.4.2(transitive)
+ Addedminimist@1.2.8(transitive)