Comments (15)
It can be closed, but a note in the README would be good that the styles should be applied in batches when used in production (for instance via fastdom).
from style-loader.
What about this? https://github.com/wilsonpage/fastdom
from style-loader.
Interesting module. How could the fastdom instance be shared between the loader and the app?
from style-loader.
With fastdom:
fastdom.write(function() {
require("./style.css");
});
Style added and removed only when component is used:
var style = require("./style.usable.css"); // and bind "style/usable!css" to ".usable.css"
constructor {
style.use();
}
destructor {
process.nextTick(function() {
style.unuse();
});
}
Nothing to do in this loader.
from style-loader.
Yep, you're right. This should be done on application level.
But this is a nice pattern. Imho it should be documented in the readme.
from style-loader.
I'm not sure if webpack should solve this either, but how about the following situations?
- A stylesheet is repeatedly
use()
d andunuse()
d, causing the stylesheet to be added and removed a lot. I believe this could be a common case if you're swapping out a top-level component for another one: all the common stylesheets will be unloaded and reloaded unnecessarily. - I'm very careful to call
use()
andunuse()
at the right time, but some dependency that uses webpack that I don't control doesn't and thrashes my layout.
It could be cool if use()
and unuse()
were async went into a queue with a pluggable batching/flushing strategy, so I could plug it into React's render loop or fastdom or something for free.
These may be the responsibility of the framework; not sure. But is the first problem even solvable with the current API?
from style-loader.
Assuming a module that uses the reference-counted API like this:
var style = require("./style.useable.css");
style.use();
style.unuse();
And the application uses the recommended configuration:
{
module: {
loaders: [
{ test: /\.css$/, exclude: /\.useable\.css$/, loader: "style!css" },
{ test: /\.useable\.css$/, loader: "style/useable!css" }
]
}
}
It's possible for the application to apply any additional strategy that does batching (etc.). Just prepend a loader that decorates the public API of the generated module:
{
module: {
loaders: [
{ test: /\.css$/, exclude: /\.useable\.css$/, loader: "style!css" },
{ test: /\.useable\.css$/, loader: "batching-style-loader!style/useable!css" }
]
}
}
// batching-style-loader.js
module.exports = function() {};
module.exports.pitch = function(remainingRequest) {
return "module.exports = require(" +
JSON.stringify(path.join(__dirname, "batching-style-decorator.js")) +
")(require(" + JSON.stringify(remainingRequest) + "));";
};
// batching-style-decorator.js
var queue = [];
module.exports = function decorate(style) {
function ref() {
// Apply now
style.ref();
queue.forEach(function(style) {
style.unref();
});
}
function unref() {
process.nextTick(function() {
// Unapply on the next ref... or whatever
queue.push(style);
});
}
return {
ref: ref,
use: ref,
unref: unref,
unuse: unref
};
};
from style-loader.
Use fastdom over process.nextTick to eliminate layout thrashing :)
from style-loader.
Have you looked into using the disabled
attribute for the module's associated style
tag, instead of removing the element? Perhaps that can avoid the issue of "all the common stylesheets will be unloaded and reloaded unnecessarily"
from style-loader.
I don't know such a disabled
attribute
from style-loader.
Link: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/style#attr-disabled
(Toggling the disabled
attribute on link
elements is how we did theme-switching in TweetDeck.)
from style-loader.
I like that suggestion. Seems the disabled
attribute is only accessible in js via HTMLStyleElement
.
Example: http://jsbin.com/reneca/2/edit
Explicitly adding it as an attribute in the tag won't work; I think is non-standard:
<style type="text/css" disabled>
body {
color:red;
}
</style>
from style-loader.
Interesting... according to this answer <style>
-tags don't have a disabled attribute, but the corresponding HTMLStyleElement
has a disabled
-property.
from style-loader.
yeah, we'd only need to use the DOM API
from style-loader.
@jhnns @necolas @dashed Can this one be closed? Is disable
still a considerable feature?
from style-loader.
Related Issues (20)
- Style-loader duplicates insert function HOT 3
- HMR does not work with webpack 5 HOT 3
- Changes to `insert` module file do not trigger cache invalidation HOT 4
- i can't download the module HOT 1
- HMR does not work HOT 1
- What style-loaders expects as input? HOT 5
- Is there a injectType=lazyStyleTag .use() & .unuse() equivalent when using css-loader + mini-css-extract-plugin HOT 11
- Add support for using lazyStyleTag use() from a JavaScript module (injecting into multiple shadow DOMs) HOT 21
- why style-loader is pitch loader? HOT 2
- Support asset-modules instead of file-loader HOT 3
- Webpack "lazyStyleTag" configuration won't copy fonts after upgrade to Angular 13 HOT 18
- Noop in environment without DOM api HOT 1
- Can not export selectors from `@import url(...)` HOT 2
- Get CSS properties / variables from JavaScript HOT 3
- Vulnerable dependencies (indirect security issues) HOT 1
- webpack 5 is returning a object instead URL HOT 2
- Promotion of unsafe code HOT 1
- Excessive whitespace and line breaks in code generated by style-loader HOT 1
- loaderContext.utils is "undefined", Causes a report that the "contextify" attribute cannot be read from "undefined". HOT 10
- CSS Module is not working. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from style-loader.