Skip to content

Commit 888cca0

Browse files
feat: replace camelCase option on exportLocalsStyle option (webpack-contrib#938)
BREAKING CHANGE: `camelCase` option was removed in favor `exportLocalsStyle` option. Option value should be `String`. Use `camelCase` if you option value was `true` and `asIs` if you option value was `false`.
1 parent 1d7a464 commit 888cca0

10 files changed

+359
-301
lines changed

README.md

+57-57
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,15 @@ module.exports = {
109109

110110
## Options
111111

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\|Object}` | `false` | Enable/Disable CSS Modules and setup their options |
117-
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `false` | Enable/Disable Sourcemaps |
118-
| **[`camelCase`](#camelcase)** | `{Boolean\|String}` | `false` | Export Classnames in CamelCase |
119-
| **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Number of loaders applied before CSS loader |
120-
| **[`exportOnlyLocals`](#exportonlylocals)** | `{Boolean}` | `false` | Export only locals |
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\|Object}` | `false` | Enable/Disable CSS Modules and setup their options |
117+
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `false` | Enable/Disable Sourcemaps |
118+
| **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Number of loaders applied before CSS loader |
119+
| **[`exportLocalsStyle`](#exportlocalsstyle)** | `{String}` | `asIs` | Setup style of exported classnames |
120+
| **[`exportOnlyLocals`](#exportonlylocals)** | `{Boolean}` | `false` | Export only locals |
121121

122122
### `url`
123123

@@ -672,7 +672,7 @@ To include source maps set the `sourceMap` option.
672672

673673
I.e. the `mini-css-extract-plugin` can handle them.
674674

675-
They are not enabled by default because they expose a runtime overhead and increase in bundle size (JS source maps do not).
675+
They are not enabled by default because they expose a runtime overhead and increase in bundle size (JS source maps do not).
676676

677677
In addition to that relative paths are buggy and you need to use an absolute public path which includes the server URL.
678678

@@ -694,52 +694,6 @@ module.exports = {
694694
};
695695
```
696696

697-
### `camelCase`
698-
699-
Type: `Boolean|String`
700-
Default: `false`
701-
702-
By default, the exported JSON keys mirror the class names.
703-
704-
| Name | Type | Description |
705-
| :----------------: | :---------: | :----------------------------------------------------------------------------------------------------------------------- |
706-
| **`false`** | `{Boolean}` | Class names won't be camelized |
707-
| **`true`** | `{Boolean}` | Class names will be camelized, the original class name will not to be removed from the locals |
708-
| **`'dashes'`** | `{String}` | Only dashes in class names will be camelized |
709-
| **`'only'`** | `{String}` | Introduced in `0.27.1`. Class names will be camelized, the original class name will be removed from the locals |
710-
| **`'dashesOnly'`** | `{String}` | Introduced in `0.27.1`. Dashes in class names will be camelized, the original class name will be removed from the locals |
711-
712-
**file.css**
713-
714-
```css
715-
.class-name {
716-
}
717-
```
718-
719-
**file.js**
720-
721-
```js
722-
import { className } from 'file.css';
723-
```
724-
725-
**webpack.config.js**
726-
727-
```js
728-
module.exports = {
729-
module: {
730-
rules: [
731-
{
732-
test: /\.css$/i,
733-
loader: 'css-loader',
734-
options: {
735-
camelCase: true,
736-
},
737-
},
738-
],
739-
},
740-
};
741-
```
742-
743697
### `importLoaders`
744698

745699
Type: `Number`
@@ -774,6 +728,52 @@ module.exports = {
774728

775729
This may change in the future when the module system (i. e. webpack) supports loader matching by origin.
776730

731+
### `exportLocalsStyle`
732+
733+
Type: `String`
734+
Default: `undefined`
735+
736+
By default, the exported JSON keys mirror the class names (i.e `asIs` value).
737+
738+
| Name | Type | Description |
739+
| :-------------------: | :--------: | :----------------------------------------------------------------------------------------------- |
740+
| **`asIs`** | `{String}` | Class names will be exported as is. |
741+
| **`'camelCase'`** | `{String}` | Class names will be camelized, the original class name will not to be removed from the locals |
742+
| **`'camelCaseOnly'`** | `{String}` | Class names will be camelized, the original class name will be removed from the locals |
743+
| **`'dashes'`** | `{String}` | Only dashes in class names will be camelized |
744+
| **`'dashesOnly'`** | `{String}` | Dashes in class names will be camelized, the original class name will be removed from the locals |
745+
746+
**file.css**
747+
748+
```css
749+
.class-name {
750+
}
751+
```
752+
753+
**file.js**
754+
755+
```js
756+
import { className } from 'file.css';
757+
```
758+
759+
**webpack.config.js**
760+
761+
```js
762+
module.exports = {
763+
module: {
764+
rules: [
765+
{
766+
test: /\.css$/i,
767+
loader: 'css-loader',
768+
options: {
769+
exportLocalsStyle: 'camelCase',
770+
},
771+
},
772+
],
773+
},
774+
};
775+
```
776+
777777
### `exportOnlyLocals`
778778

779779
Type: `Boolean`
@@ -821,7 +821,7 @@ module.exports = {
821821
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/,
822822
loader: 'url-loader',
823823
options: {
824-
limit: 10000,
824+
limit: 8192,
825825
},
826826
},
827827
],

src/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export default function loader(content, map, meta) {
104104
.forEach((warning) => this.emitWarning(new Warning(warning)));
105105

106106
const messages = result.messages || [];
107-
const { exportOnlyLocals, importLoaders, camelCase } = options;
107+
const { exportOnlyLocals, importLoaders, exportLocalsStyle } = options;
108108

109109
// Run other loader (`postcss-loader`, `sass-loader` and etc) for importing CSS
110110
const importPrefix = getImportPrefix(this, importLoaders);
@@ -117,7 +117,11 @@ export default function loader(content, map, meta) {
117117
exportOnlyLocals
118118
);
119119

120-
const exports = getExports(messages, camelCase, importItemReplacer);
120+
const exports = getExports(
121+
messages,
122+
exportLocalsStyle,
123+
importItemReplacer
124+
);
121125

122126
if (exportOnlyLocals) {
123127
return callback(

src/options.json

+5-8
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,21 @@
7474
"sourceMap": {
7575
"type": "boolean"
7676
},
77-
"camelCase": {
77+
"importLoaders": {
7878
"anyOf": [
7979
{
8080
"type": "boolean"
8181
},
8282
{
83-
"type": "string",
84-
"enum": ["dashes", "only", "dashesOnly"]
83+
"type": "number"
8584
}
8685
]
8786
},
88-
"importLoaders": {
87+
"exportLocalsStyle": {
8988
"anyOf": [
9089
{
91-
"type": "boolean"
92-
},
93-
{
94-
"type": "number"
90+
"type": "string",
91+
"enum": ["asIs", "camelCase","camelCaseOnly", "dashes", "dashesOnly"]
9592
}
9693
]
9794
},

src/utils.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ function getImportItemReplacer(
152152
};
153153
}
154154

155-
function getExports(messages, exportStyle, importItemReplacer) {
155+
function getExports(messages, exportLocalsStyle, importItemReplacer) {
156156
return messages
157157
.filter((message) => message.type === 'export')
158158
.reduce((accumulator, message) => {
@@ -171,15 +171,18 @@ function getExports(messages, exportStyle, importItemReplacer) {
171171

172172
let targetKey;
173173

174-
switch (exportStyle) {
175-
case true:
174+
switch (exportLocalsStyle) {
175+
case 'camelCase':
176176
addEntry(key);
177177
targetKey = camelCase(key);
178178

179179
if (targetKey !== key) {
180180
addEntry(targetKey);
181181
}
182182
break;
183+
case 'camelCaseOnly':
184+
addEntry(camelCase(key));
185+
break;
183186
case 'dashes':
184187
addEntry(key);
185188
targetKey = dashesCamelCase(key);
@@ -188,12 +191,10 @@ function getExports(messages, exportStyle, importItemReplacer) {
188191
addEntry(targetKey);
189192
}
190193
break;
191-
case 'only':
192-
addEntry(camelCase(key));
193-
break;
194194
case 'dashesOnly':
195195
addEntry(dashesCamelCase(key));
196196
break;
197+
case 'asIs':
197198
default:
198199
addEntry(key);
199200
break;
@@ -338,8 +339,6 @@ export {
338339
getImportPrefix,
339340
getLocalIdent,
340341
placholderRegExps,
341-
camelCase,
342-
dashesCamelCase,
343342
getFilter,
344343
getImportItemReplacer,
345344
getExports,

0 commit comments

Comments
 (0)