Skip to content

Commit 365e5bc

Browse files
feat: allow to disable css modules and disable their by default
BREAKING CHANGE: by default css modules are disabled (now `modules: false` disable css modules), you can return old behaviour change this on `modules: 'global'`
1 parent 1dad1fb commit 365e5bc

10 files changed

+2858
-711
lines changed

README.md

+25-9
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ It's useful when you, for instance, need to post process the CSS as a string.
9797
|:--:|:--:|:-----:|:----------|
9898
|**[`url`](#url)**|`{Boolean}`|`true`| Enable/Disable `url()` handling|
9999
|**[`import`](#import)** |`{Boolean}`|`true`| Enable/Disable @import handling|
100-
|**[`modules`](#modules)**|`{Boolean}`|`false`|Enable/Disable CSS Modules|
100+
|**[`modules`](#modules)**|`{Boolean\|String}`|`false`|Enable/Disable CSS Modules and setup mode|
101101
|**[`localIdentName`](#localidentname)**|`{String}`|`[hash:base64]`|Configure the generated ident|
102102
|**[`sourceMap`](#sourcemap)**|`{Boolean}`|`false`|Enable/Disable Sourcemaps|
103103
|**[`camelCase`](#camelcase)**|`{Boolean\|String}`|`false`|Export Classnames in CamelCase|
@@ -129,17 +129,33 @@ To import styles from a node module path, prefix it with a `~`:
129129

130130
### [`modules`](https://github.com/css-modules/css-modules)
131131

132-
The query parameter `modules` enables the **CSS Modules** spec.
132+
The `modules` option enables/disables the **CSS Modules** spec and setup basic behaviour.
133133

134-
This enables local scoped CSS by default. (You can switch it off with `:global(...)` or `:global` for selectors and/or rules.).
134+
|Name|Type|Description|
135+
|:--:|:--:|:----------|
136+
|**`true`**|`{Boolean}`|Enables local scoped CSS by default (use **local** mode by default)|
137+
|**`false`**|`{Boolean}`|Disable the **CSS Modules** spec, all **CSS Modules** features (like `@value`, `:local`, `:global` and `composes`) will not work|
138+
|**`'local'`** |`{String}`|Enables local scoped CSS by default (same as `true` value)|
139+
|**`'global'`**|`{String}`|Enables global scoped CSS by default|
140+
141+
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.
142+
143+
You can read about **modes** below
135144

136-
#### `Scope`
145+
##### `Scope`
146+
147+
Using `local` value requires you to specify `:global` classes.
148+
Using `global` value requires you to specify `:local` classes.
137149

138-
By default CSS exports all classnames into a global selector scope. Styles can be locally scoped to avoid globally scoping styles.
150+
You can find more information [here](https://github.com/css-modules/css-modules).
151+
152+
Styles can be locally scoped to avoid globally scoping styles.
139153

140154
The syntax `:local(.className)` can be used to declare `className` in the local scope. The local identifiers are exported by the module.
141155

142-
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.
156+
With `:local` (without brackets) local mode can be switched on for this selector.
157+
The `:global(.className)` nocation can be used to declare an explicit global selector.
158+
With `:global` (without brackets) global mode can be switched on for this selector.
143159

144160
The loader replaces local selectors with unique identifiers. The chosen unique identifiers are exported by the module.
145161

@@ -177,7 +193,7 @@ file.png => ./file.png
177193

178194
You can use `:local(#someId)`, but this is not recommended. Use classes instead of ids.
179195

180-
#### `Composing`
196+
##### `Composing`
181197

182198
When declaring a local classname you can compose a local class from another local classname.
183199

@@ -213,7 +229,7 @@ exports.locals = {
213229
}
214230
```
215231

216-
#### `Importing`
232+
##### `Importing`
217233

218234
To import a local classname from another module.
219235

@@ -282,7 +298,7 @@ You can also specify the absolute path to your custom `getLocalIdent` function t
282298

283299
To include source maps set the `sourceMap` option.
284300

285-
I.e. the extract-text-webpack-plugin can handle them.
301+
I.e. the `mini-css-extract-plugin` can handle them.
286302

287303
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.
288304

lib/loader.js

+46-34
Original file line numberDiff line numberDiff line change
@@ -62,43 +62,55 @@ module.exports = function loader(content, map, meta) {
6262

6363
const resolveImport = options.import !== false;
6464
const resolveUrl = options.url !== false;
65-
const loaderContext = this;
66-
67-
const plugins = [
68-
modulesValues,
69-
localByDefault({
70-
mode: options.modules ? 'local' : 'global',
71-
rewriteUrl(global, url) {
72-
if (resolveUrl) {
73-
// eslint-disable-next-line no-param-reassign
74-
url = url.trim();
75-
76-
if (!url.replace(/\s/g, '').length || !isUrlRequest(url)) {
77-
return url;
78-
}
7965

80-
if (global) {
81-
return urlToRequest(url);
66+
const plugins = [];
67+
68+
if (options.modules) {
69+
const loaderContext = this;
70+
const mode =
71+
typeof options.modules === 'boolean' ? 'local' : options.modules;
72+
73+
plugins.push(
74+
modulesValues,
75+
localByDefault({
76+
mode,
77+
rewriteUrl(global, url) {
78+
if (resolveUrl) {
79+
// eslint-disable-next-line no-param-reassign
80+
url = url.trim();
81+
82+
if (!url.replace(/\s/g, '').length || !isUrlRequest(url)) {
83+
return url;
84+
}
85+
86+
if (global) {
87+
return urlToRequest(url);
88+
}
8289
}
83-
}
8490

85-
return url;
86-
},
87-
}),
88-
extractImports(),
89-
modulesScope({
90-
generateScopedName: function generateScopedName(exportName) {
91-
const localIdentName = options.localIdentName || '[hash:base64]';
92-
const customGetLocalIdent = options.getLocalIdent || getLocalIdent;
93-
94-
return customGetLocalIdent(loaderContext, localIdentName, exportName, {
95-
regExp: options.localIdentRegExp,
96-
hashPrefix: options.hashPrefix || '',
97-
context: options.context,
98-
});
99-
},
100-
}),
101-
];
91+
return url;
92+
},
93+
}),
94+
extractImports(),
95+
modulesScope({
96+
generateScopedName: function generateScopedName(exportName) {
97+
const localIdentName = options.localIdentName || '[hash:base64]';
98+
const customGetLocalIdent = options.getLocalIdent || getLocalIdent;
99+
100+
return customGetLocalIdent(
101+
loaderContext,
102+
localIdentName,
103+
exportName,
104+
{
105+
regExp: options.localIdentRegExp,
106+
hashPrefix: options.hashPrefix || '',
107+
context: options.context,
108+
}
109+
);
110+
},
111+
})
112+
);
113+
}
102114

103115
if (resolveImport) {
104116
plugins.push(importParser());

0 commit comments

Comments
 (0)