Skip to content

Commit e1c55e4

Browse files
refactor: rename the onlyLocals option (#1116)
BREAKING CHANGE: the `onlyLocals` option was renamed to the `exportOnlyLocals` option and moved to the `module` option
1 parent ac5f413 commit e1c55e4

10 files changed

+355
-359
lines changed

README.md

+32-30
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ module.exports = {
116116
| **[`modules`](#modules)** | `{Boolean\|String\|Object}` | `false` | Enables/Disables CSS Modules and their configuration |
117117
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps |
118118
| **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Enables/Disables or setups number of loaders applied before CSS loader |
119-
| **[`onlyLocals`](#onlylocals)** | `{Boolean}` | `false` | Export only locals |
120119
| **[`esModule`](#esmodule)** | `{Boolean}` | `false` | Use ES modules syntax |
121120

122121
### `url`
@@ -535,6 +534,7 @@ module.exports = {
535534
context: path.resolve(__dirname, 'src'),
536535
hashPrefix: 'my-custom-hash',
537536
namedExport: true,
537+
exportOnlyLocals: false,
538538
},
539539
},
540540
},
@@ -968,6 +968,37 @@ module.exports = {
968968
};
969969
```
970970

971+
### `exportOnlyLocals`
972+
973+
Type: `Boolean`
974+
Default: `false`
975+
976+
Export only locals.
977+
978+
**Useful** when you use **css modules** for pre-rendering (for example SSR).
979+
For pre-rendering with `mini-css-extract-plugin` you should use this option instead of `style-loader!css-loader` **in the pre-rendering bundle**.
980+
It doesn't embed CSS but only exports the identifier mappings.
981+
982+
**webpack.config.js**
983+
984+
```js
985+
module.exports = {
986+
module: {
987+
rules: [
988+
{
989+
test: /\.css$/i,
990+
loader: 'css-loader',
991+
options: {
992+
modules: {
993+
exportOnlyLocals: true,
994+
},
995+
},
996+
},
997+
],
998+
},
999+
};
1000+
```
1001+
9711002
### `sourceMap`
9721003

9731004
Type: `Boolean`
@@ -1032,35 +1063,6 @@ module.exports = {
10321063

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

1035-
### `onlyLocals`
1036-
1037-
Type: `Boolean`
1038-
Default: `false`
1039-
1040-
Export only locals.
1041-
1042-
**Useful** when you use **css modules** for pre-rendering (for example SSR).
1043-
For pre-rendering with `mini-css-extract-plugin` you should use this option instead of `style-loader!css-loader` **in the pre-rendering bundle**.
1044-
It doesn't embed CSS but only exports the identifier mappings.
1045-
1046-
**webpack.config.js**
1047-
1048-
```js
1049-
module.exports = {
1050-
module: {
1051-
rules: [
1052-
{
1053-
test: /\.css$/i,
1054-
loader: 'css-loader',
1055-
options: {
1056-
onlyLocals: true,
1057-
},
1058-
},
1059-
],
1060-
},
1061-
};
1062-
```
1063-
10641066
### `esModule`
10651067

10661068
Type: `Boolean`

src/options.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@
104104
"namedExport": {
105105
"description": "Use the named export ES modules.",
106106
"type": "boolean"
107+
},
108+
"exportOnlyLocals": {
109+
"description": "Export only locals (https://github.com/webpack-contrib/css-loader#exportonlylocals).",
110+
"type": "boolean"
107111
}
108112
}
109113
}
@@ -124,10 +128,6 @@
124128
}
125129
]
126130
},
127-
"onlyLocals": {
128-
"description": "Export only locals (https://github.com/webpack-contrib/css-loader#onlylocals).",
129-
"type": "boolean"
130-
},
131131
"esModule": {
132132
"description": "Use the ES modules syntax (https://github.com/webpack-contrib/css-loader#esmodule).",
133133
"type": "boolean"

src/utils.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ function getModulesOptions(rawOptions, loaderContext) {
123123
hashPrefix: '',
124124
exportGlobals: false,
125125
namedExport: false,
126+
exportOnlyLocals: false,
126127
};
127128

128129
if (
@@ -183,17 +184,13 @@ function normalizeOptions(rawOptions, loaderContext) {
183184
? rawOptions.sourceMap
184185
: loaderContext.sourceMap,
185186
importLoaders: rawOptions.importLoaders,
186-
onlyLocals:
187-
typeof rawOptions.onlyLocals !== 'undefined'
188-
? rawOptions.onlyLocals
189-
: false,
190187
esModule:
191188
typeof rawOptions.esModule === 'undefined' ? true : rawOptions.esModule,
192189
};
193190
}
194191

195192
function shouldUseImportPlugin(options) {
196-
if (options.onlyLocals) {
193+
if (options.modules.exportOnlyLocals) {
197194
return false;
198195
}
199196

@@ -205,7 +202,7 @@ function shouldUseImportPlugin(options) {
205202
}
206203

207204
function shouldUseURLPlugin(options) {
208-
if (options.onlyLocals) {
205+
if (options.modules.exportOnlyLocals) {
209206
return false;
210207
}
211208

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

329-
if (options.onlyLocals !== true) {
326+
if (options.modules.exportOnlyLocals !== true) {
330327
const apiUrl = stringifyRequest(
331328
loaderContext,
332329
require.resolve('./runtime/api')
@@ -357,7 +354,7 @@ function getModuleCode(
357354
icssReplacements,
358355
options
359356
) {
360-
if (options.onlyLocals === true) {
357+
if (options.modules.exportOnlyLocals === true) {
361358
return 'var ___CSS_LOADER_EXPORT___ = {};\n';
362359
}
363360

0 commit comments

Comments
 (0)