UNPKG

8.4 kBMarkdownView Raw
1# css loader for webpack
2
3## installation
4
5`npm install css-loader --save-dev`
6
7## Usage
8
9[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)
10
11``` javascript
12var css = require("css-loader!./file.css");
13// => returns css code from file.css, resolves imports and url(...)
14```
15
16`@import` and `url(...)` are interpreted like `require()` and will be resolved by the css-loader.
17Good loaders for requiring your assets are the [file-loader](https://github.com/webpack/file-loader)
18and the [url-loader](https://github.com/webpack/url-loader) which you should specify in your config (see below).
19
20To be compatible with existing css files (if not in CSS Module mode):
21* `url(image.png)` => `require("./image.png")`
22* `url(~module/image.png)` => `require("module/image.png")`
23
24### Example config
25
26This webpack config can load css files, embed small png images as Data Urls and jpg images as files.
27
28``` javascript
29module.exports = {
30 module: {
31 loaders: [
32 { test: /\.css$/, loader: "style-loader!css-loader" },
33 { test: /\.png$/, loader: "url-loader?limit=100000" },
34 { test: /\.jpg$/, loader: "file-loader" }
35 ]
36 }
37};
38```
39
40### 'Root-relative' urls
41
42For urls that start with a `/`, the default behavior is to not translate them:
43* `url(/image.png)` => `url(/image.png)`
44
45If a `root` query parameter is set, however, it will be prepended to the url
46and then translated:
47
48With a config like:
49
50``` javascript
51 loaders: [
52 { test: /\.css$/, loader: "style-loader!css-loader?root=." },
53 ...
54 ]
55```
56
57The result is:
58
59* `url(/image.png)` => `require("./image.png")`
60
61Using 'Root-relative' urls is not recommended. You should only use it for legacy CSS files.
62
63### Local scope
64
65By default CSS exports all class names into a global selector scope. Styles can be locally scoped to avoid globally scoping styles.
66
67The syntax `:local(.className)` can be used to declare `className` in the local scope. The local identifiers are exported by the module.
68
69With `:local` (without brackets) local mode can be switched on for this selector. `:global(.className)` can be used to declare an explicit global selector. With `:global` (without brackets) global mode can be switched on for this selector.
70
71The loader replaces local selectors with unique identifiers. The choosen unique identifiers are exported by the module.
72
73Example:
74
75``` css
76:local(.className) { background: red; }
77:local .className { color: green; }
78:local(.className .subClass) { color: green; }
79:local .className .subClass :global(.global-class-name) { color: blue; }
80```
81
82is transformed to
83
84``` css
85._23_aKvs-b8bW2Vg3fwHozO { background: red; }
86._23_aKvs-b8bW2Vg3fwHozO { color: green; }
87._23_aKvs-b8bW2Vg3fwHozO ._13LGdX8RMStbBE9w-t0gZ1 { color: green; }
88._23_aKvs-b8bW2Vg3fwHozO ._13LGdX8RMStbBE9w-t0gZ1 .global-class-name { color: blue; }
89```
90
91and the identifiers are exported:
92
93``` js
94exports.locals = {
95 className: "_23_aKvs-b8bW2Vg3fwHozO",
96 subClass: "_13LGdX8RMStbBE9w-t0gZ1"
97}
98```
99
100Camelcasing is recommended for local selectors. They are easier to use in the importing javascript module.
101
102`url(...)` URLs in block scoped (`:local .abc`) rules behave like requests in modules:
103 * `./file.png` instead of `file.png`
104 * `module/file.png` instead of `~module/file.png`
105
106
107You can use `:local(#someId)`, but this is not recommended. Use classes instead of ids.
108
109You can configure the generated ident with the `localIdentName` query parameter (default `[hash:base64]`). Example: `css-loader?localIdentName=[path][name]---[local]---[hash:base64:5]` for easier debugging.
110
111You can also specify the absolute path to your custom `getLocalIdent` function to generate classname based on a different schema. Note that this requires `webpack@2` since to be able to pass function in. For example:
112
113```js
114{
115 test: /\.css$/,
116 loaders: [
117 {
118 loader: 'css-loader',
119 query: {
120 modules: true,
121 importLoaders: 1,
122 getLocalIdent: function (loaderContext, localIdentName, localName, options) {
123 return 'whatever_random_class_name'
124 }
125 }
126 }
127 ]
128},
129```
130
131
132Note: For prerendering with extract-text-webpack-plugin you should use `css-loader/locals` instead of `style-loader!css-loader` **in the prerendering bundle**. It doesn't embed CSS but only exports the identifier mappings.
133
134### CSS Modules
135
136See [CSS Modules](https://github.com/css-modules/css-modules).
137
138The query parameter `modules` enables the **CSS Modules** spec. (`css-loader?modules`)
139
140This enables Local scoped CSS by default. (You can switch it off with `:global(...)` or `:global` for selectors and/or rules.)
141
142### Composing CSS classes
143
144When declaring a local class name you can compose a local class from another local class name.
145
146``` css
147:local(.className) {
148 background: red;
149 color: yellow;
150}
151
152:local(.subClass) {
153 composes: className;
154 background: blue;
155}
156```
157
158This doesn't result in any change to the CSS itself but exports multiple class names:
159
160``` js
161exports.locals = {
162 className: "_23_aKvs-b8bW2Vg3fwHozO",
163 subClass: "_13LGdX8RMStbBE9w-t0gZ1 _23_aKvs-b8bW2Vg3fwHozO"
164}
165```
166
167and CSS is transformed to:
168
169``` css
170._23_aKvs-b8bW2Vg3fwHozO {
171 background: red;
172 color: yellow;
173}
174
175._13LGdX8RMStbBE9w-t0gZ1 {
176 background: blue;
177}
178```
179
180### Importing local class names
181
182To import a local class name from another module:
183
184``` css
185:local(.continueButton) {
186 composes: button from "library/button.css";
187 background: red;
188}
189```
190
191``` css
192:local(.nameEdit) {
193 composes: edit highlight from "./edit.css";
194 background: red;
195}
196```
197
198To import from multiple modules use multiple `composes:` rules.
199
200``` css
201:local(.className) {
202 composes: edit hightlight from "./edit.css";
203 composes: button from "module/button.css";
204 composes: classFromThisModule;
205 background: red;
206}
207```
208
209### SourceMaps
210
211To include SourceMaps set the `sourceMap` query param.
212
213`require("css-loader?sourceMap!./file.css")`
214
215I. e. the extract-text-webpack-plugin can handle them.
216
217They are not enabled by default because they expose a runtime overhead and increase in bundle size (JS SourceMap do not). In addition to that relative paths are buggy and you need to use an absolute public path which include the server url.
218
219### importing and chained loaders
220
221The query parameter `importLoaders` allow to configure which loaders should be applied to `@import`ed resources.
222
223`importLoaders` (int): That many loaders after the css-loader are used to import resources.
224
225Examples:
226
227``` js
228require("style-loader!css-loader?importLoaders=1!postcss-loader!...")
229// => imported resources are handled this way:
230require("css-loader?importLoaders=1!postcss-loader!...")
231
232require("style-loader!css-loader!stylus-loader!...")
233// => imported resources are handled this way:
234require("css-loader!...")
235```
236
237This may change in the future, when the module system (i. e. webpack) supports loader matching by origin.
238
239### Minification
240
241By default the css-loader minimizes the css if specified by the module system.
242
243In some cases the minification is destructive to the css, so you can provide some options to it. cssnano is used for minification and you find a [list of options here](http://cssnano.co/options/). Just provide them as query parameter: i. e. `require("css-loader?-colormin")` to disable making color values as small as possible.
244
245You can also disable or enforce minification with the `minimize` query parameter.
246
247`require("css-loader?minimize!./file.css")` (enforced)
248
249`require("css-loader?-minimize!./file.css")` (disabled)
250
251### Disable behavior
252
253`css-loader?-url` disables `url(...)` handling.
254
255`css-loader?-import` disables `@import` handling.
256
257### Camel case
258
259By default, the exported JSON keys mirror the class names. If you want to camelize class names (useful in Javascript), pass the query parameter `camelCase` to the loader.
260
261Example:
262
263`css-loader?camelCase`
264
265Usage:
266```css
267/* file.css */
268
269.class-name { /* ... */ }
270```
271
272```js
273// javascript
274
275require('file.css').className
276```
277
278## License
279
280MIT (http://www.opensource.org/licenses/mit-license.php)