Skip to content

Commit e9600d7

Browse files
refactor: drop css modules
BREAKING CHANGE: use `postcss-modules` plugin with `postcss-loader`
1 parent 43179a8 commit e9600d7

29 files changed

+9
-1031
lines changed

README.md

+1-156
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,8 @@ It's useful when you, for instance, need to post process the CSS as a string.
9696
|:--:|:--:|:-----:|:----------|
9797
|**[`url`](#url)**|`{Boolean}`|`true`| Enable/Disable `url()` handling|
9898
|**[`import`](#import)** |`{Boolean}`|`true`| Enable/Disable @import handling|
99-
|**[`modules`](#modules)**|`{Boolean}`|`false`|Enable/Disable CSS Modules|
100-
|**[`localIdentName`](#localidentname)**|`{String}`|`[hash:base64]`|Configure the generated ident|
10199
|**[`sourceMap`](#sourcemap)**|`{Boolean}`|`false`|Enable/Disable Sourcemaps|
102-
|**[`camelCase`](#camelcase)**|`{Boolean\|String}`|`false`|Export Classnames in CamelCase|
100+
|**[`camelCase`](#camelcase)**|`{Boolean\|String}`|`false`|Export in CamelCase|
103101
|**[`importLoaders`](#importloaders)**|`{Number}`|`0`|Number of loaders applied before CSS loader|
104102

105103
### `url`
@@ -123,159 +121,6 @@ To disable `@import` resolving by `css-loader` set the option to `false`
123121

124122
> _⚠️ Use with caution, since this disables resolving for **all** `@import`s, including css modules `composes: xxx from 'path/to/file.css'` feature._
125123
126-
### [`modules`](https://github.com/css-modules/css-modules)
127-
128-
The query parameter `modules` enables the **CSS Modules** spec.
129-
130-
This enables local scoped CSS by default. (You can switch it off with `:global(...)` or `:global` for selectors and/or rules.).
131-
132-
#### `Scope`
133-
134-
By default CSS exports all classnames into a global selector scope. Styles can be locally scoped to avoid globally scoping styles.
135-
136-
The syntax `:local(.className)` can be used to declare `className` in the local scope. The local identifiers are exported by the module.
137-
138-
With `: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.
139-
140-
The loader replaces local selectors with unique identifiers. The choosen unique identifiers are exported by the module.
141-
142-
```css
143-
:local(.className) { background: red; }
144-
:local .className { color: green; }
145-
:local(.className .subClass) { color: green; }
146-
:local .className .subClass :global(.global-class-name) { color: blue; }
147-
```
148-
149-
```css
150-
._23_aKvs-b8bW2Vg3fwHozO { background: red; }
151-
._23_aKvs-b8bW2Vg3fwHozO { color: green; }
152-
._23_aKvs-b8bW2Vg3fwHozO ._13LGdX8RMStbBE9w-t0gZ1 { color: green; }
153-
._23_aKvs-b8bW2Vg3fwHozO ._13LGdX8RMStbBE9w-t0gZ1 .global-class-name { color: blue; }
154-
```
155-
156-
> ℹ️ Identifiers are exported
157-
158-
```js
159-
exports.locals = {
160-
className: '_23_aKvs-b8bW2Vg3fwHozO',
161-
subClass: '_13LGdX8RMStbBE9w-t0gZ1'
162-
}
163-
```
164-
165-
CamelCase is recommended for local selectors. They are easier to use in the within the imported JS module.
166-
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-
You can use `:local(#someId)`, but this is not recommended. Use classes instead of ids.
175-
176-
#### `Composing`
177-
178-
When declaring a local classname you can compose a local class from another local classname.
179-
180-
```css
181-
:local(.className) {
182-
background: red;
183-
color: yellow;
184-
}
185-
186-
:local(.subClass) {
187-
composes: className;
188-
background: blue;
189-
}
190-
```
191-
192-
This doesn't result in any change to the CSS itself but exports multiple classnames.
193-
194-
```js
195-
exports.locals = {
196-
className: '_23_aKvs-b8bW2Vg3fwHozO',
197-
subClass: '_13LGdX8RMStbBE9w-t0gZ1 _23_aKvs-b8bW2Vg3fwHozO'
198-
}
199-
```
200-
201-
``` css
202-
._23_aKvs-b8bW2Vg3fwHozO {
203-
background: red;
204-
color: yellow;
205-
}
206-
207-
._13LGdX8RMStbBE9w-t0gZ1 {
208-
background: blue;
209-
}
210-
```
211-
212-
#### `Importing`
213-
214-
To import a local classname from another module.
215-
216-
```css
217-
:local(.continueButton) {
218-
composes: button from 'library/button.css';
219-
background: red;
220-
}
221-
```
222-
223-
```css
224-
:local(.nameEdit) {
225-
composes: edit highlight from './edit.css';
226-
background: red;
227-
}
228-
```
229-
230-
To import from multiple modules use multiple `composes:` rules.
231-
232-
```css
233-
:local(.className) {
234-
composes: edit hightlight from './edit.css';
235-
composes: button from 'module/button.css';
236-
composes: classFromThisModule;
237-
background: red;
238-
}
239-
```
240-
241-
### `localIdentName`
242-
243-
You can configure the generated ident with the `localIdentName` query parameter. See [loader-utils's documentation](https://github.com/webpack/loader-utils#interpolatename) for more information on options.
244-
245-
**webpack.config.js**
246-
```js
247-
{
248-
test: /\.css$/,
249-
use: [
250-
{
251-
loader: 'css-loader',
252-
options: {
253-
modules: true,
254-
localIdentName: '[path][name]__[local]--[hash:base64:5]'
255-
}
256-
}
257-
]
258-
}
259-
```
260-
261-
You can also specify the absolute path to your custom `getLocalIdent` function to generate classname based on a different schema. This requires `webpack >= 2.2.1` (it supports functions in the `options` object).
262-
263-
**webpack.config.js**
264-
```js
265-
{
266-
loader: 'css-loader',
267-
options: {
268-
modules: true,
269-
localIdentName: '[path][name]__[local]--[hash:base64:5]',
270-
getLocalIdent: (context, localIdentName, localName, options) => {
271-
return 'whatever_random_class_name'
272-
}
273-
}
274-
}
275-
```
276-
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-
279124
### `sourceMap`
280125

281126
To include source maps set the `sourceMap` option.

lib/getLocalIdent.js

-23
This file was deleted.

lib/loader.js

-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ var compileExports = require("./compile-exports");
1111
module.exports = function(content, map) {
1212
var callback = this.async();
1313
var query = loaderUtils.getOptions(this) || {};
14-
var moduleMode = query.modules;
1514
var camelCaseKeys = query.camelCase;
1615
var sourceMap = query.sourceMap || false;
1716

@@ -34,7 +33,6 @@ module.exports = function(content, map) {
3433
}
3534

3635
processCss(content, map, {
37-
mode: moduleMode ? "local" : "global",
3836
from: loaderUtils.getRemainingRequest(this).split("!").pop(),
3937
to: loaderUtils.getCurrentRequest(this).split("!").pop(),
4038
query: query,

lib/localsLoader.js

-44
This file was deleted.

lib/processCss.js

+2-39
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@ var formatCodeFrame = require("babel-code-frame");
66
var Tokenizer = require("css-selector-tokenizer");
77
var postcss = require("postcss");
88
var loaderUtils = require("loader-utils");
9-
var getLocalIdent = require("./getLocalIdent");
109

1110
var icssUtils = require('icss-utils');
12-
var localByDefault = require("postcss-modules-local-by-default");
13-
var extractImports = require("postcss-modules-extract-imports");
14-
var modulesScope = require("postcss-modules-scope");
15-
var modulesValues = require("postcss-modules-values");
1611
var valueParser = require('postcss-value-parser');
1712

1813
var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
@@ -104,7 +99,8 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
10499
item.stringType = "";
105100
delete item.innerSpacingBefore;
106101
delete item.innerSpacingAfter;
107-
var url = item.url;
102+
// For backward-compat after dropping css modules
103+
var url = loaderUtils.urlToRequest(item.url.trim());
108104
item.url = "___CSS_LOADER_URL___" + urlItems.length + "___";
109105
urlItems.push({
110106
url: url
@@ -135,47 +131,14 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
135131

136132
module.exports = function processCss(inputSource, inputMap, options, callback) {
137133
var query = options.query;
138-
var context = query.context;
139-
var localIdentName = query.localIdentName || "[hash:base64]";
140-
var localIdentRegExp = query.localIdentRegExp;
141-
142-
var customGetLocalIdent = query.getLocalIdent || getLocalIdent;
143134

144135
var parserOptions = {
145-
mode: options.mode,
146136
url: query.url !== false,
147137
import: query.import !== false,
148138
resolve: options.resolve
149139
};
150140

151141
var pipeline = postcss([
152-
modulesValues,
153-
localByDefault({
154-
mode: options.mode,
155-
rewriteUrl: function(global, url) {
156-
if(parserOptions.url){
157-
url = url.trim();
158-
159-
if(!url.replace(/\s/g, '').length || !loaderUtils.isUrlRequest(url)) {
160-
return url;
161-
}
162-
if(global) {
163-
return loaderUtils.urlToRequest(url);
164-
}
165-
}
166-
return url;
167-
}
168-
}),
169-
extractImports(),
170-
modulesScope({
171-
generateScopedName: function generateScopedName (exportName) {
172-
return customGetLocalIdent(options.loaderContext, localIdentName, exportName, {
173-
regExp: localIdentRegExp,
174-
hashPrefix: query.hashPrefix || "",
175-
context: context
176-
});
177-
}
178-
}),
179142
parserPlugin(parserOptions)
180143
]);
181144

locals.js

-5
This file was deleted.

package.json

+2-9
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,15 @@
99
},
1010
"files": [
1111
"lib",
12-
"index.js",
13-
"locals.js"
12+
"index.js"
1413
],
1514
"dependencies": {
1615
"babel-code-frame": "^6.26.0",
1716
"css-selector-tokenizer": "^0.7.0",
1817
"icss-utils": "^2.1.0",
1918
"loader-utils": "^1.0.2",
20-
"lodash.camelcase": "^4.3.0",
2119
"postcss": "^6.0.23",
22-
"postcss-modules-extract-imports": "^1.2.0",
23-
"postcss-modules-local-by-default": "^1.2.0",
24-
"postcss-modules-scope": "^1.1.0",
25-
"postcss-modules-values": "^1.3.0",
26-
"postcss-value-parser": "^3.3.0",
27-
"source-list-map": "^2.0.0"
20+
"postcss-value-parser": "^3.3.0"
2821
},
2922
"devDependencies": {
3023
"codecov": "^1.0.1",

0 commit comments

Comments
 (0)