Skip to content

Commit

Permalink
feat: replace camelCase option on exportLocalsStyle option (#938)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
evilebottnawi committed May 28, 2019
1 parent 1d7a464 commit 888cca0
Show file tree
Hide file tree
Showing 10 changed files with 359 additions and 301 deletions.
114 changes: 57 additions & 57 deletions README.md
Expand Up @@ -109,15 +109,15 @@ module.exports = {

## Options

| Name | Type | Default | Description |
| :-----------------------------------------: | :-------------------------: | :-----: | :------------------------------------------------- |
| **[`url`](#url)** | `{Boolean\|Function}` | `true` | Enable/Disable `url()` handling |
| **[`import`](#import)** | `{Boolean\|Function}` | `true` | Enable/Disable @import handling |
| **[`modules`](#modules)** | `{Boolean\|String\|Object}` | `false` | Enable/Disable CSS Modules and setup their options |
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `false` | Enable/Disable Sourcemaps |
| **[`camelCase`](#camelcase)** | `{Boolean\|String}` | `false` | Export Classnames in CamelCase |
| **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Number of loaders applied before CSS loader |
| **[`exportOnlyLocals`](#exportonlylocals)** | `{Boolean}` | `false` | Export only locals |
| Name | Type | Default | Description |
| :-------------------------------------------: | :-------------------------: | :-----: | :------------------------------------------------- |
| **[`url`](#url)** | `{Boolean\|Function}` | `true` | Enable/Disable `url()` handling |
| **[`import`](#import)** | `{Boolean\|Function}` | `true` | Enable/Disable @import handling |
| **[`modules`](#modules)** | `{Boolean\|String\|Object}` | `false` | Enable/Disable CSS Modules and setup their options |
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `false` | Enable/Disable Sourcemaps |
| **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Number of loaders applied before CSS loader |
| **[`exportLocalsStyle`](#exportlocalsstyle)** | `{String}` | `asIs` | Setup style of exported classnames |
| **[`exportOnlyLocals`](#exportonlylocals)** | `{Boolean}` | `false` | Export only locals |

### `url`

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

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

They are not enabled by default because they expose a runtime overhead and increase in bundle size (JS source maps do not).
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.

Expand All @@ -694,52 +694,6 @@ module.exports = {
};
```

### `camelCase`

Type: `Boolean|String`
Default: `false`

By default, the exported JSON keys mirror the class names.

| Name | Type | Description |
| :----------------: | :---------: | :----------------------------------------------------------------------------------------------------------------------- |
| **`false`** | `{Boolean}` | Class names won't be camelized |
| **`true`** | `{Boolean}` | Class names will be camelized, the original class name will not to be removed from the locals |
| **`'dashes'`** | `{String}` | Only dashes in class names will be camelized |
| **`'only'`** | `{String}` | Introduced in `0.27.1`. Class names will be camelized, the original class name will be removed from the locals |
| **`'dashesOnly'`** | `{String}` | Introduced in `0.27.1`. Dashes in class names will be camelized, the original class name will be removed from the locals |

**file.css**

```css
.class-name {
}
```

**file.js**

```js
import { className } from 'file.css';
```

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'css-loader',
options: {
camelCase: true,
},
},
],
},
};
```

### `importLoaders`

Type: `Number`
Expand Down Expand Up @@ -774,6 +728,52 @@ module.exports = {

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

### `exportLocalsStyle`

Type: `String`
Default: `undefined`

By default, the exported JSON keys mirror the class names (i.e `asIs` value).

| Name | Type | Description |
| :-------------------: | :--------: | :----------------------------------------------------------------------------------------------- |
| **`asIs`** | `{String}` | Class names will be exported as is. |
| **`'camelCase'`** | `{String}` | Class names will be camelized, the original class name will not to be removed from the locals |
| **`'camelCaseOnly'`** | `{String}` | Class names will be camelized, the original class name will be removed from the locals |
| **`'dashes'`** | `{String}` | Only dashes in class names will be camelized |
| **`'dashesOnly'`** | `{String}` | Dashes in class names will be camelized, the original class name will be removed from the locals |

**file.css**

```css
.class-name {
}
```

**file.js**

```js
import { className } from 'file.css';
```

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'css-loader',
options: {
exportLocalsStyle: 'camelCase',
},
},
],
},
};
```

### `exportOnlyLocals`

Type: `Boolean`
Expand Down Expand Up @@ -821,7 +821,7 @@ module.exports = {
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/,
loader: 'url-loader',
options: {
limit: 10000,
limit: 8192,
},
},
],
Expand Down
8 changes: 6 additions & 2 deletions src/index.js
Expand Up @@ -104,7 +104,7 @@ export default function loader(content, map, meta) {
.forEach((warning) => this.emitWarning(new Warning(warning)));

const messages = result.messages || [];
const { exportOnlyLocals, importLoaders, camelCase } = options;
const { exportOnlyLocals, importLoaders, exportLocalsStyle } = options;

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

const exports = getExports(messages, camelCase, importItemReplacer);
const exports = getExports(
messages,
exportLocalsStyle,
importItemReplacer
);

if (exportOnlyLocals) {
return callback(
Expand Down
13 changes: 5 additions & 8 deletions src/options.json
Expand Up @@ -74,24 +74,21 @@
"sourceMap": {
"type": "boolean"
},
"camelCase": {
"importLoaders": {
"anyOf": [
{
"type": "boolean"
},
{
"type": "string",
"enum": ["dashes", "only", "dashesOnly"]
"type": "number"
}
]
},
"importLoaders": {
"exportLocalsStyle": {
"anyOf": [
{
"type": "boolean"
},
{
"type": "number"
"type": "string",
"enum": ["asIs", "camelCase","camelCaseOnly", "dashes", "dashesOnly"]
}
]
},
Expand Down
15 changes: 7 additions & 8 deletions src/utils.js
Expand Up @@ -152,7 +152,7 @@ function getImportItemReplacer(
};
}

function getExports(messages, exportStyle, importItemReplacer) {
function getExports(messages, exportLocalsStyle, importItemReplacer) {
return messages
.filter((message) => message.type === 'export')
.reduce((accumulator, message) => {
Expand All @@ -171,15 +171,18 @@ function getExports(messages, exportStyle, importItemReplacer) {

let targetKey;

switch (exportStyle) {
case true:
switch (exportLocalsStyle) {
case 'camelCase':
addEntry(key);
targetKey = camelCase(key);

if (targetKey !== key) {
addEntry(targetKey);
}
break;
case 'camelCaseOnly':
addEntry(camelCase(key));
break;
case 'dashes':
addEntry(key);
targetKey = dashesCamelCase(key);
Expand All @@ -188,12 +191,10 @@ function getExports(messages, exportStyle, importItemReplacer) {
addEntry(targetKey);
}
break;
case 'only':
addEntry(camelCase(key));
break;
case 'dashesOnly':
addEntry(dashesCamelCase(key));
break;
case 'asIs':
default:
addEntry(key);
break;
Expand Down Expand Up @@ -338,8 +339,6 @@ export {
getImportPrefix,
getLocalIdent,
placholderRegExps,
camelCase,
dashesCamelCase,
getFilter,
getImportItemReplacer,
getExports,
Expand Down

0 comments on commit 888cca0

Please sign in to comment.