Skip to content

Commit a78e007

Browse files
committed
Merge pull request webpack-contrib#78 from webpack/postcss
Use PostCSS
2 parents 51e11f3 + 633cdec commit a78e007

17 files changed

+462
-713
lines changed

README.md

+25-18
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var css = require("css!./file.css");
1717
Good loaders for requiring your assets are the [file-loader](https://github.com/webpack/file-loader)
1818
and the [url-loader](https://github.com/webpack/url-loader) which you should specify in your config (see below).
1919

20-
To be compatible with existing css files:
20+
To be compatible with existing css files (if not in CSS Module mode):
2121
* `url(image.png)` => `require("./image.png")`
2222
* `url(~module/image.png)` => `require("module/image.png")`
2323

@@ -58,6 +58,8 @@ The result is:
5858

5959
* `url(/image.png)` => `require("./image.png")`
6060

61+
Using 'Root-relative' urls is not recommended. You should only use it for legacy CSS files.
62+
6163
### Local scope
6264

6365
By default CSS exports all class names into a global selector scope. This is a feature which offer a local selector scope.
@@ -97,28 +99,30 @@ exports.locals = {
9799

98100
Camelcasing is recommended for local selectors. They are easier to use in the importing javascript module.
99101

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+
100107
You can use `:local(#someId)`, but this is not recommended. Use classes instead of ids.
101108

102109
You 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.
103110

104-
Note: 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.
111+
Note: 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.
105112

106113
### Module mode
107114

108115
(experimental)
109116

110-
The query parameter `module` enables **CSS Module** mode. (`css-loader?module`)
117+
See [CSS Modules](https://github.com/css-modules/css-modules).
111118

112-
* Local scoped by default.
113-
* `url(...)` URLs behave like requests in modules:
114-
* `./file.png` instead of `file.png`
115-
* `module/file.png` instead of `~module/file.png`
119+
The query parameter `module` enables **CSS Module** mode. (`css-loader?module`)
116120

117-
Thanks to [@markdalgleish](https://github.com/markdalgleish) for prior work on this topic.
121+
This enables Local scoped CSS by default. (You can leave it with `:global(...)` or `:global` for selectors and/or rules.)
118122

119-
### Inheriting
123+
### Composing CSS classes
120124

121-
When declaring a local class name you can inherit from another local class name.
125+
When declaring a local class name you can compose a local class from another local class name.
122126

123127
``` css
124128
:local(.className) {
@@ -127,7 +131,7 @@ When declaring a local class name you can inherit from another local class name.
127131
}
128132

129133
:local(.subClass) {
130-
extends: className;
134+
composes: className;
131135
background: blue;
132136
}
133137
```
@@ -160,26 +164,25 @@ To import a local class name from another module:
160164

161165
``` css
162166
:local(.continueButton) {
163-
extends: button from "library/button.css";
167+
composes: button from "library/button.css";
164168
background: red;
165169
}
166170
```
167171

168172
``` css
169173
:local(.nameEdit) {
170-
extends: edit highlight from "./edit.css";
174+
composes: edit highlight from "./edit.css";
171175
background: red;
172176
}
173177
```
174178

175-
To import from multiple modules use multiple `extends:` rules. You can also use `url(...)` to specify the module (it behave a bit different).
179+
To import from multiple modules use multiple `composes:` rules.
176180

177181
``` css
178182
:local(.className) {
179-
extends: edit hightlight from "./edit.css";
180-
extends: button from url("button.css");
181-
/* equal to 'extends: button from "./button.css";' */
182-
extends: classFromThisModule;
183+
composes: edit hightlight from "./edit.css";
184+
composes: button from "module/button.css";
185+
composes: classFromThisModule;
183186
background: red;
184187
}
185188
```
@@ -192,6 +195,8 @@ To include SourceMaps set the `sourceMap` query param.
192195

193196
I. e. the extract-text-webpack-plugin can handle them.
194197

198+
They 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.
199+
195200
### importing and chained loaders
196201

197202
The query parameter `importLoaders` allow to configure which loaders should be applied to `@import`ed resources.
@@ -210,6 +215,8 @@ require("style-loader!css-loader!stylus-loader!...")
210215
require("css-loader!...")
211216
```
212217

218+
This may change in the future, when the module system (i. e. webpack) supports loader matching by origin.
219+
213220
### Minification
214221

215222
By default the css-loader minimizes the css if specified by the module system.

lib/ReplaceMany.js

-29
This file was deleted.

lib/generateLocals.js

-38
This file was deleted.

lib/getImportPrefix.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
module.exports = function getImportPrefix(loaderContext, query) {
6+
if(query.importLoaders === false)
7+
return "";
8+
var importLoaders = parseInt(query.importLoaders, 10) || 0;
9+
var loadersRequest = loaderContext.loaders.slice(
10+
loaderContext.loaderIndex,
11+
loaderContext.loaderIndex + 1 + importLoaders
12+
).map(function(x) { return x.request; }).join("!");
13+
return "-!" + loadersRequest + "!";
14+
};

0 commit comments

Comments
 (0)