Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace camelCase option on exportLocalsStyle option #938

Merged
merged 1 commit into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 57 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading