Skip to content

Commit

Permalink
Add optional cssHref param to style-loader (#27)
Browse files Browse the repository at this point in the history
* Add optional cssHref param to style-loader

This uses a css href reload method rather than injecting an updated styl

This makes styl property deletions reflect in the UI as well

* Fix messaging

* Use t=
  • Loading branch information
nojvek authored Jan 23, 2018
1 parent c06abbb commit 404c5d3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
17 changes: 15 additions & 2 deletions hot/style-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand All @@ -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});
Expand Down
14 changes: 14 additions & 0 deletions hot/update-css-href.js
Original file line number Diff line number Diff line change
@@ -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`);
}
};
2 changes: 1 addition & 1 deletion hot/update-panel-elems.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 '.../<elemName>/index.js' or '.../<elemName>.js'`);
console.warn(`[HMR Panel] Exepect file path to be '.../<elemName>/index.<ext>' or '.../<elemName>.<ext>'`);
}

return numUpdated;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down

0 comments on commit 404c5d3

Please sign in to comment.