Skip to content

refactor: rename the onlyLocals option #1116

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

Merged
merged 1 commit into from
Jul 21, 2020
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
62 changes: 32 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ module.exports = {
| **[`modules`](#modules)** | `{Boolean\|String\|Object}` | `false` | Enables/Disables CSS Modules and their configuration |
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps |
| **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Enables/Disables or setups number of loaders applied before CSS loader |
| **[`onlyLocals`](#onlylocals)** | `{Boolean}` | `false` | Export only locals |
| **[`esModule`](#esmodule)** | `{Boolean}` | `false` | Use ES modules syntax |

### `url`
Expand Down Expand Up @@ -535,6 +534,7 @@ module.exports = {
context: path.resolve(__dirname, 'src'),
hashPrefix: 'my-custom-hash',
namedExport: true,
exportOnlyLocals: false,
},
},
},
Expand Down Expand Up @@ -968,6 +968,37 @@ module.exports = {
};
```

### `exportOnlyLocals`

Type: `Boolean`
Default: `false`

Export only locals.

**Useful** when you use **css modules** for pre-rendering (for example SSR).
For pre-rendering with `mini-css-extract-plugin` you should use this option instead of `style-loader!css-loader` **in the pre-rendering bundle**.
It doesn't embed CSS but only exports the identifier mappings.

**webpack.config.js**

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

### `sourceMap`

Type: `Boolean`
Expand Down Expand Up @@ -1032,35 +1063,6 @@ module.exports = {

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

### `onlyLocals`

Type: `Boolean`
Default: `false`

Export only locals.

**Useful** when you use **css modules** for pre-rendering (for example SSR).
For pre-rendering with `mini-css-extract-plugin` you should use this option instead of `style-loader!css-loader` **in the pre-rendering bundle**.
It doesn't embed CSS but only exports the identifier mappings.

**webpack.config.js**

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

### `esModule`

Type: `Boolean`
Expand Down
8 changes: 4 additions & 4 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@
"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"
}
}
}
Expand All @@ -124,10 +128,6 @@
}
]
},
"onlyLocals": {
"description": "Export only locals (https://github.com/webpack-contrib/css-loader#onlylocals).",
"type": "boolean"
},
"esModule": {
"description": "Use the ES modules syntax (https://github.com/webpack-contrib/css-loader#esmodule).",
"type": "boolean"
Expand Down
13 changes: 5 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ function getModulesOptions(rawOptions, loaderContext) {
hashPrefix: '',
exportGlobals: false,
namedExport: false,
exportOnlyLocals: false,
};

if (
Expand Down Expand Up @@ -183,17 +184,13 @@ function normalizeOptions(rawOptions, loaderContext) {
? rawOptions.sourceMap
: loaderContext.sourceMap,
importLoaders: rawOptions.importLoaders,
onlyLocals:
typeof rawOptions.onlyLocals !== 'undefined'
? rawOptions.onlyLocals
: false,
esModule:
typeof rawOptions.esModule === 'undefined' ? true : rawOptions.esModule,
};
}

function shouldUseImportPlugin(options) {
if (options.onlyLocals) {
if (options.modules.exportOnlyLocals) {
return false;
}

Expand All @@ -205,7 +202,7 @@ function shouldUseImportPlugin(options) {
}

function shouldUseURLPlugin(options) {
if (options.onlyLocals) {
if (options.modules.exportOnlyLocals) {
return false;
}

Expand Down Expand Up @@ -326,7 +323,7 @@ function getPreRequester({ loaders, loaderIndex }) {
function getImportCode(loaderContext, imports, options) {
let code = '';

if (options.onlyLocals !== true) {
if (options.modules.exportOnlyLocals !== true) {
const apiUrl = stringifyRequest(
loaderContext,
require.resolve('./runtime/api')
Expand Down Expand Up @@ -357,7 +354,7 @@ function getModuleCode(
icssReplacements,
options
) {
if (options.onlyLocals === true) {
if (options.modules.exportOnlyLocals === true) {
return 'var ___CSS_LOADER_EXPORT___ = {};\n';
}

Expand Down
Loading