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

refactor: the modules.localsConvention option was renamed to the modules.exportLocalsConvention option #1120

Merged
Merged
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
122 changes: 61 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
@@ -534,7 +534,7 @@ module.exports = {
context: path.resolve(__dirname, 'src'),
localIdentHashPrefix: 'my-custom-hash',
namedExport: true,
localsConvention: 'camelCase',
exportLocalsConvention: 'camelCase',
exportOnlyLocals: false,
},
},
@@ -758,55 +758,6 @@ module.exports = {
};
```

##### `localsConvention`

Type: `String`
Default: `'asIs'`

Style of exported classnames.

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: {
mode: 'local',
localsConvention: 'camelCase',
},
},
],
},
};
```

##### `localIdentContext`

Type: `String`
@@ -861,14 +812,11 @@ module.exports = {
};
```

##### `getLocalIdent`
##### `localIdentRegExp`

Type: `Function`
Type: `String|RegExp`
Default: `undefined`

You can also specify the absolute path to your custom `getLocalIdent` function to generate classname based on a different schema.
By default we use built-in function to generate a classname.

**webpack.config.js**

```js
@@ -880,9 +828,7 @@ module.exports = {
loader: 'css-loader',
options: {
modules: {
getLocalIdent: (context, localIdentName, localName, options) => {
return 'whatever_random_class_name';
},
localIdentRegExp: /page-(.*)\.css/i,
},
},
},
@@ -891,11 +837,14 @@ module.exports = {
};
```

##### `localIdentRegExp`
##### `getLocalIdent`

Type: `String|RegExp`
Type: `Function`
Default: `undefined`

You can also specify the absolute path to your custom `getLocalIdent` function to generate classname based on a different schema.
By default we use built-in function to generate a classname.

**webpack.config.js**

```js
@@ -907,7 +856,9 @@ module.exports = {
loader: 'css-loader',
options: {
modules: {
localIdentRegExp: /page-(.*)\.css/i,
getLocalIdent: (context, localIdentName, localName, options) => {
return 'whatever_random_class_name';
},
},
},
},
@@ -968,6 +919,55 @@ module.exports = {
};
```

##### `exportlocalsConvention`

Type: `String`
Default: `'asIs'`

Style of exported class names.

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: {
mode: 'local',
localsConvention: 'camelCase',
},
},
],
},
};
```

##### `exportOnlyLocals`

Type: `Boolean`
10 changes: 5 additions & 5 deletions src/options.json
Original file line number Diff line number Diff line change
@@ -95,7 +95,11 @@
}
]
},
"localsConvention": {
"namedExport": {
"description": "Use the named export ES modules.",
"type": "boolean"
},
"exportLocalsConvention": {
"description": "Style of exported classnames (https://github.com/webpack-contrib/css-loader#localsconvention).",
"enum": [
"asIs",
@@ -105,10 +109,6 @@
"dashesOnly"
]
},
"namedExport": {
"description": "Use the named export ES modules.",
"type": "boolean"
},
"exportOnlyLocals": {
"description": "Export only locals (https://github.com/webpack-contrib/css-loader#exportonlylocals).",
"type": "boolean"
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -127,8 +127,8 @@ function getModulesOptions(rawOptions, loaderContext) {
// eslint-disable-next-line no-undefined
localIdentRegExp: undefined,
getLocalIdent,
localsConvention: 'asIs',
namedExport: false,
exportLocalsConvention: 'asIs',
exportOnlyLocals: false,
};

@@ -440,7 +440,7 @@ function getExportCode(exports, icssReplacements, options) {
};

for (const { name, value } of exports) {
switch (options.modules.localsConvention) {
switch (options.modules.exportLocalsConvention) {
case 'camelCase': {
addExportToLocalsCode(name, value);

Loading