diff --git a/hot/style-loader.js b/hot/style-loader.js index e1edf691..333d93e9 100644 --- a/hot/style-loader.js +++ b/hot/style-loader.js @@ -11,9 +11,23 @@ module.exports.pitch = function(remainingReq) { } const moduleId = loaderUtils.stringifyRequest(this, `!!${remainingReq}`); + const options = loaderUtils.getOptions(this); const resourcePath = this.resourcePath; const elemName = helpers.getElemName(resourcePath); + let updateSnippet = ``; + if (typeof options.cssHref === `string`) { + updateSnippet = ` + const updateCssHref = require('panel/hot/update-css-href'); + updateCssHref('${options.cssHref}'); + `; + } else { + updateSnippet = ` + const updateStyle = require('panel/hot/update-style'); + updateStyle(newStyle.toString(), ${JSON.stringify(resourcePath)}); + `; + } + return ` module.hot.accept(${moduleId}, () => { const newStyle = module.exports = require(${moduleId}); @@ -25,8 +39,7 @@ module.exports.pitch = function(remainingReq) { } }); if (!updateCount) { - const updateStyle = require('panel/hot/update-style'); - updateStyle(newStyle.toString(), ${JSON.stringify(resourcePath)}); + ${updateSnippet.trim()} } }); module.exports = require(${moduleId}); diff --git a/hot/update-css-href.js b/hot/update-css-href.js new file mode 100644 index 00000000..d58e0a40 --- /dev/null +++ b/hot/update-css-href.js @@ -0,0 +1,14 @@ +/* eslint-env commonjs */ +module.exports = function updateCssHref(cssLinkHref) { + const cssLink = document.querySelector(`link[href][rel=stylesheet][href*='${cssLinkHref}']`); + if (cssLink) { + // Add hash param to css url to trigger style refresh + // We use #t=.. so if css hasn't changed then webpack-dev-server can reply with a 304 Not modified + // Which does a no-op for style calculation and paint + const cssUrl = cssLink.getAttribute(`href`).split(`#`)[0]; + cssLink.setAttribute(`href`, `${cssUrl}#t=${new Date().getTime()}`); + console.info(`[HMR Panel] Refreshed ${cssUrl}`); + } else { + console.warn(`[HMR Panel] Could not find stylesheet matching '${cssLinkHref}' in document`); + } +}; diff --git a/hot/update-panel-elems.js b/hot/update-panel-elems.js index 462d79a7..c71ea521 100644 --- a/hot/update-panel-elems.js +++ b/hot/update-panel-elems.js @@ -34,7 +34,7 @@ module.exports = function updatePanelElems(elemName, updateFn) { console.info(`[HMR Panel] Updated ${elems.length} ${elemName} elems`); } else if (!elems.length) { console.warn(`[HMR Panel] No ${elemName} elems found`); - console.warn(`[HMR Panel] Exepect file path to be '...//index.js' or '.../.js'`); + console.warn(`[HMR Panel] Exepect file path to be '...//index.' or '.../.'`); } return numUpdated; diff --git a/package-lock.json b/package-lock.json index 6fe97512..0ffbac3b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "panel", - "version": "0.13.0", + "version": "0.13.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 188d5ad0..73c01c04 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "panel", - "version": "0.13.1", + "version": "0.13.2", "description": "Web Components with Virtual DOM: lightweight composable web apps", "main": "build/index.js", "files": [