可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have two questions.
1) CSS Loader and Style Loader are two webpack loaders. I couldnt grasp the difference between the both. Why do I have to use two loaders when they both do the same job?
2) What is this .useable.less and .useable.css mentioned in the above Readme.md files?
回答1:
The CSS loader takes a CSS file and returns the CSS with imports
and url(...)
resolved via webpack's require
functionality:
var css = require("css!./file.css"); // => returns css code from file.css, resolves imports and url(...)
It doesn't actually do anything with the returned CSS.
The style loader takes CSS and actually inserts it into the page so that the styles are active on the page.
They perform different operations, but it's often useful to chain them together, like Unix pipes. For example, if you were using the Less CSS preprocessor, you could use
require("style!css!less!./file.less")
to
- Turn
file.less
into plain CSS with the Less loader - Resolve all the
imports
and url(...)
s in the CSS with the CSS loader - Insert those styles into the page with the style loader
回答2:
css-loader
reads in a css file as a string. You could replace it with raw-loader
and get the same effect in a lot of situations. Since it just reads the file contents and nothing else, it's basically useless unless you chain it with another loader.
style-loader
takes those styles and creates a
tag in the page's
element containing those styles.
If you look at the javascript inside bundle.js
after using style-loader
you'll see a comment in the generated code that says
// style-loader: Adds some css to the DOM by adding a tag
For example,
That example comes from this tutorial. If you remove the style-loader
from the pipeline by changing the line
require("!style-loader!css-loader!./style.css");
to
require("css-loader!./style.css");
you will see that the
goes away.
回答3:
To answer the second question "What is this .useable.less and .useable.css mentioned in the above Readme.md files?", by default when a style is require'd
, the style-loader module automatically injects a
tag into the DOM, and that tag remains in the DOM until the browser window is closed or reloaded. The style-loader module also offers a so-called "reference-counted API" that allows the developer to add styles and remove them later when they're no longer needed. The API works like this:
const style = require('style/loader!css!./style.css') // The "reference counter" for this style starts at 0 // The style has not yet been injected into the DOM style.use() // increments counter to 1, injects a
By convention, style sheets loaded using this API have an extension ".usable.css" rather than simply ".css" as above.