css-loader 1.0.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +72 -0
- package/README.md +442 -206
- package/dist/CssSyntaxError.js +38 -0
- package/dist/Warning.js +32 -0
- package/dist/cjs.js +5 -0
- package/dist/index.js +266 -0
- package/dist/options.json +93 -0
- package/dist/plugins/index.js +31 -0
- package/dist/plugins/postcss-icss-parser.js +101 -0
- package/dist/plugins/postcss-import-parser.js +110 -0
- package/dist/plugins/postcss-url-parser.js +155 -0
- package/dist/runtime/api.js +85 -0
- package/dist/runtime/url-escape.js +20 -0
- package/dist/utils.js +72 -0
- package/package.json +105 -36
- package/index.js +0 -5
- package/lib/compile-exports.js +0 -51
- package/lib/css-base.js +0 -76
- package/lib/getImportPrefix.js +0 -14
- package/lib/getLocalIdent.js +0 -23
- package/lib/loader.js +0 -144
- package/lib/localsLoader.js +0 -44
- package/lib/processCss.js +0 -244
- package/lib/url/escape.js +0 -16
- package/locals.js +0 -5
package/README.md
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
[![npm][npm]][npm-url]
|
|
2
|
-
[![node][node]][node-url]
|
|
3
|
-
[![deps][deps]][deps-url]
|
|
4
|
-
[![tests][tests]][tests-url]
|
|
5
|
-
[![coverage][cover]][cover-url]
|
|
6
|
-
[![chat][chat]][chat-url]
|
|
7
|
-
|
|
8
1
|
<div align="center">
|
|
9
2
|
<img width="180" height="180" vspace="20"
|
|
10
3
|
src="https://cdn.worldvectorlogo.com/logos/css-3.svg">
|
|
@@ -12,55 +5,72 @@
|
|
|
12
5
|
<img width="200" height="200"
|
|
13
6
|
src="https://webpack.js.org/assets/icon-square-big.svg">
|
|
14
7
|
</a>
|
|
15
|
-
<h1>CSS Loader</h1>
|
|
16
8
|
</div>
|
|
17
9
|
|
|
18
|
-
|
|
10
|
+
[![npm][npm]][npm-url]
|
|
11
|
+
[![node][node]][node-url]
|
|
12
|
+
[![deps][deps]][deps-url]
|
|
13
|
+
[![tests][tests]][tests-url]
|
|
14
|
+
[![coverage][cover]][cover-url]
|
|
15
|
+
[![chat][chat]][chat-url]
|
|
16
|
+
[![size][size]][size-url]
|
|
19
17
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
# css-loader
|
|
19
|
+
|
|
20
|
+
The `css-loader` interprets `@import` and `url()` like `import/require()` and will resolve them.
|
|
23
21
|
|
|
24
|
-
|
|
22
|
+
## Getting Started
|
|
25
23
|
|
|
26
|
-
|
|
27
|
-
and will resolve them.
|
|
24
|
+
To begin, you'll need to install `css-loader`:
|
|
28
25
|
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
```console
|
|
27
|
+
npm install --save-dev css-loader
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Then add the plugin to your `webpack` config. For example:
|
|
31
31
|
|
|
32
32
|
**file.js**
|
|
33
|
+
|
|
33
34
|
```js
|
|
34
35
|
import css from 'file.css';
|
|
35
36
|
```
|
|
36
37
|
|
|
37
38
|
**webpack.config.js**
|
|
39
|
+
|
|
38
40
|
```js
|
|
39
41
|
module.exports = {
|
|
40
42
|
module: {
|
|
41
43
|
rules: [
|
|
42
44
|
{
|
|
43
45
|
test: /\.css$/,
|
|
44
|
-
use: [
|
|
45
|
-
}
|
|
46
|
-
]
|
|
47
|
-
}
|
|
48
|
-
}
|
|
46
|
+
use: ['style-loader', 'css-loader'],
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
};
|
|
49
51
|
```
|
|
50
52
|
|
|
53
|
+
Good loaders for requiring your assets are the [file-loader](https://github.com/webpack/file-loader) and the [url-loader](https://github.com/webpack/url-loader) which you should specify in your config (see [below](https://github.com/webpack-contrib/css-loader#assets)).
|
|
54
|
+
|
|
55
|
+
And run `webpack` via your preferred method.
|
|
56
|
+
|
|
51
57
|
### `toString`
|
|
52
58
|
|
|
53
59
|
You can also use the css-loader results directly as a string, such as in Angular's component style.
|
|
54
60
|
|
|
55
61
|
**webpack.config.js**
|
|
62
|
+
|
|
56
63
|
```js
|
|
57
|
-
{
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
+
module.exports = {
|
|
65
|
+
module: {
|
|
66
|
+
rules: [
|
|
67
|
+
{
|
|
68
|
+
test: /\.css$/,
|
|
69
|
+
use: ['to-string-loader', 'css-loader'],
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
},
|
|
73
|
+
};
|
|
64
74
|
```
|
|
65
75
|
|
|
66
76
|
or
|
|
@@ -79,78 +89,271 @@ might want to check out the [extract-loader](https://github.com/peerigon/extract
|
|
|
79
89
|
It's useful when you, for instance, need to post process the CSS as a string.
|
|
80
90
|
|
|
81
91
|
**webpack.config.js**
|
|
92
|
+
|
|
82
93
|
```js
|
|
83
|
-
{
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
94
|
+
module.exports = {
|
|
95
|
+
module: {
|
|
96
|
+
rules: [
|
|
97
|
+
{
|
|
98
|
+
test: /\.css$/,
|
|
99
|
+
use: [
|
|
100
|
+
'handlebars-loader', // handlebars loader expects raw resource string
|
|
101
|
+
'extract-loader',
|
|
102
|
+
'css-loader',
|
|
103
|
+
],
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
},
|
|
107
|
+
};
|
|
91
108
|
```
|
|
92
109
|
|
|
93
|
-
|
|
110
|
+
## Options
|
|
94
111
|
|
|
95
|
-
|Name|Type|Default|Description|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
112
|
+
| Name | Type | Default | Description |
|
|
113
|
+
| :-----------------------------------------: | :-------------------: | :-------------: | :------------------------------------------ |
|
|
114
|
+
| **[`url`](#url)** | `{Boolean\|Function}` | `true` | Enable/Disable `url()` handling |
|
|
115
|
+
| **[`import`](#import)** | `{Boolean\/Function}` | `true` | Enable/Disable @import handling |
|
|
116
|
+
| **[`modules`](#modules)** | `{Boolean\|String}` | `false` | Enable/Disable CSS Modules and setup mode |
|
|
117
|
+
| **[`localIdentName`](#localidentname)** | `{String}` | `[hash:base64]` | Configure the generated ident |
|
|
118
|
+
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `false` | Enable/Disable Sourcemaps |
|
|
119
|
+
| **[`camelCase`](#camelcase)** | `{Boolean\|String}` | `false` | Export Classnames in CamelCase |
|
|
120
|
+
| **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Number of loaders applied before CSS loader |
|
|
121
|
+
| **[`exportOnlyLocals`](#exportonlylocals)** | `{Boolean}` | `false` | Export only locals |
|
|
104
122
|
|
|
105
123
|
### `url`
|
|
106
124
|
|
|
107
|
-
|
|
125
|
+
Type: `Boolean|Function`
|
|
126
|
+
Default: `true`
|
|
127
|
+
|
|
128
|
+
Control `url()` resolving. Absolute urls are not resolving.
|
|
108
129
|
|
|
109
|
-
|
|
130
|
+
Examples resolutions:
|
|
110
131
|
|
|
111
132
|
```
|
|
112
133
|
url(image.png) => require('./image.png')
|
|
134
|
+
url('image.png') => require('./image.png')
|
|
135
|
+
url(./image.png) => require('./image.png')
|
|
136
|
+
url('./image.png') => require('./image.png')
|
|
137
|
+
url('http://dontwritehorriblecode.com/2112.png') => require('http://dontwritehorriblecode.com/2112.png')
|
|
138
|
+
image-set(url('image2x.png') 1x, url('image1x.png') 2x) => require('./image1x.png') and require('./image2x.png')
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
To import assets from a `node_modules` path (include `resolve.modules`) and for `alias`, prefix it with a `~`:
|
|
142
|
+
|
|
143
|
+
```
|
|
113
144
|
url(~module/image.png) => require('module/image.png')
|
|
145
|
+
url('~module/image.png') => require('module/image.png')
|
|
146
|
+
url(~aliasDirectory/image.png) => require('otherDirectory/image.png')
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
#### `Boolean`
|
|
150
|
+
|
|
151
|
+
Enable/disable `url()` resolving.
|
|
152
|
+
|
|
153
|
+
**webpack.config.js**
|
|
154
|
+
|
|
155
|
+
```js
|
|
156
|
+
module.exports = {
|
|
157
|
+
module: {
|
|
158
|
+
rules: [
|
|
159
|
+
{
|
|
160
|
+
test: /\.css$/,
|
|
161
|
+
loader: 'css-loader',
|
|
162
|
+
options: {
|
|
163
|
+
url: true,
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
],
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
#### `Function`
|
|
172
|
+
|
|
173
|
+
Allow to filter `url()`. All filtered `url()` will not be resolved (left in the code as they were written).
|
|
174
|
+
|
|
175
|
+
**webpack.config.js**
|
|
176
|
+
|
|
177
|
+
```js
|
|
178
|
+
module.exports = {
|
|
179
|
+
module: {
|
|
180
|
+
rules: [
|
|
181
|
+
{
|
|
182
|
+
test: /\.css$/,
|
|
183
|
+
loader: 'css-loader',
|
|
184
|
+
options: {
|
|
185
|
+
url: (url, resourcePath) => {
|
|
186
|
+
// resourcePath - path to css file
|
|
187
|
+
|
|
188
|
+
// `url()` with `img.png` stay untouched
|
|
189
|
+
return url.includes('img.png');
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
],
|
|
194
|
+
},
|
|
195
|
+
};
|
|
114
196
|
```
|
|
115
197
|
|
|
116
198
|
### `import`
|
|
117
199
|
|
|
118
|
-
|
|
200
|
+
Type: `Boolean`
|
|
201
|
+
Default: `true`
|
|
119
202
|
|
|
120
|
-
|
|
121
|
-
|
|
203
|
+
Control `@import` resolving. Absolute urls in `@import` will be moved in runtime code.
|
|
204
|
+
|
|
205
|
+
Examples resolutions:
|
|
206
|
+
|
|
207
|
+
```
|
|
208
|
+
@import 'style.css' => require('./style.css')
|
|
209
|
+
@import url(style.css) => require('./style.css')
|
|
210
|
+
@import url('style.css') => require('./style.css')
|
|
211
|
+
@import './style.css' => require('./style.css')
|
|
212
|
+
@import url(./style.css) => require('./style.css')
|
|
213
|
+
@import url('./style.css') => require('./style.css')
|
|
214
|
+
@import url('http://dontwritehorriblecode.com/style.css') => @import url('http://dontwritehorriblecode.com/style.css') in runtime
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
To import styles from a `node_modules` path (include `resolve.modules`) and for `alias`, prefix it with a `~`:
|
|
218
|
+
|
|
219
|
+
```
|
|
220
|
+
@import url(~module/style.css) => require('module/style.css')
|
|
221
|
+
@import url('~module/style.css') => require('module/style.css')
|
|
222
|
+
@import url(~aliasDirectory/style.css) => require('otherDirectory/style.css')
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
#### `Boolean`
|
|
226
|
+
|
|
227
|
+
Enable/disable `@import` resolving.
|
|
228
|
+
|
|
229
|
+
**webpack.config.js**
|
|
230
|
+
|
|
231
|
+
```js
|
|
232
|
+
module.exports = {
|
|
233
|
+
module: {
|
|
234
|
+
rules: [
|
|
235
|
+
{
|
|
236
|
+
test: /\.css$/,
|
|
237
|
+
loader: 'css-loader',
|
|
238
|
+
options: {
|
|
239
|
+
import: true,
|
|
240
|
+
},
|
|
241
|
+
},
|
|
242
|
+
],
|
|
243
|
+
},
|
|
244
|
+
};
|
|
122
245
|
```
|
|
123
246
|
|
|
124
|
-
|
|
247
|
+
#### `Function`
|
|
248
|
+
|
|
249
|
+
Allow to filter `@import`. All filtered `@import` will not be resolved (left in the code as they were written).
|
|
250
|
+
|
|
251
|
+
**webpack.config.js**
|
|
252
|
+
|
|
253
|
+
```js
|
|
254
|
+
module.exports = {
|
|
255
|
+
module: {
|
|
256
|
+
rules: [
|
|
257
|
+
{
|
|
258
|
+
test: /\.css$/,
|
|
259
|
+
loader: 'css-loader',
|
|
260
|
+
options: {
|
|
261
|
+
import: (parsedImport, resourcePath) => {
|
|
262
|
+
// parsedImport.url - url of `@import`
|
|
263
|
+
// parsedImport.media - media query of `@import`
|
|
264
|
+
// resourcePath - path to css file
|
|
265
|
+
|
|
266
|
+
// `@import` with `style.css` stay untouched
|
|
267
|
+
return parsedImport.url.includes('style.css');
|
|
268
|
+
},
|
|
269
|
+
},
|
|
270
|
+
},
|
|
271
|
+
],
|
|
272
|
+
},
|
|
273
|
+
};
|
|
274
|
+
```
|
|
125
275
|
|
|
126
276
|
### [`modules`](https://github.com/css-modules/css-modules)
|
|
127
277
|
|
|
128
|
-
|
|
278
|
+
Type: `Boolean|String`
|
|
279
|
+
Default: `false`
|
|
280
|
+
|
|
281
|
+
The `modules` option enables/disables the **CSS Modules** spec and setup basic behaviour.
|
|
282
|
+
|
|
283
|
+
| Name | Type | Description |
|
|
284
|
+
| :------------: | :---------: | :------------------------------------------------------------------------------------------------------------------------------- |
|
|
285
|
+
| **`true`** | `{Boolean}` | Enables local scoped CSS by default (use **local** mode by default) |
|
|
286
|
+
| **`false`** | `{Boolean}` | Disable the **CSS Modules** spec, all **CSS Modules** features (like `@value`, `:local`, `:global` and `composes`) will not work |
|
|
287
|
+
| **`'local'`** | `{String}` | Enables local scoped CSS by default (same as `true` value) |
|
|
288
|
+
| **`'global'`** | `{String}` | Enables global scoped CSS by default |
|
|
129
289
|
|
|
130
|
-
|
|
290
|
+
Using `false` value increase performance because we avoid parsing **CSS Modules** features, it will be useful for developers who use vanilla css or use other technologies.
|
|
131
291
|
|
|
132
|
-
|
|
292
|
+
You can read about **modes** below.
|
|
133
293
|
|
|
134
|
-
|
|
294
|
+
**webpack.config.js**
|
|
295
|
+
|
|
296
|
+
```js
|
|
297
|
+
module.exports = {
|
|
298
|
+
module: {
|
|
299
|
+
rules: [
|
|
300
|
+
{
|
|
301
|
+
test: /\.css$/,
|
|
302
|
+
loader: 'css-loader',
|
|
303
|
+
options: {
|
|
304
|
+
modules: true,
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
],
|
|
308
|
+
},
|
|
309
|
+
};
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
##### `Scope`
|
|
313
|
+
|
|
314
|
+
Using `local` value requires you to specify `:global` classes.
|
|
315
|
+
Using `global` value requires you to specify `:local` classes.
|
|
316
|
+
|
|
317
|
+
You can find more information [here](https://github.com/css-modules/css-modules).
|
|
318
|
+
|
|
319
|
+
Styles can be locally scoped to avoid globally scoping styles.
|
|
135
320
|
|
|
136
321
|
The syntax `:local(.className)` can be used to declare `className` in the local scope. The local identifiers are exported by the module.
|
|
137
322
|
|
|
138
|
-
With `:local` (without brackets) local mode can be switched on for this selector.
|
|
323
|
+
With `:local` (without brackets) local mode can be switched on for this selector.
|
|
324
|
+
The `:global(.className)` nocation can be used to declare an explicit global selector.
|
|
325
|
+
With `:global` (without brackets) global mode can be switched on for this selector.
|
|
139
326
|
|
|
140
327
|
The loader replaces local selectors with unique identifiers. The chosen unique identifiers are exported by the module.
|
|
141
328
|
|
|
142
329
|
```css
|
|
143
|
-
:local(.className) {
|
|
144
|
-
:
|
|
145
|
-
|
|
146
|
-
:local .className
|
|
330
|
+
:local(.className) {
|
|
331
|
+
background: red;
|
|
332
|
+
}
|
|
333
|
+
:local .className {
|
|
334
|
+
color: green;
|
|
335
|
+
}
|
|
336
|
+
:local(.className .subClass) {
|
|
337
|
+
color: green;
|
|
338
|
+
}
|
|
339
|
+
:local .className .subClass :global(.global-class-name) {
|
|
340
|
+
color: blue;
|
|
341
|
+
}
|
|
147
342
|
```
|
|
148
343
|
|
|
149
344
|
```css
|
|
150
|
-
._23_aKvs-b8bW2Vg3fwHozO {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
._23_aKvs-b8bW2Vg3fwHozO
|
|
345
|
+
._23_aKvs-b8bW2Vg3fwHozO {
|
|
346
|
+
background: red;
|
|
347
|
+
}
|
|
348
|
+
._23_aKvs-b8bW2Vg3fwHozO {
|
|
349
|
+
color: green;
|
|
350
|
+
}
|
|
351
|
+
._23_aKvs-b8bW2Vg3fwHozO ._13LGdX8RMStbBE9w-t0gZ1 {
|
|
352
|
+
color: green;
|
|
353
|
+
}
|
|
354
|
+
._23_aKvs-b8bW2Vg3fwHozO ._13LGdX8RMStbBE9w-t0gZ1 .global-class-name {
|
|
355
|
+
color: blue;
|
|
356
|
+
}
|
|
154
357
|
```
|
|
155
358
|
|
|
156
359
|
> ℹ️ Identifiers are exported
|
|
@@ -158,22 +361,15 @@ The loader replaces local selectors with unique identifiers. The chosen unique i
|
|
|
158
361
|
```js
|
|
159
362
|
exports.locals = {
|
|
160
363
|
className: '_23_aKvs-b8bW2Vg3fwHozO',
|
|
161
|
-
subClass: '_13LGdX8RMStbBE9w-t0gZ1'
|
|
162
|
-
}
|
|
364
|
+
subClass: '_13LGdX8RMStbBE9w-t0gZ1',
|
|
365
|
+
};
|
|
163
366
|
```
|
|
164
367
|
|
|
165
368
|
CamelCase is recommended for local selectors. They are easier to use within the imported JS module.
|
|
166
369
|
|
|
167
|
-
`url()` URLs in block scoped (`:local .abc`) rules behave like requests in modules.
|
|
168
|
-
|
|
169
|
-
```
|
|
170
|
-
file.png => ./file.png
|
|
171
|
-
~module/file.png => module/file.png
|
|
172
|
-
```
|
|
173
|
-
|
|
174
370
|
You can use `:local(#someId)`, but this is not recommended. Use classes instead of ids.
|
|
175
371
|
|
|
176
|
-
|
|
372
|
+
##### `Composing`
|
|
177
373
|
|
|
178
374
|
When declaring a local classname you can compose a local class from another local classname.
|
|
179
375
|
|
|
@@ -194,11 +390,11 @@ This doesn't result in any change to the CSS itself but exports multiple classna
|
|
|
194
390
|
```js
|
|
195
391
|
exports.locals = {
|
|
196
392
|
className: '_23_aKvs-b8bW2Vg3fwHozO',
|
|
197
|
-
subClass: '_13LGdX8RMStbBE9w-t0gZ1 _23_aKvs-b8bW2Vg3fwHozO'
|
|
198
|
-
}
|
|
393
|
+
subClass: '_13LGdX8RMStbBE9w-t0gZ1 _23_aKvs-b8bW2Vg3fwHozO',
|
|
394
|
+
};
|
|
199
395
|
```
|
|
200
396
|
|
|
201
|
-
```
|
|
397
|
+
```css
|
|
202
398
|
._23_aKvs-b8bW2Vg3fwHozO {
|
|
203
399
|
background: red;
|
|
204
400
|
color: yellow;
|
|
@@ -209,7 +405,7 @@ exports.locals = {
|
|
|
209
405
|
}
|
|
210
406
|
```
|
|
211
407
|
|
|
212
|
-
|
|
408
|
+
##### `Importing`
|
|
213
409
|
|
|
214
410
|
To import a local classname from another module.
|
|
215
411
|
|
|
@@ -240,200 +436,237 @@ To import from multiple modules use multiple `composes:` rules.
|
|
|
240
436
|
|
|
241
437
|
### `localIdentName`
|
|
242
438
|
|
|
243
|
-
|
|
439
|
+
Type: `String`
|
|
440
|
+
Default: `[hash:base64]`
|
|
441
|
+
|
|
442
|
+
You can configure the generated ident with the `localIdentName` query parameter.
|
|
443
|
+
See [loader-utils's documentation](https://github.com/webpack/loader-utils#interpolatename) for more information on options.
|
|
444
|
+
|
|
445
|
+
**webpack.config.js**
|
|
244
446
|
|
|
245
|
-
**webpack.config.js**
|
|
246
447
|
```js
|
|
247
|
-
{
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
448
|
+
module.exports = {
|
|
449
|
+
module: {
|
|
450
|
+
rules: [
|
|
451
|
+
{
|
|
452
|
+
test: /\.css$/,
|
|
453
|
+
loader: 'css-loader',
|
|
454
|
+
options: {
|
|
455
|
+
modules: true,
|
|
456
|
+
localIdentName: '[path][name]__[local]--[hash:base64:5]',
|
|
457
|
+
},
|
|
458
|
+
},
|
|
459
|
+
],
|
|
460
|
+
},
|
|
461
|
+
};
|
|
259
462
|
```
|
|
260
463
|
|
|
261
|
-
You can also specify the absolute path to your custom `getLocalIdent` function to generate classname based on a different schema.
|
|
464
|
+
You can also specify the absolute path to your custom `getLocalIdent` function to generate classname based on a different schema.
|
|
262
465
|
|
|
263
466
|
**webpack.config.js**
|
|
467
|
+
|
|
264
468
|
```js
|
|
265
|
-
{
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
469
|
+
module.exports = {
|
|
470
|
+
module: {
|
|
471
|
+
rules: [
|
|
472
|
+
{
|
|
473
|
+
test: /\.css$/,
|
|
474
|
+
loader: 'css-loader',
|
|
475
|
+
options: {
|
|
476
|
+
modules: true,
|
|
477
|
+
context: path.resolve(__dirname, 'context'), // Allow to redefine basic loader context for `local-ident-name`
|
|
478
|
+
hashPrefix: 'hash', // Allow to add custom hash to generate more unique classes
|
|
479
|
+
localIdentName: '[path][name]__[local]--[hash:base64:5]',
|
|
480
|
+
getLocalIdent: (context, localIdentName, localName, options) => {
|
|
481
|
+
return 'whatever_random_class_name';
|
|
482
|
+
},
|
|
483
|
+
},
|
|
484
|
+
},
|
|
485
|
+
],
|
|
486
|
+
},
|
|
487
|
+
};
|
|
275
488
|
```
|
|
276
489
|
|
|
277
|
-
> ℹ️ 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.
|
|
278
|
-
|
|
279
490
|
### `sourceMap`
|
|
280
491
|
|
|
492
|
+
Type: `Boolean`
|
|
493
|
+
Default: `true`
|
|
494
|
+
|
|
281
495
|
To include source maps set the `sourceMap` option.
|
|
282
496
|
|
|
283
|
-
I.e. the
|
|
497
|
+
I.e. the `mini-css-extract-plugin` can handle them.
|
|
284
498
|
|
|
285
499
|
They are not enabled by default because they expose a runtime overhead and increase in bundle size (JS source maps do not). In addition to that relative paths are buggy and you need to use an absolute public path which includes the server URL.
|
|
286
500
|
|
|
287
501
|
**webpack.config.js**
|
|
502
|
+
|
|
288
503
|
```js
|
|
289
|
-
{
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
504
|
+
module.exports = {
|
|
505
|
+
module: {
|
|
506
|
+
rules: [
|
|
507
|
+
{
|
|
508
|
+
test: /\.css$/,
|
|
509
|
+
loader: 'css-loader',
|
|
510
|
+
options: {
|
|
511
|
+
sourceMap: true,
|
|
512
|
+
},
|
|
513
|
+
},
|
|
514
|
+
],
|
|
515
|
+
},
|
|
516
|
+
};
|
|
295
517
|
```
|
|
296
518
|
|
|
297
519
|
### `camelCase`
|
|
298
520
|
|
|
521
|
+
Type: `Boolean|String`
|
|
522
|
+
Default: `false`
|
|
523
|
+
|
|
299
524
|
By default, the exported JSON keys mirror the class names. If you want to camelize class names (useful in JS), pass the query parameter `camelCase` to css-loader.
|
|
300
525
|
|
|
301
|
-
|Name|Type|Description|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
526
|
+
| Name | Type | Description |
|
|
527
|
+
| :----------------: | :---------: | :----------------------------------------------------------------------------------------------------------------------- |
|
|
528
|
+
| **`false`** | `{Boolean}` | Class names will be camelized, the original class name will not to be removed from the locals |
|
|
529
|
+
| **`true`** | `{Boolean}` | Class names will be camelized |
|
|
530
|
+
| **`'dashes'`** | `{String}` | Only dashes in class names will be camelized |
|
|
531
|
+
| **`'only'`** | `{String}` | Introduced in `0.27.1`. Class names will be camelized, the original class name will be removed from the locals |
|
|
532
|
+
| **`'dashesOnly'`** | `{String}` | Introduced in `0.27.1`. Dashes in class names will be camelized, the original class name will be removed from the locals |
|
|
307
533
|
|
|
308
534
|
**file.css**
|
|
535
|
+
|
|
309
536
|
```css
|
|
310
|
-
.class-name {
|
|
537
|
+
.class-name {
|
|
538
|
+
}
|
|
311
539
|
```
|
|
312
540
|
|
|
313
541
|
**file.js**
|
|
542
|
+
|
|
314
543
|
```js
|
|
315
544
|
import { className } from 'file.css';
|
|
316
545
|
```
|
|
317
546
|
|
|
318
547
|
**webpack.config.js**
|
|
548
|
+
|
|
319
549
|
```js
|
|
320
|
-
{
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
550
|
+
module.exports = {
|
|
551
|
+
module: {
|
|
552
|
+
rules: [
|
|
553
|
+
{
|
|
554
|
+
test: /\.css$/,
|
|
555
|
+
loader: 'css-loader',
|
|
556
|
+
options: {
|
|
557
|
+
camelCase: true,
|
|
558
|
+
},
|
|
559
|
+
},
|
|
560
|
+
],
|
|
561
|
+
},
|
|
562
|
+
};
|
|
326
563
|
```
|
|
327
564
|
|
|
328
565
|
### `importLoaders`
|
|
329
566
|
|
|
330
|
-
|
|
567
|
+
Type: `Number`
|
|
568
|
+
Default: `0`
|
|
569
|
+
|
|
570
|
+
The option `importLoaders` allows you to configure how many loaders before `css-loader` should be applied to `@import`ed resources.
|
|
331
571
|
|
|
332
572
|
**webpack.config.js**
|
|
573
|
+
|
|
333
574
|
```js
|
|
334
|
-
{
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
575
|
+
module.exports = {
|
|
576
|
+
module: {
|
|
577
|
+
rules: [
|
|
578
|
+
{
|
|
579
|
+
test: /\.css$/,
|
|
580
|
+
use: [
|
|
581
|
+
'style-loader',
|
|
582
|
+
{
|
|
583
|
+
loader: 'css-loader',
|
|
584
|
+
options: {
|
|
585
|
+
importLoaders: 2, // 0 => no loaders (default); 1 => postcss-loader; 2 => postcss-loader, sass-loader
|
|
586
|
+
},
|
|
587
|
+
},
|
|
588
|
+
'postcss-loader',
|
|
589
|
+
'sass-loader',
|
|
590
|
+
],
|
|
591
|
+
},
|
|
592
|
+
],
|
|
593
|
+
},
|
|
594
|
+
};
|
|
348
595
|
```
|
|
349
596
|
|
|
350
597
|
This may change in the future when the module system (i. e. webpack) supports loader matching by origin.
|
|
351
598
|
|
|
352
|
-
|
|
599
|
+
### `exportOnlyLocals`
|
|
600
|
+
|
|
601
|
+
Type: `Boolean`
|
|
602
|
+
Default: `false`
|
|
603
|
+
|
|
604
|
+
Export only locals (**useful** when you use **css modules**).
|
|
605
|
+
For pre-rendering with `mini-css-extract-plugin` you should use this option instead of `style-loader!css-loader` **in the pre-rendering bundle**.
|
|
606
|
+
It doesn't embed CSS but only exports the identifier mappings.
|
|
607
|
+
|
|
608
|
+
**webpack.config.js**
|
|
609
|
+
|
|
610
|
+
```js
|
|
611
|
+
module.exports = {
|
|
612
|
+
module: {
|
|
613
|
+
rules: [
|
|
614
|
+
{
|
|
615
|
+
test: /\.css$/,
|
|
616
|
+
loader: 'css-loader',
|
|
617
|
+
options: {
|
|
618
|
+
exportOnlyLocals: true,
|
|
619
|
+
},
|
|
620
|
+
},
|
|
621
|
+
],
|
|
622
|
+
},
|
|
623
|
+
};
|
|
624
|
+
```
|
|
625
|
+
|
|
626
|
+
## Examples
|
|
353
627
|
|
|
354
628
|
### Assets
|
|
355
629
|
|
|
356
630
|
The following `webpack.config.js` can load CSS files, embed small PNG/JPG/GIF/SVG images as well as fonts as [Data URLs](https://tools.ietf.org/html/rfc2397) and copy larger files to the output directory.
|
|
357
631
|
|
|
358
632
|
**webpack.config.js**
|
|
633
|
+
|
|
359
634
|
```js
|
|
360
635
|
module.exports = {
|
|
361
636
|
module: {
|
|
362
637
|
rules: [
|
|
363
638
|
{
|
|
364
639
|
test: /\.css$/,
|
|
365
|
-
use: [
|
|
640
|
+
use: ['style-loader', 'css-loader'],
|
|
366
641
|
},
|
|
367
642
|
{
|
|
368
643
|
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
|
|
369
644
|
loader: 'url-loader',
|
|
370
645
|
options: {
|
|
371
|
-
limit: 10000
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
]
|
|
375
|
-
}
|
|
376
|
-
}
|
|
646
|
+
limit: 10000,
|
|
647
|
+
},
|
|
648
|
+
},
|
|
649
|
+
],
|
|
650
|
+
},
|
|
651
|
+
};
|
|
377
652
|
```
|
|
378
653
|
|
|
379
654
|
### Extract
|
|
380
655
|
|
|
381
656
|
For production builds it's recommended to extract the CSS from your bundle being able to use parallel loading of CSS/JS resources later on.
|
|
382
|
-
This can be achieved by using the [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin) to extract the CSS when running in production mode.
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
<td align="center">
|
|
390
|
-
<img width="150" height="150"
|
|
391
|
-
src="https://github.com/bebraw.png?v=3&s=150">
|
|
392
|
-
</br>
|
|
393
|
-
<a href="https://github.com/bebraw">Juho Vepsäläinen</a>
|
|
394
|
-
</td>
|
|
395
|
-
<td align="center">
|
|
396
|
-
<img width="150" height="150"
|
|
397
|
-
src="https://github.com/d3viant0ne.png?v=3&s=150">
|
|
398
|
-
</br>
|
|
399
|
-
<a href="https://github.com/d3viant0ne">Joshua Wiens</a>
|
|
400
|
-
</td>
|
|
401
|
-
<td align="center">
|
|
402
|
-
<img width="150" height="150"
|
|
403
|
-
src="https://github.com/SpaceK33z.png?v=3&s=150">
|
|
404
|
-
</br>
|
|
405
|
-
<a href="https://github.com/SpaceK33z">Kees Kluskens</a>
|
|
406
|
-
</td>
|
|
407
|
-
<td align="center">
|
|
408
|
-
<img width="150" height="150"
|
|
409
|
-
src="https://github.com/TheLarkInn.png?v=3&s=150">
|
|
410
|
-
</br>
|
|
411
|
-
<a href="https://github.com/TheLarkInn">Sean Larkin</a>
|
|
412
|
-
</td>
|
|
413
|
-
</tr>
|
|
414
|
-
<tr>
|
|
415
|
-
<td align="center">
|
|
416
|
-
<img width="150" height="150"
|
|
417
|
-
src="https://github.com/michael-ciniawsky.png?v=3&s=150">
|
|
418
|
-
</br>
|
|
419
|
-
<a href="https://github.com/michael-ciniawsky">Michael Ciniawsky</a>
|
|
420
|
-
</td>
|
|
421
|
-
<td align="center">
|
|
422
|
-
<img width="150" height="150"
|
|
423
|
-
src="https://github.com/evilebottnawi.png?v=3&s=150">
|
|
424
|
-
</br>
|
|
425
|
-
<a href="https://github.com/evilebottnawi">Evilebot Tnawi</a>
|
|
426
|
-
</td>
|
|
427
|
-
<td align="center">
|
|
428
|
-
<img width="150" height="150"
|
|
429
|
-
src="https://github.com/joscha.png?v=3&s=150">
|
|
430
|
-
</br>
|
|
431
|
-
<a href="https://github.com/joscha">Joscha Feth</a>
|
|
432
|
-
</td>
|
|
433
|
-
</tr>
|
|
434
|
-
<tbody>
|
|
435
|
-
</table>
|
|
657
|
+
- This can be achieved by using the [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin) to extract the CSS when running in production mode.
|
|
658
|
+
|
|
659
|
+
- As an alternative, if seeking better development performance and css outputs that mimic production. [extract-css-chunks-webpack-plugin](https://github.com/faceyspacey/extract-css-chunks-webpack-plugin) offers a hot module reload friendly, extended version of mini-css-extract-plugin. HMR real CSS files in dev, works like mini-css in non-dev
|
|
660
|
+
|
|
661
|
+
## Contributing
|
|
662
|
+
|
|
663
|
+
Please take a moment to read our contributing guidelines if you haven't yet done so.
|
|
436
664
|
|
|
665
|
+
[CONTRIBUTING](./.github/CONTRIBUTING.md)
|
|
666
|
+
|
|
667
|
+
## License
|
|
668
|
+
|
|
669
|
+
[MIT](./LICENSE)
|
|
437
670
|
|
|
438
671
|
[npm]: https://img.shields.io/npm/v/css-loader.svg
|
|
439
672
|
[npm-url]: https://npmjs.com/package/css-loader
|
|
@@ -444,11 +677,14 @@ This can be achieved by using the [mini-css-extract-plugin](https://github.com/w
|
|
|
444
677
|
[deps]: https://david-dm.org/webpack-contrib/css-loader.svg
|
|
445
678
|
[deps-url]: https://david-dm.org/webpack-contrib/css-loader
|
|
446
679
|
|
|
447
|
-
[tests]:
|
|
448
|
-
[tests-url]: https://
|
|
680
|
+
[tests]: https://img.shields.io/circleci/project/github/webpack-contrib/css-loader.svg
|
|
681
|
+
[tests-url]: https://circleci.com/gh/webpack-contrib/css-loader
|
|
449
682
|
|
|
450
683
|
[cover]: https://codecov.io/gh/webpack-contrib/css-loader/branch/master/graph/badge.svg
|
|
451
684
|
[cover-url]: https://codecov.io/gh/webpack-contrib/css-loader
|
|
452
685
|
|
|
453
686
|
[chat]: https://badges.gitter.im/webpack/webpack.svg
|
|
454
687
|
[chat-url]: https://gitter.im/webpack/webpack
|
|
688
|
+
|
|
689
|
+
[size]: https://packagephobia.now.sh/badge?p=css-loader
|
|
690
|
+
[size-url]: https://packagephobia.now.sh/result?p=css-loader
|